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

CQTexperiment
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 convertEntered;
BOOL emittingSilence;
BOOL paused;
BOOL outputFormatChanged;
BOOL skipResampler;

View File

@ -51,7 +51,8 @@ void PrintStreamDesc (AudioStreamBasicDescription *inDesc)
stopping = NO;
convertEntered = NO;
emittingSilence = NO;
paused = NO;
outputFormatChanged = 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];
if (!amountConverted)
{
if (emittingSilence)
if (paused)
{
memset(writeBuf, 0, sizeof(writeBuf));
amountConverted = CHUNK_SIZE;
while (paused)
usleep(500);
continue;
}
else break;
}
@ -427,13 +429,7 @@ static void extrapolate(float *buffer, size_t channels, size_t frameSize, size_t
int extrapolateStart = 0;
int extrapolateEnd = 0;
size_t amountToSkip = 0;
BOOL inputRetry = NO;
if (emittingSilence)
{
memset(dest, 0, amount);
return amount;
}
BOOL endOfInputStream = NO;
if (stopping)
return 0;
@ -494,7 +490,7 @@ tryagain:
}
size_t amountToWrite = ioNumberPackets * inputFormat.mBytesPerPacket;
if (!inputRetry) amountToSkip = 0;
amountToSkip = 0;
BOOL isFloat = !!(inputFormat.mFormatFlags & kAudioFormatFlagIsFloat);
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;
continue;
size_t bytesRead = [self readData:inputBuffer + amountToSkip + bytesReadFromInput amount:(int)(amountToWrite - bytesReadFromInput)];
bytesReadFromInput += bytesRead;
if (!bytesRead)
usleep(500);
}
bytesReadFromInput += amountToSkip;
@ -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
stopping = NO;
convertEntered = NO;
emittingSilence = NO;
paused = NO;
outputFormatChanged = NO;
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.outputResampling"];
paused = NO;
[self cleanUp];
}
@ -876,13 +876,20 @@ static float db_to_scale(float db)
{
DLog(@"SETTING OUTPUT FORMAT!");
outputFormat = format;
outputFormatChanged = YES;
}
- (void)inputFormatDidChange:(AudioStreamBasicDescription)format
{
DLog(@"FORMAT CHANGED");
emittingSilence = YES;
paused = YES;
[self cleanUp];
if (outputFormatChanged)
{
[writeLock lock];
[buffer empty];
[writeLock unlock];
}
[self setupWithInputFormat:format outputFormat:outputFormat];
}