Changeset 78ebc27


Ignore:
Timestamp:
Nov 4, 2018, 8:00:04 PM (5 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
Children:
2c8ada6
Parents:
a5c3f2a
Message:

[py] improve style of cmd.py

File:
1 edited

Legend:

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

    ra5c3f2a r78ebc27  
    169169
    170170    def add_verbose_help(self):
    171         self.add_argument("-v","--verbose",
     171        self.add_argument("-v", "--verbose",
    172172                action="count", dest="verbose", default=1,
    173173                help="make lots of noise [default]")
    174         self.add_argument("-q","--quiet",
     174        self.add_argument("-q", "--quiet",
    175175                action="store_const", dest="verbose", const=0,
    176176                help="be quiet")
     
    181181
    182182    def add_buf_size(self, buf_size=512):
    183         self.add_argument("-B","--bufsize",
     183        self.add_argument("-B", "--bufsize",
    184184                action="store", dest="buf_size", default=buf_size,
    185185                metavar = "<size>", type=int,
     
    187187
    188188    def add_hop_size(self, hop_size=256):
    189         self.add_argument("-H","--hopsize",
     189        self.add_argument("-H", "--hopsize",
    190190                metavar = "<size>", type=int,
    191191                action="store", dest="hop_size", default=hop_size,
     
    193193
    194194    def add_method(self, method='default', helpstr='method'):
    195         self.add_argument("-m","--method",
     195        self.add_argument("-m", "--method",
    196196                metavar = "<method>", type=str,
    197197                action="store", dest="method", default=method,
     
    199199
    200200    def add_threshold(self, default=None):
    201         self.add_argument("-t","--threshold",
     201        self.add_argument("-t", "--threshold",
    202202                metavar = "<threshold>", type=float,
    203203                action="store", dest="threshold", default=default,
     
    240240
    241241    def add_slicer_options(self):
    242         self.add_argument("-o","--output", type = str,
     242        self.add_argument("-o", "--output", type = str,
    243243                metavar = "<outputdir>",
    244244                action="store", dest="output_directory", default=None,
    245                 help="specify path where slices of the original file should be created")
     245                help="specify path where slices of the original file should'
     246                ' be created")
    246247        self.add_argument("--cut-until-nsamples", type = int,
    247248                metavar = "<samples>",
    248249                action = "store", dest = "cut_until_nsamples", default = None,
    249                 help="how many extra samples should be added at the end of each slice")
     250                help="how many extra samples should be added at the end of'
     251                ' each slice")
    250252        self.add_argument("--cut-every-nslices", type = int,
    251253                metavar = "<samples>",
     
    255257                metavar = "<slices>",
    256258                action = "store", dest = "cut_until_nslices", default = None,
    257                 help="how many extra slices should be added at the end of each slice")
     259                help="how many extra slices should be added at the end of'
     260                ' each slice")
    258261        self.add_argument("--create-first",
    259262                action = "store_true", dest = "create_first", default = False,
     
    289292        if args.verbose > 2 and hasattr(self, 'options'):
    290293            name = type(self).__name__.split('_')[1]
    291             optstr = ' '.join(['running', name, 'with options', repr(self.options), '\n'])
     294            optstr = ' '.join(['running', name, 'with options',
     295                repr(self.options), '\n'])
    292296            sys.stderr.write(optstr)
    293297    def flush(self, frames_read, samplerate):
     
    297301    def parse_options(self, args, valid_opts):
    298302        # 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}
     303        options = {k: v for k, v in vars(args).items() if k in valid_opts}
    300304        self.options = options
    301305
     
    378382            outstr = "unknown bpm"
    379383        else:
    380             bpms = 60./ np.diff(self.beat_locations)
     384            bpms = 60. / np.diff(self.beat_locations)
    381385            median_bpm = np.mean(bpms)
    382386            if len(self.beat_locations) < 10:
     
    399403        return self.notes(block)
    400404    def repr_res(self, res, frames_read, samplerate):
    401         if res[2] != 0: # note off
     405        if res[2] != 0:  # note off
    402406            fmt_out = self.time2string(frames_read, samplerate)
    403407            sys.stdout.write(fmt_out + '\n')
    404         if res[0] != 0: # note on
     408        if res[0] != 0:  # note on
    405409            lastmidi = res[0]
    406410            fmt_out = "%f\t" % lastmidi
    407411            fmt_out += self.time2string(frames_read, samplerate)
    408             sys.stdout.write(fmt_out) # + '\t')
     412            sys.stdout.write(fmt_out)  # + '\t')
    409413    def flush(self, frames_read, samplerate):
    410414        eof = self.time2string(frames_read, samplerate)
     
    473477            if self.wassilence != 1:
    474478                self.wassilence = 1
    475                 return 2 # newly found silence
    476             return 1 # silence again
     479                return 2   # newly found silence
     480            return 1       # silence again
    477481        else:
    478482            if self.wassilence != 0:
    479483                self.wassilence = 0
    480                 return -1 # newly found noise
    481             return 0 # noise again
     484                return -1  # newly found noise
     485            return 0       # noise again
    482486
    483487    def repr_res(self, res, frames_read, samplerate):
     
    499503    def __call__(self, block):
    500504        ret = super(process_cut, self).__call__(block)
    501         if ret: self.slices.append(self.onset.get_last())
     505        if ret:
     506            self.slices.append(self.onset.get_last())
    502507        return ret
    503508
     
    505510        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
     
    526532                action="store_true", dest="show_version")
    527533        args, extras = parser_root.parse_known_args()
    528         if args.show_version == False: # no -V, forward to parser
     534        if not args.show_version: # no -V, forward to parser
    529535            args = parser.parse_args(extras, namespace=args)
    530         elif len(extras) != 0: # -V with other arguments, print help
     536        elif len(extras) != 0:     # -V with other arguments, print help
    531537            parser.print_help()
    532538            sys.exit(1)
    533     else: # in py3, we can simply use parser directly
     539    else:  # in py3, we can simply use parser directly
    534540        args = parser.parse_args()
    535541    if 'show_version' in args and args.show_version:
     
    538544    elif 'verbose' in args and args.verbose > 3:
    539545        sys.stderr.write('aubio version ' + aubio.version + '\n')
    540     if 'command' not in args or args.command is None or args.command in ['help']:
     546    if 'command' not in args or args.command is None \
     547            or args.command in ['help']:
    541548        # no command given, print help and return 1
    542549        parser.print_help()
     
    572579                frames_read += read
    573580                # exit loop at end of file
    574                 if read < a_source.hop_size: break
     581                if read < a_source.hop_size:
     582                    break
    575583            # flush the processor if needed
    576584            processor.flush(frames_read, a_source.samplerate)
     
    580588                fmt_string += " from {:s} at {:d}Hz\n"
    581589                sys.stderr.write(fmt_string.format(
    582                         frames_read/float(a_source.samplerate),
     590                        frames_read / float(a_source.samplerate),
    583591                        frames_read,
    584592                        frames_read // a_source.hop_size + 1,
Note: See TracChangeset for help on using the changeset viewer.