Daniel@0
|
1 # Part of DML (Digital Music Laboratory)
|
Daniel@0
|
2 #
|
Daniel@0
|
3 # This program is free software; you can redistribute it and/or
|
Daniel@0
|
4 # modify it under the terms of the GNU General Public License
|
Daniel@0
|
5 # as published by the Free Software Foundation; either version 2
|
Daniel@0
|
6 # of the License, or (at your option) any later version.
|
Daniel@0
|
7 #
|
Daniel@0
|
8 # This program is distributed in the hope that it will be useful,
|
Daniel@0
|
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Daniel@0
|
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Daniel@0
|
11 # GNU General Public License for more details.
|
Daniel@0
|
12 #
|
Daniel@0
|
13 # You should have received a copy of the GNU General Public
|
Daniel@0
|
14 # License along with this library; if not, write to the Free Software
|
Daniel@0
|
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Daniel@0
|
16
|
Daniel@0
|
17 #!/usr/bin/python
|
Daniel@0
|
18 # -*- coding: utf-8 -*-
|
Daniel@0
|
19 """
|
Daniel@0
|
20 Created on Fri Oct 11 13:22:37 2013
|
Daniel@0
|
21
|
Daniel@0
|
22 @author: thomas
|
Daniel@0
|
23 """
|
Daniel@0
|
24
|
Daniel@0
|
25 from __future__ import division
|
Daniel@0
|
26
|
Daniel@0
|
27 import sys
|
Daniel@0
|
28 import shutil
|
Daniel@0
|
29 import os
|
Daniel@0
|
30 import errno
|
Daniel@0
|
31 import subprocess
|
Daniel@0
|
32 import time
|
Daniel@0
|
33 import random
|
Daniel@0
|
34 import hashlib
|
Daniel@0
|
35
|
Daniel@0
|
36 # uses a separate console process to achieve the file conversion
|
Daniel@0
|
37 def vamp_host_process(argslist):
|
Daniel@0
|
38 #"""Call sonic annotator"""
|
Daniel@0
|
39
|
Daniel@0
|
40 vamp_host = 'sonic-annotator'
|
Daniel@0
|
41 command = [vamp_host]
|
Daniel@0
|
42 command.extend(argslist)
|
Daniel@0
|
43
|
Daniel@0
|
44 # which sa version?
|
Daniel@0
|
45 #p = subprocess.Popen([vamp_host, '-v'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
Daniel@0
|
46 #print(p.stdout.read())
|
Daniel@0
|
47
|
Daniel@0
|
48
|
Daniel@0
|
49 #stdout = subprocess.check_output(command, stderr=subprocess.STDOUT)
|
Daniel@0
|
50 time.sleep(random.random()*1.0)
|
Daniel@0
|
51 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
Daniel@0
|
52 text = p.stdout.read()
|
Daniel@0
|
53 retcode = p.wait()
|
Daniel@0
|
54 if (retcode==0):
|
Daniel@0
|
55 print "Finished."
|
Daniel@0
|
56 return 1
|
Daniel@0
|
57 else:
|
Daniel@0
|
58 print "Error " + text
|
Daniel@0
|
59 return 0
|
Daniel@0
|
60
|
Daniel@0
|
61 #time.sleep(random.random()*1.0)
|
Daniel@0
|
62 #print command
|
Daniel@0
|
63 #subprocess.call(command,shell=True)
|
Daniel@0
|
64 return 1
|
Daniel@0
|
65
|
Daniel@0
|
66
|
Daniel@0
|
67 # processes the given file using a vamp plugin and sonic annotator
|
Daniel@0
|
68 # @param string wav_file file to be processed
|
Daniel@0
|
69 def transform(wav_file = 'sweep.mp3',
|
Daniel@0
|
70 transform_file = 'bbc_speechmusic.n3'):
|
Daniel@0
|
71
|
Daniel@0
|
72 # get transform hash
|
Daniel@0
|
73 BLOCKSIZE = 65536
|
Daniel@0
|
74 hasher = hashlib.sha1()
|
Daniel@0
|
75 with open(transform_file, 'rb') as afile:
|
Daniel@0
|
76 buf = afile.read(BLOCKSIZE)
|
Daniel@0
|
77 while len(buf) > 0:
|
Daniel@0
|
78 hasher.update(buf)
|
Daniel@0
|
79 buf = afile.read(BLOCKSIZE)
|
Daniel@0
|
80 tpath = os.path.split(transform_file)
|
Daniel@0
|
81
|
Daniel@0
|
82 # prepare output directory:
|
Daniel@0
|
83 # get audio file subpath
|
Daniel@0
|
84 # create directory _Features for output if doesnt exist
|
Daniel@0
|
85 spath = os.path.split(wav_file)
|
Daniel@0
|
86 hash = str(hasher.hexdigest())
|
Daniel@0
|
87 featpath = spath[0] + '/_Analysis/' + tpath[1] + "_" + hash[:5]
|
Daniel@0
|
88
|
Daniel@0
|
89 #if not os.path.exists(featpath):
|
Daniel@0
|
90 # os.makedirs(featpath)
|
Daniel@0
|
91 print 'Creating directory' + featpath
|
Daniel@0
|
92 try:
|
Daniel@0
|
93 os.makedirs(featpath)
|
Daniel@0
|
94 except OSError as exception:
|
Daniel@0
|
95 if exception.errno!= errno.EEXIST:
|
Daniel@0
|
96 raise
|
Daniel@0
|
97
|
Daniel@0
|
98 # copy transform file into directory
|
Daniel@0
|
99 shutil.copy(transform_file, featpath + '/' + tpath[1][:-3] + '_' + hash[:5] + '.n3')
|
Daniel@0
|
100
|
Daniel@0
|
101 #./sonic-annotator -t silvet_settings.n3 input.wav -w csv
|
Daniel@0
|
102 # prepare arguments
|
Daniel@0
|
103 args = ['-t', transform_file, wav_file, '-w', 'csv', '-w', 'rdf',
|
Daniel@0
|
104 '--rdf-basedir',featpath,'--csv-basedir',featpath, '--rdf-many-files', '--rdf-append']
|
Daniel@0
|
105 #args = ['-t', transform_file, wav_file, '-w', 'csv', '--csv-force','--csv-basedir',featpath]
|
Daniel@0
|
106
|
Daniel@0
|
107
|
Daniel@0
|
108 print "Analysing " + wav_file
|
Daniel@0
|
109
|
Daniel@0
|
110 result = vamp_host_process(args)
|
Daniel@0
|
111 # execute vamp host
|
Daniel@0
|
112 return [wav_file, result]
|
Daniel@0
|
113
|
Daniel@0
|
114
|
Daniel@0
|
115 # entry function only for testing
|
Daniel@0
|
116 if __name__ == "__main__":
|
Daniel@0
|
117 if len(sys.argv) >= 2:
|
Daniel@0
|
118 transform(sys.argv[1])
|
Daniel@0
|
119 else:
|
Daniel@0
|
120 transform()
|
Daniel@0
|
121
|