forked from Chiro-Canto/TadaridaTools
Add fix to avoid non wav treatment failure
This commit is contained in:
parent
0d019d3b6f
commit
175b3fdde3
20
README.md
20
README.md
@ -8,3 +8,23 @@ Set of Tools to perform acoustic analysis and preprocess audio recordings to be
|
|||||||
* 7z
|
* 7z
|
||||||
* python3
|
* python3
|
||||||
* python soundfile
|
* 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
|
||||||
|
```
|
||||||
|
26
src/tadam.py
26
src/tadam.py
@ -26,6 +26,7 @@ import zipfile
|
|||||||
|
|
||||||
verbose = False
|
verbose = False
|
||||||
|
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
usage_str = """
|
usage_str = """
|
||||||
|
|
||||||
@ -59,11 +60,13 @@ def usage():
|
|||||||
"""
|
"""
|
||||||
print(usage_str)
|
print(usage_str)
|
||||||
|
|
||||||
|
|
||||||
def rate(infile, outfile, rate_ratio):
|
def rate(infile, outfile, rate_ratio):
|
||||||
data, samplerate = sf.read(infile)
|
data, samplerate = sf.read(infile)
|
||||||
new_samplerate = int(samplerate * rate_ratio)
|
new_samplerate = int(samplerate * rate_ratio)
|
||||||
sf.write(outfile, data, new_samplerate)
|
sf.write(outfile, data, new_samplerate)
|
||||||
|
|
||||||
|
|
||||||
def convert_folder(infolder, outfolder, rate_ratio, prefix):
|
def convert_folder(infolder, outfolder, rate_ratio, prefix):
|
||||||
try:
|
try:
|
||||||
os.mkdir(outfolder)
|
os.mkdir(outfolder)
|
||||||
@ -73,24 +76,35 @@ def convert_folder(infolder, outfolder, rate_ratio, prefix):
|
|||||||
n = len(infiles)
|
n = len(infiles)
|
||||||
i = 1
|
i = 1
|
||||||
for infile in infiles:
|
for infile in infiles:
|
||||||
|
extension = infile.split(".")[-1]
|
||||||
|
if not extension in ["wav", "WAV"]:
|
||||||
|
continue
|
||||||
outfile = prefix + infile.replace('.WAV', '.wav')
|
outfile = prefix + infile.replace('.WAV', '.wav')
|
||||||
if verbose:
|
if verbose:
|
||||||
print(f"{str(i).zfill(len(str(n)))}/{n} Processing {infile} → {outfile}...")
|
print(
|
||||||
rate(os.path.join(infolder, infile), os.path.join(outfolder, outfile), rate_ratio)
|
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
|
i += 1
|
||||||
|
|
||||||
|
|
||||||
def chunk(outfolder, length):
|
def chunk(outfolder, length):
|
||||||
for file in os.listdir(outfolder):
|
for file in os.listdir(outfolder):
|
||||||
infile = os.path.join(outfolder, file)
|
infile = os.path.join(outfolder, file)
|
||||||
|
extension = infile.split(".")[-1]
|
||||||
|
if not extension in ["wav", "WAV"]:
|
||||||
|
continue
|
||||||
data, samplerate = sf.read(infile)
|
data, samplerate = sf.read(infile)
|
||||||
if verbose:
|
if verbose:
|
||||||
print(f"\t Splitting {infile} in {length}s-long chunks.")
|
print(f"\t Splitting {infile} in {length}s-long chunks.")
|
||||||
sections = int(np.ceil(len(data) / samplerate / length))
|
sections = int(np.ceil(len(data) / samplerate / length))
|
||||||
for i in range(sections):
|
for i in range(sections):
|
||||||
if verbose:
|
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')
|
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)
|
sf.write(filename, temp, samplerate)
|
||||||
os.remove(infile)
|
os.remove(infile)
|
||||||
|
|
||||||
@ -103,7 +117,8 @@ def zip_folder(outfolder):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
try:
|
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:
|
except getopt.GetoptError as err:
|
||||||
print(err)
|
print(err)
|
||||||
|
|
||||||
@ -136,7 +151,6 @@ def main():
|
|||||||
prefix = a
|
prefix = a
|
||||||
else:
|
else:
|
||||||
assert False, "unhandled option"
|
assert False, "unhandled option"
|
||||||
|
|
||||||
convert_folder(infolder, outfolder, rate_ratio, prefix)
|
convert_folder(infolder, outfolder, rate_ratio, prefix)
|
||||||
if chunk_length != -1:
|
if chunk_length != -1:
|
||||||
chunk(outfolder, chunk_length)
|
chunk(outfolder, chunk_length)
|
||||||
|
Loading…
Reference in New Issue
Block a user