From 175b3fdde32297601db1060a411377f93df2b63d Mon Sep 17 00:00:00 2001 From: Samuel ORTION Date: Fri, 15 Jul 2022 14:50:24 +0200 Subject: [PATCH] Add fix to avoid non wav treatment failure --- README.md | 20 ++++++++++++++++++++ src/tadam.py | 38 ++++++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 805d1e8..85833e6 100644 --- a/README.md +++ b/README.md @@ -8,3 +8,23 @@ Set of Tools to perform acoustic analysis and preprocess audio recordings to be * 7z * python3 * python soundfile + +## tadam usage + +### Quick install + +```bash +sudo cp ./src/tadam.py /usr/local/bin/tadam.py +sudo chmod +x /usr/local/bin/tadam.py +``` + +### Example usage + +```bash +$ ls +raw/ # Folder containing a set of wav files in real time (before time expansion) +``` + +```bash +tadam.py -v -i raw -o exp -l 5 -z -p "Car910130-2021-Pass1-Z1-AudioMoth_" -r 0.1 +``` diff --git a/src/tadam.py b/src/tadam.py index af860e4..09c60e5 100755 --- a/src/tadam.py +++ b/src/tadam.py @@ -26,6 +26,7 @@ import zipfile verbose = False + def usage(): usage_str = """ @@ -59,11 +60,13 @@ def usage(): """ print(usage_str) + def rate(infile, outfile, rate_ratio): data, samplerate = sf.read(infile) new_samplerate = int(samplerate * rate_ratio) sf.write(outfile, data, new_samplerate) + def convert_folder(infolder, outfolder, rate_ratio, prefix): try: os.mkdir(outfolder) @@ -73,27 +76,38 @@ def convert_folder(infolder, outfolder, rate_ratio, prefix): n = len(infiles) i = 1 for infile in infiles: + extension = infile.split(".")[-1] + if not extension in ["wav", "WAV"]: + continue outfile = prefix + infile.replace('.WAV', '.wav') if verbose: - print(f"{str(i).zfill(len(str(n)))}/{n} Processing {infile} → {outfile}...") - rate(os.path.join(infolder, infile), os.path.join(outfolder, outfile), rate_ratio) + print( + f"{str(i).zfill(len(str(n)))}/{n} Processing {infile} → {outfile}...") + rate(os.path.join(infolder, infile), + os.path.join(outfolder, outfile), rate_ratio) i += 1 + def chunk(outfolder, length): for file in os.listdir(outfolder): infile = os.path.join(outfolder, file) + extension = infile.split(".")[-1] + if not extension in ["wav", "WAV"]: + continue data, samplerate = sf.read(infile) if verbose: print(f"\t Splitting {infile} in {length}s-long chunks.") sections = int(np.ceil(len(data) / samplerate / length)) for i in range(sections): if verbose: - print(f"\t {str(i).zfill(len(str(sections)))}/{sections} \t {infile}") + print( + f"\t {str(i).zfill(len(str(sections)))}/{sections} \t {infile}") filename = infile.replace('.wav', f'_{str(i).zfill(3)}.wav') - temp = data[i*samplerate*length : i*samplerate*length+samplerate*length] + temp = data[i*samplerate*length: i * + samplerate*length+samplerate*length] sf.write(filename, temp, samplerate) os.remove(infile) - + def zip_folder(outfolder): if verbose: print("Compressing .zip archive...") @@ -103,13 +117,14 @@ def zip_folder(outfolder): def main(): try: - opts, args = getopt.getopt(sys.argv[1:], "hi:o:vr:l:zp:", ["help", "input=", "output=", "verbose", "ratio=", "length=", "zip", "prefix="]) + opts, args = getopt.getopt(sys.argv[1:], "hi:o:vr:l:zp:", [ + "help", "input=", "output=", "verbose", "ratio=", "length=", "zip", "prefix="]) except getopt.GetoptError as err: print(err) - + global verbose - verbose = False - rate_ratio = 0.1 # Default is x10 time expansion + verbose = False + rate_ratio = 0.1 # Default is x10 time expansion infolder = "raw" outfolder = "exp" compress = False @@ -136,7 +151,6 @@ def main(): prefix = a else: assert False, "unhandled option" - convert_folder(infolder, outfolder, rate_ratio, prefix) if chunk_length != -1: chunk(outfolder, chunk_length) @@ -144,6 +158,6 @@ def main(): zip_folder(outfolder) if verbose: print("Done.") - + if __name__ == "__main__": - main() \ No newline at end of file + main()