2022-06-24 06:17:31 +00:00
|
|
|
//
|
|
|
|
// OutputAVFoundation.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Christopher Snowhill on 6/23/22.
|
|
|
|
// Copyright 2022 Christopher Snowhill. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "OutputAVFoundation.h"
|
|
|
|
#import "OutputNode.h"
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
#import "BadSampleCleaner.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#import "Logging.h"
|
|
|
|
|
|
|
|
#import <Accelerate/Accelerate.h>
|
|
|
|
|
2023-02-05 05:52:52 +00:00
|
|
|
#import "rsstate.h"
|
2022-06-24 09:46:23 +00:00
|
|
|
|
2022-07-10 22:21:09 +00:00
|
|
|
#import "FSurroundFilter.h"
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
extern void scale_by_volume(float *buffer, size_t count, float volume);
|
|
|
|
|
|
|
|
static NSString *CogPlaybackDidBeginNotficiation = @"CogPlaybackDidBeginNotficiation";
|
|
|
|
|
|
|
|
@implementation OutputAVFoundation
|
|
|
|
|
|
|
|
static void *kOutputAVFoundationContext = &kOutputAVFoundationContext;
|
|
|
|
|
|
|
|
static void fillBuffers(AudioBufferList *ioData, const float *inbuffer, size_t count, size_t offset) {
|
|
|
|
const size_t channels = ioData->mNumberBuffers;
|
|
|
|
for(int i = 0; i < channels; ++i) {
|
|
|
|
const size_t maxCount = (ioData->mBuffers[i].mDataByteSize / sizeof(float)) - offset;
|
|
|
|
float *output = ((float *)ioData->mBuffers[i].mData) + offset;
|
|
|
|
const float *input = inbuffer + i;
|
|
|
|
cblas_scopy((int)((count > maxCount) ? maxCount : count), input, (int)channels, output, 1);
|
|
|
|
ioData->mBuffers[i].mNumberChannels = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clearBuffers(AudioBufferList *ioData, size_t count, size_t offset) {
|
|
|
|
for(int i = 0; i < ioData->mNumberBuffers; ++i) {
|
|
|
|
memset(ioData->mBuffers[i].mData + offset * sizeof(float), 0, count * sizeof(float));
|
|
|
|
ioData->mBuffers[i].mNumberChannels = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus eqRenderCallback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) {
|
2022-07-10 22:21:09 +00:00
|
|
|
if(inNumberFrames > 4096 || !inRefCon) {
|
2022-06-24 06:17:31 +00:00
|
|
|
clearBuffers(ioData, inNumberFrames, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputAVFoundation *_self = (__bridge OutputAVFoundation *)inRefCon;
|
|
|
|
|
2022-07-10 22:21:09 +00:00
|
|
|
fillBuffers(ioData, _self->samplePtr, inNumberFrames, 0);
|
2022-06-24 06:17:31 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-10 22:21:09 +00:00
|
|
|
- (int)renderInput:(int)amountToRead toBuffer:(float *)buffer {
|
|
|
|
int amountRead = 0;
|
2022-06-24 06:17:31 +00:00
|
|
|
|
|
|
|
if(stopping == YES || [outputController shouldContinue] == NO) {
|
|
|
|
// Chain is dead, fill out the serial number pointer forever with silence
|
|
|
|
stopping = YES;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-15 10:34:10 +00:00
|
|
|
AudioStreamBasicDescription format;
|
|
|
|
uint32_t config;
|
|
|
|
if([outputController peekFormat:&format channelConfig:&config]) {
|
2022-06-24 06:17:31 +00:00
|
|
|
// XXX ERROR with AirPods - Can't go higher than CD*8 surround - 192k stereo
|
|
|
|
// Emits to console: [AUScotty] Initialize: invalid FFT size 16384
|
|
|
|
// DSD256 stereo emits: [AUScotty] Initialize: invalid FFT size 65536
|
2022-07-15 10:34:10 +00:00
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
BOOL formatClipped = NO;
|
|
|
|
BOOL isSurround = format.mChannelsPerFrame > 2;
|
|
|
|
const double maxSampleRate = isSurround ? 352800.0 : 192000.0;
|
2022-06-24 09:46:23 +00:00
|
|
|
double srcRate = format.mSampleRate;
|
|
|
|
double dstRate = srcRate;
|
2022-06-24 06:17:31 +00:00
|
|
|
if(format.mSampleRate > maxSampleRate) {
|
|
|
|
format.mSampleRate = maxSampleRate;
|
2022-06-24 09:46:23 +00:00
|
|
|
dstRate = maxSampleRate;
|
2022-06-24 06:17:31 +00:00
|
|
|
formatClipped = YES;
|
|
|
|
}
|
2022-07-09 09:30:52 +00:00
|
|
|
if(!streamFormatStarted || config != realStreamChannelConfig || memcmp(&newFormat, &format, sizeof(format)) != 0) {
|
2022-06-24 10:45:16 +00:00
|
|
|
[currentPtsLock lock];
|
2022-06-24 06:17:31 +00:00
|
|
|
if(formatClipped) {
|
|
|
|
ALog(@"Sample rate clipped to no more than %f Hz!", maxSampleRate);
|
2023-02-05 05:52:52 +00:00
|
|
|
if(rsstate) {
|
|
|
|
rsold = rsstate;
|
|
|
|
rsstate = NULL;
|
2022-06-24 09:46:23 +00:00
|
|
|
}
|
2023-02-05 05:52:52 +00:00
|
|
|
rsstate = rsstate_new(format.mChannelsPerFrame, srcRate, dstRate);
|
|
|
|
} else if(rsstate) {
|
|
|
|
rsold = rsstate;
|
|
|
|
rsstate = NULL;
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
2022-06-24 10:45:16 +00:00
|
|
|
[currentPtsLock unlock];
|
2022-06-24 09:46:23 +00:00
|
|
|
newFormat = format;
|
2022-06-25 06:10:35 +00:00
|
|
|
newChannelConfig = config;
|
2022-06-24 06:17:31 +00:00
|
|
|
streamFormatStarted = YES;
|
2022-06-24 09:46:23 +00:00
|
|
|
|
|
|
|
visFormat = format;
|
|
|
|
visFormat.mChannelsPerFrame = 1;
|
|
|
|
visFormat.mBytesPerFrame = visFormat.mChannelsPerFrame * (visFormat.mBitsPerChannel / 8);
|
|
|
|
visFormat.mBytesPerPacket = visFormat.mBytesPerFrame * visFormat.mFramesPerPacket;
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
downmixerForVis = [[DownmixProcessor alloc] initWithInputFormat:format inputConfig:config andOutputFormat:visFormat outputConfig:AudioConfigMono];
|
2023-02-05 05:52:52 +00:00
|
|
|
if(!rsold) {
|
2022-07-09 09:30:52 +00:00
|
|
|
realStreamFormat = format;
|
|
|
|
realStreamChannelConfig = config;
|
2022-07-15 10:34:10 +00:00
|
|
|
streamFormatChanged = YES;
|
2022-06-24 09:46:23 +00:00
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
2022-07-15 10:34:10 +00:00
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-07-15 10:34:10 +00:00
|
|
|
if(streamFormatChanged) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioChunk *chunk = [outputController readChunk:amountToRead];
|
|
|
|
|
|
|
|
int frameCount = (int)[chunk frameCount];
|
|
|
|
format = [chunk format];
|
|
|
|
config = [chunk channelConfig];
|
|
|
|
double chunkDuration = 0;
|
|
|
|
|
|
|
|
if(frameCount) {
|
2022-06-24 06:17:31 +00:00
|
|
|
chunkDuration = [chunk duration];
|
|
|
|
|
|
|
|
NSData *samples = [chunk removeSamples:frameCount];
|
|
|
|
#ifdef _DEBUG
|
|
|
|
[BadSampleCleaner cleanSamples:(float *)[samples bytes]
|
|
|
|
amount:frameCount * format.mChannelsPerFrame
|
|
|
|
location:@"pre downmix"];
|
|
|
|
#endif
|
2022-06-24 09:46:23 +00:00
|
|
|
// It should be fine to request up to double, we'll only get downsampled
|
|
|
|
const float *outputPtr = (const float *)[samples bytes];
|
2023-02-05 05:52:52 +00:00
|
|
|
if(rsstate) {
|
2022-06-24 09:46:23 +00:00
|
|
|
size_t inDone = 0;
|
2022-06-24 10:45:16 +00:00
|
|
|
[currentPtsLock lock];
|
2023-02-05 05:52:52 +00:00
|
|
|
size_t framesDone = rsstate_resample(rsstate, outputPtr, frameCount, &inDone, &rsTempBuffer[0], amountToRead);
|
2022-06-24 10:45:16 +00:00
|
|
|
[currentPtsLock unlock];
|
2022-06-24 09:46:23 +00:00
|
|
|
if(!framesDone) return 0;
|
|
|
|
frameCount = (int)framesDone;
|
2023-02-05 05:52:52 +00:00
|
|
|
outputPtr = &rsTempBuffer[0];
|
2022-06-24 09:46:23 +00:00
|
|
|
chunkDuration = frameCount / newFormat.mSampleRate;
|
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
[downmixerForVis process:outputPtr
|
2022-06-24 06:17:31 +00:00
|
|
|
frameCount:frameCount
|
2022-06-24 10:43:50 +00:00
|
|
|
output:&visAudio[0]];
|
|
|
|
|
|
|
|
[visController postSampleRate:44100.0];
|
|
|
|
|
|
|
|
if(newFormat.mSampleRate != 44100.0) {
|
|
|
|
if(newFormat.mSampleRate != lastVisRate) {
|
2023-02-05 05:52:52 +00:00
|
|
|
if(rsvis) {
|
2022-06-24 10:43:50 +00:00
|
|
|
for(;;) {
|
|
|
|
int samplesFlushed;
|
|
|
|
[currentPtsLock lock];
|
2023-02-05 05:52:52 +00:00
|
|
|
samplesFlushed = (int)rsstate_flush(rsvis, &visTemp[0], 8192);
|
2022-06-24 10:43:50 +00:00
|
|
|
[currentPtsLock unlock];
|
|
|
|
if(samplesFlushed) {
|
|
|
|
[visController postVisPCM:visTemp amount:samplesFlushed];
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[currentPtsLock lock];
|
2023-02-05 05:52:52 +00:00
|
|
|
rsstate_delete(rsvis);
|
|
|
|
rsvis = NULL;
|
2022-06-24 10:43:50 +00:00
|
|
|
[currentPtsLock unlock];
|
|
|
|
}
|
|
|
|
lastVisRate = newFormat.mSampleRate;
|
|
|
|
[currentPtsLock lock];
|
2023-02-05 05:52:52 +00:00
|
|
|
rsvis = rsstate_new(1, lastVisRate, 44100.0);
|
2022-06-24 10:43:50 +00:00
|
|
|
[currentPtsLock unlock];
|
|
|
|
}
|
2023-02-05 05:52:52 +00:00
|
|
|
if(rsvis) {
|
2022-06-24 10:43:50 +00:00
|
|
|
int samplesProcessed;
|
2022-06-26 08:09:55 +00:00
|
|
|
size_t totalDone = 0;
|
|
|
|
size_t inDone = 0;
|
2022-06-26 10:57:19 +00:00
|
|
|
size_t visFrameCount = frameCount;
|
2022-06-26 08:09:55 +00:00
|
|
|
{
|
|
|
|
[currentPtsLock lock];
|
2023-02-05 05:52:52 +00:00
|
|
|
samplesProcessed = (int)rsstate_resample(rsvis, &visAudio[totalDone], visFrameCount, &inDone, &visTemp[0], 8192);
|
2022-06-26 08:09:55 +00:00
|
|
|
[currentPtsLock unlock];
|
|
|
|
if(samplesProcessed) {
|
|
|
|
[visController postVisPCM:&visTemp[0] amount:samplesProcessed];
|
|
|
|
}
|
|
|
|
totalDone += inDone;
|
2022-06-26 10:57:19 +00:00
|
|
|
visFrameCount -= inDone;
|
|
|
|
} while(samplesProcessed && visFrameCount);
|
2022-06-24 10:43:50 +00:00
|
|
|
}
|
2023-02-05 05:52:52 +00:00
|
|
|
} else if(rsvis) {
|
2022-06-24 10:43:50 +00:00
|
|
|
for(;;) {
|
|
|
|
int samplesFlushed;
|
|
|
|
[currentPtsLock lock];
|
2023-02-05 05:52:52 +00:00
|
|
|
samplesFlushed = (int)rsstate_flush(rsvis, &visTemp[0], 8192);
|
2022-06-24 10:43:50 +00:00
|
|
|
[currentPtsLock unlock];
|
|
|
|
if(samplesFlushed) {
|
|
|
|
[visController postVisPCM:visTemp amount:samplesFlushed];
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[currentPtsLock lock];
|
2023-02-05 05:52:52 +00:00
|
|
|
rsstate_delete(rsvis);
|
|
|
|
rsvis = NULL;
|
2022-06-24 10:43:50 +00:00
|
|
|
[currentPtsLock unlock];
|
|
|
|
[visController postVisPCM:&visAudio[0] amount:frameCount];
|
|
|
|
} else {
|
|
|
|
[visController postVisPCM:&visAudio[0] amount:frameCount];
|
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-07-10 22:21:09 +00:00
|
|
|
cblas_scopy((int)(frameCount * newFormat.mChannelsPerFrame), outputPtr, 1, &buffer[0], 1);
|
2022-06-24 06:17:31 +00:00
|
|
|
amountRead = frameCount;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-25 02:14:48 +00:00
|
|
|
if(stopping) return 0;
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
float volumeScale = 1.0;
|
|
|
|
double sustained;
|
2022-06-25 02:14:48 +00:00
|
|
|
sustained = secondsHdcdSustained;
|
2022-06-24 06:17:31 +00:00
|
|
|
if(sustained > 0) {
|
|
|
|
if(sustained < amountRead) {
|
2022-06-25 02:14:48 +00:00
|
|
|
secondsHdcdSustained = 0;
|
2022-06-24 06:17:31 +00:00
|
|
|
} else {
|
2022-06-25 02:14:48 +00:00
|
|
|
secondsHdcdSustained -= chunkDuration;
|
2022-06-24 06:17:31 +00:00
|
|
|
volumeScale = 0.5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(eqEnabled) {
|
|
|
|
volumeScale *= eqPreamp;
|
|
|
|
}
|
|
|
|
|
2022-07-10 22:21:09 +00:00
|
|
|
scale_by_volume(&buffer[0], amountRead * newFormat.mChannelsPerFrame, volumeScale);
|
2022-06-24 06:17:31 +00:00
|
|
|
|
|
|
|
return amountRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initWithController:(OutputNode *)c {
|
|
|
|
self = [super init];
|
|
|
|
if(self) {
|
|
|
|
outputController = c;
|
|
|
|
volume = 1.0;
|
|
|
|
outputDeviceID = -1;
|
|
|
|
|
|
|
|
secondsHdcdSustained = 0;
|
|
|
|
|
2022-06-24 10:45:16 +00:00
|
|
|
currentPtsLock = [[NSLock alloc] init];
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
#ifdef OUTPUT_LOG
|
2022-07-14 09:39:25 +00:00
|
|
|
NSString *logName = [NSTemporaryDirectory() stringByAppendingPathComponent:@"CogAudioLog.raw"];
|
|
|
|
_logFile = fopen([logName UTF8String], "wb");
|
2022-06-24 06:17:31 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus
|
|
|
|
default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, void *inUserData) {
|
|
|
|
OutputAVFoundation *this = (__bridge OutputAVFoundation *)inUserData;
|
|
|
|
return [this setOutputDeviceByID:-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus
|
|
|
|
current_device_listener(AudioObjectID inObjectID, UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses, void *inUserData) {
|
|
|
|
OutputAVFoundation *this = (__bridge OutputAVFoundation *)inUserData;
|
|
|
|
for(UInt32 i = 0; i < inNumberAddresses; ++i) {
|
|
|
|
switch(inAddresses[i].mSelector) {
|
|
|
|
case kAudioDevicePropertyDeviceIsAlive:
|
|
|
|
return [this setOutputDeviceByID:-1];
|
|
|
|
|
|
|
|
case kAudioDevicePropertyNominalSampleRate:
|
|
|
|
case kAudioDevicePropertyStreamFormat:
|
|
|
|
this->outputdevicechanged = YES;
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
|
|
|
|
if(context != kOutputAVFoundationContext) {
|
|
|
|
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if([keyPath isEqualToString:@"values.outputDevice"]) {
|
|
|
|
NSDictionary *device = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] objectForKey:@"outputDevice"];
|
|
|
|
|
|
|
|
[self setOutputDeviceWithDeviceDict:device];
|
|
|
|
} else if([keyPath isEqualToString:@"values.GraphicEQenable"]) {
|
|
|
|
BOOL enabled = [[[[NSUserDefaultsController sharedUserDefaultsController] defaults] objectForKey:@"GraphicEQenable"] boolValue];
|
|
|
|
|
|
|
|
[self setEqualizerEnabled:enabled];
|
|
|
|
} else if([keyPath isEqualToString:@"values.eqPreamp"]) {
|
|
|
|
float preamp = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] floatForKey:@"eqPreamp"];
|
|
|
|
eqPreamp = pow(10.0, preamp / 20.0);
|
2022-07-09 09:30:52 +00:00
|
|
|
} else if([keyPath isEqualToString:@"values.enableHrtf"]) {
|
|
|
|
enableHrtf = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] boolForKey:@"enableHrtf"];
|
2022-07-15 10:34:10 +00:00
|
|
|
if(streamFormatStarted)
|
|
|
|
resetStreamFormat = YES;
|
2022-07-14 10:02:08 +00:00
|
|
|
} else if([keyPath isEqualToString:@"values.enableFSurround"]) {
|
|
|
|
enableFSurround = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] boolForKey:@"enableFSurround"];
|
2022-07-15 10:34:10 +00:00
|
|
|
if(streamFormatStarted)
|
|
|
|
resetStreamFormat = YES;
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)signalEndOfStream:(double)latency {
|
2022-06-25 12:12:43 +00:00
|
|
|
stopped = YES;
|
2022-06-24 06:17:31 +00:00
|
|
|
BOOL ret = [outputController selectNextBuffer];
|
2022-06-25 12:12:43 +00:00
|
|
|
stopped = ret;
|
2022-06-24 06:17:31 +00:00
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(NSEC_PER_SEC * latency)), dispatch_get_main_queue(), ^{
|
|
|
|
[self->outputController endOfInputPlayed];
|
|
|
|
[self->outputController resetAmountPlayed];
|
|
|
|
self->lastCheckpointPts = self->trackPts;
|
|
|
|
self->trackPts = kCMTimeZero;
|
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
- (BOOL)processEndOfStream {
|
2022-06-25 02:14:48 +00:00
|
|
|
if(stopping || ([outputController endOfStream] == YES && [self signalEndOfStream:secondsLatency])) {
|
2022-06-24 09:46:23 +00:00
|
|
|
stopping = YES;
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
- (void)threadEntry:(id)arg {
|
|
|
|
running = YES;
|
|
|
|
started = NO;
|
2022-12-10 05:18:47 +00:00
|
|
|
shouldPlayOutBuffer = NO;
|
2022-06-24 06:17:31 +00:00
|
|
|
secondsLatency = 1.0;
|
|
|
|
|
|
|
|
while(!stopping) {
|
|
|
|
if([outputController shouldReset]) {
|
|
|
|
[outputController setShouldReset:NO];
|
|
|
|
[self removeSynchronizerBlock];
|
|
|
|
[renderSynchronizer setRate:0];
|
|
|
|
[audioRenderer stopRequestingMediaData];
|
|
|
|
[audioRenderer flush];
|
|
|
|
currentPts = kCMTimeZero;
|
|
|
|
lastPts = kCMTimeZero;
|
|
|
|
outputPts = kCMTimeZero;
|
|
|
|
trackPts = kCMTimeZero;
|
|
|
|
lastCheckpointPts = kCMTimeZero;
|
|
|
|
secondsLatency = 1.0;
|
|
|
|
started = NO;
|
2022-06-26 08:09:01 +00:00
|
|
|
restarted = NO;
|
2022-06-24 06:17:31 +00:00
|
|
|
[self synchronizerBlock];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(stopping)
|
|
|
|
break;
|
|
|
|
|
2022-06-25 13:00:11 +00:00
|
|
|
@autoreleasepool {
|
|
|
|
while(!stopping && [audioRenderer isReadyForMoreMediaData]) {
|
|
|
|
CMSampleBufferRef bufferRef = [self makeSampleBuffer];
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-06-25 13:00:11 +00:00
|
|
|
if(bufferRef) {
|
|
|
|
CMTime chunkDuration = CMSampleBufferGetDuration(bufferRef);
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-07-19 14:31:04 +00:00
|
|
|
[currentPtsLock lock];
|
2022-06-25 13:00:11 +00:00
|
|
|
outputPts = CMTimeAdd(outputPts, chunkDuration);
|
2022-07-19 14:31:04 +00:00
|
|
|
[currentPtsLock unlock];
|
2022-06-25 13:00:11 +00:00
|
|
|
trackPts = CMTimeAdd(trackPts, chunkDuration);
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-06-25 13:00:11 +00:00
|
|
|
[audioRenderer enqueueSampleBuffer:bufferRef];
|
|
|
|
|
|
|
|
CFRelease(bufferRef);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!paused && !started) {
|
|
|
|
[self resume];
|
|
|
|
}
|
|
|
|
|
|
|
|
if([outputController shouldContinue] == NO) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
usleep(5000);
|
|
|
|
}
|
|
|
|
|
|
|
|
stopped = YES;
|
2022-06-24 10:46:01 +00:00
|
|
|
if(!stopInvoked) {
|
2022-06-25 02:14:48 +00:00
|
|
|
[self doStop];
|
2022-06-24 10:46:01 +00:00
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (OSStatus)setOutputDeviceByID:(AudioDeviceID)deviceID {
|
|
|
|
OSStatus err;
|
|
|
|
BOOL defaultDevice = NO;
|
|
|
|
AudioObjectPropertyAddress theAddress = {
|
|
|
|
.mSelector = kAudioHardwarePropertyDefaultOutputDevice,
|
|
|
|
.mScope = kAudioObjectPropertyScopeGlobal,
|
|
|
|
.mElement = kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
if(deviceID == -1) {
|
|
|
|
defaultDevice = YES;
|
|
|
|
UInt32 size = sizeof(AudioDeviceID);
|
|
|
|
err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &theAddress, 0, NULL, &size, &deviceID);
|
|
|
|
|
|
|
|
if(err != noErr) {
|
|
|
|
DLog(@"THERE'S NO DEFAULT OUTPUT DEVICE");
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(audioRenderer) {
|
|
|
|
if(defaultdevicelistenerapplied && !defaultDevice) {
|
|
|
|
/* Already set above
|
|
|
|
* theAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice; */
|
|
|
|
AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &theAddress, default_device_changed, (__bridge void *_Nullable)(self));
|
|
|
|
defaultdevicelistenerapplied = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputdevicechanged = NO;
|
|
|
|
|
|
|
|
if(outputDeviceID != deviceID) {
|
|
|
|
if(currentdevicelistenerapplied) {
|
|
|
|
if(devicealivelistenerapplied) {
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyDeviceIsAlive;
|
|
|
|
AudioObjectRemovePropertyListener(outputDeviceID, &theAddress, current_device_listener, (__bridge void *_Nullable)(self));
|
|
|
|
devicealivelistenerapplied = NO;
|
|
|
|
}
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyStreamFormat;
|
|
|
|
AudioObjectRemovePropertyListener(outputDeviceID, &theAddress, current_device_listener, (__bridge void *_Nullable)(self));
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyNominalSampleRate;
|
|
|
|
AudioObjectRemovePropertyListener(outputDeviceID, &theAddress, current_device_listener, (__bridge void *_Nullable)(self));
|
|
|
|
currentdevicelistenerapplied = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
DLog(@"Device: %i\n", deviceID);
|
|
|
|
outputDeviceID = deviceID;
|
|
|
|
|
|
|
|
CFStringRef deviceUID = NULL;
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyDeviceUID;
|
|
|
|
UInt32 size = sizeof(deviceUID);
|
|
|
|
|
|
|
|
err = AudioObjectGetPropertyData(outputDeviceID, &theAddress, 0, NULL, &size, &deviceUID);
|
|
|
|
if(err != noErr) {
|
|
|
|
DLog(@"Unable to get UID of device");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
[audioRenderer setAudioOutputDeviceUniqueID:(__bridge NSString *)deviceUID];
|
|
|
|
CFRelease(deviceUID);
|
|
|
|
|
|
|
|
outputdevicechanged = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!currentdevicelistenerapplied) {
|
|
|
|
if(!devicealivelistenerapplied && !defaultDevice) {
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyDeviceIsAlive;
|
|
|
|
AudioObjectAddPropertyListener(outputDeviceID, &theAddress, current_device_listener, (__bridge void *_Nullable)(self));
|
|
|
|
devicealivelistenerapplied = YES;
|
|
|
|
}
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyStreamFormat;
|
|
|
|
AudioObjectAddPropertyListener(outputDeviceID, &theAddress, current_device_listener, (__bridge void *_Nullable)(self));
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyNominalSampleRate;
|
|
|
|
AudioObjectAddPropertyListener(outputDeviceID, &theAddress, current_device_listener, (__bridge void *_Nullable)(self));
|
|
|
|
currentdevicelistenerapplied = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!defaultdevicelistenerapplied && defaultDevice) {
|
|
|
|
theAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
|
|
|
|
AudioObjectAddPropertyListener(kAudioObjectSystemObject, &theAddress, default_device_changed, (__bridge void *_Nullable)(self));
|
|
|
|
defaultdevicelistenerapplied = YES;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = noErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(err != noErr) {
|
|
|
|
DLog(@"No output device with ID %d could be found.", deviceID);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)setOutputDeviceWithDeviceDict:(NSDictionary *)deviceDict {
|
|
|
|
NSNumber *deviceIDNum = [deviceDict objectForKey:@"deviceID"];
|
|
|
|
AudioDeviceID outputDeviceID = [deviceIDNum unsignedIntValue] ?: -1;
|
|
|
|
|
|
|
|
__block OSStatus err = [self setOutputDeviceByID:outputDeviceID];
|
|
|
|
|
|
|
|
if(err != noErr) {
|
|
|
|
// Try matching by name.
|
|
|
|
NSString *userDeviceName = deviceDict[@"name"];
|
|
|
|
|
|
|
|
[self enumerateAudioOutputsUsingBlock:
|
|
|
|
^(NSString *deviceName, AudioDeviceID deviceID, AudioDeviceID systemDefaultID, BOOL *stop) {
|
|
|
|
if([deviceName isEqualToString:userDeviceName]) {
|
|
|
|
err = [self setOutputDeviceByID:deviceID];
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// Disable. Would cause loop by triggering `-observeValueForKeyPath:ofObject:change:context:` above.
|
|
|
|
// Update `outputDevice`, in case the ID has changed.
|
|
|
|
NSDictionary *deviceInfo = @{
|
|
|
|
@"name": deviceName,
|
|
|
|
@"deviceID": @(deviceID),
|
|
|
|
};
|
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:deviceInfo forKey:@"outputDevice"];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DLog(@"Found output device: \"%@\" (%d).", deviceName, deviceID);
|
|
|
|
|
|
|
|
*stop = YES;
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(err != noErr) {
|
|
|
|
ALog(@"No output device could be found, your random error code is %d. Have a nice day!", err);
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following is largely a copy pasta of -awakeFromNib from "OutputsArrayController.m".
|
|
|
|
// TODO: Share the code. (How to do this across xcodeproj?)
|
|
|
|
- (void)enumerateAudioOutputsUsingBlock:(void(NS_NOESCAPE ^ _Nonnull)(NSString *deviceName, AudioDeviceID deviceID, AudioDeviceID systemDefaultID, BOOL *stop))block {
|
|
|
|
UInt32 propsize;
|
|
|
|
AudioObjectPropertyAddress theAddress = {
|
|
|
|
.mSelector = kAudioHardwarePropertyDevices,
|
|
|
|
.mScope = kAudioObjectPropertyScopeGlobal,
|
|
|
|
.mElement = kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
__Verify_noErr(AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &theAddress, 0, NULL, &propsize));
|
|
|
|
UInt32 nDevices = propsize / (UInt32)sizeof(AudioDeviceID);
|
|
|
|
AudioDeviceID *devids = malloc(propsize);
|
|
|
|
__Verify_noErr(AudioObjectGetPropertyData(kAudioObjectSystemObject, &theAddress, 0, NULL, &propsize, devids));
|
|
|
|
|
|
|
|
theAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
|
|
|
|
AudioDeviceID systemDefault;
|
|
|
|
propsize = sizeof(systemDefault);
|
|
|
|
__Verify_noErr(AudioObjectGetPropertyData(kAudioObjectSystemObject, &theAddress, 0, NULL, &propsize, &systemDefault));
|
|
|
|
|
|
|
|
theAddress.mScope = kAudioDevicePropertyScopeOutput;
|
|
|
|
|
|
|
|
for(UInt32 i = 0; i < nDevices; ++i) {
|
|
|
|
UInt32 isAlive = 0;
|
|
|
|
propsize = sizeof(isAlive);
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyDeviceIsAlive;
|
|
|
|
__Verify_noErr(AudioObjectGetPropertyData(devids[i], &theAddress, 0, NULL, &propsize, &isAlive));
|
|
|
|
if(!isAlive) continue;
|
|
|
|
|
|
|
|
CFStringRef name = NULL;
|
|
|
|
propsize = sizeof(name);
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyDeviceNameCFString;
|
|
|
|
__Verify_noErr(AudioObjectGetPropertyData(devids[i], &theAddress, 0, NULL, &propsize, &name));
|
|
|
|
|
|
|
|
propsize = 0;
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyStreamConfiguration;
|
|
|
|
__Verify_noErr(AudioObjectGetPropertyDataSize(devids[i], &theAddress, 0, NULL, &propsize));
|
|
|
|
|
|
|
|
if(propsize < sizeof(UInt32)) {
|
|
|
|
if(name) CFRelease(name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioBufferList *bufferList = (AudioBufferList *)malloc(propsize);
|
|
|
|
__Verify_noErr(AudioObjectGetPropertyData(devids[i], &theAddress, 0, NULL, &propsize, bufferList));
|
|
|
|
UInt32 bufferCount = bufferList->mNumberBuffers;
|
|
|
|
free(bufferList);
|
|
|
|
|
|
|
|
if(!bufferCount) {
|
|
|
|
if(name) CFRelease(name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL stop = NO;
|
|
|
|
NSString *deviceName = name ? [NSString stringWithString:(__bridge NSString *)name] : [NSString stringWithFormat:@"Unknown device %u", (unsigned int)devids[i]];
|
|
|
|
|
|
|
|
block(deviceName,
|
|
|
|
devids[i],
|
|
|
|
systemDefault,
|
|
|
|
&stop);
|
|
|
|
|
|
|
|
CFRelease(name);
|
|
|
|
|
|
|
|
if(stop) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(devids);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateStreamFormat {
|
|
|
|
/* Set the channel layout for the audio queue */
|
2022-07-14 10:16:21 +00:00
|
|
|
resetStreamFormat = NO;
|
2022-07-10 22:21:09 +00:00
|
|
|
|
|
|
|
uint32_t channels = realStreamFormat.mChannelsPerFrame;
|
|
|
|
uint32_t channelConfig = realStreamChannelConfig;
|
|
|
|
|
|
|
|
if(enableFSurround && channels == 2 && channelConfig == AudioConfigStereo) {
|
2022-07-15 10:34:10 +00:00
|
|
|
[currentPtsLock lock];
|
2022-07-10 22:21:09 +00:00
|
|
|
fsurround = [[FSurroundFilter alloc] initWithSampleRate:realStreamFormat.mSampleRate];
|
2022-07-15 10:34:10 +00:00
|
|
|
[currentPtsLock unlock];
|
2022-07-10 22:21:09 +00:00
|
|
|
channels = [fsurround channelCount];
|
|
|
|
channelConfig = [fsurround channelConfig];
|
2022-07-15 10:34:10 +00:00
|
|
|
FSurroundDelayRemoved = NO;
|
2022-07-10 22:21:09 +00:00
|
|
|
} else {
|
2022-07-15 10:34:10 +00:00
|
|
|
[currentPtsLock lock];
|
2022-07-10 22:21:09 +00:00
|
|
|
fsurround = nil;
|
2022-07-15 10:34:10 +00:00
|
|
|
[currentPtsLock unlock];
|
2022-07-10 22:21:09 +00:00
|
|
|
}
|
2022-07-14 10:02:08 +00:00
|
|
|
|
2022-07-10 01:42:03 +00:00
|
|
|
/* Apple's audio processor really only supports common 1-8 channel formats */
|
2022-07-10 22:21:09 +00:00
|
|
|
if(enableHrtf || channels > 8 || ((channelConfig & ~(AudioConfig6Point1|AudioConfig7Point1)) != 0)) {
|
2022-07-09 09:30:52 +00:00
|
|
|
NSURL *presetUrl = [[NSBundle mainBundle] URLForResource:@"SADIE_D02-96000" withExtension:@"mhr"];
|
|
|
|
|
2022-07-10 22:21:09 +00:00
|
|
|
hrtf = [[HeadphoneFilter alloc] initWithImpulseFile:presetUrl forSampleRate:realStreamFormat.mSampleRate withInputChannels:channels withConfig:channelConfig];
|
2022-07-09 09:30:52 +00:00
|
|
|
|
2022-07-10 22:21:09 +00:00
|
|
|
channels = 2;
|
|
|
|
channelConfig = AudioChannelSideLeft | AudioChannelSideRight;
|
2022-07-09 09:30:52 +00:00
|
|
|
} else {
|
|
|
|
hrtf = nil;
|
|
|
|
}
|
2022-07-14 10:02:08 +00:00
|
|
|
|
2022-07-10 22:21:09 +00:00
|
|
|
streamFormat = realStreamFormat;
|
|
|
|
streamFormat.mChannelsPerFrame = channels;
|
|
|
|
streamFormat.mBytesPerFrame = sizeof(float) * channels;
|
|
|
|
streamFormat.mFramesPerPacket = 1;
|
|
|
|
streamFormat.mBytesPerPacket = sizeof(float) * channels;
|
|
|
|
streamChannelConfig = channelConfig;
|
2022-07-09 09:30:52 +00:00
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
AudioChannelLayoutTag tag = 0;
|
2022-06-25 06:12:14 +00:00
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
AudioChannelLayout layout = { 0 };
|
2022-06-25 06:12:14 +00:00
|
|
|
switch(streamChannelConfig) {
|
|
|
|
case AudioConfigMono:
|
2022-06-24 06:17:31 +00:00
|
|
|
tag = kAudioChannelLayoutTag_Mono;
|
|
|
|
break;
|
2022-06-25 06:12:14 +00:00
|
|
|
case AudioConfigStereo:
|
2022-06-24 06:17:31 +00:00
|
|
|
tag = kAudioChannelLayoutTag_Stereo;
|
|
|
|
break;
|
2022-06-25 06:12:14 +00:00
|
|
|
case AudioConfig3Point0:
|
2022-06-24 06:17:31 +00:00
|
|
|
tag = kAudioChannelLayoutTag_WAVE_3_0;
|
|
|
|
break;
|
2022-06-25 06:12:14 +00:00
|
|
|
case AudioConfig4Point0:
|
2022-06-24 06:17:31 +00:00
|
|
|
tag = kAudioChannelLayoutTag_WAVE_4_0_A;
|
|
|
|
break;
|
2022-06-25 06:12:14 +00:00
|
|
|
case AudioConfig5Point0:
|
2022-06-24 06:17:31 +00:00
|
|
|
tag = kAudioChannelLayoutTag_WAVE_5_0_A;
|
|
|
|
break;
|
2022-06-25 06:12:14 +00:00
|
|
|
case AudioConfig5Point1:
|
2022-06-24 06:17:31 +00:00
|
|
|
tag = kAudioChannelLayoutTag_WAVE_5_1_A;
|
|
|
|
break;
|
2022-06-25 06:12:14 +00:00
|
|
|
case AudioConfig6Point1:
|
2022-06-24 06:17:31 +00:00
|
|
|
tag = kAudioChannelLayoutTag_WAVE_6_1;
|
|
|
|
break;
|
2022-06-25 06:12:14 +00:00
|
|
|
case AudioConfig7Point1:
|
2022-06-24 06:17:31 +00:00
|
|
|
tag = kAudioChannelLayoutTag_WAVE_7_1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
tag = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tag) {
|
|
|
|
layout.mChannelLayoutTag = tag;
|
2022-06-25 06:12:14 +00:00
|
|
|
} else {
|
|
|
|
layout.mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelBitmap;
|
|
|
|
layout.mChannelBitmap = streamChannelConfig;
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-25 13:00:11 +00:00
|
|
|
if(audioFormatDescription) {
|
|
|
|
CFRelease(audioFormatDescription);
|
|
|
|
audioFormatDescription = NULL;
|
|
|
|
}
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
if(CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &streamFormat, sizeof(layout), &layout, 0, NULL, NULL, &audioFormatDescription) != noErr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-04 05:39:43 +00:00
|
|
|
if(eqInitialized) {
|
|
|
|
AudioUnitUninitialize(_eq);
|
|
|
|
eqInitialized = NO;
|
|
|
|
}
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
AudioStreamBasicDescription asbd = streamFormat;
|
|
|
|
|
2022-07-04 05:39:43 +00:00
|
|
|
// Of course, non-interleaved has only one sample per frame/packet, per buffer
|
|
|
|
asbd.mFormatFlags |= kAudioFormatFlagIsNonInterleaved;
|
|
|
|
asbd.mBytesPerFrame = sizeof(float);
|
|
|
|
asbd.mBytesPerPacket = sizeof(float);
|
|
|
|
asbd.mFramesPerPacket = 1;
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-07-10 22:21:09 +00:00
|
|
|
UInt32 maximumFrames = 4096;
|
2022-06-24 06:17:31 +00:00
|
|
|
AudioUnitSetProperty(_eq, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, &maximumFrames, sizeof(maximumFrames));
|
|
|
|
|
|
|
|
AudioUnitSetProperty(_eq, kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Input, 0, &asbd, sizeof(asbd));
|
|
|
|
|
|
|
|
AudioUnitSetProperty(_eq, kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Output, 0, &asbd, sizeof(asbd));
|
|
|
|
AudioUnitReset(_eq, kAudioUnitScope_Input, 0);
|
|
|
|
AudioUnitReset(_eq, kAudioUnitScope_Output, 0);
|
|
|
|
|
|
|
|
AudioUnitReset(_eq, kAudioUnitScope_Global, 0);
|
|
|
|
|
2022-07-04 05:39:43 +00:00
|
|
|
if(AudioUnitInitialize(_eq) != noErr) {
|
|
|
|
eqEnabled = NO;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
eqInitialized = YES;
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
eqEnabled = [[[[NSUserDefaultsController sharedUserDefaultsController] defaults] objectForKey:@"GraphicEQenable"] boolValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CMSampleBufferRef)makeSampleBuffer {
|
|
|
|
CMBlockBufferRef blockBuffer = nil;
|
|
|
|
|
|
|
|
int samplesRendered = [self makeBlockBuffer:&blockBuffer];
|
|
|
|
if(!samplesRendered) return nil;
|
|
|
|
|
|
|
|
CMSampleBufferRef sampleBuffer = nil;
|
|
|
|
|
|
|
|
OSStatus err = CMAudioSampleBufferCreateReadyWithPacketDescriptions(kCFAllocatorDefault, blockBuffer, audioFormatDescription, samplesRendered, outputPts, nil, &sampleBuffer);
|
2022-07-10 22:21:09 +00:00
|
|
|
CFRelease(blockBuffer);
|
2022-06-24 06:17:31 +00:00
|
|
|
if(err != noErr) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sampleBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int)makeBlockBuffer:(CMBlockBufferRef *)blockBufferOut {
|
|
|
|
OSStatus status;
|
|
|
|
CMBlockBufferRef blockListBuffer = nil;
|
|
|
|
|
|
|
|
status = CMBlockBufferCreateEmpty(kCFAllocatorDefault, 0, 0, &blockListBuffer);
|
|
|
|
if(status != noErr || !blockListBuffer) return 0;
|
|
|
|
|
2022-07-15 10:34:10 +00:00
|
|
|
if(resetStreamFormat || streamFormatChanged) {
|
|
|
|
streamFormatChanged = NO;
|
2022-07-14 10:16:21 +00:00
|
|
|
[self updateStreamFormat];
|
|
|
|
}
|
|
|
|
|
2022-07-15 10:34:10 +00:00
|
|
|
int inputRendered = inputBufferLastTime;
|
|
|
|
int bytesRendered = inputRendered * newFormat.mBytesPerPacket;
|
|
|
|
|
|
|
|
while(inputRendered < 4096) {
|
2022-07-10 22:21:09 +00:00
|
|
|
int maxToRender = MIN(4096 - inputRendered, 512);
|
2022-07-11 21:37:10 +00:00
|
|
|
int rendered = [self renderInput:maxToRender toBuffer:&tempBuffer[0]];
|
|
|
|
if(rendered > 0) {
|
|
|
|
memcpy((((uint8_t *)inputBuffer) + bytesRendered), &tempBuffer[0], rendered * newFormat.mBytesPerPacket);
|
|
|
|
}
|
2022-07-10 22:21:09 +00:00
|
|
|
inputRendered += rendered;
|
|
|
|
bytesRendered += rendered * newFormat.mBytesPerPacket;
|
|
|
|
if(streamFormatChanged) {
|
|
|
|
streamFormatChanged = NO;
|
2022-07-15 10:34:10 +00:00
|
|
|
if(inputRendered) {
|
|
|
|
resetStreamFormat = YES;
|
2022-07-10 22:21:09 +00:00
|
|
|
break;
|
2022-07-15 10:34:10 +00:00
|
|
|
} else {
|
|
|
|
[self updateStreamFormat];
|
2022-07-10 22:21:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-24 09:46:23 +00:00
|
|
|
if([self processEndOfStream]) break;
|
2022-07-15 10:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inputBufferLastTime = inputRendered;
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
int samplesRenderedTotal = 0;
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
for(size_t i = 0; i < 2;) {
|
|
|
|
int samplesRendered;
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
if(i == 0) {
|
2023-02-05 05:52:52 +00:00
|
|
|
if(!rsold) {
|
2022-06-24 09:46:23 +00:00
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-24 10:45:16 +00:00
|
|
|
[currentPtsLock lock];
|
2023-02-05 05:52:52 +00:00
|
|
|
samplesRendered = rsstate_flush(rsold, &rsTempBuffer[0], 4096);
|
2022-06-24 10:45:16 +00:00
|
|
|
[currentPtsLock unlock];
|
2022-06-24 09:46:23 +00:00
|
|
|
if(!samplesRendered) {
|
2023-02-05 05:52:52 +00:00
|
|
|
rsstate_delete(rsold);
|
|
|
|
rsold = NULL;
|
|
|
|
rsDone = YES;
|
2022-07-10 22:21:09 +00:00
|
|
|
++i;
|
|
|
|
continue;
|
2022-06-24 09:46:23 +00:00
|
|
|
}
|
2023-02-05 05:52:52 +00:00
|
|
|
samplePtr = &rsTempBuffer[0];
|
2022-06-24 09:46:23 +00:00
|
|
|
} else {
|
|
|
|
samplesRendered = inputRendered;
|
|
|
|
samplePtr = &inputBuffer[0];
|
2023-02-05 05:52:52 +00:00
|
|
|
if(rsDone) {
|
|
|
|
rsDone = NO;
|
2022-07-09 09:30:52 +00:00
|
|
|
realStreamFormat = newFormat;
|
|
|
|
realStreamChannelConfig = newChannelConfig;
|
2022-07-15 10:34:10 +00:00
|
|
|
streamFormatChanged = YES;
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 10:34:10 +00:00
|
|
|
if(samplesRendered || fsurround) {
|
2022-07-10 22:21:09 +00:00
|
|
|
if(fsurround) {
|
2022-07-15 10:34:10 +00:00
|
|
|
int countToProcess = samplesRendered;
|
|
|
|
if(countToProcess < 4096) {
|
|
|
|
bzero(samplePtr + countToProcess * 2, (4096 - countToProcess) * 2 * sizeof(float));
|
|
|
|
countToProcess = 4096;
|
|
|
|
}
|
|
|
|
[fsurround process:samplePtr output:&fsurroundBuffer[0] count:countToProcess];
|
2022-07-10 22:21:09 +00:00
|
|
|
samplePtr = &fsurroundBuffer[0];
|
2022-07-15 10:34:10 +00:00
|
|
|
if(resetStreamFormat || samplesRendered < 4096) {
|
|
|
|
bzero(&fsurroundBuffer[4096 * 6], 4096 * 2 * sizeof(float));
|
|
|
|
[fsurround process:&fsurroundBuffer[4096 * 6] output:&fsurroundBuffer[4096 * 6] count:4096];
|
|
|
|
samplesRendered += 2048;
|
|
|
|
}
|
|
|
|
if(!FSurroundDelayRemoved) {
|
|
|
|
FSurroundDelayRemoved = YES;
|
|
|
|
if(samplesRendered > 2048) {
|
|
|
|
samplePtr += 2048 * 6;
|
|
|
|
samplesRendered -= 2048;
|
|
|
|
}
|
|
|
|
}
|
2022-07-10 22:21:09 +00:00
|
|
|
}
|
2022-07-15 10:34:10 +00:00
|
|
|
|
|
|
|
if(!samplesRendered) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-07-10 01:42:03 +00:00
|
|
|
if(hrtf) {
|
2022-07-09 09:30:52 +00:00
|
|
|
[hrtf process:samplePtr sampleCount:samplesRendered toBuffer:&hrtfBuffer[0]];
|
|
|
|
samplePtr = &hrtfBuffer[0];
|
|
|
|
}
|
|
|
|
|
2022-07-04 05:39:43 +00:00
|
|
|
if(eqEnabled && eqInitialized) {
|
2022-06-24 09:46:23 +00:00
|
|
|
const int channels = streamFormat.mChannelsPerFrame;
|
|
|
|
if(channels > 0) {
|
|
|
|
const size_t channelsminusone = channels - 1;
|
|
|
|
uint8_t tempBuffer[sizeof(AudioBufferList) + sizeof(AudioBuffer) * channelsminusone];
|
|
|
|
AudioBufferList *ioData = (AudioBufferList *)&tempBuffer[0];
|
|
|
|
|
|
|
|
ioData->mNumberBuffers = channels;
|
|
|
|
for(size_t i = 0; i < channels; ++i) {
|
2022-07-10 22:21:09 +00:00
|
|
|
ioData->mBuffers[i].mData = &eqBuffer[4096 * i];
|
2022-07-04 05:39:43 +00:00
|
|
|
ioData->mBuffers[i].mDataByteSize = samplesRendered * sizeof(float);
|
2022-06-24 09:46:23 +00:00
|
|
|
ioData->mBuffers[i].mNumberChannels = 1;
|
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
status = AudioUnitRender(_eq, NULL, &timeStamp, 0, samplesRendered, ioData);
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
if(status != noErr) {
|
2022-06-25 13:00:11 +00:00
|
|
|
CFRelease(blockListBuffer);
|
2022-06-24 09:46:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
timeStamp.mSampleTime += ((double)samplesRendered) / streamFormat.mSampleRate;
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
for(int i = 0; i < channels; ++i) {
|
2022-07-10 22:21:09 +00:00
|
|
|
cblas_scopy(samplesRendered, &eqBuffer[4096 * i], 1, samplePtr + i, channels);
|
2022-06-24 09:46:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CMBlockBufferRef blockBuffer = nil;
|
|
|
|
size_t dataByteSize = samplesRendered * sizeof(float) * streamFormat.mChannelsPerFrame;
|
|
|
|
|
2022-07-14 09:39:25 +00:00
|
|
|
#ifdef OUTPUT_LOG
|
|
|
|
fwrite(samplePtr, 1, dataByteSize, _logFile);
|
|
|
|
#endif
|
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault, nil, dataByteSize, kCFAllocatorDefault, nil, 0, dataByteSize, kCMBlockBufferAssureMemoryNowFlag, &blockBuffer);
|
|
|
|
|
|
|
|
if(status != noErr || !blockBuffer) {
|
2022-06-25 13:00:11 +00:00
|
|
|
CFRelease(blockListBuffer);
|
2022-06-24 09:46:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = CMBlockBufferReplaceDataBytes(samplePtr, blockBuffer, 0, dataByteSize);
|
|
|
|
|
|
|
|
if(status != noErr) {
|
2022-06-25 13:00:11 +00:00
|
|
|
CFRelease(blockBuffer);
|
|
|
|
CFRelease(blockListBuffer);
|
2022-06-24 09:46:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
status = CMBlockBufferAppendBufferReference(blockListBuffer, blockBuffer, 0, CMBlockBufferGetDataLength(blockBuffer), 0);
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
if(status != noErr) {
|
2022-06-25 13:00:11 +00:00
|
|
|
CFRelease(blockBuffer);
|
|
|
|
CFRelease(blockListBuffer);
|
2022-06-24 09:46:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-06-25 13:00:11 +00:00
|
|
|
|
|
|
|
CFRelease(blockBuffer);
|
2022-06-24 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(i == 0) {
|
2022-07-15 10:34:10 +00:00
|
|
|
samplesRenderedTotal += samplesRendered;
|
|
|
|
if(samplesRendered) {
|
|
|
|
break;
|
2022-06-24 09:46:23 +00:00
|
|
|
}
|
2022-07-15 10:34:10 +00:00
|
|
|
++i;
|
2022-06-24 09:46:23 +00:00
|
|
|
} else {
|
2022-07-15 10:34:10 +00:00
|
|
|
inputBufferLastTime = 0;
|
2022-06-24 09:46:23 +00:00
|
|
|
samplesRenderedTotal += samplesRendered;
|
2022-07-10 22:21:09 +00:00
|
|
|
++i;
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*blockBufferOut = blockListBuffer;
|
|
|
|
|
2022-06-24 09:46:23 +00:00
|
|
|
return samplesRenderedTotal;
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)setup {
|
|
|
|
if(audioRenderer || renderSynchronizer)
|
|
|
|
[self stop];
|
|
|
|
|
|
|
|
@synchronized(self) {
|
|
|
|
stopInvoked = NO;
|
2022-06-25 02:14:48 +00:00
|
|
|
stopCompleted = NO;
|
|
|
|
commandStop = NO;
|
2022-12-10 05:18:47 +00:00
|
|
|
shouldPlayOutBuffer = NO;
|
2022-06-24 06:17:31 +00:00
|
|
|
|
2022-06-25 13:00:11 +00:00
|
|
|
audioFormatDescription = NULL;
|
2022-07-14 10:16:21 +00:00
|
|
|
|
|
|
|
resetStreamFormat = NO;
|
2022-07-15 10:34:10 +00:00
|
|
|
streamFormatChanged = NO;
|
|
|
|
|
|
|
|
inputBufferLastTime = 0;
|
2022-07-14 10:16:21 +00:00
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
running = NO;
|
|
|
|
stopping = NO;
|
|
|
|
stopped = NO;
|
|
|
|
paused = NO;
|
|
|
|
outputDeviceID = -1;
|
|
|
|
restarted = NO;
|
|
|
|
|
|
|
|
downmixerForVis = nil;
|
|
|
|
|
2023-02-05 05:52:52 +00:00
|
|
|
rsDone = NO;
|
|
|
|
rsstate = NULL;
|
|
|
|
rsold = NULL;
|
2022-06-24 09:46:23 +00:00
|
|
|
|
2023-02-05 05:52:52 +00:00
|
|
|
rsvis = NULL;
|
2022-06-24 10:43:50 +00:00
|
|
|
lastVisRate = 44100.0;
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
AudioComponentDescription desc;
|
|
|
|
NSError *err;
|
|
|
|
|
|
|
|
desc.componentType = kAudioUnitType_Output;
|
|
|
|
desc.componentSubType = kAudioUnitSubType_HALOutput;
|
|
|
|
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
|
|
|
|
desc.componentFlags = 0;
|
|
|
|
desc.componentFlagsMask = 0;
|
|
|
|
|
|
|
|
audioRenderer = [[AVSampleBufferAudioRenderer alloc] init];
|
|
|
|
renderSynchronizer = [[AVSampleBufferRenderSynchronizer alloc] init];
|
|
|
|
|
|
|
|
if(audioRenderer == nil || renderSynchronizer == nil)
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
if(@available(macOS 12.0, *)) {
|
|
|
|
[audioRenderer setAllowedAudioSpatializationFormats:AVAudioSpatializationFormatMonoStereoAndMultichannel];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioComponent comp = NULL;
|
|
|
|
|
|
|
|
desc.componentType = kAudioUnitType_Effect;
|
|
|
|
desc.componentSubType = kAudioUnitSubType_GraphicEQ;
|
|
|
|
|
|
|
|
comp = AudioComponentFindNext(comp, &desc);
|
|
|
|
if(!comp)
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
OSStatus _err = AudioComponentInstanceNew(comp, &_eq);
|
|
|
|
if(err)
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
UInt32 value;
|
|
|
|
UInt32 size = sizeof(value);
|
|
|
|
|
|
|
|
value = CHUNK_SIZE;
|
|
|
|
AudioUnitSetProperty(_eq, kAudioUnitProperty_MaximumFramesPerSlice,
|
|
|
|
kAudioUnitScope_Global, 0, &value, size);
|
|
|
|
|
|
|
|
value = 127;
|
|
|
|
AudioUnitSetProperty(_eq, kAudioUnitProperty_RenderQuality,
|
|
|
|
kAudioUnitScope_Global, 0, &value, size);
|
|
|
|
|
|
|
|
AURenderCallbackStruct callbackStruct;
|
|
|
|
callbackStruct.inputProcRefCon = (__bridge void *)self;
|
|
|
|
callbackStruct.inputProc = eqRenderCallback;
|
|
|
|
AudioUnitSetProperty(_eq, kAudioUnitProperty_SetRenderCallback,
|
|
|
|
kAudioUnitScope_Input, 0, &callbackStruct, sizeof(callbackStruct));
|
|
|
|
|
|
|
|
AudioUnitReset(_eq, kAudioUnitScope_Input, 0);
|
|
|
|
AudioUnitReset(_eq, kAudioUnitScope_Output, 0);
|
|
|
|
|
|
|
|
AudioUnitReset(_eq, kAudioUnitScope_Global, 0);
|
|
|
|
|
|
|
|
_err = AudioUnitInitialize(_eq);
|
|
|
|
if(_err)
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
eqInitialized = YES;
|
|
|
|
|
|
|
|
[self setEqualizerEnabled:[[[[NSUserDefaultsController sharedUserDefaultsController] defaults] objectForKey:@"GraphicEQenable"] boolValue]];
|
|
|
|
|
|
|
|
[outputController beginEqualizer:_eq];
|
|
|
|
|
|
|
|
visController = [VisualizationController sharedController];
|
|
|
|
|
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.outputDevice" options:0 context:kOutputAVFoundationContext];
|
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.GraphicEQenable" options:0 context:kOutputAVFoundationContext];
|
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.eqPreamp" options:(NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew) context:kOutputAVFoundationContext];
|
2022-07-09 09:30:52 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.enableHrtf" options:(NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew) context:kOutputAVFoundationContext];
|
2022-07-14 10:02:08 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.enableFSurround" options:(NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew) context:kOutputAVFoundationContext];
|
2022-06-24 06:17:31 +00:00
|
|
|
observersapplied = YES;
|
|
|
|
|
|
|
|
[renderSynchronizer addRenderer:audioRenderer];
|
|
|
|
|
|
|
|
currentPts = kCMTimeZero;
|
|
|
|
lastPts = kCMTimeZero;
|
|
|
|
outputPts = kCMTimeZero;
|
|
|
|
trackPts = kCMTimeZero;
|
|
|
|
lastCheckpointPts = kCMTimeZero;
|
|
|
|
bzero(&timeStamp, sizeof(timeStamp));
|
|
|
|
timeStamp.mFlags = kAudioTimeStampSampleTimeValid;
|
|
|
|
|
|
|
|
[self synchronizerBlock];
|
|
|
|
|
|
|
|
[audioRenderer setVolume:volume];
|
|
|
|
|
|
|
|
return (err == nil);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)synchronizerBlock {
|
2022-06-24 10:45:16 +00:00
|
|
|
NSLock *lock = currentPtsLock;
|
2022-06-24 06:17:31 +00:00
|
|
|
CMTime interval = CMTimeMakeWithSeconds(1.0 / 60.0, 1000000000);
|
|
|
|
CMTime *lastPts = &self->lastPts;
|
|
|
|
CMTime *outputPts = &self->outputPts;
|
|
|
|
OutputNode *outputController = self->outputController;
|
|
|
|
double *latencySecondsOut = &self->secondsLatency;
|
|
|
|
VisualizationController *visController = self->visController;
|
2023-02-05 05:52:52 +00:00
|
|
|
void **rsstate = &self->rsstate;
|
|
|
|
void **rsold = &self->rsold;
|
|
|
|
void **rsvis = &self->rsvis;
|
2022-07-15 10:34:10 +00:00
|
|
|
FSurroundFilter *const *fsurroundtest = &self->fsurround;
|
2022-06-24 06:17:31 +00:00
|
|
|
currentPtsObserver = [renderSynchronizer addPeriodicTimeObserverForInterval:interval
|
|
|
|
queue:NULL
|
|
|
|
usingBlock:^(CMTime time) {
|
|
|
|
[lock lock];
|
|
|
|
self->currentPts = time;
|
|
|
|
[lock unlock];
|
|
|
|
CMTime timeToAdd = CMTimeSubtract(time, *lastPts);
|
|
|
|
self->lastPts = time;
|
|
|
|
double timeAdded = CMTimeGetSeconds(timeToAdd);
|
|
|
|
if(timeAdded > 0) {
|
|
|
|
[outputController incrementAmountPlayed:timeAdded];
|
|
|
|
}
|
2022-07-19 14:31:04 +00:00
|
|
|
[lock lock];
|
2022-06-24 06:17:31 +00:00
|
|
|
CMTime latencyTime = CMTimeSubtract(*outputPts, time);
|
2022-07-19 14:31:04 +00:00
|
|
|
[lock unlock];
|
2022-06-24 06:17:31 +00:00
|
|
|
double latencySeconds = CMTimeGetSeconds(latencyTime);
|
2022-06-24 10:45:16 +00:00
|
|
|
double latencyVis = 0.0;
|
|
|
|
[lock lock];
|
2023-02-05 05:52:52 +00:00
|
|
|
if(*rsstate) {
|
|
|
|
latencySeconds += rsstate_latency(*rsstate);
|
2022-06-24 10:45:16 +00:00
|
|
|
}
|
2023-02-05 05:52:52 +00:00
|
|
|
if(*rsold) {
|
|
|
|
latencySeconds += rsstate_latency(*rsold);
|
2022-06-24 10:45:16 +00:00
|
|
|
}
|
2023-02-05 05:52:52 +00:00
|
|
|
if(*rsvis) {
|
|
|
|
latencyVis = rsstate_latency(*rsvis);
|
2022-06-24 10:45:16 +00:00
|
|
|
}
|
2022-07-15 10:34:10 +00:00
|
|
|
if(*fsurroundtest) {
|
|
|
|
latencyVis += 2048.0 / [(*fsurroundtest) srate];
|
|
|
|
}
|
2022-06-24 10:45:16 +00:00
|
|
|
[lock unlock];
|
2022-06-24 06:17:31 +00:00
|
|
|
if(latencySeconds < 0)
|
|
|
|
latencySeconds = 0;
|
2022-06-24 10:45:16 +00:00
|
|
|
latencyVis = latencySeconds - latencyVis;
|
2022-06-26 08:09:01 +00:00
|
|
|
if(latencyVis < 0 || latencyVis > 30.0) {
|
|
|
|
if(latencyVis > 30.0 && !self->restarted) {
|
|
|
|
self->restarted = YES;
|
|
|
|
[outputController setShouldReset:YES];
|
|
|
|
}
|
|
|
|
latencyVis = 0.0;
|
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
*latencySecondsOut = latencySeconds;
|
2022-06-24 10:45:16 +00:00
|
|
|
[visController postLatency:latencyVis];
|
2022-06-24 06:17:31 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeSynchronizerBlock {
|
2022-07-16 05:51:59 +00:00
|
|
|
if(renderSynchronizer && currentPtsObserver) {
|
|
|
|
[renderSynchronizer removeTimeObserver:currentPtsObserver];
|
|
|
|
currentPtsObserver = nil;
|
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setVolume:(double)v {
|
|
|
|
volume = v * 0.01f;
|
|
|
|
if(audioRenderer) {
|
|
|
|
[audioRenderer setVolume:volume];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setEqualizerEnabled:(BOOL)enabled {
|
|
|
|
if(enabled && !eqEnabled) {
|
|
|
|
if(_eq) {
|
|
|
|
AudioUnitReset(_eq, kAudioUnitScope_Input, 0);
|
|
|
|
AudioUnitReset(_eq, kAudioUnitScope_Output, 0);
|
|
|
|
AudioUnitReset(_eq, kAudioUnitScope_Global, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eqEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
2022-06-25 13:42:56 +00:00
|
|
|
- (double)latency {
|
|
|
|
if(secondsLatency > 0) return secondsLatency;
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
- (void)start {
|
|
|
|
[self threadEntry:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stop {
|
2022-06-25 02:14:48 +00:00
|
|
|
commandStop = YES;
|
|
|
|
[self doStop];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)doStop {
|
|
|
|
if(stopInvoked) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
@synchronized(self) {
|
|
|
|
stopInvoked = YES;
|
|
|
|
if(observersapplied) {
|
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.outputDevice" context:kOutputAVFoundationContext];
|
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.GraphicEQenable" context:kOutputAVFoundationContext];
|
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.eqPreamp" context:kOutputAVFoundationContext];
|
2022-07-09 09:30:52 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.enableHrtf" context:kOutputAVFoundationContext];
|
2022-07-14 10:02:08 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.enableFSurround" context:kOutputAVFoundationContext];
|
2022-06-24 06:17:31 +00:00
|
|
|
observersapplied = NO;
|
|
|
|
}
|
|
|
|
stopping = YES;
|
|
|
|
paused = NO;
|
|
|
|
if(defaultdevicelistenerapplied || currentdevicelistenerapplied || devicealivelistenerapplied) {
|
|
|
|
AudioObjectPropertyAddress theAddress = {
|
|
|
|
.mScope = kAudioObjectPropertyScopeGlobal,
|
|
|
|
.mElement = kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
if(defaultdevicelistenerapplied) {
|
|
|
|
theAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
|
|
|
|
AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &theAddress, default_device_changed, (__bridge void *_Nullable)(self));
|
|
|
|
defaultdevicelistenerapplied = NO;
|
|
|
|
}
|
|
|
|
if(devicealivelistenerapplied) {
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyDeviceIsAlive;
|
|
|
|
AudioObjectRemovePropertyListener(outputDeviceID, &theAddress, current_device_listener, (__bridge void *_Nullable)(self));
|
|
|
|
devicealivelistenerapplied = NO;
|
|
|
|
}
|
|
|
|
if(currentdevicelistenerapplied) {
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyStreamFormat;
|
|
|
|
AudioObjectRemovePropertyListener(outputDeviceID, &theAddress, current_device_listener, (__bridge void *_Nullable)(self));
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyNominalSampleRate;
|
|
|
|
AudioObjectRemovePropertyListener(outputDeviceID, &theAddress, current_device_listener, (__bridge void *_Nullable)(self));
|
|
|
|
currentdevicelistenerapplied = NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(renderSynchronizer || audioRenderer) {
|
|
|
|
if(renderSynchronizer) {
|
2022-12-10 05:18:47 +00:00
|
|
|
if(shouldPlayOutBuffer && !commandStop) {
|
2022-06-24 10:46:01 +00:00
|
|
|
int compareVal = 0;
|
2022-06-25 13:42:56 +00:00
|
|
|
double secondsLatency = self->secondsLatency >= 0 ? self->secondsLatency : 0;
|
|
|
|
int compareMax = (((1000000 / 5000) * secondsLatency) + (10000 / 5000)); // latency plus 10ms, divide by sleep intervals
|
2022-06-24 10:46:01 +00:00
|
|
|
do {
|
|
|
|
[currentPtsLock lock];
|
|
|
|
compareVal = CMTimeCompare(outputPts, currentPts);
|
|
|
|
[currentPtsLock unlock];
|
|
|
|
usleep(5000);
|
2022-06-25 13:42:56 +00:00
|
|
|
} while(!commandStop && compareVal > 0 && compareMax-- > 0);
|
2022-06-24 10:46:01 +00:00
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
[self removeSynchronizerBlock];
|
2022-06-24 10:46:01 +00:00
|
|
|
[renderSynchronizer setRate:0];
|
2022-07-16 05:52:46 +00:00
|
|
|
if(audioRenderer) {
|
|
|
|
[renderSynchronizer removeRenderer:audioRenderer atTime:kCMTimeZero completionHandler:^(BOOL didRemoveRenderer) {
|
|
|
|
if(!didRemoveRenderer) {
|
|
|
|
DLog(@"Error removing renderer!");
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
|
|
|
if(audioRenderer) {
|
|
|
|
[audioRenderer stopRequestingMediaData];
|
|
|
|
[audioRenderer flush];
|
|
|
|
}
|
|
|
|
renderSynchronizer = nil;
|
|
|
|
audioRenderer = nil;
|
|
|
|
}
|
|
|
|
if(running) {
|
|
|
|
while(!stopped) {
|
|
|
|
stopping = YES;
|
|
|
|
usleep(5000);
|
|
|
|
}
|
|
|
|
}
|
2022-06-25 13:00:11 +00:00
|
|
|
if(audioFormatDescription) {
|
|
|
|
CFRelease(audioFormatDescription);
|
|
|
|
audioFormatDescription = NULL;
|
|
|
|
}
|
2022-06-24 06:17:31 +00:00
|
|
|
if(_eq) {
|
|
|
|
[outputController endEqualizer:_eq];
|
|
|
|
if(eqInitialized) {
|
|
|
|
AudioUnitUninitialize(_eq);
|
|
|
|
eqInitialized = NO;
|
|
|
|
}
|
|
|
|
AudioComponentInstanceDispose(_eq);
|
|
|
|
_eq = NULL;
|
|
|
|
}
|
|
|
|
if(downmixerForVis) {
|
|
|
|
downmixerForVis = nil;
|
|
|
|
}
|
|
|
|
#ifdef OUTPUT_LOG
|
|
|
|
if(_logFile) {
|
|
|
|
fclose(_logFile);
|
|
|
|
_logFile = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
outputController = nil;
|
|
|
|
visController = nil;
|
2023-02-05 05:52:52 +00:00
|
|
|
if(rsstate) {
|
|
|
|
rsstate_delete(rsstate);
|
|
|
|
rsstate = NULL;
|
2022-06-24 09:46:23 +00:00
|
|
|
}
|
2023-02-05 05:52:52 +00:00
|
|
|
if(rsold) {
|
|
|
|
rsstate_delete(rsold);
|
|
|
|
rsold = NULL;
|
2022-06-24 09:46:23 +00:00
|
|
|
}
|
2023-02-05 05:52:52 +00:00
|
|
|
if(rsvis) {
|
|
|
|
rsstate_delete(rsvis);
|
|
|
|
rsvis = NULL;
|
2022-06-24 10:43:50 +00:00
|
|
|
}
|
2022-06-25 02:14:48 +00:00
|
|
|
stopCompleted = YES;
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
[self stop];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)pause {
|
|
|
|
paused = YES;
|
|
|
|
if(started)
|
|
|
|
[renderSynchronizer setRate:0];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)resume {
|
|
|
|
[renderSynchronizer setRate:1.0 time:currentPts];
|
|
|
|
paused = NO;
|
|
|
|
started = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)sustainHDCD {
|
2022-06-25 02:14:48 +00:00
|
|
|
secondsHdcdSustained = 10.0;
|
2022-06-24 06:17:31 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 05:18:47 +00:00
|
|
|
- (void)setShouldPlayOutBuffer:(BOOL)s {
|
|
|
|
shouldPlayOutBuffer = s;
|
|
|
|
}
|
|
|
|
|
2022-06-24 06:17:31 +00:00
|
|
|
@end
|