comparison pyspark/sonic-annotator-notimeside/sonic_annotator_vamp.py @ 0:e34cf1b6fe09 tip

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