-
Notifications
You must be signed in to change notification settings - Fork 9
/
audacityAnnotation2WAVs.py.bak
64 lines (54 loc) · 2.52 KB
/
audacityAnnotation2WAVs.py.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import glob, os
import audioBasicIO, sys, csv
import scipy.io.wavfile as wavfile
def annotation2files(wavFile, csvFile):
'''
Break an audio stream to segments of interest,
defined by a csv file
- wavFile: path to input wavfile
- csvFile: path to csvFile of segment limits
Input CSV file must be of the format <T1>\t<T2>\t<Label>
'''
[Fs, x] = audioBasicIO.readAudioFile(wavFile)
with open(csvFile, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter='\t', quotechar='|')
for j, row in enumerate(reader):
T1 = float(row[0].replace(",","."))
T2 = float(row[1].replace(",","."))
label = "%s_%s_%.2f_%.2f.wav" % (wavFile, row[2], T1, T2)
label = label.replace(" ", "_")
xtemp = x[int(round(T1*Fs)):int(round(T2*Fs))]
print T1, T2, label, xtemp.shape
wavfile.write(label, Fs, xtemp)
def main(argv):
if argv[1] == "-f":
wavFile = argv[2]
annotationFile = argv[3]
annotation2files(wavFile, annotationFile)
elif argv[1] == "-d":
inputFolder = argv[2]
types = ('*.txt', '*.csv')
annotationFilesList = []
for files in types:
annotationFilesList.extend(glob.glob(os.path.join(inputFolder, files)))
for anFile in annotationFilesList:
wavFile = os.path.splitext(anFile)[0] + ".wav"
if not os.path.isfile(wavFile):
wavFile = os.path.splitext(anFile)[0] + ".mp3"
if not os.path.isfile(wavFile):
print "Audio file not found!"
return
annotation2files(wavFile, anFile)
if __name__ == '__main__':
# Used to extract a series of annotated WAV files based on (a) an audio file (mp3 or wav) and
# (b) a segment annotation file e.g. a "label" file generated in audacity
#
# usage 1:
# python audacityAnnotation2WAVs.py -f <audiofilepath> <annotationfilepath>
# The <annotationfilepath> is actually a tab-seperated file where each line has the format <startTime>\t<entTime>\t<classLabel>
# The result of this process is a series of WAV files with a file name <audiofilepath>_<startTime>_<endTime>_<classLabel>
#
# usage 2:
# python audacityAnnotation2WAVs.py -d <annotationfolderpath>
# Same but searches all .txt and .csv annotation files. Audio files are supposed to be in the same path / filename with a WAV extension
main(sys.argv)