MCPcopy Create free account
hub / github.com/adamstark/BTrack / detectBeats

Function detectBeats

plugins/python-module/BTrackPythonModule.cpp:10–54  ·  view source on GitHub ↗

=======================================================================

Source from the content-addressed store, hash-verified

8
9//=======================================================================
10static PyObject* detectBeats (PyObject* dummy, PyObject* args)
11{
12 PyObject* inputObject = nullptr;
13
14 if (! PyArg_ParseTuple (args, "O", &inputObject))
15 return nullptr;
16
17 PyArrayObject* inputArray = (PyArrayObject*) PyArray_FROM_OTF (inputObject, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
18
19 if (! inputArray)
20 return nullptr;
21
22 const double* audioSampleArray = static_cast<double*> (PyArray_DATA (inputArray));
23 long signalLength = PyArray_Size ((PyObject*)inputArray);
24 constexpr int hopSize = 512;
25 constexpr int frameSize = 1024;
26 constexpr int sampleRate = 44100;
27 int numFrames = signalLength / hopSize;
28
29 std::vector<double> buffer (hopSize); // buffer to hold one hopsize worth of audio samples
30
31 BTrack b (hopSize, frameSize);
32
33 std::vector<double> beats;
34 beats.reserve (numFrames);
35
36 for (int i = 0; i < numFrames; i++)
37 {
38 std::copy_n (audioSampleArray + (i * hopSize), hopSize, buffer.begin());
39
40 b.processAudioFrame (buffer.data()); // process the current audio frame
41
42 // if a beat is currently scheduled, store the time
43 if (b.beatDueInCurrentFrame())
44 beats.push_back (BTrack::getBeatTimeInSeconds (i, hopSize, sampleRate));
45 }
46
47 npy_intp dims = static_cast<npy_intp> (beats.size());
48 PyObject* outputArray = PyArray_SimpleNew (1, &dims, NPY_DOUBLE);
49 double* out = static_cast<double*> (PyArray_DATA ((PyArrayObject*)outputArray));
50 std::copy (beats.begin(), beats.end(), out);
51
52 Py_DECREF (inputArray);
53 return outputArray;
54}
55
56//=======================================================================
57static PyObject* calculateOnsetDetectionFunction (PyObject* dummy, PyObject* args)

Callers

nothing calls this directly

Calls 3

processAudioFrameMethod · 0.80
beatDueInCurrentFrameMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected