peterf@0
|
1 #!/usr/bin/python
|
peterf@0
|
2 #
|
peterf@0
|
3 # annotationkit_create_annotation_protocol.py:
|
peterf@1
|
4 # Prepare annotation protocol CSV from list of audio files
|
peterf@1
|
5 #
|
peterf@1
|
6 # Read list of files from standard input and write CSV to standard output
|
peterf@1
|
7 # See annotationkit_create_annotation_protocol_wrapper.sh for usage example
|
peterf@0
|
8 #
|
peterf@0
|
9 # Author: Peter Foster
|
peterf@1
|
10 # (c) 2015 Peter Foster
|
peterf@0
|
11 #
|
peterf@0
|
12
|
peterf@0
|
13 import fileinput
|
peterf@0
|
14 from scikits.audiolab import Sndfile
|
peterf@0
|
15 from pandas import DataFrame
|
peterf@0
|
16 import numpy as np
|
peterf@0
|
17 import sys
|
peterf@0
|
18
|
peterf@0
|
19 #Maximum number of chunks to sample from each file
|
peterf@0
|
20 nChunksPerFile = np.inf
|
peterf@0
|
21 #Duration of each chunk in seconds
|
peterf@0
|
22 chunkDuration = 4
|
peterf@0
|
23 #Expected sample rate
|
peterf@0
|
24 sampleRate = 48000
|
peterf@0
|
25
|
peterf@0
|
26 AudioChunks = []
|
peterf@0
|
27 np.random.seed(4756)
|
peterf@0
|
28
|
peterf@0
|
29 for audioFile in fileinput.input():
|
peterf@0
|
30 audioFile = audioFile.strip()
|
peterf@0
|
31 sf = Sndfile(audioFile, "r")
|
peterf@0
|
32 if sf.samplerate != sampleRate: raise ValueError("wanted sample rate %g - got %g." % (sampleRate, sf.samplerate))
|
peterf@0
|
33
|
peterf@0
|
34 nChunksInFile = int(sf.nframes / (sf.samplerate * chunkDuration))
|
peterf@0
|
35 #Sample without replacement random chunks from file
|
peterf@0
|
36 sampledChunks = np.random.choice(nChunksInFile, min(nChunksInFile,nChunksPerFile), replace=False)
|
peterf@0
|
37
|
peterf@0
|
38 for chunk in sampledChunks:
|
peterf@0
|
39 frameStart = chunk * chunkDuration * sf.samplerate
|
peterf@0
|
40 AudioChunks.append((audioFile, chunk, frameStart))
|
peterf@0
|
41
|
peterf@0
|
42 sf.close()
|
peterf@0
|
43
|
peterf@0
|
44
|
peterf@0
|
45 #Create DataFrame
|
peterf@0
|
46 AudioChunks = DataFrame(AudioChunks)
|
peterf@0
|
47 AudioChunks.columns = ['audiofile', 'chunk', 'framestart']
|
peterf@0
|
48
|
peterf@0
|
49 sys.stderr.write("Processed " + str(len(AudioChunks)) + " chunks in total, corresponding to " + str(len(AudioChunks) * chunkDuration / float(60)) + " minutes of audio.\n")
|
peterf@0
|
50
|
peterf@0
|
51 #Write to CSV (stdout)
|
peterf@0
|
52 AudioChunks.to_csv(sys.stdout)
|