shithub: aubio

ref: 0c77921f75bf0a825abe51ef97d9812529a9fa98
dir: /python.old/aubioplot-yinfft/

View raw version
#! /usr/bin/python

""" this file was written by Paul Brossier 
  it is released under the GNU/GPL license.
"""

import sys,time
from aubio.task import task,taskparams
from aubio.aubioclass import fvec
from aubio.gnuplot import gnuplot_create
from aubio.aubiowrapper import *

usage = "usage: %s [options] -i soundfile" % sys.argv[0]

def parse_args():
        from optparse import OptionParser
        parser = OptionParser(usage=usage)
        parser.add_option("-i","--input",
                          action="store", dest="filename", 
                          help="input sound file")
        parser.add_option("-n","--printframe",
                          action="store", dest="printframe", default=-1, 
                          help="make a plot of the n_th frame")
        parser.add_option("-x","--xsize",
                          action="store", dest="xsize", default=1., 
                          help="define xsize for plot")
        parser.add_option("-y","--ysize",
                          action="store", dest="ysize", default=1., 
                          help="define ysize for plot")
        parser.add_option("-O","--outplot",
                          action="store", dest="outplot", default=None, 
                          help="save plot to output.{ps,png}")
        (options, args) = parser.parse_args()
        if not options.filename: 
                 print "no file name given\n", usage
                 sys.exit(1)
        return options, args

def plotdata(x,y,plottitle="",**keyw):
	import Gnuplot
	return Gnuplot.Data(x, y, title="%s" % plottitle,**keyw)

options, args = parse_args()
filename = options.filename
xsize = float(options.xsize)
ysize = float(options.ysize)*2

printframe = int(options.printframe)
if printframe == -1:
  print "Will wait for ^D to skip to next plot"
  print "Press enter before to print to file"


g = gnuplot_create()
params = taskparams()
params.hopsize = 2048 # 512 
params.bufsize = params.hopsize #2048
taskfile = task(filename,params=params)

yin = fvec(params.bufsize/2,1)

t = [i for i in range(params.bufsize)]
a = [0 for i in range(params.bufsize)]

while (taskfile.readsize == params.hopsize):
  taskfile()

  n = [i for i in range(params.bufsize/2)]
  a = [taskfile.myvec.get(i,0) for i in range(params.hopsize/2)]
  aubio_pitchyin_diff(taskfile.myvec(),yin()) # compute d[t]
  c = [yin.get(i,0) for i in range(params.bufsize/2)]
  aubio_pitchyin_getcum(yin()) # compute d'[t]
  y = [yin.get(i,0) for i in range(params.bufsize/2)]
  thresh = [0.1 for i in range(params.bufsize/2)]
  #t.append((i/float(params.hopsize)+taskfile.frameread)*params.step),t.pop(0)
  d = [plotdata(n,a,plottitle="signal", with_='lines'),
    plotdata(n,c,plottitle="d[t]",axes='x1y2', with_='lines lt 1'),
    plotdata(n,y,plottitle="d'[t]",axes='x1y1', with_='lines lt 2'),
    plotdata(n,thresh,plottitle="threshold",axes='x1y1', with_='lines lt 3')]
  #g('set xrange [%f:%f]' % (t[0],t[-1]))
  #time.sleep(.2)
  g.reset()
  g('set yrange [-1:3]')
  g('set xrange [0:%d]' % (params.bufsize/2))
  g('set title \"%s\"' %  "Example of period detection using YIN")
  if printframe == -1:
    g.replot(*d)
    a = sys.stdin.read()
  if a == "\n" or printframe == taskfile.frameread:
      from os.path import basename
      outplot = "_".join([basename(sys.argv[0]),'_'.join(basename(filename).split('.')),"%d" % taskfile.frameread])
      print outplot
      f = gnuplot_create(outplot=outplot,extension='ps')
      f('set size %f,%f;' % (xsize,ysize) )
      f('set lmargin %f' % (15*xsize))
      f('set rmargin %f' % (10*xsize))
      #f('set title \"%s\"' %  "Example of period detection using YIN")
      f('set multiplot')
      f.ylabel('amplitude',offset=(+.5,0))
      f.xlabel('time (samples)')
      f('set size %f,%f;' % (xsize,ysize*0.4) )
      f('set orig %f,%f;' % (0,ysize*0.6) )
      sigmax = max(abs(min(a)),abs(max(a)))
      f('set yrange [%f:%f]' % (-1.3*sigmax,1.3*sigmax))
      f('set xrange [0:%d]' % (params.bufsize/2))
      f.plot(d[0])

      f.ylabel('')
      f.xlabel('lag (samples)')
      f('set bmargin %f' % (4*ysize))
      f('set size %f,%f;' % (xsize,ysize*0.6) )
      f('set orig %f,%f;' % (0,0) )
      f('set autoscale')
      f('set xrange [0:%d]' % (params.bufsize/2))
      f('set notitle')
      f('set y2tics')
      f('set ytics nomirror')
      f('set noytics')
      f('set key right')
      f.plot(d[1])

      f.ylabel('amplitude')
      f.xlabel('')
      f('set y2tics nomirror')
      f('set ytics nomirror')
      f('set noy2tics')
      f('set noxtics')
      f('set ytics')
      f('set key left')
      f.plot(d[2],d[3])
      #f('set yrange [-1:3]')
      #f.plot(*d)
      print "saved plot", outplot, 'ps'
  elif printframe < taskfile.frameread:
      break