Core Audio output: Hopefully stop buffer looping on device stop

CQTexperiment
Christopher Snowhill 2022-01-22 14:37:37 -08:00
parent 6011982a63
commit 502e52d699
3 changed files with 17 additions and 0 deletions

View File

@ -176,4 +176,5 @@
{ {
[output sustainHDCD]; [output sustainHDCD];
} }
@end @end

View File

@ -38,6 +38,7 @@
BOOL stopped; BOOL stopped;
BOOL started; BOOL started;
BOOL paused; BOOL paused;
BOOL stopNext;
BOOL eqEnabled; BOOL eqEnabled;

View File

@ -160,6 +160,7 @@ static OSStatus renderCallback( void *inRefCon, AudioUnitRenderActionFlags *ioAc
listenerapplied = NO; listenerapplied = NO;
running = NO; running = NO;
started = NO; started = NO;
stopNext = NO;
atomic_init(&bytesRendered, 0); atomic_init(&bytesRendered, 0);
atomic_init(&bytesHdcdSustained, 0); atomic_init(&bytesHdcdSustained, 0);
@ -210,6 +211,7 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
{ {
running = YES; running = YES;
started = NO; started = NO;
stopNext = NO;
size_t eventCount = 0; size_t eventCount = 0;
atomic_store(&bytesRendered, 0); atomic_store(&bytesRendered, 0);
NSMutableArray *delayedEvents = [[NSMutableArray alloc] init]; NSMutableArray *delayedEvents = [[NSMutableArray alloc] init];
@ -277,6 +279,10 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
else else
bytesBuffered -= CHUNK_SIZE; bytesBuffered -= CHUNK_SIZE;
} }
else {
stopNext = YES;
break;
}
[delayedEvents addObject:[NSNumber numberWithLong:bytesBuffered]]; [delayedEvents addObject:[NSNumber numberWithLong:bytesBuffered]];
delayedEventsPopped = NO; delayedEventsPopped = NO;
if (!started) { if (!started) {
@ -554,6 +560,7 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
stopping = NO; stopping = NO;
stopped = NO; stopped = NO;
paused = NO; paused = NO;
stopNext = NO;
outputDeviceID = -1; outputDeviceID = -1;
AudioComponentDescription desc; AudioComponentDescription desc;
@ -722,6 +729,14 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
- (void)stop - (void)stop
{ {
if (stopNext && started && !paused) {
while (![[outputController buffer] isEmpty])
usleep(500);
}
if (stopNext) {
stopNext = NO;
[self signalEndOfStream];
}
stopInvoked = YES; stopInvoked = YES;
stopping = YES; stopping = YES;
paused = NO; paused = NO;