Optimized batch callback

This commit is contained in:
DataHoarder 2022-01-30 15:30:31 +01:00
parent d524cd1909
commit 72e5031167
3 changed files with 13 additions and 6 deletions

View file

@ -61,11 +61,15 @@ public:
void gabor_transform(float* audio_block, int audio_block_length, gaborator_transform_callback callback, uintptr_t callback_data) {
resultCache.clear();
if (audio_block == nullptr || audio_block_length == 0) { //finish
finish(callback, callback_data);
} else {
analyze(audio_block, audio_block_length, callback, callback_data);
}
callback(callback_data, resultCache.data(), resultCache.size(), static_cast<int>(numberOfBandsCache));
}
int64_t analysis_support() const {
@ -113,7 +117,7 @@ private:
auto& currentCoefficient = *coefficients[circularIndex];
callback(callback_data, currentCoefficient.data(), static_cast<int>(numberOfBandsCache));
resultCache.insert(resultCache.end(), currentCoefficient.begin(), currentCoefficient.end());
// fill the oldest with zeros
std::fill(currentCoefficient.begin(), currentCoefficient.end(), 0);
}
@ -138,7 +142,7 @@ private:
mostRecentCoefficentIndex = coefficientIndex;
// "copy" the oldest data to the history
// the slice can be reused thanks to the oldest being filled with zeros just after
callback(callback_data, currentCoefficient.data(), static_cast<int>(numberOfBandsCache));
resultCache.insert(resultCache.end(), currentCoefficient.begin(), currentCoefficient.end());
// fill the oldest with zeros
std::fill(currentCoefficient.begin(), currentCoefficient.end(), 0);
}
@ -152,6 +156,8 @@ private:
private:
std::vector<float> resultCache;
std::vector<std::unique_ptr<std::vector<float>>> coefficients;
int firstBandCache = -1;
int numberOfBandsCache = 0;

View file

@ -3,11 +3,13 @@
#ifdef __cplusplus
extern "C" {
#include <cstdint>
#include <cstdio>
#else
#include <stdint.h>
#include <stdio.h>
#endif
typedef void (*gaborator_transform_callback)(uintptr_t callback_data, float* data, int size);
typedef void (*gaborator_transform_callback)(uintptr_t callback_data, float* data, size_t size, size_t slice_size);
uintptr_t gaborator_initialize(int blockSize, double sampleRate, int bandsPerOctave, double minimumFrequency, double referenceFrequency, double maximumFrequency, int stepSize);
void gaborator_release(uintptr_t ptr);

View file

@ -1,6 +1,5 @@
#include "cgaborator.h"
#include <cstdio>
#include <iostream>
#define BLOCK_SIZE 8192
@ -18,7 +17,7 @@ int main() {
while (true){
auto read = fread(&audioData[0], sizeof(audioData[0]), sizeof(audioData) / sizeof(audioData[0]), fp);
gaborator_transform(ptr, &audioData[0], sizeof(audioData) / sizeof(audioData[0]), [](uintptr_t, float*, int){
gaborator_transform(ptr, &audioData[0], sizeof(audioData) / sizeof(audioData[0]), [](uintptr_t, float*, size_t, size_t){
}, 0);
@ -27,7 +26,7 @@ int main() {
}
}
gaborator_transform(ptr, nullptr, 0, [](uintptr_t, float*, int){
gaborator_transform(ptr, nullptr, 0, [](uintptr_t, float*, size_t, size_t){
}, 0);