14 #define CHECK_SAMP(sam) \
15 if (fabs(*sam) > maxSampEver) \
16 maxSampEver = fabs(*sam);
20 static OSStatus
RenderCallback(
void * userData, AudioUnitRenderActionFlags * ioActionFlags,
21 const AudioTimeStamp * inTimeStamp, UInt32 inOutputBusNumber,
22 UInt32 inNumberFrames, AudioBufferList * ioData) {
24 unsigned numChannels = ioData->buffer(0].mNumberChannels;
38 SInt16 * outData = (SInt16 *) ioData->buffer(0].mData;
40 if (numChannels == 1) {
41 for (
unsigned i = 0; i < inNumberFrames; i++) {
42 final = (SInt16) (*samp++ * 32768.0f);
47 for (
unsigned i = 0; i < inNumberFrames; i++) {
49 final = (SInt16) (*samp++ * 32767.0f);
52 final = (SInt16) (*samp2++ * 32767.0f);
62 mOutputBuffer.setSize(2, CGestalt::maxBufferFrames());
63 mOutputBuffer.allocateBuffers();
66 AUIO :: AUIO(
unsigned s_rate,
unsigned b_size,
int in_device,
int out_device,
67 unsigned in_chans,
unsigned out_chans)
68 :
IO(s_rate, b_size, in_device, out_device, in_chans, out_chans) {
69 mOutputBuffer.setSize(2, CGestalt::maxBufferFrames());
70 mOutputBuffer.allocateBuffers();
93 AURenderCallbackStruct renderCallback;
96 renderCallback.inputProcRefCon =
this;
98 result = AudioUnitSetProperty (
mAudioUnit, kAudioUnitProperty_SetRenderCallback,
99 kAudioUnitScope_Input, 0, &renderCallback,
sizeof(renderCallback));
106 AURenderCallbackStruct renderCallback;
108 renderCallback.inputProc = NULL;
109 renderCallback.inputProcRefCon =
this;
111 result = AudioUnitSetProperty (
mAudioUnit, kAudioUnitProperty_SetRenderCallback,
112 kAudioUnitScope_Input, 0, &renderCallback,
sizeof(renderCallback));
131 throw IOError(
"Can't get unopened input");
132 mInputBuffer.mIsPopulated =
true;
133 return(mInputBuffer);
149 int in_device,
int out_device,
unsigned in_chans,
unsigned out_chans)
150 :
AUIO(s_rate, b_size, in_device, out_device, in_chans, out_chans) {
167 AudioUnitUninitialize(mAudioUnit);
174 OSStatus result = noErr;
175 AudioComponentDescription desc;
176 desc.componentType = kAudioUnitType_Output;
180 desc.componentSubType = kAudioUnitSubType_RemoteIO;
181 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
182 desc.componentFlags = 0;
183 desc.componentFlagsMask = 0;
185 AudioComponent comp = AudioComponentFindNext(NULL, &desc);
187 logMsg(
kLogError,
"An error occured while finding the AudioUnit default output\n");
188 throw IOError(
"CoreAudio error in AudioComponentFindNext");
190 result = AudioComponentInstanceNew(comp, &
mAudioUnit);
198 AudioStreamBasicDescription audioFormat;
200 audioFormat.mFormatID = kAudioFormatLinearPCM;
201 audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
202 audioFormat.mFramesPerPacket = 1;
203 audioFormat.mChannelsPerFrame = 2;
204 audioFormat.mBitsPerChannel = 16;
205 audioFormat.mBytesPerPacket = 4;
206 audioFormat.mBytesPerFrame = 4;
209 kAudioUnitProperty_StreamFormat,
210 kAudioUnitScope_Output,
213 sizeof(audioFormat));
225 AudioSessionInitialize(NULL, NULL, NULL,
this);
228 UInt32 audioCategory = kAudioSessionCategory_LiveAudio;
229 AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
sizeof(audioCategory), &audioCategory);
230 UInt32 getAudioCategory =
sizeof(audioCategory);
231 AudioSessionGetProperty(kAudioSessionProperty_AudioCategory, &getAudioCategory, &getAudioCategory);
236 AudioSessionSetProperty (kAudioSessionProperty_PreferredHardwareIOBufferDuration,
237 sizeof(preferredBufferSize), &preferredBufferSize);
240 AudioSessionSetActive(
true);
252 OSStatus result = AudioOutputUnitStart(
mAudioUnit);
255 logMsg(
"iPhoneIO :: start");
260 OSStatus result = AudioOutputUnitStop(
mAudioUnit);
263 logMsg(
"iPhoneIO :: stop");
void logMsg(const char *format,...)
These are the public logging messages.
long mTimeSum
for printing run-time statistics
unsigned mNumFrames
num frames used in each buffer
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
void pullInput(Buffer &outBuffer, SampleBuffer out=0)
get a buffer from the CSL graph
void handleError(OSStatus result)
virtual SampleBuffer buffer(unsigned bufNum)
convenience accessors for sample buffers
void open()
open/close start/stop methods
UnitGenerator * mGraph
the root of my client DSP graph, often a mixer or panner
static unsigned blockSize()
the default block size
unsigned mNumChannels
num channels in buffer (num mono buffers)
virtual void close()
open/close start/stop methods
float sample
(could be changed to int, or double)
IO – the abstract I/O scheduling class; subclasses interface to specific I/O APIs.
static unsigned frameRate()
default frame rate
static OSStatus RenderCallback(void *userData, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inOutputBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData)
void close()
open/close start/stop methods
unsigned mNumFramesPlayed
counter of frames I've played
static unsigned mNumInChannels
The actual start-up values are defined in CSL_Types.h.
virtual void open()
open/close start/stop methods
void handleError(OSStatus result)
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Buffer mOutputBuffer
the output buffer I use (passed to nextBuffer calls)
Buffer mInputBuffer
the most recent input buffer (if it's turned on)
virtual Buffer & getInput()
get the current input buffer
General-purpose AudioUnit IO class.
Base class of CSL exceptions (written upper-case). Has a string message.
vector< IODevice * > gIODevices