HRIR Filter: Quality of life improvements
The memory allocation is now nicer, and only allocates what is needed. Signed-off-by: Christopher Snowhill <kode54@gmail.com>CQTexperiment
parent
61a30c959c
commit
641f6390c5
|
@ -22,8 +22,6 @@
|
||||||
size_t paddedBufferSize;
|
size_t paddedBufferSize;
|
||||||
size_t channelCount;
|
size_t channelCount;
|
||||||
|
|
||||||
double sampleRate;
|
|
||||||
|
|
||||||
COMPLEX_SPLIT signal_fft;
|
COMPLEX_SPLIT signal_fft;
|
||||||
COMPLEX_SPLIT input_filtered_signal_per_channel[2];
|
COMPLEX_SPLIT input_filtered_signal_per_channel[2];
|
||||||
COMPLEX_SPLIT * impulse_responses;
|
COMPLEX_SPLIT * impulse_responses;
|
||||||
|
@ -48,6 +46,8 @@
|
||||||
|
|
||||||
- (void)process:(const float*)inBuffer sampleCount:(size_t)count toBuffer:(float *)outBuffer;
|
- (void)process:(const float*)inBuffer sampleCount:(size_t)count toBuffer:(float *)outBuffer;
|
||||||
|
|
||||||
|
- (void)reset;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#endif /* HeadphoneFilter_h */
|
#endif /* HeadphoneFilter_h */
|
||||||
|
|
|
@ -143,7 +143,7 @@ static const int8_t speakers_to_hesuvi_14[8][2][8] = {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
float * impulseBuffer = calloc(sizeof(float), (sampleCount + 1024) * sizeof(float) * impulseChannels);
|
float * impulseBuffer = (float *) malloc(sampleCount * sizeof(float) * impulseChannels);
|
||||||
if (!impulseBuffer) {
|
if (!impulseBuffer) {
|
||||||
[decoder close];
|
[decoder close];
|
||||||
decoder = nil;
|
decoder = nil;
|
||||||
|
@ -180,7 +180,17 @@ static const int8_t speakers_to_hesuvi_14[8][2][8] = {
|
||||||
int resamplerLatencyIn = (int) resampler->latency(resampler_data);
|
int resamplerLatencyIn = (int) resampler->latency(resampler_data);
|
||||||
int resamplerLatencyOut = (int)ceil(resamplerLatencyIn * sampleRatio);
|
int resamplerLatencyOut = (int)ceil(resamplerLatencyIn * sampleRatio);
|
||||||
|
|
||||||
float * resampledImpulse = calloc(sizeof(float), (resampledCount + resamplerLatencyOut * 2 + 128) * sizeof(float) * impulseChannels);
|
float * tempImpulse = (float *) realloc(impulseBuffer, (sampleCount + resamplerLatencyIn * 2 + 1024) * sizeof(float) * impulseChannels);
|
||||||
|
if (!tempImpulse) {
|
||||||
|
free(impulseBuffer);
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
impulseBuffer = tempImpulse;
|
||||||
|
|
||||||
|
resampledCount += resamplerLatencyOut * 2 + 1024;
|
||||||
|
|
||||||
|
float * resampledImpulse = (float *) malloc(resampledCount * sizeof(float) * impulseChannels);
|
||||||
if (!resampledImpulse) {
|
if (!resampledImpulse) {
|
||||||
free(impulseBuffer);
|
free(impulseBuffer);
|
||||||
return nil;
|
return nil;
|
||||||
|
@ -219,7 +229,7 @@ static const int8_t speakers_to_hesuvi_14[8][2][8] = {
|
||||||
while (fftSize > 2) { pow++; fftSize /= 2; }
|
while (fftSize > 2) { pow++; fftSize /= 2; }
|
||||||
fftSize = 2 << pow;
|
fftSize = 2 << pow;
|
||||||
|
|
||||||
float * deinterleavedImpulseBuffer = (float *) memalign_calloc(16, sizeof(float), fftSize * impulseChannels);
|
float * deinterleavedImpulseBuffer = (float *) memalign_alloc(16, fftSize * sizeof(float) * impulseChannels);
|
||||||
if (!deinterleavedImpulseBuffer) {
|
if (!deinterleavedImpulseBuffer) {
|
||||||
free(impulseBuffer);
|
free(impulseBuffer);
|
||||||
return nil;
|
return nil;
|
||||||
|
@ -227,6 +237,7 @@ static const int8_t speakers_to_hesuvi_14[8][2][8] = {
|
||||||
|
|
||||||
for (size_t i = 0; i < impulseChannels; ++i) {
|
for (size_t i = 0; i < impulseChannels; ++i) {
|
||||||
cblas_scopy(sampleCount, impulseBuffer + i, impulseChannels, deinterleavedImpulseBuffer + i * fftSize, 1);
|
cblas_scopy(sampleCount, impulseBuffer + i, impulseChannels, deinterleavedImpulseBuffer + i * fftSize, 1);
|
||||||
|
vDSP_vclr(deinterleavedImpulseBuffer + i * fftSize + sampleCount, 1, fftSize - sampleCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(impulseBuffer);
|
free(impulseBuffer);
|
||||||
|
@ -242,29 +253,29 @@ static const int8_t speakers_to_hesuvi_14[8][2][8] = {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
paddedSignal = (float *) memalign_calloc(16, sizeof(float), paddedBufferSize);
|
paddedSignal = (float *) memalign_alloc(16, sizeof(float) * paddedBufferSize);
|
||||||
if (!paddedSignal) {
|
if (!paddedSignal) {
|
||||||
memalign_free(deinterleavedImpulseBuffer);
|
memalign_free(deinterleavedImpulseBuffer);
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
signal_fft.realp = (float *) memalign_calloc(16, sizeof(float), fftSizeOver2);
|
signal_fft.realp = (float *) memalign_alloc(16, sizeof(float) * fftSizeOver2);
|
||||||
signal_fft.imagp = (float *) memalign_calloc(16, sizeof(float), fftSizeOver2);
|
signal_fft.imagp = (float *) memalign_alloc(16, sizeof(float) * fftSizeOver2);
|
||||||
if (!signal_fft.realp || !signal_fft.imagp) {
|
if (!signal_fft.realp || !signal_fft.imagp) {
|
||||||
memalign_free(deinterleavedImpulseBuffer);
|
memalign_free(deinterleavedImpulseBuffer);
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
input_filtered_signal_per_channel[0].realp = (float *) memalign_calloc(16, sizeof(float), fftSizeOver2);
|
input_filtered_signal_per_channel[0].realp = (float *) memalign_alloc(16, sizeof(float) * fftSizeOver2);
|
||||||
input_filtered_signal_per_channel[0].imagp = (float *) memalign_calloc(16, sizeof(float), fftSizeOver2);
|
input_filtered_signal_per_channel[0].imagp = (float *) memalign_alloc(16, sizeof(float) * fftSizeOver2);
|
||||||
if (!input_filtered_signal_per_channel[0].realp ||
|
if (!input_filtered_signal_per_channel[0].realp ||
|
||||||
!input_filtered_signal_per_channel[0].imagp) {
|
!input_filtered_signal_per_channel[0].imagp) {
|
||||||
memalign_free(deinterleavedImpulseBuffer);
|
memalign_free(deinterleavedImpulseBuffer);
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
input_filtered_signal_per_channel[1].realp = (float *) memalign_calloc(16, sizeof(float), fftSizeOver2);
|
input_filtered_signal_per_channel[1].realp = (float *) memalign_alloc(16, sizeof(float) * fftSizeOver2);
|
||||||
input_filtered_signal_per_channel[1].imagp = (float *) memalign_calloc(16, sizeof(float), fftSizeOver2);
|
input_filtered_signal_per_channel[1].imagp = (float *) memalign_alloc(16, sizeof(float) * fftSizeOver2);
|
||||||
if (!input_filtered_signal_per_channel[1].realp ||
|
if (!input_filtered_signal_per_channel[1].realp ||
|
||||||
!input_filtered_signal_per_channel[1].imagp) {
|
!input_filtered_signal_per_channel[1].imagp) {
|
||||||
memalign_free(deinterleavedImpulseBuffer);
|
memalign_free(deinterleavedImpulseBuffer);
|
||||||
|
@ -278,10 +289,10 @@ static const int8_t speakers_to_hesuvi_14[8][2][8] = {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < channels; ++i) {
|
for (size_t i = 0; i < channels; ++i) {
|
||||||
impulse_responses[i * 2 + 0].realp = (float *) memalign_calloc(16, sizeof(float), fftSizeOver2);
|
impulse_responses[i * 2 + 0].realp = (float *) memalign_alloc(16, sizeof(float) * fftSizeOver2);
|
||||||
impulse_responses[i * 2 + 0].imagp = (float *) memalign_calloc(16, sizeof(float), fftSizeOver2);
|
impulse_responses[i * 2 + 0].imagp = (float *) memalign_alloc(16, sizeof(float) * fftSizeOver2);
|
||||||
impulse_responses[i * 2 + 1].realp = (float *) memalign_calloc(16, sizeof(float), fftSizeOver2);
|
impulse_responses[i * 2 + 1].realp = (float *) memalign_alloc(16, sizeof(float) * fftSizeOver2);
|
||||||
impulse_responses[i * 2 + 1].imagp = (float *) memalign_calloc(16, sizeof(float), fftSizeOver2);
|
impulse_responses[i * 2 + 1].imagp = (float *) memalign_alloc(16, sizeof(float) * fftSizeOver2);
|
||||||
|
|
||||||
if (!impulse_responses[i * 2 + 0].realp || !impulse_responses[i * 2 + 0].imagp ||
|
if (!impulse_responses[i * 2 + 0].realp || !impulse_responses[i * 2 + 0].imagp ||
|
||||||
!impulse_responses[i * 2 + 1].realp || !impulse_responses[i * 2 + 1].imagp) {
|
!impulse_responses[i * 2 + 1].realp || !impulse_responses[i * 2 + 1].imagp) {
|
||||||
|
@ -304,7 +315,7 @@ static const int8_t speakers_to_hesuvi_14[8][2][8] = {
|
||||||
if (leftInChannel == -1 || rightInChannel == -1) {
|
if (leftInChannel == -1 || rightInChannel == -1) {
|
||||||
float * temp;
|
float * temp;
|
||||||
if (impulseChannels == 7) {
|
if (impulseChannels == 7) {
|
||||||
temp = (float *) calloc(sizeof(float), fftSize);
|
temp = (float *) malloc(sizeof(float) * fftSize);
|
||||||
if (!temp) {
|
if (!temp) {
|
||||||
memalign_free(deinterleavedImpulseBuffer);
|
memalign_free(deinterleavedImpulseBuffer);
|
||||||
return nil;
|
return nil;
|
||||||
|
@ -317,7 +328,7 @@ static const int8_t speakers_to_hesuvi_14[8][2][8] = {
|
||||||
vDSP_ctoz((DSPComplex *)temp, 2, &impulse_responses[i * 2 + 1], 1, fftSizeOver2);
|
vDSP_ctoz((DSPComplex *)temp, 2, &impulse_responses[i * 2 + 1], 1, fftSizeOver2);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
temp = (float *) calloc(sizeof(float), fftSize * 2);
|
temp = (float *) malloc(sizeof(float) * fftSize * 2);
|
||||||
if (!temp) {
|
if (!temp) {
|
||||||
memalign_free(deinterleavedImpulseBuffer);
|
memalign_free(deinterleavedImpulseBuffer);
|
||||||
return nil;
|
return nil;
|
||||||
|
@ -346,18 +357,18 @@ static const int8_t speakers_to_hesuvi_14[8][2][8] = {
|
||||||
|
|
||||||
memalign_free(deinterleavedImpulseBuffer);
|
memalign_free(deinterleavedImpulseBuffer);
|
||||||
|
|
||||||
left_result = (float *) memalign_calloc(16, sizeof(float), fftSize);
|
left_result = (float *) memalign_alloc(16, sizeof(float) * fftSize);
|
||||||
right_result = (float *) memalign_calloc(16, sizeof(float), fftSize);
|
right_result = (float *) memalign_alloc(16, sizeof(float) * fftSize);
|
||||||
if (!left_result || !right_result)
|
if (!left_result || !right_result)
|
||||||
return nil;
|
return nil;
|
||||||
|
|
||||||
prevOverlapLeft = (float *) memalign_calloc(16, sizeof(float), fftSize);
|
prevOverlapLeft = (float *) memalign_alloc(16, sizeof(float) * fftSize);
|
||||||
prevOverlapRight = (float *) memalign_calloc(16, sizeof(float), fftSize);
|
prevOverlapRight = (float *) memalign_alloc(16, sizeof(float) * fftSize);
|
||||||
if (!prevOverlapLeft || !prevOverlapRight)
|
if (!prevOverlapLeft || !prevOverlapRight)
|
||||||
return nil;
|
return nil;
|
||||||
|
|
||||||
left_mix_result = (float *) memalign_calloc(16, sizeof(float), fftSize);
|
left_mix_result = (float *) memalign_alloc(16, sizeof(float) * fftSize);
|
||||||
right_mix_result = (float *) memalign_calloc(16, sizeof(float), fftSize);
|
right_mix_result = (float *) memalign_alloc(16, sizeof(float) * fftSize);
|
||||||
if (!left_mix_result || !right_mix_result)
|
if (!left_mix_result || !right_mix_result)
|
||||||
return nil;
|
return nil;
|
||||||
|
|
||||||
|
@ -470,4 +481,8 @@ static const int8_t speakers_to_hesuvi_14[8][2][8] = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)reset {
|
||||||
|
prevOverlapLength = 0;
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Reference in New Issue