source: python/aubio/bench/node.py @ 257debc

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 257debc was 257debc, checked in by Paul Brossier <piem@altern.org>, 19 years ago

clarify list functions, add task class
clarify list functions, add task class

  • Property mode set to 100644
File size: 3.7 KB
Line 
1from config import *
2import commands,sys
3import re
4
5def runcommand(cmd,debug=0):
6        if VERBOSE >= VERBOSE_CMD or debug: print cmd
7        if debug: return 
8        status, output = commands.getstatusoutput(cmd)
9        if status == 0 or VERBOSE >= VERBOSE_OUT:
10                output = output.split('\n')
11        if VERBOSE >= VERBOSE_OUT: 
12                for i in output: 
13                        if i: print i
14        if not status == 0: 
15                print 'error:',status,output
16                print 'command returning error was',cmd
17                #sys.exit(1)
18        if output == '' or output == ['']: return
19        return output
20
21def list_files(datapath,filter='f', maxdepth = -1):
22        if maxdepth >= 0: maxstring = " -maxdepth %d " % maxdepth       
23        else: maxstring = ""
24        cmd = '%s' * 5 % ('find ',datapath,maxstring,' -type ',filter)
25        return runcommand(cmd)
26
27def list_wav_files(datapath,maxdepth = -1):
28        return list_files(datapath, filter="f -name '*.wav'",maxdepth = maxdepth)
29
30def list_snd_files(datapath,maxdepth = -1):
31        return list_files(datapath, filter="f -name '*.wav' -o -name '*.aif'", 
32                maxdepth = maxdepth)
33
34def list_res_files(datapath,maxdepth = -1):
35        return list_files(datapath, filter="f -name '*.txt'", maxdepth = maxdepth)
36
37def list_dirs(datapath):
38        return list_files(datapath, filter="d")
39
40def mkdir(path):
41        cmd = '%s%s' % ('mkdir -p ',path)
42        return runcommand(cmd)
43
44def act_on_data (action,datapath,respath,suffix='.txt',filter='f',sub='\.wav$',**keywords):
45        """ execute action(datafile,resfile) on all files in datapath """
46        dirlist = list_files(datapath,filter=filter)
47        if dirlist == ['']: dirlist = []
48        respath_in_datapath = re.split(datapath, respath,maxsplit=1)[1:]
49        if(respath_in_datapath and suffix == ''): 
50                print 'error: respath in datapath and no suffix used'
51                #sys.exit(1)
52        for i in dirlist:
53                j = re.split(datapath, i,maxsplit=1)[1]
54                j = re.sub(sub,'',j)
55                #j = "%s%s%s"%(respath,j,suffix)
56                j = "%s%s"%(respath,j)
57                if sub != '':
58                        j = re.sub(sub,suffix,j)
59                else:
60                        j = "%s%s" % (j,suffix)
61                action(i,j,**keywords)
62
63def act_on_results (action,datapath,respath,filter='d'):
64        """ execute action(respath) an all subdirectories in respath """
65        dirlist = list_files(datapath,filter='d')
66        respath_in_datapath = re.split(datapath, respath,maxsplit=1)[1:]
67        if(respath_in_datapath and not filter == 'd' and suffix == ''): 
68                print 'warning: respath is in datapath'
69        for i in dirlist:
70                s = re.split(datapath, i ,maxsplit=1)[1]
71                action("%s%s%s"%(respath,'/',s))
72
73class task:
74
75        def __init__(self,datadir,resdir):
76                self.datadir = datadir
77                self.resdir = resdir
78                print "Checking data directory", self.datadir
79                self.checkdata()
80                self.checkres()
81       
82        def checkdata(self):
83                print "Listing directories in data directory",
84                self.dirlist = list_dirs(self.datadir)
85                print " (%d elements)" % len(self.dirlist)
86                #for each in self.dirlist: print each
87                print "Listing sound files in data directory",
88                self.sndlist = list_snd_files(self.datadir)
89                print " (%d elements)" % len(self.sndlist)
90                #for each in self.sndlist: print each
91                print "Listing annotations in data directory",
92                self.reslist = list_res_files(self.datadir)
93                print " (%d elements)" % len(self.reslist)
94                #for each in self.reslist: print each
95                if not self.reslist or len(self.reslist) < len (self.sndlist):
96                        print "ERR: not enough annotations"
97                        return -1
98                else:
99                        print "Found enough annotations"
100       
101        def checkres(self):
102                print "Creating results directory"
103                act_on_results(mkdir,self.datadir,self.resdir,filter='d')
Note: See TracBrowser for help on using the repository browser.