title: Detect beat and play (wav) file in a synchronised manner
body: <p>I am trying my hands at Audio Processing in python with <a href="http://archive.gamedev.net/archive/reference/programming/features/beatdetection/index.html" rel="nofollow">this</a> Beat Detection algorithm. I have implemented the first (non-optimized version) from the aforementioned article. While it prints some results, I have no way to detect whether it works with some accuracy or not as I do not know how to play sound with it.</p>
  
  <p>Currently, I am using <code>Popen</code> to asynchronously start my media player with the song before going into the computation loop, but I am not sure if this strategy works and is giving synchronous results.</p>
  
  <pre><code>#!/usr/bin/python
  
  import scipy.io.wavfile, numpy, sys, subprocess
  
  # Some abstractions for computation
  def sumsquared(arr):
      sum = 0
      for i in arr:
              sum = sum + (i[0] * i[0]) + (i[1] * i[1])
  
      return sum
  
  if sys.argv.__len__() &lt; 2:
      print 'USAGE: wavdsp &lt;wavfile&gt;'
      sys.exit(1)
  
  numpy.set_printoptions(threshold='nan')
  rate, data = scipy.io.wavfile.read(sys.argv[1])
  
  
  # Beat detection algorithm begin 
  # the algorithm has been implemented as per GameDev Article
  # Initialisation
  data_len = data.__len__()
  idx = 0
  hist_last = 44032
  instant_energy = 0
  local_energy = 0
  le_multi = 0.023219955 # Local energy multiplier ~ 1024/44100
  
  
  # Play the song
  p = subprocess.Popen(['audacious', sys.argv[1]])
  
  while idx &lt; data_len - 48000:
      dat = data[idx:idx+1024]
      history = data[idx:hist_last]
      instant_energy = sumsquared(dat)
      local_energy = le_multi * sumsquared(history)
      print instant_energy, local_energy
      if instant_energy &gt; (local_energy * 1.3):
              print 'Beat'
  
      idx = idx + 1024
      hist_last = hist_last + 1024 # Right shift history buffer
  
   p.terminate()
  </code></pre>
  
  <p>What modification/additions can I make to the script in order to get audio output and the algorithm (console) output in a time synchronised manner? i.e When console outputs result for a particular frame, that frame must be playing on the speakers. </p>