shithub: aubio

Download patch

ref: 85ebab8aa7504e6e9ffc700657dcfc35dd058668
parent: d00e22308e1b4b3bd36da7de94c169298f9c8fef
parent: dea34a1fa660219ac8f27471ec65da0bb16c8b72
author: Paul Brossier <piem@piem.org>
date: Fri Jul 27 06:39:20 EDT 2012

Merge branch 'develop' into io

--- /dev/null
+++ b/interfaces/python/demo_spectrogram.py
@@ -1,0 +1,45 @@
+#! /usr/bin/python
+
+import sys
+from aubio import pvoc, source
+from numpy import array, arange, zeros, shape, log10, vstack
+from pylab import imshow, show, cm, axis, ylabel, xlabel, xticks, yticks
+
+def get_spectrogram(filename):
+  samplerate = 44100
+  win_s = 512                                        # fft window size
+  hop_s = win_s / 2                                  # hop size
+  fft_s = win_s / 2 + 1                              # spectrum bins
+
+  a = source(filename, samplerate, hop_s)            # source file
+  pv = pvoc(win_s, hop_s)                            # phase vocoder
+  specgram = zeros([0, fft_s], dtype='float32')      # numpy array to store spectrogram
+
+  # analysis
+  while True:
+    samples, read = a()                              # read file
+    specgram = vstack((specgram,pv(samples).norm))   # store new norm vector
+    if read < a.hop_size: break
+
+  # plotting
+  imshow(log10(specgram.T), origin = 'bottom', aspect = 'auto', cmap=cm.gray_r)
+  axis([0, len(specgram), 0, len(specgram[0])])
+  ylabel('Frequency (Hz)')
+  xlabel('Time (s)')
+  # show axes in Hz and seconds
+  time_step = hop_s / float(samplerate)
+  total_time = len(specgram) * time_step
+  ticks = 10
+  xticks( arange(ticks) / float(ticks) * len(specgram),
+      [x * total_time / float(ticks) for x in range(ticks) ] )
+  yticks( arange(ticks) / float(ticks) * len(specgram[0]),
+      [x * samplerate / 2. / float(ticks) for x in range(ticks) ] )
+
+if __name__ == '__main__':
+  if len(sys.argv) < 2:
+    print "Usage: %s <filename>" % sys.argv[0]
+  else:
+    for soundfile in sys.argv[1:]:
+      get_spectrogram(soundfile)
+      # display graph
+      show()
--- /dev/null
+++ b/interfaces/python/demo_tss.py
@@ -1,0 +1,47 @@
+#! /usr/bin/python
+
+import sys
+from aubio import source, sink, pvoc, tss
+
+if __name__ == '__main__':
+  if len(sys.argv) < 2:
+    print 'usage: %s <inputfile> <outputfile_transient> <outputfile_steady>' % sys.argv[0]
+    sys.exit(1)
+
+  samplerate = 44100
+  win_s = 512                 # fft size
+  hop_s = win_s / 2           # block size
+  threshold = 0.26
+
+  f = source(sys.argv[1], samplerate, hop_s)
+  g = sink(sys.argv[2], samplerate)
+  h = sink(sys.argv[3], samplerate)
+
+  pv = pvoc(win_s, hop_s)     # phase vocoder
+  pw = pvoc(win_s, hop_s)     # another phase vocoder
+  t = tss(win_s, hop_s)       # transient steady state separation
+
+  t.set_threshold(threshold)
+
+  read = hop_s
+
+  while read:
+    samples, read = f()               # read file
+    spec = pv(samples)                # compute spectrum
+    trans_spec, stead_spec = t(spec)  # transient steady-state separation
+    transients = pv.rdo(trans_spec)   # overlap-add synthesis of transients
+    steadstate = pw.rdo(stead_spec)   # overlap-add synthesis of steady states
+    g(transients, read)               # write transients to output
+    h(steadstate, read)               # write steady states to output
+
+  del f, g, h                         # finish writing the files now
+
+  from demo_spectrogram import get_spectrogram
+  from pylab import subplot, show
+  subplot(311)
+  get_spectrogram(sys.argv[1])
+  subplot(312)
+  get_spectrogram(sys.argv[2])
+  subplot(313)
+  get_spectrogram(sys.argv[3])
+  show()
--- a/src/spectral/tss.c
+++ b/src/spectral/tss.c
@@ -60,32 +60,30 @@
         -2.0*theta1[j]+theta2[j]);
     theta2[j] = theta1[j];
     theta1[j] = input->phas[j];
-  }
 
-  for (j=0;j<nbins; j++){
     /* transient analysis */
     test = (ABS(dev[j]) > parm*oft1[j]);
     trans->norm[j] = input->norm[j] * test;
     trans->phas[j] = input->phas[j] * test;
-  }
 
-  for (j=0;j<nbins; j++){
     /* steady state analysis */
     test = (ABS(dev[j]) < parm*oft2[j]);
     stead->norm[j] = input->norm[j] * test;
     stead->phas[j] = input->phas[j] * test;
 
-    /*increase sstate probability for sines */
+    /*increase probability for transient */
     test = (trans->norm[j]==0.);
     oft1[j]  = test;
-    test = (stead->norm[j]==0.);
-    oft2[j]  = test;
     test = (trans->norm[j]>0.);
     oft1[j] += alpha*test;
-    test = (stead->norm[j]>0.);
-    oft2[j] += alpha*test;
     test = (oft1[j]>1. && trans->norm[j]>0.);
     oft1[j] += beta*test;
+
+    /*increase probability for steady states */
+    test = (stead->norm[j]==0.);
+    oft2[j]  = test;
+    test = (stead->norm[j]>0.);
+    oft2[j] += alpha*test;
     test = (oft2[j]>1. && stead->norm[j]>0.);
     oft2[j] += beta*test;
   }