Changeset af98cb8 for python/lib/aubio


Ignore:
Timestamp:
Mar 24, 2017, 2:23:41 AM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, sampler
Children:
86026a0
Parents:
b7208f8
Message:

python/lib/aubio/cmd.py: small refactor, add comments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/lib/aubio/cmd.py

    rb7208f8 raf98cb8  
    162162# some utilities
    163163
    164 def parse_options(args, valid_opts):
    165     options = {k :v for k,v in vars(args).items() if k in valid_opts}
    166     return options
    167 
    168 def remap_pvoc_options(options):
    169     # remap buf_size to win_s, hop_size to hop_s
    170     # FIXME: adjust python/ext/py-phasevoc.c to understand buf_size/hop_size
    171     options['win_s'] = options['buf_size']
    172     del options['buf_size']
    173     options['hop_s'] = options['hop_size']
    174     del options['hop_size']
    175     return options
    176 
    177164def samples2seconds(n_frames, samplerate):
    178165    return "%f\t" % (n_frames / float(samplerate))
     
    205192            sys.stderr.write(optstr)
    206193    def flush(self, n_frames, samplerate):
     194        # optionally called at the end of process
    207195        pass
     196
     197    def parse_options(self, args, valid_opts):
     198        # get any valid options found in a dictionnary of arguments
     199        options = {k :v for k,v in vars(args).items() if k in valid_opts}
     200        self.options = options
     201
     202    def remap_pvoc_options(self, options):
     203        # FIXME: we need to remap buf_size to win_s, hop_size to hop_s
     204        # adjust python/ext/py-phasevoc.c to understand buf_size/hop_size
     205        if 'buf_size' in options:
     206            options['win_s'] = options['buf_size']
     207            del options['buf_size']
     208        if 'hop_size' in options:
     209            options['hop_s'] = options['hop_size']
     210            del options['hop_size']
     211        self.options = options
    208212
    209213class process_onset(default_process):
    210214    valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate']
    211215    def __init__(self, args):
    212         self.options = parse_options(args, self.valid_opts)
     216        self.parse_options(args, self.valid_opts)
    213217        self.onset = aubio.onset(**self.options)
    214218        if args.threshold is not None:
     
    234238    valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate']
    235239    def __init__(self, args):
    236         self.options = parse_options(args, self.valid_opts)
     240        self.parse_options(args, self.valid_opts)
    237241        self.pitch = aubio.pitch(**self.options)
    238242        if args.threshold is not None:
     
    250254    valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate']
    251255    def __init__(self, args):
    252         self.options = parse_options(args, self.valid_opts)
     256        self.parse_options(args, self.valid_opts)
    253257        self.tempo = aubio.tempo(**self.options)
    254258        super(process_beat, self).__init__(args)
     
    276280    valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate']
    277281    def __init__(self, args):
    278         self.options = parse_options(args, self.valid_opts)
     282        self.parse_options(args, self.valid_opts)
    279283        self.notes = aubio.notes(**self.options)
    280284        super(process_notes, self).__init__(args)
     
    296300class process_mfcc(default_process):
    297301    def __init__(self, args):
    298         valid_opts = ['hop_size', 'buf_size']
    299         options = parse_options(args, valid_opts)
    300         self.options = remap_pvoc_options(options)
    301         self.pv = aubio.pvoc(**options)
    302 
    303         valid_opts = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate']
    304         options = parse_options(args, valid_opts)
    305         self.mfcc = aubio.mfcc(**options)
    306         self.options.update(options)
     302        valid_opts1 = ['hop_size', 'buf_size']
     303        self.parse_options(args, valid_opts1)
     304        self.remap_pvoc_options(self.options)
     305        self.pv = aubio.pvoc(**self.options)
     306
     307        valid_opts2 = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate']
     308        self.parse_options(args, valid_opts2)
     309        self.mfcc = aubio.mfcc(**self.options)
     310
     311        # remember all options
     312        self.parse_options(args, list(set(valid_opts1 + valid_opts2)))
    307313
    308314        super(process_mfcc, self).__init__(args)
     
    320326        self.args = args
    321327        valid_opts = ['hop_size', 'buf_size']
    322         options = parse_options(args, valid_opts)
    323         options = remap_pvoc_options(options)
    324         self.pv = aubio.pvoc(**options)
     328        self.parse_options(args, valid_opts)
     329        self.remap_pvoc_options(self.options)
     330        self.pv = aubio.pvoc(**self.options)
    325331
    326332        valid_opts = ['buf_size', 'n_filters']
    327         options = {k :v for k,v in vars(args).items() if k in valid_opts}
    328         # FIXME
    329         options['win_s'] = options['buf_size']
    330         del options['buf_size']
    331         self.filterbank = aubio.filterbank(**options)
     333        self.parse_options(args, valid_opts)
     334        self.remap_pvoc_options(self.options)
     335        self.filterbank = aubio.filterbank(**self.options)
    332336        self.filterbank.set_mel_coeffs_slaney(args.samplerate)
    333337
     
    362366        with aubio.source(args.source_uri, hop_size=args.hop_size,
    363367                samplerate=args.samplerate) as a_source:
     368            # always update args.samplerate to native samplerate, in case
     369            # source was opened with args.samplerate=0
    364370            args.samplerate = a_source.samplerate
    365371            # create the processor for this subcommand
Note: See TracChangeset for help on using the changeset viewer.