Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>A Simpler, Non-Realtime Approach</h2> <p>I'm not optimistic about synchronizing console output with realtime audio. My approach would be a bit simpler. As you read through the file and process it, write the samples out to a new audio file. Whenever a beat is detected, add some hard-to-miss sound, like a loud, short sine tone to the audio you're writing. That way, you can aurally evaluate the quality of the results.</p> <p>Synthesize your beat indicator sound:</p> <pre><code>def testsignal(hz,seconds=5.,sr=44100.): ''' Create a sine wave at hz for n seconds ''' # cycles per sample cps = hz / sr # total samples ts = seconds * sr return np.sin(np.arange(0,ts*cps,cps) * (2*np.pi)) signal = testsignal(880,seconds = .02) </code></pre> <p>In your <code>while</code> loop, add the testsignal to the input frame if a beat is detected, and leave the frame unaltered if no beat is detected. Write those frames out to a file and listen to it to evaluate the quality of the beat detection.</p> <p>This is the approach used by the <a href="http://aubio.org/" rel="nofollow noreferrer">aubio</a> library to evaluate beat detection results. See the documentation <a href="http://aubio.org/aubioonset.html" rel="nofollow noreferrer">here</a>. Of particular interest is the documentation for the <code>--output</code> command line option:</p> <blockquote> <p>Save results in this file. The file will be created on the model of the input file. Results are marked by a very short wood-block sample.</p> </blockquote> <h2>Optimization</h2> <p>Since numpy is already a dependency, use its capabilities to speed up your algorithm. You can rewrite your <code>sumsquared</code> function as:</p> <pre><code>def sumsquared(arr): return (arr**2).sum() </code></pre> <p>Getting rid of the Python for-loop and pushing those calculations down into C code should give you a speed improvement.</p> <p>Also, take a look at <a href="https://stackoverflow.com/q/4936620/1015178">this question</a> or <a href="https://stackoverflow.com/q/8070349/1015178">this question</a> to get an idea of how you might vectorize the local to instantaneous energy comparisons in the <code>while</code> loop, using the <code>numpy.lib.stride_tricks</code> method.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload