Changeset 5d5d6b9


Ignore:
Timestamp:
Mar 8, 2013, 11:01:55 PM (12 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:
daa0d5d
Parents:
2cedc83
Message:

python/demos: add demo_pitch.py and demo_waveform_plot.py

Location:
python/demos
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • python/demos/demo_onset.py

    r2cedc83 r5d5d6b9  
    1818s = source(filename, samplerate, hop_s)
    1919samplerate = s.samplerate
     20
    2021o = onset("default", win_s, hop_s, samplerate)
    2122
  • python/demos/demo_simple_robot_voice.py

    r2cedc83 r5d5d6b9  
    2020    samples, read = f()
    2121    spectrum = pv(samples)            # compute spectrum
     22    #spectrum.norm *= .8               # reduce amplitude a bit
    2223    spectrum.phas[:] = 0.             # zero phase
    2324    new_samples = pv.rdo(spectrum)    # compute modified samples
  • python/demos/demo_specdesc.py

    r2cedc83 r5d5d6b9  
    11#! /usr/bin/env python
    22
    3 from numpy import random, sin, arange, ones, zeros
    4 from math import pi
    5 from aubio import fvec, onset
     3import sys
     4from aubio import fvec, source, pvoc, specdesc
     5from numpy import hstack
    66
    7 def build_sinusoid(length, freqs, samplerate):
    8   return sin( 2. * pi * arange(length) * freqs / samplerate)
     7win_s = 512                 # fft size
     8hop_s = win_s / 4           # hop size
    99
    10 def run_onset(p, input_vec):
    11   f = fvec (p.hop_size)
    12   cands = []
    13   count = 0
    14   for vec_slice in input_vec.reshape((-1, p.hop_size)):
    15     f[:] = vec_slice
    16     cands.append(o(f))
    17   return cands
     10if len(sys.argv) < 2:
     11    print "Usage: %s <filename> [samplerate]" % sys.argv[0]
     12    sys.exit(1)
    1813
    19 methods = ['default',
    20            'energy',
    21            'complex',
    22            'phase',
    23            'specdiff',
    24            'kl',
    25            'mkl',
    26            'specflux',
    27            'centroid',
    28            'spread',
    29            'skewness',
    30            'kurtosis',
    31            'slope',
    32            'decrease',
    33            'rolloff',
    34           ]
     14filename = sys.argv[1]
    3515
    36 cands = {}
    37 buf_size = 2048
    38 hop_size = 512
    39 samplerate = 44100
    40 sin_length = (samplerate * 10) % 512 * 512
    41 freqs = zeros(sin_length)
     16samplerate = 0
     17if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
    4218
    43 partition = sin_length / 8
    44 pointer = 0
     19s = source(filename, samplerate, hop_s)
     20samplerate = s.samplerate
    4521
    46 pointer += partition
    47 freqs[pointer: pointer + partition] = 440
     22pv = pvoc(win_s, hop_s)
    4823
    49 pointer += partition
    50 pointer += partition
    51 freqs[ pointer : pointer + partition ] = 740
     24methods = ['default', 'energy', 'hfc', 'complex', 'phase', 'specdiff', 'kl', 'mkl',
     25    'specflux', 'centroid', 'spread', 'skewness', 'kurtosis', 'slope', 'decrease',
     26    'rolloff', ]
    5227
    53 pointer += partition
    54 freqs[ pointer : pointer + partition ] = 1480
    55 
    56 pointer += partition
    57 pointer += partition
    58 freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8)
    59 
    60 a = build_sinusoid(sin_length, freqs, samplerate)
     28all_descs = {}
     29o = {}
    6130
    6231for method in methods:
    63   o = onset(method, buf_size, hop_size, samplerate)
    64   cands[method] = run_onset(o, a)
     32    cands = []
     33    all_descs[method] = fvec(0)
     34    o[method] = specdesc(method, win_s)
    6535
    66 print "done computing"
     36total_frames = 0
     37downsample = 2
     38
     39while True:
     40    samples, read = s()
     41    fftgrain = pv(samples)
     42    print "%f" % ( total_frames / float(samplerate) ),
     43    for method in methods:
     44        specdesc_val = o[method](fftgrain)[0]
     45        all_descs[method] = hstack ( [all_descs[method], specdesc_val] )
     46        print "%f" % specdesc_val,
     47    print
     48    total_frames += read
     49    if read < hop_s: break
    6750
    6851if 1:
    69   from pylab import plot, show, xlabel, ylabel, legend, ylim, subplot
    70   subplot (211)
    71   legend(methods+['ground truth'], 'upper right')
    72   xlabel('time (s)')
    73   ylabel('amplitude')
    74   ramp = arange(0, sin_length).astype('float') / samplerate
    75   plot(ramp, a, ':')
    76   subplot (212)
    77   ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate
    78   for method in methods:
    79     plot(ramp, cands[method],'.-')
    80     legend(methods, 'upper right')
    81     xlabel('time (s)')
    82   ylabel('spectral descriptor value')
    83   show()
     52    print "done computing, now plotting"
     53    import matplotlib.pyplot as plt
     54    from demo_waveform_plot import get_waveform_plot
     55    fig = plt.figure()
     56    plt.rc('lines',linewidth='.8')
     57    wave = plt.axes([0.1, 0.75, 0.8, 0.19])
     58    get_waveform_plot(filename, samplerate, ax = wave )
     59    wave.yaxis.set_visible(False)
     60    wave.xaxis.set_visible(False)
    8461
     62    all_desc_times = [ x * hop_s  for x in range(len(all_descs["default"])) ]
     63    n_methods = len(methods)
     64    for i, method in enumerate(methods):
     65        #ax = fig.add_subplot (n_methods, 1, i)
     66        #plt2 = plt.axes([0.1, 0.1, 0.8, 0.65], sharex = plt1)
     67        ax = plt.axes ( [0.1, 0.75 - ((i+1) * 0.65 / n_methods),  0.8, 0.65 / n_methods], sharex = wave )
     68        ax.plot(all_desc_times, all_descs[method], '-', label = method)
     69        #ax.set_ylabel(method, rotation = 0)
     70        ax.xaxis.set_visible(False)
     71        ax.yaxis.set_visible(False)
     72        ax.axis(xmax = all_desc_times[-1], xmin = all_desc_times[0])
     73        ax.annotate(method, xy=(-10, 10),  xycoords='axes points',
     74                horizontalalignment='right', verticalalignment='bottom',
     75                )
     76    if all_desc_times[-1] / float(samplerate) > 60:
     77        plt.xlabel('time (mm:ss)')
     78        ax.set_xticklabels([ "%02d:%02d" % (t/float(samplerate)/60, (t/float(samplerate))%60) for t in ax.get_xticks()[:-1]], rotation = 50)
     79    else:
     80        plt.xlabel('time (ss.mm)')
     81        ax.set_xticklabels([ "%02d.%02d" % (t/float(samplerate), 100*((t/float(samplerate))%1) ) for t in ax.get_xticks()[:-1]], rotation = 50)
     82    #plt.ylabel('spectral descriptor value')
     83    ax.xaxis.set_visible(True)
     84    plt.show()
Note: See TracChangeset for help on using the changeset viewer.