51 #if defined (SUPPORT_MACOSX_AUDIO) 53 #include <CoreServices/CoreServices.h> 56 #include <CoreAudio/CoreAudio.h> 57 #include <AudioUnit/AudioUnit.h> 67 OSStatus render_callback(
void *inref,
68 AudioUnitRenderActionFlags *inflags,
69 const AudioTimeStamp *instamp,
72 AudioBufferList *ioData)
77 UInt32 channels = ioData->mNumberBuffers;
78 int totalNumberOfBytes = waveSize;
79 int channelBytesLeft = totalNumberOfBytes - waveIndex;
80 int bufferSize = ioData->mBuffers[0].mDataByteSize;
82 if(channelBytesLeft > 0) {
83 if(channelBytesLeft < bufferSize) {
84 for(UInt32 i = 0; i < channels; ++i) {
85 waveMatrix->
copy_column((
int)i, (
int short*)ioData->mBuffers[i].mData, waveIndex/2, channelBytesLeft/2);
86 memset((
char*)ioData->mBuffers[i].mData + channelBytesLeft, 0, bufferSize - channelBytesLeft) ;
88 waveIndex += channelBytesLeft;
90 for(UInt32 i = 0; i < channels; ++i)
91 waveMatrix->
copy_column((
int)i, (
int short*)ioData->mBuffers[i].mData, waveIndex/2, bufferSize/2);
92 waveIndex += bufferSize;
95 for(UInt32 i = 0; i < channels; ++i)
96 memset(ioData->mBuffers[i].mData, 0, ioData->mBuffers[i].mDataByteSize);
104 void CreateDefaultAU()
106 OSStatus
err = noErr;
109 ComponentDescription desc;
110 desc.componentType = kAudioUnitType_Output;
111 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
112 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
113 desc.componentFlags = 0;
114 desc.componentFlagsMask = 0;
116 Component comp = FindNextComponent(
NULL, &desc);
117 if (comp ==
NULL) { printf (
"FindNextComponent\n");
return; }
119 err = OpenAComponent(comp, &outau);
120 if (comp ==
NULL) { printf (
"OpenAComponent=%ld\n",
long(err));
return; }
123 AURenderCallbackStruct input;
124 input.inputProc = render_callback;
125 input.inputProcRefCon =
NULL;
127 err = AudioUnitSetProperty (outau,
128 kAudioUnitProperty_SetRenderCallback,
129 kAudioUnitScope_Input,
133 if (err) { printf (
"AudioUnitSetProperty-CB=%ld\n",
long(err));
return; }
140 AudioStreamBasicDescription waveformat, outformat;
141 UInt32 size =
sizeof(AudioStreamBasicDescription);
149 waveformat.mSampleRate = (Float64)inwave.
sample_rate();
150 waveformat.mFormatID = kAudioFormatLinearPCM;
151 waveformat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger
152 | kAudioFormatFlagsNativeEndian
153 | kLinearPCMFormatFlagIsPacked
154 | kAudioFormatFlagIsNonInterleaved;
155 waveformat.mFramesPerPacket = 1;
157 waveformat.mBytesPerPacket = 2;
158 waveformat.mBytesPerFrame = 2;
159 waveformat.mBitsPerChannel = 16;
161 err = AudioUnitSetProperty(outau,
162 kAudioUnitProperty_StreamFormat,
163 kAudioUnitScope_Input,
168 cerr <<
"Error setting input audio stream format." << endl;
169 CloseComponent(outau);
173 err = AudioUnitGetProperty(outau,
174 kAudioUnitProperty_StreamFormat,
175 kAudioUnitScope_Output,
180 cerr <<
"Error getting output audio stream format." << endl;
181 CloseComponent(outau);
185 err = AudioUnitInitialize(outau);
187 printf (
"AudioUnitInitialize=%ld\n",
long(err));
193 waveMatrix = &inwave.
values();
197 err = AudioOutputUnitStart(outau);
199 cerr <<
"Error starting audio outup: " << err << endl;
200 CloseComponent(outau);
206 size =
sizeof(UInt32);
209 err = AudioUnitGetProperty(outau, kAudioOutputUnitProperty_IsRunning,
210 kAudioUnitScope_Global, 0, &running, &size);
211 }
while (err == noErr && running && !done);
213 CloseComponent (outau);
226 cerr <<
"OS X Core Audio in not supported in this configuration." << endl;
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
void copy_column(ssize_t c, T *buf, ptrdiff_t offset=0, int num=-1) const
ssize_t num_samples() const
return the number of samples in the waveform
void err(const char *message, LISP x) EST_NORETURN
const EST_SMatrix & values() const
int play_macosx_wave(EST_Wave &inwave, EST_Option &al)
int sample_rate() const
return the sampling rate (frequency)
ssize_t num_channels() const
return the number of channels in the waveform