Cog Audio: Fix track end extrapolator used by resampler

CQTexperiment
Christopher Snowhill 2022-01-14 07:39:55 -08:00
parent 748891f285
commit 3ce3edde88
2 changed files with 31 additions and 30 deletions

View File

@ -46,9 +46,6 @@
size_t floatBufferSize; size_t floatBufferSize;
size_t floatSize, floatOffset; size_t floatSize, floatOffset;
size_t floatConvertedSize;
float floatConvertedLast[CHUNK_SIZE];
void *extrapolateBuffer; void *extrapolateBuffer;
size_t extrapolateBufferSize; size_t extrapolateBufferSize;

View File

@ -792,21 +792,21 @@ tryagain:
if (!skipResampler && !latencyPostfill) if (!skipResampler && !latencyPostfill)
{ {
ioNumberPackets = (int)resampler->latency(resampler_data); ioNumberPackets = (int)resampler->latency(resampler_data);
if (dsd2pcm) ioNumberPackets += dsd2pcmLatency;
newSize = ioNumberPackets * inputFormat.mBytesPerPacket; newSize = ioNumberPackets * inputFormat.mBytesPerPacket;
newSize += bytesReadFromInput; newSize += bytesReadFromInput;
if (!inputBuffer || inputBufferSize < newSize) if (!inputBuffer || inputBufferSize < newSize)
inputBuffer = realloc( inputBuffer, inputBufferSize = newSize * 3); inputBuffer = realloc( inputBuffer, inputBufferSize = newSize * 3);
latencyPostfill = YES;
extrapolateEnd = ioNumberPackets;
latencyPostfill = ioNumberPackets; memset(inputBuffer + bytesReadFromInput, fillByte, extrapolateEnd * inputFormat.mBytesPerPacket);
memset(inputBuffer + bytesReadFromInput, fillByte, latencyPostfill * inputFormat.mBytesPerPacket);
bytesReadFromInput = newSize; bytesReadFromInput = newSize;
extrapolateEnd = ioNumberPackets;
if (dsd2pcm) if (dsd2pcm)
{ {
extrapolateEnd += dsd2pcmLatency;
eatFromEnd = dsd2pcmLatency * sampleRatio; eatFromEnd = dsd2pcmLatency * sampleRatio;
} }
} }
@ -839,7 +839,7 @@ tryagain:
{ {
size_t bitsPerSample = inputFormat.mBitsPerChannel; size_t bitsPerSample = inputFormat.mBitsPerChannel;
if (bitsPerSample == 1) { if (bitsPerSample == 1) {
samplesRead = bytesReadFromInput / inputFormat.mChannelsPerFrame; samplesRead = bytesReadFromInput / inputFormat.mBytesPerPacket;
convert_dsd_to_f32(inputBuffer + bytesReadFromInput, inputBuffer, samplesRead, inputFormat.mChannelsPerFrame, dsd2pcm); convert_dsd_to_f32(inputBuffer + bytesReadFromInput, inputBuffer, samplesRead, inputFormat.mChannelsPerFrame, dsd2pcm);
memmove(inputBuffer, inputBuffer + bytesReadFromInput, samplesRead * inputFormat.mChannelsPerFrame * sizeof(float)); memmove(inputBuffer, inputBuffer + bytesReadFromInput, samplesRead * inputFormat.mChannelsPerFrame * sizeof(float));
bitsPerSample = 32; bitsPerSample = 32;
@ -898,25 +898,16 @@ tryagain:
extrapolate( inputBuffer, floatFormat.mChannelsPerFrame, bytesReadFromInput / floatFormat.mBytesPerPacket, extrapolateStart, YES, &extrapolateBuffer, &extrapolateBufferSize); extrapolate( inputBuffer, floatFormat.mChannelsPerFrame, bytesReadFromInput / floatFormat.mBytesPerPacket, extrapolateStart, YES, &extrapolateBuffer, &extrapolateBufferSize);
extrapolateStart = 0; extrapolateStart = 0;
} }
else if (extrapolateEnd)
if (extrapolateEnd)
{ {
extrapolate( inputBuffer, floatFormat.mChannelsPerFrame, bytesReadFromInput / floatFormat.mBytesPerPacket, extrapolateEnd, NO, &extrapolateBuffer, &extrapolateBufferSize); extrapolate( inputBuffer, floatFormat.mChannelsPerFrame, bytesReadFromInput / floatFormat.mBytesPerPacket, extrapolateEnd, NO, &extrapolateBuffer, &extrapolateBufferSize);
extrapolateEnd = 0; extrapolateEnd = 0;
bytesReadFromInput -= eatFromEnd * floatFormat.mBytesPerPacket;
if (bytesReadFromInput < 0)
bytesReadFromInput = 0;
} }
// Input now contains bytesReadFromInput worth of floats, in the input sample rate // Input now contains bytesReadFromInput worth of floats, in the input sample rate
inpSize = bytesReadFromInput; inpSize = bytesReadFromInput;
inpOffset = 0; inpOffset = 0;
// Preserve last samples, for end extrapolator, if needed
size_t preserved = inpSize;
if (preserved > sizeof(floatConvertedLast))
preserved = sizeof(floatConvertedLast);
memcpy(floatConvertedLast, inputBuffer + inpSize - preserved, preserved);
floatConvertedSize = preserved;
} }
if (inpOffset != inpSize && floatOffset == floatSize) if (inpOffset != inpSize && floatOffset == floatSize)
@ -962,18 +953,33 @@ tryagain:
inpOffset += inputSamples * floatFormat.mBytesPerPacket; inpOffset += inputSamples * floatFormat.mBytesPerPacket;
if (!skipResampler && latencyEaten) if (!skipResampler)
{ {
if (src_data.output_frames > latencyEaten) if (latencyEaten)
{ {
src_data.output_frames -= latencyEaten; if (src_data.output_frames > latencyEaten)
memmove(src_data.data_out, src_data.data_out + latencyEaten * inputFormat.mChannelsPerFrame, src_data.output_frames * floatFormat.mBytesPerPacket); {
latencyEaten = 0; src_data.output_frames -= latencyEaten;
memmove(src_data.data_out, src_data.data_out + latencyEaten * inputFormat.mChannelsPerFrame, src_data.output_frames * floatFormat.mBytesPerPacket);
latencyEaten = 0;
}
else
{
latencyEaten -= src_data.output_frames;
src_data.output_frames = 0;
}
} }
else else if (eatFromEnd)
{ {
latencyEaten -= src_data.output_frames; if (src_data.output_frames > eatFromEnd)
src_data.output_frames = 0; {
src_data.output_frames -= eatFromEnd;
}
else
{
src_data.output_frames = 0;
}
eatFromEnd = 0;
} }
} }
@ -1125,8 +1131,6 @@ static float db_to_scale(float db)
floatOffset = 0; floatOffset = 0;
floatSize = 0; floatSize = 0;
floatConvertedSize = 0;
// This is a post resampler, post-down/upmix format // This is a post resampler, post-down/upmix format
dmFloatFormat = floatFormat; dmFloatFormat = floatFormat;