Cog Audio: Enhance track end extrapolator so it will always have a reasonable amount of buffered data for extrapolation

CQTexperiment
Christopher Snowhill 2022-01-13 23:03:53 -08:00
parent e6124335d1
commit 7cc89c9f92
2 changed files with 38 additions and 3 deletions

View File

@ -45,6 +45,9 @@
void *floatBuffer;
size_t floatBufferSize;
size_t floatSize, floatOffset;
size_t floatConvertedSize;
float floatConvertedLast[CHUNK_SIZE];
AudioStreamBasicDescription inputFormat;
AudioStreamBasicDescription floatFormat;

View File

@ -485,12 +485,35 @@ tryagain:
extrapolateEnd = ioNumberPackets;
// Extrapolate end samples
if (inpSize)
if ( inpSize < sizeof(floatConvertedLast))
{
extrapolate( inputBuffer, floatFormat.mChannelsPerFrame, inpSize / floatFormat.mBytesPerPacket, extrapolateEnd, NO);
size_t inpTotal = newSize + floatConvertedSize;
if (inpTotal > sizeof(floatConvertedLast))
inpTotal = sizeof(floatConvertedLast);
if (inpTotal - newSize < floatConvertedSize)
{
memmove(floatConvertedLast, ((uint8_t*)floatConvertedLast) + newSize, inpTotal - newSize);
floatConvertedSize = inpTotal - newSize;
}
memcpy(((uint8_t*)floatConvertedLast) + floatConvertedSize, inputBuffer, inpSize);
extrapolate( floatConvertedLast, floatFormat.mChannelsPerFrame, inpTotal / floatFormat.mBytesPerPacket, extrapolateEnd, NO );
newSize = ioNumberPackets * floatFormat.mBytesPerPacket;
memcpy( inputBuffer, ((uint8_t*)floatConvertedLast) + inpTotal - newSize, newSize );
inpSize = newSize;
inpOffset = 0;
}
else
{
extrapolate( inputBuffer, floatFormat.mChannelsPerFrame, newSize / floatFormat.mBytesPerPacket, extrapolateEnd, NO );
inpOffset = inpSize;
inpSize += extrapolateEnd * floatFormat.mBytesPerPacket;
inpSize = newSize;
}
latencyPostfill = YES;
break;
@ -628,6 +651,13 @@ tryagain:
// Input now contains bytesReadFromInput worth of floats, in the input sample rate
inpSize = bytesReadFromInput;
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)
@ -823,6 +853,8 @@ static float db_to_scale(float db)
floatOffset = 0;
floatSize = 0;
floatConvertedSize = 0;
// This is a post resampler, post-down/upmix format
dmFloatFormat = floatFormat;