Cog Audio: Implemented device output and input file format changing support

CQTexperiment
Christopher Snowhill 2022-01-12 21:31:22 -08:00
parent 9feaffc92d
commit 5fef62dd03
4 changed files with 91 additions and 57 deletions

View File

@ -99,10 +99,10 @@
{
DLog(@"SOMETHING CHANGED!");
if ([keyPath isEqual:@"properties"]) {
//Setup converter!
//Inform something of properties change
//Disable support until it is properly implemented.
//[controller inputFormatDidChange: propertiesToASBD([decoder properties])];
DLog(@"Input format changed");
// Converter doesn't need resetting for this, as output format hasn't changed
ConverterNode *converter = [[[controller controller] bufferChain] converter];
[converter inputFormatDidChange:[[[controller controller] bufferChain] inputFormat]];
}
else if ([keyPath isEqual:@"metadata"]) {
//Inform something of metadata change
@ -125,8 +125,8 @@
DLog(@"SEEKING! Resetting Buffer");
amountInBuffer = 0;
// This resets the converter's buffer
[self resetBuffer];
[converter resetBuffer];
[converter inputFormatDidChange:[[[controller controller] bufferChain] inputFormat]];
DLog(@"Reset buffer!");

View File

@ -89,6 +89,20 @@
- (void)setFormat:(AudioStreamBasicDescription *)f
{
format = *f;
BufferChain *bufferChain = [controller bufferChain];
if (bufferChain)
{
InputNode *input = [bufferChain inputNode];
ConverterNode *converter = [bufferChain converter];
if (input && converter)
{
// Need to clear the buffer, as it contains converted output
// targeting the previous output format
[input resetBuffer];
[converter setOutputFormat:format];
[converter inputFormatDidChange:[bufferChain inputFormat]];
}
}
}
- (void)close

View File

@ -28,6 +28,8 @@
float volume;
AVAudioFormat *_deviceFormat;
AudioDeviceID outputDeviceID;
AudioStreamBasicDescription deviceFormat; // info about the default device

View File

@ -56,8 +56,13 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
- (void)threadEntry:(id)arg
{
running = YES;
size_t eventCount = 0;
while (!stopping) {
dispatch_semaphore_wait(_sema, DISPATCH_TIME_FOREVER);
if (++eventCount == 128) {
[self updateDeviceFormat];
eventCount = 0;
}
}
stopped = YES;
[self stop];
@ -229,59 +234,26 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
free(devids);
}
- (BOOL)setup
- (BOOL)updateDeviceFormat
{
if (_au)
[self stop];
AVAudioFormat *format = _au.outputBusses[0].format;
running = NO;
stopping = NO;
stopped = NO;
outputDeviceID = -1;
AVAudioFormat *format, *renderFormat;
AudioComponentDescription desc;
if (!_deviceFormat || ![_deviceFormat isEqual:format])
{
NSError *err;
AVAudioFormat *renderFormat;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_HALOutput;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
_au = [[AUAudioUnit alloc] initWithComponentDescription:desc error:&err];
if (err != nil)
return NO;
// Setup the output device before mucking with settings
NSDictionary *device = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] objectForKey:@"outputDevice"];
if (device) {
BOOL ok = [self setOutputDeviceWithDeviceDict:device];
if (!ok) {
//Ruh roh.
[self setOutputDeviceWithDeviceDict:nil];
[[[NSUserDefaultsController sharedUserDefaultsController] defaults] removeObjectForKey:@"outputDevice"];
}
}
else {
[self setOutputDeviceWithDeviceDict:nil];
}
format = _au.outputBusses[0].format;
_deviceFormat = format;
deviceFormat = *(format.streamDescription);
///Seems some 3rd party devices return incorrect stuff...or I just don't like noninterleaved data.
deviceFormat.mFormatFlags &= ~kLinearPCMFormatFlagIsNonInterleaved;
// deviceFormat.mFormatFlags &= ~kLinearPCMFormatFlagIsFloat;
// deviceFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
// deviceFormat.mFormatFlags &= ~kLinearPCMFormatFlagIsFloat;
// deviceFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
// We don't want more than 8 channels
if (deviceFormat.mChannelsPerFrame > 8) {
deviceFormat.mChannelsPerFrame = 8;
}
// And force a default rate for crappy devices
if (deviceFormat.mSampleRate < 32000)
deviceFormat.mSampleRate = 48000;
deviceFormat.mBytesPerFrame = deviceFormat.mChannelsPerFrame*(deviceFormat.mBitsPerChannel/8);
deviceFormat.mBytesPerPacket = deviceFormat.mBytesPerFrame * deviceFormat.mFramesPerPacket;
@ -319,6 +291,54 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
if (err != nil)
return NO;
[outputController setFormat:&deviceFormat];
}
return YES;
}
- (BOOL)setup
{
if (_au)
[self stop];
running = NO;
stopping = NO;
stopped = NO;
outputDeviceID = -1;
AudioComponentDescription desc;
NSError *err;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_HALOutput;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
_au = [[AUAudioUnit alloc] initWithComponentDescription:desc error:&err];
if (err != nil)
return NO;
// Setup the output device before mucking with settings
NSDictionary *device = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] objectForKey:@"outputDevice"];
if (device) {
BOOL ok = [self setOutputDeviceWithDeviceDict:device];
if (!ok) {
//Ruh roh.
[self setOutputDeviceWithDeviceDict:nil];
[[[NSUserDefaultsController sharedUserDefaultsController] defaults] removeObjectForKey:@"outputDevice"];
}
}
else {
[self setOutputDeviceWithDeviceDict:nil];
}
_deviceFormat = nil;
[self updateDeviceFormat];
__block dispatch_semaphore_t sema = _sema;
__block OutputNode * outputController = self->outputController;
__block float * volume = &self->volume;
@ -368,8 +388,6 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
[NSThread detachNewThreadSelector:@selector(threadEntry:) toTarget:self withObject:nil];
[outputController setFormat:&deviceFormat];
return (err == nil);
}