annotate ipcluster/sonic_annotator_vamp.py @ 0:e34cf1b6fe09 tip

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