Ignore:
Timestamp:
Nov 17, 2018, 10:19:27 PM (5 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/constantq
Children:
d1d4ad4
Parents:
088760e (diff), a114fe0 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into feature/constantq

File:
1 edited

Legend:

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

    r088760e rc03d191  
    1212import sys
    1313import argparse
     14import warnings
    1415import aubio
    1516
     
    169170
    170171    def add_verbose_help(self):
    171         self.add_argument("-v","--verbose",
     172        self.add_argument("-v", "--verbose",
    172173                action="count", dest="verbose", default=1,
    173174                help="make lots of noise [default]")
    174         self.add_argument("-q","--quiet",
     175        self.add_argument("-q", "--quiet",
    175176                action="store_const", dest="verbose", const=0,
    176177                help="be quiet")
     
    181182
    182183    def add_buf_size(self, buf_size=512):
    183         self.add_argument("-B","--bufsize",
     184        self.add_argument("-B", "--bufsize",
    184185                action="store", dest="buf_size", default=buf_size,
    185186                metavar = "<size>", type=int,
     
    187188
    188189    def add_hop_size(self, hop_size=256):
    189         self.add_argument("-H","--hopsize",
     190        self.add_argument("-H", "--hopsize",
    190191                metavar = "<size>", type=int,
    191192                action="store", dest="hop_size", default=hop_size,
     
    193194
    194195    def add_method(self, method='default', helpstr='method'):
    195         self.add_argument("-m","--method",
     196        self.add_argument("-m", "--method",
    196197                metavar = "<method>", type=str,
    197198                action="store", dest="method", default=method,
     
    199200
    200201    def add_threshold(self, default=None):
    201         self.add_argument("-t","--threshold",
     202        self.add_argument("-t", "--threshold",
    202203                metavar = "<threshold>", type=float,
    203204                action="store", dest="threshold", default=default,
     
    240241
    241242    def add_slicer_options(self):
    242         self.add_argument("-o","--output", type = str,
     243        self.add_argument("-o", "--output", type = str,
    243244                metavar = "<outputdir>",
    244245                action="store", dest="output_directory", default=None,
    245                 help="specify path where slices of the original file should be created")
     246                help="specify path where slices of the original file should"
     247                " be created")
    246248        self.add_argument("--cut-until-nsamples", type = int,
    247249                metavar = "<samples>",
    248250                action = "store", dest = "cut_until_nsamples", default = None,
    249                 help="how many extra samples should be added at the end of each slice")
     251                help="how many extra samples should be added at the end of"
     252                " each slice")
    250253        self.add_argument("--cut-every-nslices", type = int,
    251254                metavar = "<samples>",
     
    255258                metavar = "<slices>",
    256259                action = "store", dest = "cut_until_nslices", default = None,
    257                 help="how many extra slices should be added at the end of each slice")
     260                help="how many extra slices should be added at the end of"
     261                " each slice")
    258262        self.add_argument("--create-first",
    259263                action = "store_true", dest = "create_first", default = False,
     
    289293        if args.verbose > 2 and hasattr(self, 'options'):
    290294            name = type(self).__name__.split('_')[1]
    291             optstr = ' '.join(['running', name, 'with options', repr(self.options), '\n'])
     295            optstr = ' '.join(['running', name, 'with options',
     296                repr(self.options), '\n'])
    292297            sys.stderr.write(optstr)
    293298    def flush(self, frames_read, samplerate):
     
    297302    def parse_options(self, args, valid_opts):
    298303        # get any valid options found in a dictionnary of arguments
    299         options = {k :v for k,v in vars(args).items() if k in valid_opts}
     304        options = {k: v for k, v in vars(args).items() if k in valid_opts}
    300305        self.options = options
    301306
     
    378383            outstr = "unknown bpm"
    379384        else:
    380             bpms = 60./ np.diff(self.beat_locations)
     385            bpms = 60. / np.diff(self.beat_locations)
    381386            median_bpm = np.mean(bpms)
    382387            if len(self.beat_locations) < 10:
     
    399404        return self.notes(block)
    400405    def repr_res(self, res, frames_read, samplerate):
    401         if res[2] != 0: # note off
     406        if res[2] != 0:  # note off
    402407            fmt_out = self.time2string(frames_read, samplerate)
    403408            sys.stdout.write(fmt_out + '\n')
    404         if res[0] != 0: # note on
     409        if res[0] != 0:  # note on
    405410            lastmidi = res[0]
    406411            fmt_out = "%f\t" % lastmidi
    407412            fmt_out += self.time2string(frames_read, samplerate)
    408             sys.stdout.write(fmt_out) # + '\t')
     413            sys.stdout.write(fmt_out)  # + '\t')
    409414    def flush(self, frames_read, samplerate):
    410415        eof = self.time2string(frames_read, samplerate)
     
    473478            if self.wassilence != 1:
    474479                self.wassilence = 1
    475                 return 2 # newly found silence
    476             return 1 # silence again
     480                return 2   # newly found silence
     481            return 1       # silence again
    477482        else:
    478483            if self.wassilence != 0:
    479484                self.wassilence = 0
    480                 return -1 # newly found noise
    481             return 0 # noise again
     485                return -1  # newly found noise
     486            return 0       # noise again
    482487
    483488    def repr_res(self, res, frames_read, samplerate):
     
    499504    def __call__(self, block):
    500505        ret = super(process_cut, self).__call__(block)
    501         if ret: self.slices.append(self.onset.get_last())
     506        if ret:
     507            self.slices.append(self.onset.get_last())
    502508        return ret
    503509
    504510    def flush(self, frames_read, samplerate):
    505         from aubio.cut import _cut_slice
    506511        _cut_slice(self.options, self.slices)
    507         duration = float (frames_read) / float(samplerate)
    508         base_info = '%(source_file)s' % {'source_file': self.options.source_uri}
     512        duration = float(frames_read) / float(samplerate)
     513        base_info = '%(source_file)s' % \
     514                    {'source_file': self.options.source_uri}
    509515        base_info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % \
    510                 {'duration': duration, 'samplerate': samplerate}
     516                     {'duration': duration, 'samplerate': samplerate}
    511517        info = "created %d slices from " % len(self.slices)
    512518        info += base_info
    513519        sys.stderr.write(info)
     520
     521def _cut_slice(options, timestamps):
     522    # cutting pass
     523    nstamps = len(timestamps)
     524    if nstamps > 0:
     525        # generate output files
     526        timestamps_end = None
     527        if options.cut_every_nslices:
     528            timestamps = timestamps[::options.cut_every_nslices]
     529            nstamps = len(timestamps)
     530        if options.cut_until_nslices and options.cut_until_nsamples:
     531            msg = "using cut_until_nslices, but cut_until_nsamples is set"
     532            warnings.warn(msg)
     533        if options.cut_until_nsamples:
     534            lag = options.cut_until_nsamples
     535            timestamps_end = [t + lag for t in timestamps[1:]]
     536            timestamps_end += [1e120]
     537        if options.cut_until_nslices:
     538            slice_lag = options.cut_until_nslices
     539            timestamps_end = [t for t in timestamps[1 + slice_lag:]]
     540            timestamps_end += [1e120] * (options.cut_until_nslices + 1)
     541        aubio.slice_source_at_stamps(options.source_uri,
     542                timestamps, timestamps_end = timestamps_end,
     543                output_dir = options.output_directory,
     544                samplerate = options.samplerate,
     545                create_first = options.create_first)
    514546
    515547def main():
     
    526558                action="store_true", dest="show_version")
    527559        args, extras = parser_root.parse_known_args()
    528         if args.show_version == False: # no -V, forward to parser
     560        if not args.show_version: # no -V, forward to parser
    529561            args = parser.parse_args(extras, namespace=args)
    530         elif len(extras) != 0: # -V with other arguments, print help
     562        elif len(extras) != 0:     # -V with other arguments, print help
    531563            parser.print_help()
    532564            sys.exit(1)
    533     else: # in py3, we can simply use parser directly
     565    else:  # in py3, we can simply use parser directly
    534566        args = parser.parse_args()
    535567    if 'show_version' in args and args.show_version:
     
    538570    elif 'verbose' in args and args.verbose > 3:
    539571        sys.stderr.write('aubio version ' + aubio.version + '\n')
    540     if 'command' not in args or args.command is None or args.command in ['help']:
     572    if 'command' not in args or args.command is None \
     573            or args.command in ['help']:
    541574        # no command given, print help and return 1
    542575        parser.print_help()
     
    572605                frames_read += read
    573606                # exit loop at end of file
    574                 if read < a_source.hop_size: break
     607                if read < a_source.hop_size:
     608                    break
    575609            # flush the processor if needed
    576610            processor.flush(frames_read, a_source.samplerate)
     
    580614                fmt_string += " from {:s} at {:d}Hz\n"
    581615                sys.stderr.write(fmt_string.format(
    582                         frames_read/float(a_source.samplerate),
     616                        frames_read / float(a_source.samplerate),
    583617                        frames_read,
    584618                        frames_read // a_source.hop_size + 1,
Note: See TracChangeset for help on using the changeset viewer.