Detect beat and play (wav) file in a synchronised manner

前端 未结 3 1954
南旧
南旧 2021-01-03 09:11

I am trying my hands at Audio Processing in python with this Beat Detection algorithm. I have implemented the first (non-optimized version) from the aforementioned article.

3条回答
  •  失恋的感觉
    2021-01-03 09:47

    A Simpler, Non-Realtime Approach

    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.

    Synthesize your beat indicator sound:

    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)
    

    In your while 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.

    This is the approach used by the aubio library to evaluate beat detection results. See the documentation here. Of particular interest is the documentation for the --output command line option:

    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.

    Optimization

    Since numpy is already a dependency, use its capabilities to speed up your algorithm. You can rewrite your sumsquared function as:

    def sumsquared(arr):
        return (arr**2).sum()
    

    Getting rid of the Python for-loop and pushing those calculations down into C code should give you a speed improvement.

    Also, take a look at this question or this question to get an idea of how you might vectorize the local to instantaneous energy comparisons in the while loop, using the numpy.lib.stride_tricks method.

提交回复
热议问题