Add fix to avoid non wav treatment failure

This commit is contained in:
Samuel Ortion 2022-07-15 14:50:24 +02:00
parent 0d019d3b6f
commit 175b3fdde3
2 changed files with 46 additions and 12 deletions

View File

@ -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
```

View File

@ -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,24 +76,35 @@ 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)
@ -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)