Changeset 4120fbc for python/demos


Ignore:
Timestamp:
May 16, 2016, 3:16:00 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, pitchshift, sampler, timestretch, yinfft+
Children:
dc654f8
Parents:
416ddd1
Message:

python/demos: python3 and double precision compatibility

Location:
python/demos
Files:
24 edited

Legend:

Unmodified
Added
Removed
  • python/demos/demo_bpm_extract.py

    r416ddd1 r4120fbc  
    4646
    4747    # Convert to periods and to bpm
    48     bpms = 60./diff(beats)
    49     b = median(bpms)
     48    if len(beats) > 1:
     49        if len(beats) < 4:
     50            print("few beats found in {:s}".format(path))
     51        bpms = 60./diff(beats)
     52        b = median(bpms)
     53    else:
     54        b = 0
     55        print("not enough beats found in {:s}".format(path))
    5056    return b
    5157
     
    5460    for f in sys.argv[1:]:
    5561        bpm = get_file_bpm(f)
    56         print "%6s" % ("%.2f" % bpm), f
     62        print("{:6s} {:s}".format("{:2f}".format(bpm), f))
  • python/demos/demo_filterbank.py

    r416ddd1 r4120fbc  
    2020f.set_coeffs(coeffs)
    2121
    22 times = vstack([arange(win_s / 2 + 1) * samplerate / win_s] * n_filters)
     22times = vstack([arange(win_s // 2 + 1) * samplerate / win_s] * n_filters)
    2323title('Bank of filters built using a simple list of boundaries\nThe middle band has been amplified by 2.')
    2424loglog(times.T, f.get_coeffs().T, '.-')
  • python/demos/demo_filterbank_slaney.py

    r416ddd1 r4120fbc  
    1212from pylab import loglog, title, show, xlim, ylim, xlabel, ylabel
    1313xlim([0,samplerate / 2])
    14 times = vstack([arange(win_s / 2 + 1) * samplerate / win_s] * 40)
     14times = vstack([arange(win_s // 2 + 1) * samplerate / win_s] * 40)
    1515loglog(times.T, f.get_coeffs().T, '.-')
    1616title('Mel frequency bands coefficients')
  • python/demos/demo_filterbank_triangle_bands.py

    r416ddd1 r4120fbc  
    1717subplot(211)
    1818title('Examples of filterbank built with set_triangle_bands and set_coeffs')
    19 times = vstack([arange(win_s / 2 + 1) * samplerate / win_s] * n_filters)
     19times = vstack([arange(win_s // 2 + 1) * samplerate / win_s] * n_filters)
    2020loglog(times.T, f.get_coeffs().T, '.-')
    2121xlim([50, samplerate/2])
     
    3838
    3939subplot(212)
    40 times = vstack([arange(win_s / 2 + 1) * samplerate / win_s] * n_filters)
     40times = vstack([arange(win_s // 2 + 1) * samplerate / win_s] * n_filters)
    4141loglog(times.T, f.get_coeffs().T, '.-')
    4242xlim([50, samplerate/2])
  • python/demos/demo_mel-energy.py

    r416ddd1 r4120fbc  
    66
    77win_s = 512                 # fft size
    8 hop_s = win_s / 4           # hop size
     8hop_s = win_s // 4          # hop size
    99
    1010if len(sys.argv) < 2:
    11     print "Usage: %s <filename> [samplerate]" % sys.argv[0]
     11    print("Usage: %s <filename> [samplerate]" % sys.argv[0])
    1212    sys.exit(1)
    1313
     
    3535    fftgrain = pv(samples)
    3636    new_energies = f(fftgrain)
    37     print '%f' % (total_frames / float(samplerate) ),
    38     print ' '.join(['%f' % b for b in new_energies])
     37    timestr = '%f' % (total_frames / float(samplerate) )
     38    print('{:s} {:s}'.format(timestr, ' '.join(['%f' % b for b in new_energies])))
    3939    energies = vstack( [energies, new_energies] )
    4040    total_frames += read
     
    4242
    4343if 1:
    44     print "done computing, now plotting"
     44    print("done computing, now plotting")
    4545    import matplotlib.pyplot as plt
    4646    from demo_waveform_plot import get_waveform_plot
  • python/demos/demo_mfcc.py

    r416ddd1 r4120fbc  
    66
    77win_s = 512                 # fft size
    8 hop_s = win_s / 4           # hop size
     8hop_s = win_s // 4          # hop size
    99n_filters = 40              # must be 40 for mfcc
    1010n_coeffs = 13
     
    1212
    1313if len(sys.argv) < 2:
    14     print "Usage: %s <source_filename>" % sys.argv[0]
     14    print("Usage: %s <source_filename>" % sys.argv[0])
    1515    sys.exit(1)
    1616
  • python/demos/demo_onset.py

    r416ddd1 r4120fbc  
    55
    66win_s = 512                 # fft size
    7 hop_s = win_s / 2           # hop size
     7hop_s = win_s // 2          # hop size
    88
    99if len(sys.argv) < 2:
    10     print "Usage: %s <filename> [samplerate]" % sys.argv[0]
     10    print("Usage: %s <filename> [samplerate]" % sys.argv[0])
    1111    sys.exit(1)
    1212
     
    2929    samples, read = s()
    3030    if o(samples):
    31         print "%f" % o.get_last_s()
     31        print("%f" % o.get_last_s())
    3232        onsets.append(o.get_last())
    3333    total_frames += read
  • python/demos/demo_onset_plot.py

    r416ddd1 r4120fbc  
    66
    77win_s = 512                 # fft size
    8 hop_s = win_s / 2           # hop size
     8hop_s = win_s // 2          # hop size
    99
    1010if len(sys.argv) < 2:
    11     print "Usage: %s <filename> [samplerate]" % sys.argv[0]
     11    print("Usage: %s <filename> [samplerate]" % sys.argv[0])
    1212    sys.exit(1)
    1313
     
    3535    samples, read = s()
    3636    if o(samples):
    37         print "%f" % (o.get_last_s())
     37        print("%f" % (o.get_last_s()))
    3838        onsets.append(o.get_last())
    3939    # keep some data to plot it later
    40     new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0)
     40    new_maxes = (abs(samples.reshape(hop_s//downsample, downsample))).max(axis=0)
    4141    allsamples_max = hstack([allsamples_max, new_maxes])
    4242    desc.append(o.get_descriptor())
     
    6262    plt1.yaxis.set_visible(False)
    6363    desc_times = [ float(t) * hop_s / samplerate for t in range(len(desc)) ]
    64     desc_plot = [d / max(desc) for d in desc]
     64    desc_max = max(desc) if max(desc) != 0 else 1.
     65    desc_plot = [d / desc_max for d in desc]
    6566    plt2.plot(desc_times, desc_plot, '-g')
    66     tdesc_plot = [d / max(desc) for d in tdesc]
     67    tdesc_plot = [d / desc_max for d in tdesc]
    6768    for stamp in onsets:
    6869        stamp /= float(samplerate)
  • python/demos/demo_pitch.py

    r416ddd1 r4120fbc  
    55
    66if len(sys.argv) < 2:
    7     print "Usage: %s <filename> [samplerate]" % sys.argv[0]
     7    print("Usage: %s <filename> [samplerate]" % sys.argv[0])
    88    sys.exit(1)
    99
     
    1111
    1212downsample = 1
    13 samplerate = 44100 / downsample
     13samplerate = 44100 // downsample
    1414if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
    1515
    16 win_s = 4096 / downsample # fft size
    17 hop_s = 512  / downsample # hop size
     16win_s = 4096 // downsample # fft size
     17hop_s = 512  // downsample # hop size
    1818
    1919s = source(filename, samplerate, hop_s)
     
    3737    confidence = pitch_o.get_confidence()
    3838    #if confidence < 0.8: pitch = 0.
    39     #print "%f %f %f" % (total_frames / float(samplerate), pitch, confidence)
     39    print("%f %f %f" % (total_frames / float(samplerate), pitch, confidence))
    4040    pitches += [pitch]
    4141    confidences += [confidence]
  • python/demos/demo_pitch_sinusoid.py

    r416ddd1 r4120fbc  
    2323freqs = np.zeros(sin_length)
    2424
    25 partition = sin_length / 8
     25partition = sin_length // 8
    2626pointer = 0
    2727
     
    4545    p = aubio.pitch(method, buf_size, hop_size, samplerate)
    4646    cands[method] = run_pitch(p, a)
    47     print cands[method]
     47    print(method)
     48    print(cands[method])
    4849
    49 print "done computing"
     50print("done computing")
    5051
    5152if 1:
  • python/demos/demo_pysoundcard_record.py

    r416ddd1 r4120fbc  
    1111    s = Stream(blocksize = hop_size, channels = 1)
    1212    g = sink(sink_path, samplerate = int(s.samplerate))
    13     print s.channels
    1413
    1514    s.start()
     
    2322            total_frames += hop_size
    2423    except KeyboardInterrupt:
    25         print "stopped after", "%.2f seconds" % (total_frames / s.samplerate)
     24        duration = total_frames / float(s.samplerate)
     25        print("stopped after %.2f seconds" % duration)
    2626    s.stop()
    2727
  • python/demos/demo_simple_robot_voice.py

    r416ddd1 r4120fbc  
    66if __name__ == '__main__':
    77    if len(sys.argv) < 2:
    8         print 'usage: %s <inputfile> <outputfile>' % sys.argv[0]
     8        print('usage: %s <inputfile> <outputfile>' % sys.argv[0])
    99        sys.exit(1)
    1010    samplerate = 44100
     
    1414
    1515    win_s = 512                          # fft size
    16     hop_s = win_s / 2                    # hop size
     16    hop_s = win_s // 2                   # hop size
    1717    pv = pvoc(win_s, hop_s)              # phase vocoder
    1818
     
    2626        total_frames += read
    2727
    28     print "wrote", total_frames, "from", f.uri, "to", g.uri
     28    format_str = "read {:d} samples from {:s}, written to {:s}"
     29    print(format_str.format(total_frames, f.uri, g.uri))
  • python/demos/demo_simple_spectral_weighting.py

    r416ddd1 r4120fbc  
    1414if __name__ == '__main__':
    1515    if len(sys.argv) < 2:
    16         print 'usage: %s <inputfile> <outputfile>' % sys.argv[0]
     16        print('usage: %s <inputfile> <outputfile>' % sys.argv[0])
    1717        sys.exit(1)
    1818    samplerate = 0
     
    2323
    2424    win_s = 512 # fft size
    25     hop_s = win_s / 2 # hop size
     25    hop_s = win_s // 2 # hop size
    2626    pv = pvoc(win_s, hop_s) # phase vocoder
    2727
     
    3131        zeros( 50 ),
    3232        1.3 * hanningz(100),
    33         zeros (win_s / 2 + 1 - 40 - 50 - 100),
     33        zeros (win_s // 2 + 1 - 40 - 50 - 100),
    3434        ] )
    3535
     
    5353        total_frames += read
    5454
    55     print "read", total_frames / float(samplerate), "seconds from", f.uri
     55    duration = total_frames / float(samplerate)
     56    print("read {:.3f}s from {:s}".format(duration, f.uri))
  • python/demos/demo_sink.py

    r416ddd1 r4120fbc  
    66if __name__ == '__main__':
    77    if len(sys.argv) < 3:
    8         print 'usage: %s <inputfile> <outputfile> [samplerate] [hop_size]' % sys.argv[0]
     8        print('usage: %s <inputfile> <outputfile> [samplerate] [hop_size]' % sys.argv[0])
    99        sys.exit(1)
    1010
     
    2323        g(vec, read)
    2424        total_frames += read
    25     print "wrote", "%.2fs" % (total_frames / float(samplerate) ),
    26     print "(", total_frames, "frames", "in",
    27     print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
    28     print "from", f.uri,
    29     print "to", g.uri
     25    outstr = "wrote %.2fs" % (total_frames / float(samplerate))
     26    outstr += " (%d frames in" % total_frames
     27    outstr += " %d blocks" % (total_frames // f.hop_size)
     28    outstr += " at %dHz)" % f.samplerate
     29    outstr += " from " + f.uri
     30    outstr += " to " + g.uri
     31    print(outstr)
  • python/demos/demo_sink_create_woodblock.py

    r416ddd1 r4120fbc  
    33import sys
    44from math import pi, e
    5 from aubio import sink
     5from aubio import sink, float_type
    66from numpy import arange, sin, exp, zeros
    77
    88if len(sys.argv) < 2:
    9     print 'usage: %s <outputfile> [samplerate]' % sys.argv[0]
     9    print('usage: %s <outputfile> [samplerate]' % sys.argv[0])
    1010    sys.exit(1)
    1111
     
    2626# create a sine lookup table
    2727tablelen = 1000
    28 sinetable = arange(tablelen + 1, dtype = 'float32')
     28sinetable = arange(tablelen + 1, dtype = float_type)
    2929sinetable = 0.7 * sin(twopi * sinetable/tablelen)
    30 sinetone = zeros((duration,), dtype = 'float32')
     30sinetone = zeros((duration,), dtype = float_type)
    3131
    3232# compute sinetone at floating point period
     
    4040
    4141# apply some envelope
    42 float_ramp = arange(duration, dtype = 'float32')
     42float_ramp = arange(duration, dtype = float_type)
    4343sinetone *= exp( - e * float_ramp / duration / decay)
    4444sinetone[:attack] *= exp( e * ( float_ramp[:attack] / attack - 1 ) )
  • python/demos/demo_sink_multi.py

    r416ddd1 r4120fbc  
    66if __name__ == '__main__':
    77    if len(sys.argv) < 3:
    8         print 'usage: %s <inputfile> <outputfile> [samplerate] [hop_size]' % sys.argv[0]
     8        print('usage: %s <inputfile> <outputfile> [samplerate] [hop_size]' % sys.argv[0])
    99        sys.exit(1)
    1010
     
    2323        g.do_multi(vec, read)
    2424        total_frames += read
    25     print "wrote", "%.2fs" % (total_frames / float(samplerate) ),
    26     print "(", total_frames, "frames", "in",
    27     print total_frames / f.hop_size, "blocks",
    28     print "of", f.channels, "channels",
    29     print "at", "%dHz" % f.samplerate, ")",
    30     print "from", f.uri,
    31     print "to", g.uri
     25    outstr = "wrote %.2fs" % (total_frames / float(samplerate))
     26    outstr += " (%d frames in" % total_frames
     27    outstr += " %d blocks" % (total_frames // f.hop_size)
     28    outstr += " of %d channels" % f.channels
     29    outstr += " at %dHz)" % f.samplerate
     30    outstr += " from " + f.uri
     31    outstr += " to " + g.uri
     32    print(outstr)
  • python/demos/demo_slicing.py

    r416ddd1 r4120fbc  
    77if __name__ == '__main__':
    88    if len(sys.argv) < 3:
    9         print 'usage: %s <inputfile> <duration>' % sys.argv[0]
     9        print('usage: %s <inputfile> <duration>' % sys.argv[0])
    1010        sys.exit(1)
    1111    source_file = sys.argv[1]
     
    4545    total_duration = total_frames_written / float(samplerate)
    4646    slice_n += 1
    47     print 'created %(slice_n)s slices from %(source_base_name)s%(source_ext)s' % locals(),
    48     print ' (total duration %(total_duration).2fs)' % locals()
     47    outstr = 'created %(slice_n)s slices from %(source_base_name)s%(source_ext)s' % locals()
     48    outstr += ' (total duration %(total_duration).2fs)' % locals()
     49    print(outstr)
    4950    # close source and sink files
    5051    del f, g
  • python/demos/demo_source.py

    r416ddd1 r4120fbc  
    66if __name__ == '__main__':
    77    if len(sys.argv) < 2:
    8         print 'usage: %s <inputfile> [samplerate] [hop_size]' % sys.argv[0]
     8        print('usage: %s <inputfile> [samplerate] [hop_size]' % sys.argv[0])
    99        sys.exit(1)
    1010    samplerate = 0
     
    2121        total_frames += read
    2222        if read < f.hop_size: break
    23     print "read", "%.2fs" % (total_frames / float(samplerate) ),
    24     print "(", total_frames, "frames", "in",
    25     print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
    26     print "from", f.uri
     23    outstr = "read %.2fs" % (total_frames / float(samplerate))
     24    outstr += " (%d frames in" % total_frames
     25    outstr += " %d blocks" % (total_frames // f.hop_size)
     26    outstr += " at %dHz)" % f.samplerate
     27    outstr += " from " + f.uri
     28    print(outstr)
  • python/demos/demo_specdesc.py

    r416ddd1 r4120fbc  
    22
    33import sys
     4import numpy as np
    45from aubio import fvec, source, pvoc, specdesc
    5 from numpy import hstack
    66
    77win_s = 512                 # fft size
    8 hop_s = win_s / 4           # hop size
     8hop_s = win_s // 4          # hop size
    99
    1010if len(sys.argv) < 2:
    11     print "Usage: %s <filename> [samplerate]" % sys.argv[0]
     11    print("Usage: %s <filename> [samplerate]" % sys.argv[0])
    1212    sys.exit(1)
    1313
     
    3131for method in methods:
    3232    cands = []
    33     all_descs[method] = fvec(0)
     33    all_descs[method] = np.array([])
    3434    o[method] = specdesc(method, win_s)
    3535
     
    4040    samples, read = s()
    4141    fftgrain = pv(samples)
    42     #print "%f" % ( total_frames / float(samplerate) ),
     42    #outstr = "%f" % ( total_frames / float(samplerate) )
    4343    for method in methods:
    4444        specdesc_val = o[method](fftgrain)[0]
    45         all_descs[method] = hstack ( [all_descs[method], specdesc_val] )
    46         #print "%f" % specdesc_val,
    47     #print
     45        all_descs[method] = np.append(all_descs[method], specdesc_val)
     46        #outstr += " %f" % specdesc_val
     47    #print(outstr)
    4848    total_frames += read
    4949    if read < hop_s: break
    5050
    5151if 1:
    52     print "done computing, now plotting"
     52    print("done computing, now plotting")
    5353    import matplotlib.pyplot as plt
    5454    from demo_waveform_plot import get_waveform_plot
  • python/demos/demo_spectrogram.py

    r416ddd1 r4120fbc  
    22
    33import sys, os.path
    4 from aubio import pvoc, source
     4from aubio import pvoc, source, float_type
    55from numpy import zeros, log10, vstack
    66import matplotlib.pyplot as plt
     
    88def get_spectrogram(filename, samplerate = 0):
    99    win_s = 512                                        # fft window size
    10     hop_s = win_s / 2                                  # hop size
    11     fft_s = win_s / 2 + 1                              # spectrum bins
     10    hop_s = win_s // 2                                 # hop size
     11    fft_s = win_s // 2 + 1                             # spectrum bins
    1212
    1313    a = source(filename, samplerate, hop_s)            # source file
    1414    if samplerate == 0: samplerate = a.samplerate
    1515    pv = pvoc(win_s, hop_s)                            # phase vocoder
    16     specgram = zeros([0, fft_s], dtype='float32')      # numpy array to store spectrogram
     16    specgram = zeros([0, fft_s], dtype=float_type)     # numpy array to store spectrogram
    1717
    1818    # analysis
     
    2929    time_step = hop_s / float(samplerate)
    3030    total_time = len(specgram) * time_step
    31     print "total time: %0.2fs" % total_time,
    32     print ", samplerate: %.2fkHz" % (samplerate / 1000.)
     31    outstr = "total time: %0.2fs" % total_time
     32    print(outstr + ", samplerate: %.2fkHz" % (samplerate / 1000.))
    3333    n_xticks = 10
    3434    n_yticks = 10
     
    6666if __name__ == '__main__':
    6767    if len(sys.argv) < 2:
    68         print "Usage: %s <filename>" % sys.argv[0]
     68        print("Usage: %s <filename>" % sys.argv[0])
    6969    else:
    7070        for soundfile in sys.argv[1:]:
    7171            fig = get_spectrogram(soundfile)
    7272            # display graph
    73             fig.show()
     73            plt.show()
    7474            #outimage = os.path.basename(soundfile) + '.png'
    7575            #print ("writing: " + outimage)
  • python/demos/demo_tempo.py

    r416ddd1 r4120fbc  
    55
    66win_s = 512                 # fft size
    7 hop_s = win_s / 2           # hop size
     7hop_s = win_s // 2          # hop size
    88
    99if len(sys.argv) < 2:
    10     print "Usage: %s <filename> [samplerate]" % sys.argv[0]
     10    print("Usage: %s <filename> [samplerate]" % sys.argv[0])
    1111    sys.exit(1)
    1212
     
    3434    if is_beat:
    3535        this_beat = int(total_frames - delay + is_beat[0] * hop_s)
    36         print "%f" % (this_beat / float(samplerate))
     36        print("%f" % (this_beat / float(samplerate)))
    3737        beats.append(this_beat)
    3838    total_frames += read
  • python/demos/demo_tempo_plot.py

    r416ddd1 r4120fbc  
    55
    66win_s = 512                 # fft size
    7 hop_s = win_s / 2           # hop size
     7hop_s = win_s // 2          # hop size
    88
    99if len(sys.argv) < 2:
    10     print "Usage: %s <filename> [samplerate]" % sys.argv[0]
     10    print("Usage: %s <filename> [samplerate]" % sys.argv[0])
    1111    sys.exit(1)
    1212
     
    4343    import matplotlib.pyplot as plt
    4444    bpms = 60./ diff(beats)
    45     print 'mean period:', "%.2f" % mean(bpms), 'bpm', 'median', "%.2f" % median(bpms), 'bpm'
    46     print 'plotting', filename
     45    print('mean period: %.2fbpm, median: %.2fbpm' % (mean(bpms), median(bpms)))
     46    print('plotting %s' % filename)
    4747    plt1 = plt.axes([0.1, 0.75, 0.8, 0.19])
    4848    plt2 = plt.axes([0.1, 0.1, 0.8, 0.65], sharex = plt1)
     
    7676
    7777else:
    78     print 'mean period:', "%.2f" % 0, 'bpm', 'median', "%.2f" % 0, 'bpm',
    79     print 'nothing to plot, file too short?'
     78    print('mean period: %.2fbpm, median: %.2fbpm' % (0, 0))
     79    print('plotting %s' % filename)
  • python/demos/demo_tss.py

    r416ddd1 r4120fbc  
    66if __name__ == '__main__':
    77    if len(sys.argv) < 2:
    8         print 'usage: %s <inputfile> <outputfile_transient> <outputfile_steady>' % sys.argv[0]
     8        print('usage: %s <inputfile> <outputfile_transient> <outputfile_steady>' % sys.argv[0])
    99        sys.exit(1)
    1010
    1111    samplerate = 44100
    12     win_s = 1024      # fft size
    13     hop_s = win_s / 4 # block size
     12    win_s = 1024       # fft size
     13    hop_s = win_s // 4 # block size
    1414    threshold = 0.5
    1515
  • python/demos/demo_waveform_plot.py

    r416ddd1 r4120fbc  
    2222        samples, read = a()
    2323        # keep some data to plot it later
    24         new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0)
     24        new_maxes = (abs(samples.reshape(hop_s//downsample, downsample))).max(axis=0)
    2525        allsamples_max = hstack([allsamples_max, new_maxes])
    2626        total_frames += read
     
    4949    import matplotlib.pyplot as plt
    5050    if len(sys.argv) < 2:
    51         print "Usage: %s <filename>" % sys.argv[0]
     51        print("Usage: %s <filename>" % sys.argv[0])
    5252    else:
    5353        for soundfile in sys.argv[1:]:
Note: See TracChangeset for help on using the changeset viewer.