2023-07-29 03:23:58 -04:00
|
|
|
//
|
|
|
|
// OutputCoreAudio.h
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Christopher Snowhill on 7/25/23.
|
2024-08-17 08:06:58 -04:00
|
|
|
// Copyright 2023-2024 Christopher Snowhill. All rights reserved.
|
2023-07-29 03:23:58 -04:00
|
|
|
//
|
|
|
|
|
|
|
|
#import <AssertMacros.h>
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
#import <AudioToolbox/AudioToolbox.h>
|
|
|
|
#import <AudioUnit/AudioUnit.h>
|
|
|
|
#import <CoreAudio/AudioHardware.h>
|
|
|
|
#import <CoreAudio/CoreAudioTypes.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
#import <atomic>
|
|
|
|
using std::atomic_long;
|
|
|
|
#else
|
|
|
|
#import <stdatomic.h>
|
|
|
|
#endif
|
|
|
|
|
2023-10-03 08:59:54 -03:00
|
|
|
#import <simd/simd.h>
|
|
|
|
|
2025-03-06 01:05:33 -03:00
|
|
|
#import <CogAudio/ChunkList.h>
|
2025-02-26 01:10:06 -03:00
|
|
|
#import <CogAudio/HeadphoneFilter.h>
|
2023-07-29 03:23:58 -04:00
|
|
|
|
|
|
|
//#define OUTPUT_LOG
|
|
|
|
#ifdef OUTPUT_LOG
|
|
|
|
#import <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
@class OutputNode;
|
|
|
|
|
|
|
|
@class AudioChunk;
|
|
|
|
|
|
|
|
@interface OutputCoreAudio : NSObject {
|
|
|
|
OutputNode *outputController;
|
|
|
|
|
|
|
|
dispatch_semaphore_t writeSemaphore;
|
|
|
|
dispatch_semaphore_t readSemaphore;
|
|
|
|
|
|
|
|
NSLock *outputLock;
|
|
|
|
|
2025-02-12 09:41:11 -03:00
|
|
|
double streamTimestamp;
|
2023-07-29 03:23:58 -04:00
|
|
|
|
|
|
|
BOOL stopInvoked;
|
|
|
|
BOOL stopCompleted;
|
|
|
|
BOOL running;
|
|
|
|
BOOL stopping;
|
|
|
|
BOOL stopped;
|
|
|
|
BOOL started;
|
|
|
|
BOOL paused;
|
|
|
|
BOOL restarted;
|
|
|
|
BOOL commandStop;
|
2025-03-07 10:37:04 -03:00
|
|
|
BOOL resetting;
|
2023-07-29 03:23:58 -04:00
|
|
|
|
2025-03-11 03:08:49 -03:00
|
|
|
BOOL cutOffInput;
|
2025-03-08 04:22:16 -03:00
|
|
|
BOOL fading, faded;
|
|
|
|
float fadeLevel;
|
|
|
|
float fadeStep;
|
|
|
|
float fadeTarget;
|
|
|
|
|
2023-07-29 03:23:58 -04:00
|
|
|
BOOL eqEnabled;
|
|
|
|
BOOL eqInitialized;
|
|
|
|
|
|
|
|
BOOL streamFormatStarted;
|
|
|
|
BOOL streamFormatChanged;
|
|
|
|
|
|
|
|
double secondsHdcdSustained;
|
|
|
|
|
|
|
|
BOOL defaultdevicelistenerapplied;
|
|
|
|
BOOL currentdevicelistenerapplied;
|
|
|
|
BOOL devicealivelistenerapplied;
|
|
|
|
BOOL observersapplied;
|
|
|
|
BOOL outputdevicechanged;
|
|
|
|
|
|
|
|
float volume;
|
|
|
|
float eqPreamp;
|
|
|
|
|
|
|
|
AVAudioFormat *_deviceFormat;
|
|
|
|
|
|
|
|
AudioDeviceID outputDeviceID;
|
|
|
|
AudioStreamBasicDescription deviceFormat;
|
|
|
|
AudioStreamBasicDescription realStreamFormat; // stream format pre-hrtf
|
|
|
|
AudioStreamBasicDescription streamFormat; // stream format last seen in render callback
|
|
|
|
|
|
|
|
uint32_t deviceChannelConfig;
|
|
|
|
uint32_t realStreamChannelConfig;
|
|
|
|
uint32_t streamChannelConfig;
|
|
|
|
|
|
|
|
AUAudioUnit *_au;
|
|
|
|
|
|
|
|
size_t _bufferSize;
|
|
|
|
|
|
|
|
BOOL resetStreamFormat;
|
|
|
|
|
|
|
|
BOOL shouldPlayOutBuffer;
|
|
|
|
|
2025-03-06 01:05:33 -03:00
|
|
|
ChunkList *outputBuffer;
|
2023-07-29 03:23:58 -04:00
|
|
|
|
|
|
|
#ifdef OUTPUT_LOG
|
|
|
|
FILE *_logFile;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initWithController:(OutputNode *)c;
|
|
|
|
|
|
|
|
- (BOOL)setup;
|
2025-03-06 19:23:44 -03:00
|
|
|
- (OSStatus)setOutputDeviceByID:(int)deviceID;
|
2023-07-29 03:23:58 -04:00
|
|
|
- (BOOL)setOutputDeviceWithDeviceDict:(NSDictionary *)deviceDict;
|
|
|
|
- (void)start;
|
|
|
|
- (void)pause;
|
|
|
|
- (void)resume;
|
|
|
|
- (void)stop;
|
|
|
|
|
2025-03-08 04:22:16 -03:00
|
|
|
- (void)fadeOut;
|
2025-03-11 03:08:49 -03:00
|
|
|
- (void)fadeOutBackground;
|
2025-03-08 04:22:16 -03:00
|
|
|
- (void)fadeIn;
|
|
|
|
|
2023-07-29 03:23:58 -04:00
|
|
|
- (double)latency;
|
|
|
|
|
2025-02-14 23:50:50 -03:00
|
|
|
- (double)volume;
|
2023-07-29 03:23:58 -04:00
|
|
|
- (void)setVolume:(double)v;
|
|
|
|
|
|
|
|
- (void)setShouldPlayOutBuffer:(BOOL)enabled;
|
|
|
|
|
|
|
|
- (void)sustainHDCD;
|
|
|
|
|
2025-02-13 19:56:18 -03:00
|
|
|
- (AudioStreamBasicDescription)deviceFormat;
|
|
|
|
- (uint32_t)deviceChannelConfig;
|
|
|
|
|
2023-07-29 03:23:58 -04:00
|
|
|
@end
|