3 * Copyright 2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio.
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #define _USE_OMNI_THREADS_
29 #include <audio_osx_source.h>
30 #include <gr_io_signature.h>
32 #include <audio_osx.h>
34 #define _OSX_AU_DEBUG_ 0
35 #define _OSX_DO_LISTENERS_ 0
37 void PrintStreamDesc (AudioStreamBasicDescription *inDesc)
40 fprintf (stderr, "PrintStreamDesc: Can't print a NULL desc!\n");
44 fprintf (stderr, " Sample Rate : %g\n", inDesc->mSampleRate);
45 fprintf (stderr, " Format ID : %4s\n", (char*)&inDesc->mFormatID);
46 fprintf (stderr, " Format Flags : %lX\n", inDesc->mFormatFlags);
47 fprintf (stderr, " Bytes per Packet : %ld\n", inDesc->mBytesPerPacket);
48 fprintf (stderr, " Frames per Packet : %ld\n", inDesc->mFramesPerPacket);
49 fprintf (stderr, " Bytes per Frame : %ld\n", inDesc->mBytesPerFrame);
50 fprintf (stderr, " Channels per Frame : %ld\n", inDesc->mChannelsPerFrame);
51 fprintf (stderr, " Bits per Channel : %ld\n", inDesc->mBitsPerChannel);
54 // FIXME these should query some kind of user preference
56 audio_osx_source::audio_osx_source (int sample_rate,
57 const std::string device_name,
61 : gr_sync_block ("audio_osx_source",
62 gr_make_io_signature (0, 0, 0),
63 gr_make_io_signature (0, 0, 0)),
64 d_deviceSampleRate (0.0), d_outputSampleRate (0.0),
66 d_inputBufferSizeFrames (0), d_inputBufferSizeBytes (0),
67 d_outputBufferSizeFrames (0), d_outputBufferSizeBytes (0),
68 d_deviceBufferSizeFrames (0), d_deviceBufferSizeBytes (0),
69 d_leadSizeFrames (0), d_leadSizeBytes (0),
70 d_trailSizeFrames (0), d_trailSizeBytes (0),
71 d_extraBufferSizeFrames (0), d_extraBufferSizeBytes (0),
72 d_queueSampleCount (0), d_max_sample_count (0),
73 d_n_AvailableInputFrames (0), d_n_ActualInputFrames (0),
74 d_n_user_channels (0), d_n_max_channels (0), d_n_deviceChannels (0),
75 d_do_block (do_block), d_passThrough (false),
76 d_internal (0), d_cond_data (0),
78 d_InputAU (0), d_InputBuffer (0), d_OutputBuffer (0),
81 if (sample_rate <= 0) {
82 fprintf (stderr, "Invalid Sample Rate: %d\n", sample_rate);
83 throw std::invalid_argument ("audio_osx_source::audio_osx_source");
85 d_outputSampleRate = (Float64) sample_rate;
87 if (channel_config <= 0 & channel_config != -1) {
88 fprintf (stderr, "Invalid Channel Config: %d\n", channel_config);
89 throw std::invalid_argument ("audio_osx_source::audio_osx_source");
90 } else if (channel_config == -1) {
91 // no user input; try "device name" instead
92 int l_n_channels = (int) strtol (device_name.data(), (char **)NULL, 10);
93 if (l_n_channels == 0 & errno) {
94 fprintf (stderr, "Error Converting Device Name: %d\n", errno);
95 throw std::invalid_argument ("audio_osx_source::audio_osx_source");
97 if (l_n_channels <= 0)
100 channel_config = l_n_channels;
103 d_channel_config = channel_config;
105 // check that the max # of samples to store is valid
107 if (max_sample_count == -1)
108 max_sample_count = sample_rate;
109 else if (max_sample_count <= 0) {
110 fprintf (stderr, "Invalid Max Sample Count: %d\n", max_sample_count);
111 throw std::invalid_argument ("audio_osx_source::audio_osx_source");
114 d_max_sample_count = max_sample_count;
117 fprintf (stderr, "source(): max # samples = %ld\n", d_max_sample_count);
120 OSStatus err = noErr;
122 // create the default AudioUnit for input
124 // Open the default input unit
125 ComponentDescription InputDesc;
127 InputDesc.componentType = kAudioUnitType_Output;
128 InputDesc.componentSubType = kAudioUnitSubType_HALOutput;
129 InputDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
130 InputDesc.componentFlags = 0;
131 InputDesc.componentFlagsMask = 0;
133 Component comp = FindNextComponent (NULL, &InputDesc);
135 fprintf (stderr, "FindNextComponent Error\n");
136 throw std::runtime_error ("audio_osx_source::audio_osx_source");
139 err = OpenAComponent (comp, &d_InputAU);
140 CheckErrorAndThrow (err, "OpenAComponent",
141 "audio_osx_source::audio_osx_source");
145 // must enable the AUHAL for input and disable output
146 // before setting the AUHAL's current device
148 // Enable input on the AUHAL
150 err = AudioUnitSetProperty (d_InputAU,
151 kAudioOutputUnitProperty_EnableIO,
152 kAudioUnitScope_Input,
156 CheckErrorAndThrow (err, "AudioUnitSetProperty Input Enable",
157 "audio_osx_source::audio_osx_source");
159 // Disable output on the AUHAL
161 err = AudioUnitSetProperty (d_InputAU,
162 kAudioOutputUnitProperty_EnableIO,
163 kAudioUnitScope_Output,
167 CheckErrorAndThrow (err, "AudioUnitSetProperty Output Disable",
168 "audio_osx_source::audio_osx_source");
170 // set the default input device for our input AU
172 SetDefaultInputDeviceAsCurrent ();
174 #if _OSX_DO_LISTENERS_
175 // set up a listener if default hardware input device changes
177 err = AudioHardwareAddPropertyListener
178 (kAudioHardwarePropertyDefaultInputDevice,
179 (AudioHardwarePropertyListenerProc) HardwareListener,
182 CheckErrorAndThrow (err, "AudioHardwareAddPropertyListener",
183 "audio_osx_source::audio_osx_source");
185 // Add a listener for any changes in the input AU's output stream
186 // the function "UnitListener" will be called if the stream format
187 // changes for whatever reason
189 err = AudioUnitAddPropertyListener
191 kAudioUnitProperty_StreamFormat,
192 (AudioUnitPropertyListenerProc) UnitListener,
194 CheckErrorAndThrow (err, "Adding Unit Property Listener",
195 "audio_osx_source::audio_osx_source");
198 // Now find out if it actually can do input.
201 UInt32 dataSize = sizeof (hasInput);
202 err = AudioUnitGetProperty (d_InputAU,
203 kAudioOutputUnitProperty_HasIO,
204 kAudioUnitScope_Input,
208 CheckErrorAndThrow (err, "AudioUnitGetProperty HasIO",
209 "audio_osx_source::audio_osx_source");
211 fprintf (stderr, "Selected Audio Device does not support Input.\n");
212 throw std::runtime_error ("audio_osx_source::audio_osx_source");
215 // Set up a callback function to retrieve input from the Audio Device
217 AURenderCallbackStruct AUCallBack;
219 AUCallBack.inputProc = (AURenderCallback)(audio_osx_source::AUInputCallback);
220 AUCallBack.inputProcRefCon = this;
222 err = AudioUnitSetProperty (d_InputAU,
223 kAudioOutputUnitProperty_SetInputCallback,
224 kAudioUnitScope_Global,
227 sizeof (AURenderCallbackStruct));
228 CheckErrorAndThrow (err, "AudioUnitSetProperty Input Callback",
229 "audio_osx_source::audio_osx_source");
232 AudioStreamBasicDescription asbd_device, asbd_client, asbd_user;
234 // asbd_device: ASBD of the device that is creating the input data stream
235 // asbd_client: ASBD of the client size (output) of the hardware device
236 // asbd_user: ASBD of the user's arguments
238 // Get the Stream Format (device side)
240 propertySize = sizeof (asbd_device);
241 err = AudioUnitGetProperty (d_InputAU,
242 kAudioUnitProperty_StreamFormat,
243 kAudioUnitScope_Input,
247 CheckErrorAndThrow (err, "AudioUnitGetProperty Device Input Stream Format",
248 "audio_osx_source::audio_osx_source");
251 fprintf (stderr, "\n---- Device Stream Format ----\n" );
252 PrintStreamDesc (&asbd_device);
255 // Get the Stream Format (client side)
256 propertySize = sizeof (asbd_client);
257 err = AudioUnitGetProperty (d_InputAU,
258 kAudioUnitProperty_StreamFormat,
259 kAudioUnitScope_Output,
263 CheckErrorAndThrow (err, "AudioUnitGetProperty Device Ouput Stream Format",
264 "audio_osx_source::audio_osx_source");
267 fprintf (stderr, "\n---- Client Stream Format ----\n");
268 PrintStreamDesc (&asbd_client);
271 // Set the format of all the AUs to the input/output devices channel count
273 // get the max number of input (& thus output) channels supported by
275 d_n_max_channels = asbd_client.mChannelsPerFrame;
277 // create the output io signature;
278 // no input siganture to set (source is hardware)
279 set_output_signature (gr_make_io_signature (1,
283 // allocate the output circular buffer(s), one per channel
284 d_buffers = (circular_buffer<float>**) new
285 circular_buffer<float>* [d_n_max_channels];
286 UInt32 n_alloc = (UInt32) ceil ((double) d_max_sample_count);
287 for (UInt32 n = 0; n < d_n_max_channels; n++) {
288 d_buffers[n] = new circular_buffer<float> (n_alloc, false, false);
291 d_deviceSampleRate = asbd_device.mSampleRate;
292 d_n_deviceChannels = asbd_device.mChannelsPerFrame;
294 // create an ASBD for the user's wants
296 asbd_user.mSampleRate = d_outputSampleRate;
297 asbd_user.mFormatID = kAudioFormatLinearPCM;
298 asbd_user.mFormatFlags = (kLinearPCMFormatFlagIsFloat |
300 kLinearPCMFormatFlagIsPacked |
301 kAudioFormatFlagIsNonInterleaved);
302 asbd_user.mBytesPerPacket = 4;
303 asbd_user.mFramesPerPacket = 1;
304 asbd_user.mBytesPerFrame = 4;
305 asbd_user.mChannelsPerFrame = d_n_max_channels;
306 asbd_user.mBitsPerChannel = 32;
308 if (d_deviceSampleRate == d_outputSampleRate) {
309 // no need to do conversion if asbd_client matches user wants
310 d_passThrough = true;
311 d_leadSizeFrames = d_trailSizeFrames = 0L;
313 d_passThrough = false;
314 // Create the audio converter
316 err = AudioConverterNew (&asbd_client, &asbd_user, &d_AudioConverter);
317 CheckErrorAndThrow (err, "AudioConverterNew",
318 "audio_osx_source::audio_osx_source");
320 // Set the audio converter sample rate quality to "max" ...
321 // requires more samples, but should sound nicer
323 UInt32 ACQuality = kAudioConverterQuality_Max;
324 propertySize = sizeof (ACQuality);
325 err = AudioConverterSetProperty (d_AudioConverter,
326 kAudioConverterSampleRateConverterQuality,
329 CheckErrorAndThrow (err, "AudioConverterSetProperty "
330 "SampleRateConverterQuality",
331 "audio_osx_source::audio_osx_source");
333 // set the audio converter's prime method to "pre",
334 // which uses both leading and trailing frames
335 // from the "current input". All of this is handled
336 // internally by the AudioConverter; we just supply
337 // the frames for conversion.
339 // UInt32 ACPrimeMethod = kConverterPrimeMethod_None;
340 UInt32 ACPrimeMethod = kConverterPrimeMethod_Pre;
341 propertySize = sizeof (ACPrimeMethod);
342 err = AudioConverterSetProperty (d_AudioConverter,
343 kAudioConverterPrimeMethod,
346 CheckErrorAndThrow (err, "AudioConverterSetProperty PrimeMethod",
347 "audio_osx_source::audio_osx_source");
349 // Get the size of the I/O buffer(s) to allow for pre-allocated buffers
351 // lead frame info (trail frame info is ignored)
353 AudioConverterPrimeInfo ACPrimeInfo = {0, 0};
354 propertySize = sizeof (ACPrimeInfo);
355 err = AudioConverterGetProperty (d_AudioConverter,
356 kAudioConverterPrimeInfo,
359 CheckErrorAndThrow (err, "AudioConverterGetProperty PrimeInfo",
360 "audio_osx_source::audio_osx_source");
362 switch (ACPrimeMethod) {
363 case (kConverterPrimeMethod_None):
365 d_trailSizeFrames = 0L;
367 case (kConverterPrimeMethod_Normal):
368 d_leadSizeFrames = 0L;
369 d_trailSizeFrames = ACPrimeInfo.trailingFrames;
372 d_leadSizeFrames = ACPrimeInfo.leadingFrames;
373 d_trailSizeFrames = ACPrimeInfo.trailingFrames;
376 d_leadSizeBytes = d_leadSizeFrames * sizeof (Float32);
377 d_trailSizeBytes = d_trailSizeFrames * sizeof (Float32);
379 propertySize = sizeof (d_deviceBufferSizeFrames);
380 err = AudioUnitGetProperty (d_InputAU,
381 kAudioDevicePropertyBufferFrameSize,
382 kAudioUnitScope_Global,
384 &d_deviceBufferSizeFrames,
386 CheckErrorAndThrow (err, "AudioUnitGetProperty Buffer Frame Size",
387 "audio_osx_source::audio_osx_source");
389 d_deviceBufferSizeBytes = d_deviceBufferSizeFrames * sizeof (Float32);
390 d_inputBufferSizeBytes = d_deviceBufferSizeBytes + d_leadSizeBytes;
391 d_inputBufferSizeFrames = d_deviceBufferSizeFrames + d_leadSizeFrames;
393 // outBufSizeBytes = floor (inBufSizeBytes * rate_out / rate_in)
394 // since this is rarely exact, we need another buffer to hold
395 // "extra" samples not processed at any given sampling period
396 // this buffer must be at least 4 floats in size, but generally
397 // follows the rule that
398 // extraBufSize = ceil (rate_in / rate_out)*sizeof(float)
400 d_extraBufferSizeFrames = ((UInt32) ceil (d_deviceSampleRate
401 / d_outputSampleRate)
403 if (d_extraBufferSizeFrames < 4)
404 d_extraBufferSizeFrames = 4;
405 d_extraBufferSizeBytes = d_extraBufferSizeFrames * sizeof (float);
407 d_outputBufferSizeFrames = (UInt32) ceil (((Float64) d_inputBufferSizeFrames)
409 / d_deviceSampleRate);
410 d_outputBufferSizeBytes = d_outputBufferSizeFrames * sizeof (float);
411 d_inputBufferSizeFrames += d_extraBufferSizeFrames;
413 // pre-alloc all buffers
415 AllocAudioBufferList (&d_InputBuffer, d_n_deviceChannels,
416 d_inputBufferSizeBytes);
417 if (d_passThrough == false) {
418 AllocAudioBufferList (&d_OutputBuffer, d_n_max_channels,
419 d_outputBufferSizeBytes);
421 d_OutputBuffer = d_InputBuffer;
424 // create the stuff to regulate I/O
426 d_cond_data = new mld_condition ();
427 if (d_cond_data == NULL)
428 CheckErrorAndThrow (errno, "new mld_condition (data)",
429 "audio_osx_source::audio_osx_source");
430 d_internal = d_cond_data->mutex ();
432 // initialize the AU for input
434 err = AudioUnitInitialize (d_InputAU);
435 CheckErrorAndThrow (err, "AudioUnitInitialize",
436 "audio_osx_source::audio_osx_source");
439 fprintf (stderr, "audio_osx_source Parameters:\n");
440 fprintf (stderr, " Device Sample Rate is %g\n", d_deviceSampleRate);
441 fprintf (stderr, " User Sample Rate is %g\n", d_outputSampleRate);
442 fprintf (stderr, " Max Sample Count is %ld\n", d_max_sample_count);
443 fprintf (stderr, " # Device Channels is %ld\n", d_n_deviceChannels);
444 fprintf (stderr, " # Max Channels is %ld\n", d_n_max_channels);
445 fprintf (stderr, " Device Buffer Size is Frames = %ld\n",
446 d_deviceBufferSizeFrames);
447 fprintf (stderr, " Lead Size is Frames = %ld\n",
449 fprintf (stderr, " Trail Size is Frames = %ld\n",
451 fprintf (stderr, " Input Buffer Size is Frames = %ld\n",
452 d_inputBufferSizeFrames);
453 fprintf (stderr, " Output Buffer Size is Frames = %ld\n",
454 d_outputBufferSizeFrames);
459 audio_osx_source::AllocAudioBufferList (AudioBufferList** t_ABL,
461 UInt32 bufferSizeBytes)
463 FreeAudioBufferList (t_ABL);
464 UInt32 propertySize = (offsetof (AudioBufferList, mBuffers[0]) +
465 (sizeof (AudioBuffer) * n_channels));
466 *t_ABL = (AudioBufferList*) calloc (1, propertySize);
467 (*t_ABL)->mNumberBuffers = n_channels;
469 int counter = n_channels;
471 while (--counter >= 0) {
472 (*t_ABL)->mBuffers[counter].mNumberChannels = 1;
473 (*t_ABL)->mBuffers[counter].mDataByteSize = bufferSizeBytes;
474 (*t_ABL)->mBuffers[counter].mData = calloc (1, bufferSizeBytes);
479 audio_osx_source::FreeAudioBufferList (AudioBufferList** t_ABL)
481 // free pre-allocated audio buffer, if it exists
482 if (*t_ABL != NULL) {
483 int counter = (*t_ABL)->mNumberBuffers;
484 while (--counter >= 0)
485 free ((*t_ABL)->mBuffers[counter].mData);
491 bool audio_osx_source::IsRunning ()
493 UInt32 AURunning = 0, AUSize = sizeof (UInt32);
495 OSStatus err = AudioUnitGetProperty (d_InputAU,
496 kAudioOutputUnitProperty_IsRunning,
497 kAudioUnitScope_Global,
501 CheckErrorAndThrow (err, "AudioUnitGetProperty IsRunning",
502 "audio_osx_source::IsRunning");
507 bool audio_osx_source::start ()
509 if (! IsRunning ()) {
510 OSStatus err = AudioOutputUnitStart (d_InputAU);
511 CheckErrorAndThrow (err, "AudioOutputUnitStart",
512 "audio_osx_source::start");
518 bool audio_osx_source::stop ()
521 OSStatus err = AudioOutputUnitStop (d_InputAU);
522 CheckErrorAndThrow (err, "AudioOutputUnitStart",
523 "audio_osx_source::stop");
524 for (UInt32 n = 0; n < d_n_user_channels; n++) {
525 d_buffers[n]->abort ();
532 audio_osx_source::~audio_osx_source ()
534 OSStatus err = noErr;
536 // stop the AudioUnit
539 #if _OSX_DO_LISTENERS_
540 // remove the listeners
542 err = AudioUnitRemovePropertyListener
544 kAudioUnitProperty_StreamFormat,
545 (AudioUnitPropertyListenerProc) UnitListener);
546 CheckError (err, "~audio_osx_source: AudioUnitRemovePropertyListener");
548 err = AudioHardwareRemovePropertyListener
549 (kAudioHardwarePropertyDefaultInputDevice,
550 (AudioHardwarePropertyListenerProc) HardwareListener);
551 CheckError (err, "~audio_osx_source: AudioHardwareRemovePropertyListener");
554 // free pre-allocated audio buffers
555 FreeAudioBufferList (&d_InputBuffer);
557 if (d_passThrough == false) {
558 err = AudioConverterDispose (d_AudioConverter);
559 CheckError (err, "~audio_osx_source: AudioConverterDispose");
560 FreeAudioBufferList (&d_OutputBuffer);
563 // remove the audio unit
564 err = AudioUnitUninitialize (d_InputAU);
565 CheckError (err, "~audio_osx_source: AudioUnitUninitialize");
567 err = CloseComponent (d_InputAU);
568 CheckError (err, "~audio_osx_source: CloseComponent");
570 // empty and delete the queues
571 for (UInt32 n = 0; n < d_n_max_channels; n++) {
578 // close and delete the control stuff
582 audio_osx_source_sptr
583 audio_osx_make_source (int sampling_freq,
584 const std::string device_name,
587 int max_sample_count)
589 return audio_osx_source_sptr (new audio_osx_source (sampling_freq,
597 audio_osx_source::check_topology (int ninputs, int noutputs)
599 // check # inputs to make sure it's valid
601 fprintf (stderr, "audio_osx_source::check_topology(): "
602 "number of input streams provided (%d) should be 0.\n",
604 throw std::runtime_error ("audio_osx_source::check_topology()");
607 // check # outputs to make sure it's valid
608 if ((noutputs < 1) | (noutputs > (int) d_n_max_channels)) {
609 fprintf (stderr, "audio_osx_source::check_topology(): "
610 "number of output streams provided (%d) should be in "
611 "[1,%ld] for the selected audio device.\n",
612 noutputs, d_n_max_channels);
613 throw std::runtime_error ("audio_osx_source::check_topology()");
616 // save the actual number of output (user) channels
617 d_n_user_channels = noutputs;
620 fprintf (stderr, "chk_topo: Actual # user output channels = %d\n",
628 audio_osx_source::work
630 gr_vector_const_void_star &input_items,
631 gr_vector_void_star &output_items)
633 // acquire control to do processing here only
637 fprintf (stderr, "work1: SC = %4ld, #OI = %4d, #Chan = %ld\n",
638 d_queueSampleCount, noutput_items, output_items.size());
641 // set the actual # of output items to the 'desired' amount then
642 // verify that data is available; if not enough data is available,
643 // either wait until it is (is "do_block" is true), return (0) is no
644 // data is available and "do_block" is false, or process the actual
645 // amount of available data.
647 UInt32 actual_noutput_items = noutput_items;
649 if (d_queueSampleCount < actual_noutput_items) {
650 if (d_queueSampleCount == 0) {
651 // no data; do_block decides what to do
652 if (d_do_block == true) {
653 while (d_queueSampleCount == 0) {
654 // release control so-as to allow data to be retrieved;
655 // block until there is data to return
656 d_cond_data->wait ();
657 // the condition's signal() was called; acquire control to
661 // no data & not blocking; return nothing
662 // release control so-as to allow data to be retrieved
663 d_internal->unlock ();
667 // use the actual amount of available data
668 actual_noutput_items = d_queueSampleCount;
671 // number of channels
672 int l_counter = (int) output_items.size();
674 // copy the items from the circular buffer(s) to 'work's output buffers
675 // verify that the number copied out is as expected.
677 while (--l_counter >= 0) {
678 UInt32 t_n_output_items = actual_noutput_items;
679 d_buffers[l_counter]->dequeue ((float*) output_items[l_counter],
681 if (t_n_output_items != actual_noutput_items) {
682 fprintf (stderr, "audio_osx_source::work(): "
683 "number of available items changing "
684 "unexpectedly; expecting %ld, got %ld.\n",
685 actual_noutput_items, t_n_output_items);
686 throw std::runtime_error ("audio_osx_source::work()");
690 // subtract the actual number of items removed from the buffer(s)
691 // from the local accounting of the number of available samples
693 d_queueSampleCount -= actual_noutput_items;
696 fprintf (stderr, "work2: SC = %4ld, act#OI = %4ld\n",
697 d_queueSampleCount, actual_noutput_items);
700 // release control to allow for other processing parts to run
702 d_internal->unlock ();
705 fprintf (stderr, "work3: Returning.\n");
708 return (actual_noutput_items);
712 audio_osx_source::ConverterCallback
713 (AudioConverterRef inAudioConverter,
714 UInt32* ioNumberDataPackets,
715 AudioBufferList* ioData,
716 AudioStreamPacketDescription** ioASPD,
719 // take current device buffers and copy them to the tail of the
720 // input buffers the lead buffer is already there in the first
721 // d_leadSizeFrames slots
723 audio_osx_source* This = static_cast<audio_osx_source*>(inUserData);
724 AudioBufferList* l_inputABL = This->d_InputBuffer;
725 UInt32 totalInputBufferSizeBytes = ((*ioNumberDataPackets) * sizeof (float));
726 int counter = This->d_n_deviceChannels;
727 ioData->mNumberBuffers = This->d_n_deviceChannels;
728 This->d_n_ActualInputFrames = (*ioNumberDataPackets);
731 fprintf (stderr, "cc1: io#DP = %ld, TIBSB = %ld, #C = %d\n",
732 *ioNumberDataPackets, totalInputBufferSizeBytes, counter);
735 while (--counter >= 0) {
736 AudioBuffer* l_ioD_AB = &(ioData->mBuffers[counter]);
737 l_ioD_AB->mNumberChannels = 1;
738 l_ioD_AB->mData = (float*)(l_inputABL->mBuffers[counter].mData);
739 l_ioD_AB->mDataByteSize = totalInputBufferSizeBytes;
743 fprintf (stderr, "cc2: Returning.\n");
750 audio_osx_source::AUInputCallback (void* inRefCon,
751 AudioUnitRenderActionFlags* ioActionFlags,
752 const AudioTimeStamp* inTimeStamp,
754 UInt32 inNumberFrames,
755 AudioBufferList* ioData)
757 OSStatus err = noErr;
758 audio_osx_source* This = static_cast<audio_osx_source*>(inRefCon);
760 This->d_internal->lock ();
763 fprintf (stderr, "cb0: in#F = %4ld, inBN = %ld, SC = %4ld\n",
764 inNumberFrames, inBusNumber, This->d_queueSampleCount);
767 // Get the new audio data from the input device
769 err = AudioUnitRender (This->d_InputAU,
774 This->d_InputBuffer);
775 CheckErrorAndThrow (err, "AudioUnitRender",
776 "audio_osx_source::AUInputCallback");
778 UInt32 AvailableInputFrames = inNumberFrames;
779 This->d_n_AvailableInputFrames = inNumberFrames;
781 // get the number of actual output frames,
782 // either via converting the buffer or not
784 UInt32 ActualOutputFrames;
786 if (This->d_passThrough == true) {
787 ActualOutputFrames = AvailableInputFrames;
789 UInt32 AvailableInputBytes = AvailableInputFrames * sizeof (float);
790 UInt32 AvailableOutputBytes = AvailableInputBytes;
791 UInt32 AvailableOutputFrames = AvailableOutputBytes / sizeof (float);
792 UInt32 propertySize = sizeof (AvailableOutputBytes);
793 err = AudioConverterGetProperty (This->d_AudioConverter,
794 kAudioConverterPropertyCalculateOutputBufferSize,
796 &AvailableOutputBytes);
797 CheckErrorAndThrow (err, "AudioConverterGetProperty CalculateOutputBufferSize", "audio_osx_source::audio_osx_source");
799 AvailableOutputFrames = AvailableOutputBytes / sizeof (float);
802 // when decimating too much, the output sounds warbly due to
803 // fluctuating # of output frames
804 // This should not be a surprise, but there's probably some
805 // clever programming that could lessed the effect ...
806 // like finding the "ideal" # of output frames, and keeping
807 // that number constant no matter the # of input frames
808 UInt32 l_InputBytes = AvailableOutputBytes;
809 propertySize = sizeof (AvailableOutputBytes);
810 err = AudioConverterGetProperty (This->d_AudioConverter,
811 kAudioConverterPropertyCalculateInputBufferSize,
814 CheckErrorAndThrow (err, "AudioConverterGetProperty CalculateInputBufferSize", "audio_osx_source::audio_osx_source");
816 if (l_InputBytes < AvailableInputBytes) {
817 // OK to zero pad the input a little
818 AvailableOutputFrames += 1;
819 AvailableOutputBytes = AvailableOutputFrames * sizeof (float);
824 fprintf (stderr, "cb1: avail: #IF = %ld, #OF = %ld\n",
825 AvailableInputFrames, AvailableOutputFrames);
827 ActualOutputFrames = AvailableOutputFrames;
829 // convert the data to the correct rate
830 // on input, ActualOutputFrames is the number of available output frames
832 err = AudioConverterFillComplexBuffer (This->d_AudioConverter,
833 (AudioConverterComplexInputDataProc)(This->ConverterCallback),
836 This->d_OutputBuffer,
838 CheckErrorAndThrow (err, "AudioConverterFillComplexBuffer",
839 "audio_osx_source::AUInputCallback");
841 // on output, ActualOutputFrames is the actual number of output frames
844 fprintf (stderr, "cb2: actual: #IF = %ld, #OF = %ld\n",
845 This->d_n_ActualInputFrames, AvailableOutputFrames);
846 if (This->d_n_ActualInputFrames != AvailableInputFrames)
847 fprintf (stderr, "cb2.1: avail#IF = %ld, actual#IF = %ld\n",
848 AvailableInputFrames, This->d_n_ActualInputFrames);
852 // add the output frames to the buffers' queue, checking for overflow
854 int l_counter = This->d_n_user_channels;
857 while (--l_counter >= 0) {
858 float* inBuffer = (float*) This->d_OutputBuffer->mBuffers[l_counter].mData;
861 fprintf (stderr, "cb3: enqueuing audio data.\n");
864 int l_res = This->d_buffers[l_counter]->enqueue (inBuffer, ActualOutputFrames);
870 // data coming in too fast
871 // drop oldest buffer
872 fputs ("aO", stderr);
874 // set the local number of samples available to the max
875 This->d_queueSampleCount = This->d_buffers[0]->buffer_length_items ();
877 // keep up the local sample count
878 This->d_queueSampleCount += ActualOutputFrames;
882 fprintf (stderr, "cb4: #OI = %4ld, #Cnt = %4ld, mSC = %ld, \n",
883 ActualOutputFrames, This->d_queueSampleCount,
884 This->d_max_sample_count);
887 // signal that data is available, if appropraite
888 This->d_cond_data->signal ();
891 fprintf (stderr, "cb5: releasing internal mutex.\n");
894 // release control to allow for other processing parts to run
895 This->d_internal->unlock ();
898 fprintf (stderr, "cb6: returning.\n");
905 audio_osx_source::SetDefaultInputDeviceAsCurrent
908 // set the default input device
909 AudioDeviceID deviceID;
910 UInt32 dataSize = sizeof (AudioDeviceID);
911 AudioHardwareGetProperty (kAudioHardwarePropertyDefaultInputDevice,
914 OSStatus err = AudioUnitSetProperty (d_InputAU,
915 kAudioOutputUnitProperty_CurrentDevice,
916 kAudioUnitScope_Global,
919 sizeof (AudioDeviceID));
920 CheckErrorAndThrow (err, "AudioUnitSetProperty Current Device",
921 "audio_osx_source::SetDefaultInputDeviceAsCurrent");
924 #if _OSX_DO_LISTENERS_
926 audio_osx_source::HardwareListener
927 (AudioHardwarePropertyID inPropertyID,
930 OSStatus err = noErr;
931 audio_osx_source* This = static_cast<audio_osx_source*>(inClientData);
933 fprintf (stderr, "a_o_s::HardwareListener\n");
935 // set the new default hardware input device for use by our AU
937 This->SetDefaultInputDeviceAsCurrent ();
939 // reset the converter to tell it that the stream has changed
941 err = AudioConverterReset (This->d_AudioConverter);
942 CheckErrorAndThrow (err, "AudioConverterReset",
943 "audio_osx_source::UnitListener");
949 audio_osx_source::UnitListener
952 AudioUnitPropertyID inID,
953 AudioUnitScope inScope,
954 AudioUnitElement inElement)
956 OSStatus err = noErr;
957 audio_osx_source* This = static_cast<audio_osx_source*>(inRefCon);
958 AudioStreamBasicDescription asbd;
960 fprintf (stderr, "a_o_s::UnitListener\n");
962 // get the converter's input ASBD (for printing)
964 UInt32 propertySize = sizeof (asbd);
965 err = AudioConverterGetProperty (This->d_AudioConverter,
966 kAudioConverterCurrentInputStreamDescription,
969 CheckErrorAndThrow (err, "AudioConverterGetProperty "
970 "CurrentInputStreamDescription",
971 "audio_osx_source::UnitListener");
973 fprintf (stderr, "UnitListener: Input Source changed.\n"
974 "Old Source Output Info:\n");
975 PrintStreamDesc (&asbd);
977 // get the new input unit's output ASBD
979 propertySize = sizeof (asbd);
980 err = AudioUnitGetProperty (This->d_InputAU,
981 kAudioUnitProperty_StreamFormat,
982 kAudioUnitScope_Output, 1,
983 &asbd, &propertySize);
984 CheckErrorAndThrow (err, "AudioUnitGetProperty StreamFormat",
985 "audio_osx_source::UnitListener");
987 fprintf (stderr, "New Source Output Info:\n");
988 PrintStreamDesc (&asbd);
990 // set the converter's input ASBD to this
992 err = AudioConverterSetProperty (This->d_AudioConverter,
993 kAudioConverterCurrentInputStreamDescription,
996 CheckErrorAndThrow (err, "AudioConverterSetProperty "
997 "CurrentInputStreamDescription",
998 "audio_osx_source::UnitListener");
1000 // reset the converter to tell it that the stream has changed
1002 err = AudioConverterReset (This->d_AudioConverter);
1003 CheckErrorAndThrow (err, "AudioConverterReset",
1004 "audio_osx_source::UnitListener");