Cog Audio: Converter node actually pauses when it is being reconfigured, and resets its buffer when the output format is changed

This commit is contained in:
Christopher Snowhill 2022-01-12 23:13:00 -08:00
parent b0f5a37e85
commit cc134ce293
2 changed files with 29 additions and 21 deletions

View file

@ -28,7 +28,8 @@
BOOL stopping; BOOL stopping;
BOOL convertEntered; BOOL convertEntered;
BOOL emittingSilence; BOOL paused;
BOOL outputFormatChanged;
BOOL skipResampler; BOOL skipResampler;

View file

@ -51,7 +51,8 @@ void PrintStreamDesc (AudioStreamBasicDescription *inDesc)
stopping = NO; stopping = NO;
convertEntered = NO; convertEntered = NO;
emittingSilence = NO; paused = NO;
outputFormatChanged = NO;
skipResampler = NO; skipResampler = NO;
@ -408,10 +409,11 @@ static void extrapolate(float *buffer, size_t channels, size_t frameSize, size_t
int amountConverted = [self convert:writeBuf amount:CHUNK_SIZE]; int amountConverted = [self convert:writeBuf amount:CHUNK_SIZE];
if (!amountConverted) if (!amountConverted)
{ {
if (emittingSilence) if (paused)
{ {
memset(writeBuf, 0, sizeof(writeBuf)); while (paused)
amountConverted = CHUNK_SIZE; usleep(500);
continue;
} }
else break; else break;
} }
@ -427,13 +429,7 @@ static void extrapolate(float *buffer, size_t channels, size_t frameSize, size_t
int extrapolateStart = 0; int extrapolateStart = 0;
int extrapolateEnd = 0; int extrapolateEnd = 0;
size_t amountToSkip = 0; size_t amountToSkip = 0;
BOOL inputRetry = NO; BOOL endOfInputStream = NO;
if (emittingSilence)
{
memset(dest, 0, amount);
return amount;
}
if (stopping) if (stopping)
return 0; return 0;
@ -494,7 +490,7 @@ tryagain:
} }
size_t amountToWrite = ioNumberPackets * inputFormat.mBytesPerPacket; size_t amountToWrite = ioNumberPackets * inputFormat.mBytesPerPacket;
if (!inputRetry) amountToSkip = 0; amountToSkip = 0;
BOOL isFloat = !!(inputFormat.mFormatFlags & kAudioFormatFlagIsFloat); BOOL isFloat = !!(inputFormat.mFormatFlags & kAudioFormatFlagIsFloat);
BOOL isUnsigned = !isFloat && !(inputFormat.mFormatFlags & kAudioFormatFlagIsSignedInteger); BOOL isUnsigned = !isFloat && !(inputFormat.mFormatFlags & kAudioFormatFlagIsSignedInteger);
@ -527,12 +523,14 @@ tryagain:
} }
} }
size_t bytesReadFromInput = [self readData:inputBuffer + amountToSkip amount:(int)amountToWrite]; size_t bytesReadFromInput = 0;
if (!bytesReadFromInput) while (bytesReadFromInput < amountToWrite && !stopping && [self shouldContinue] == YES && [self endOfStream] == NO)
{ {
inputRetry = YES; size_t bytesRead = [self readData:inputBuffer + amountToSkip + bytesReadFromInput amount:(int)(amountToWrite - bytesReadFromInput)];
continue; bytesReadFromInput += bytesRead;
if (!bytesRead)
usleep(500);
} }
bytesReadFromInput += amountToSkip; bytesReadFromInput += amountToSkip;
@ -604,7 +602,7 @@ tryagain:
// Extrapolate start // Extrapolate start
if (extrapolateStart) if (extrapolateStart)
{ {
extrapolate( inputBuffer, floatFormat.mChannelsPerFrame, bytesReadFromInput / floatFormat.mBytesPerPacket, extrapolateStart, YES); extrapolate( inputBuffer, floatFormat.mChannelsPerFrame, bytesReadFromInput / floatFormat.mBytesPerPacket, extrapolateStart, YES);
extrapolateStart = 0; extrapolateStart = 0;
} }
@ -856,7 +854,8 @@ static float db_to_scale(float db)
// Move this here so process call isn't running the resampler until it's allocated // Move this here so process call isn't running the resampler until it's allocated
stopping = NO; stopping = NO;
convertEntered = NO; convertEntered = NO;
emittingSilence = NO; paused = NO;
outputFormatChanged = NO;
return YES; return YES;
} }
@ -868,6 +867,7 @@ static float db_to_scale(float db)
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.volumeScaling"]; [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.volumeScaling"];
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.outputResampling"]; [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.outputResampling"];
paused = NO;
[self cleanUp]; [self cleanUp];
} }
@ -876,13 +876,20 @@ static float db_to_scale(float db)
{ {
DLog(@"SETTING OUTPUT FORMAT!"); DLog(@"SETTING OUTPUT FORMAT!");
outputFormat = format; outputFormat = format;
outputFormatChanged = YES;
} }
- (void)inputFormatDidChange:(AudioStreamBasicDescription)format - (void)inputFormatDidChange:(AudioStreamBasicDescription)format
{ {
DLog(@"FORMAT CHANGED"); DLog(@"FORMAT CHANGED");
emittingSilence = YES; paused = YES;
[self cleanUp]; [self cleanUp];
if (outputFormatChanged)
{
[writeLock lock];
[buffer empty];
[writeLock unlock];
}
[self setupWithInputFormat:format outputFormat:outputFormat]; [self setupWithInputFormat:format outputFormat:outputFormat];
} }