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_internal = new mld_mutex ();
427 if (d_internal == NULL)
428 CheckErrorAndThrow (errno, "new mld_mutex (internal)",
429 "audio_osx_source::audio_osx_source");
431 d_cond_data = new mld_condition ();
432 if (d_cond_data == NULL)
433 CheckErrorAndThrow (errno, "new mld_condition (data)",
434 "audio_osx_source::audio_osx_source");
436 // initialize the AU for input
438 err = AudioUnitInitialize (d_InputAU);
439 CheckErrorAndThrow (err, "AudioUnitInitialize",
440 "audio_osx_source::audio_osx_source");
443 fprintf (stderr, "audio_osx_source Parameters:\n");
444 fprintf (stderr, " Device Sample Rate is %g\n", d_deviceSampleRate);
445 fprintf (stderr, " User Sample Rate is %g\n", d_outputSampleRate);
446 fprintf (stderr, " Max Sample Count is %ld\n", d_max_sample_count);
447 fprintf (stderr, " # Device Channels is %ld\n", d_n_deviceChannels);
448 fprintf (stderr, " # Max Channels is %ld\n", d_n_max_channels);
449 fprintf (stderr, " Device Buffer Size is Frames = %ld\n",
450 d_deviceBufferSizeFrames);
451 fprintf (stderr, " Lead Size is Frames = %ld\n",
453 fprintf (stderr, " Trail Size is Frames = %ld\n",
455 fprintf (stderr, " Input Buffer Size is Frames = %ld\n",
456 d_inputBufferSizeFrames);
457 fprintf (stderr, " Output Buffer Size is Frames = %ld\n",
458 d_outputBufferSizeFrames);
463 audio_osx_source::AllocAudioBufferList (AudioBufferList** t_ABL,
465 UInt32 bufferSizeBytes)
467 FreeAudioBufferList (t_ABL);
468 UInt32 propertySize = (offsetof (AudioBufferList, mBuffers[0]) +
469 (sizeof (AudioBuffer) * n_channels));
470 *t_ABL = (AudioBufferList*) calloc (1, propertySize);
471 (*t_ABL)->mNumberBuffers = n_channels;
473 int counter = n_channels;
475 while (--counter >= 0) {
476 (*t_ABL)->mBuffers[counter].mNumberChannels = 1;
477 (*t_ABL)->mBuffers[counter].mDataByteSize = bufferSizeBytes;
478 (*t_ABL)->mBuffers[counter].mData = calloc (1, bufferSizeBytes);
483 audio_osx_source::FreeAudioBufferList (AudioBufferList** t_ABL)
485 // free pre-allocated audio buffer, if it exists
486 if (*t_ABL != NULL) {
487 int counter = (*t_ABL)->mNumberBuffers;
488 while (--counter >= 0)
489 free ((*t_ABL)->mBuffers[counter].mData);
495 bool audio_osx_source::IsRunning ()
497 UInt32 AURunning = 0, AUSize = sizeof (UInt32);
499 OSStatus err = AudioUnitGetProperty (d_InputAU,
500 kAudioOutputUnitProperty_IsRunning,
501 kAudioUnitScope_Global,
505 CheckErrorAndThrow (err, "AudioUnitGetProperty IsRunning",
506 "audio_osx_source::IsRunning");
511 bool audio_osx_source::start ()
513 if (! IsRunning ()) {
514 OSStatus err = AudioOutputUnitStart (d_InputAU);
515 CheckErrorAndThrow (err, "AudioOutputUnitStart",
516 "audio_osx_source::start");
522 bool audio_osx_source::stop ()
525 OSStatus err = AudioOutputUnitStop (d_InputAU);
526 CheckErrorAndThrow (err, "AudioOutputUnitStart",
527 "audio_osx_source::stop");
528 for (UInt32 n = 0; n < d_n_user_channels; n++) {
529 d_buffers[n]->abort ();
536 audio_osx_source::~audio_osx_source ()
538 OSStatus err = noErr;
540 // stop the AudioUnit
543 #if _OSX_DO_LISTENERS_
544 // remove the listeners
546 err = AudioUnitRemovePropertyListener
548 kAudioUnitProperty_StreamFormat,
549 (AudioUnitPropertyListenerProc) UnitListener);
550 CheckError (err, "~audio_osx_source: AudioUnitRemovePropertyListener");
552 err = AudioHardwareRemovePropertyListener
553 (kAudioHardwarePropertyDefaultInputDevice,
554 (AudioHardwarePropertyListenerProc) HardwareListener);
555 CheckError (err, "~audio_osx_source: AudioHardwareRemovePropertyListener");
558 // free pre-allocated audio buffers
559 FreeAudioBufferList (&d_InputBuffer);
561 if (d_passThrough == false) {
562 err = AudioConverterDispose (d_AudioConverter);
563 CheckError (err, "~audio_osx_source: AudioConverterDispose");
564 FreeAudioBufferList (&d_OutputBuffer);
567 // remove the audio unit
568 err = AudioUnitUninitialize (d_InputAU);
569 CheckError (err, "~audio_osx_source: AudioUnitUninitialize");
571 err = CloseComponent (d_InputAU);
572 CheckError (err, "~audio_osx_source: CloseComponent");
574 // empty and delete the queues
575 for (UInt32 n = 0; n < d_n_max_channels; n++) {
582 // close and delete the control stuff
587 audio_osx_source_sptr
588 audio_osx_make_source (int sampling_freq,
589 const std::string device_name,
592 int max_sample_count)
594 return audio_osx_source_sptr (new audio_osx_source (sampling_freq,
602 audio_osx_source::check_topology (int ninputs, int noutputs)
604 // check # inputs to make sure it's valid
606 fprintf (stderr, "audio_osx_source::check_topology(): "
607 "number of input streams provided (%d) should be 0.\n",
609 throw std::runtime_error ("audio_osx_source::check_topology()");
612 // check # outputs to make sure it's valid
613 if ((noutputs < 1) | (noutputs > (int) d_n_max_channels)) {
614 fprintf (stderr, "audio_osx_source::check_topology(): "
615 "number of output streams provided (%d) should be in "
616 "[1,%ld] for the selected audio device.\n",
617 noutputs, d_n_max_channels);
618 throw std::runtime_error ("audio_osx_source::check_topology()");
621 // save the actual number of output (user) channels
622 d_n_user_channels = noutputs;
625 fprintf (stderr, "chk_topo: Actual # user output channels = %d\n",
633 audio_osx_source::work
635 gr_vector_const_void_star &input_items,
636 gr_vector_void_star &output_items)
638 // acquire control to do processing here only
642 fprintf (stderr, "work1: SC = %4ld, #OI = %4d, #Chan = %ld\n",
643 d_queueSampleCount, noutput_items, output_items.size());
646 // set the actual # of output items to the 'desired' amount then
647 // verify that data is available; if not enough data is available,
648 // either wait until it is (is "do_block" is true), return (0) is no
649 // data is available and "do_block" is false, or process the actual
650 // amount of available data.
652 UInt32 actual_noutput_items = noutput_items;
654 if (d_queueSampleCount < actual_noutput_items) {
655 if (d_queueSampleCount == 0) {
656 // no data; do_block decides what to do
657 if (d_do_block == true) {
658 while (d_queueSampleCount == 0) {
659 // release control so-as to allow data to be retrieved
661 // block until there is data to return
662 d_cond_data->wait ();
663 // the condition's signal() was called; acquire control to
668 // no data & not blocking; return nothing
669 // release control so-as to allow data to be retrieved
674 // use the actual amount of available data
675 actual_noutput_items = d_queueSampleCount;
678 // number of channels
679 int l_counter = (int) output_items.size();
681 // copy the items from the circular buffer(s) to 'work's output buffers
682 // verify that the number copied out is as expected.
684 while (--l_counter >= 0) {
685 UInt32 t_n_output_items = actual_noutput_items;
686 d_buffers[l_counter]->dequeue ((float*) output_items[l_counter],
688 if (t_n_output_items != actual_noutput_items) {
689 fprintf (stderr, "audio_osx_source::work(): "
690 "number of available items changing "
691 "unexpectedly; expecting %ld, got %ld.\n",
692 actual_noutput_items, t_n_output_items);
693 throw std::runtime_error ("audio_osx_source::work()");
697 // subtract the actual number of items removed from the buffer(s)
698 // from the local accounting of the number of available samples
700 d_queueSampleCount -= actual_noutput_items;
703 fprintf (stderr, "work2: SC = %4ld, act#OI = %4ld\n",
704 d_queueSampleCount, actual_noutput_items);
707 // release control to allow for other processing parts to run
712 fprintf (stderr, "work3: Returning.\n");
715 return (actual_noutput_items);
719 audio_osx_source::ConverterCallback
720 (AudioConverterRef inAudioConverter,
721 UInt32* ioNumberDataPackets,
722 AudioBufferList* ioData,
723 AudioStreamPacketDescription** ioASPD,
726 // take current device buffers and copy them to the tail of the
727 // input buffers the lead buffer is already there in the first
728 // d_leadSizeFrames slots
730 audio_osx_source* This = static_cast<audio_osx_source*>(inUserData);
731 AudioBufferList* l_inputABL = This->d_InputBuffer;
732 UInt32 totalInputBufferSizeBytes = ((*ioNumberDataPackets) * sizeof (float));
733 int counter = This->d_n_deviceChannels;
734 ioData->mNumberBuffers = This->d_n_deviceChannels;
735 This->d_n_ActualInputFrames = (*ioNumberDataPackets);
738 fprintf (stderr, "cc1: io#DP = %ld, TIBSB = %ld, #C = %d\n",
739 *ioNumberDataPackets, totalInputBufferSizeBytes, counter);
742 while (--counter >= 0) {
743 AudioBuffer* l_ioD_AB = &(ioData->mBuffers[counter]);
744 l_ioD_AB->mNumberChannels = 1;
745 l_ioD_AB->mData = (float*)(l_inputABL->mBuffers[counter].mData);
746 l_ioD_AB->mDataByteSize = totalInputBufferSizeBytes;
750 fprintf (stderr, "cc2: Returning.\n");
757 audio_osx_source::AUInputCallback (void* inRefCon,
758 AudioUnitRenderActionFlags* ioActionFlags,
759 const AudioTimeStamp* inTimeStamp,
761 UInt32 inNumberFrames,
762 AudioBufferList* ioData)
764 OSStatus err = noErr;
765 audio_osx_source* This = static_cast<audio_osx_source*>(inRefCon);
767 This->d_internal->wait ();
770 fprintf (stderr, "cb0: in#F = %4ld, inBN = %ld, SC = %4ld\n",
771 inNumberFrames, inBusNumber, This->d_queueSampleCount);
774 // Get the new audio data from the input device
776 err = AudioUnitRender (This->d_InputAU,
781 This->d_InputBuffer);
782 CheckErrorAndThrow (err, "AudioUnitRender",
783 "audio_osx_source::AUInputCallback");
785 UInt32 AvailableInputFrames = inNumberFrames;
786 This->d_n_AvailableInputFrames = inNumberFrames;
788 // get the number of actual output frames,
789 // either via converting the buffer or not
791 UInt32 ActualOutputFrames;
793 if (This->d_passThrough == true) {
794 ActualOutputFrames = AvailableInputFrames;
796 UInt32 AvailableInputBytes = AvailableInputFrames * sizeof (float);
797 UInt32 AvailableOutputBytes = AvailableInputBytes;
798 UInt32 AvailableOutputFrames = AvailableOutputBytes / sizeof (float);
799 UInt32 propertySize = sizeof (AvailableOutputBytes);
800 err = AudioConverterGetProperty (This->d_AudioConverter,
801 kAudioConverterPropertyCalculateOutputBufferSize,
803 &AvailableOutputBytes);
804 CheckErrorAndThrow (err, "AudioConverterGetProperty CalculateOutputBufferSize", "audio_osx_source::audio_osx_source");
806 AvailableOutputFrames = AvailableOutputBytes / sizeof (float);
809 // when decimating too much, the output sounds warbly due to
810 // fluctuating # of output frames
811 // This should not be a surprise, but there's probably some
812 // clever programming that could lessed the effect ...
813 // like finding the "ideal" # of output frames, and keeping
814 // that number constant no matter the # of input frames
815 UInt32 l_InputBytes = AvailableOutputBytes;
816 propertySize = sizeof (AvailableOutputBytes);
817 err = AudioConverterGetProperty (This->d_AudioConverter,
818 kAudioConverterPropertyCalculateInputBufferSize,
821 CheckErrorAndThrow (err, "AudioConverterGetProperty CalculateInputBufferSize", "audio_osx_source::audio_osx_source");
823 if (l_InputBytes < AvailableInputBytes) {
824 // OK to zero pad the input a little
825 AvailableOutputFrames += 1;
826 AvailableOutputBytes = AvailableOutputFrames * sizeof (float);
831 fprintf (stderr, "cb1: avail: #IF = %ld, #OF = %ld\n",
832 AvailableInputFrames, AvailableOutputFrames);
834 ActualOutputFrames = AvailableOutputFrames;
836 // convert the data to the correct rate
837 // on input, ActualOutputFrames is the number of available output frames
839 err = AudioConverterFillComplexBuffer (This->d_AudioConverter,
840 (AudioConverterComplexInputDataProc)(This->ConverterCallback),
843 This->d_OutputBuffer,
845 CheckErrorAndThrow (err, "AudioConverterFillComplexBuffer",
846 "audio_osx_source::AUInputCallback");
848 // on output, ActualOutputFrames is the actual number of output frames
851 fprintf (stderr, "cb2: actual: #IF = %ld, #OF = %ld\n",
852 This->d_n_ActualInputFrames, AvailableOutputFrames);
853 if (This->d_n_ActualInputFrames != AvailableInputFrames)
854 fprintf (stderr, "cb2.1: avail#IF = %ld, actual#IF = %ld\n",
855 AvailableInputFrames, This->d_n_ActualInputFrames);
859 // add the output frames to the buffers' queue, checking for overflow
861 int l_counter = This->d_n_user_channels;
864 while (--l_counter >= 0) {
865 float* inBuffer = (float*) This->d_OutputBuffer->mBuffers[l_counter].mData;
868 fprintf (stderr, "cb3: enqueuing audio data.\n");
871 int l_res = This->d_buffers[l_counter]->enqueue (inBuffer, ActualOutputFrames);
877 // data coming in too fast
878 // drop oldest buffer
879 fputs ("aO", stderr);
881 // set the local number of samples available to the max
882 This->d_queueSampleCount = This->d_buffers[0]->buffer_length_items ();
884 // keep up the local sample count
885 This->d_queueSampleCount += ActualOutputFrames;
889 fprintf (stderr, "cb4: #OI = %4ld, #Cnt = %4ld, mSC = %ld, \n",
890 ActualOutputFrames, This->d_queueSampleCount,
891 This->d_max_sample_count);
894 // signal that data is available, if appropraite
895 This->d_cond_data->signal ();
898 fprintf (stderr, "cb5: releasing internal mutex.\n");
901 // release control to allow for other processing parts to run
902 This->d_internal->post ();
905 fprintf (stderr, "cb6: returning.\n");
912 audio_osx_source::SetDefaultInputDeviceAsCurrent
915 // set the default input device
916 AudioDeviceID deviceID;
917 UInt32 dataSize = sizeof (AudioDeviceID);
918 AudioHardwareGetProperty (kAudioHardwarePropertyDefaultInputDevice,
921 OSStatus err = AudioUnitSetProperty (d_InputAU,
922 kAudioOutputUnitProperty_CurrentDevice,
923 kAudioUnitScope_Global,
926 sizeof (AudioDeviceID));
927 CheckErrorAndThrow (err, "AudioUnitSetProperty Current Device",
928 "audio_osx_source::SetDefaultInputDeviceAsCurrent");
931 #if _OSX_DO_LISTENERS_
933 audio_osx_source::HardwareListener
934 (AudioHardwarePropertyID inPropertyID,
937 OSStatus err = noErr;
938 audio_osx_source* This = static_cast<audio_osx_source*>(inClientData);
940 fprintf (stderr, "a_o_s::HardwareListener\n");
942 // set the new default hardware input device for use by our AU
944 This->SetDefaultInputDeviceAsCurrent ();
946 // reset the converter to tell it that the stream has changed
948 err = AudioConverterReset (This->d_AudioConverter);
949 CheckErrorAndThrow (err, "AudioConverterReset",
950 "audio_osx_source::UnitListener");
956 audio_osx_source::UnitListener
959 AudioUnitPropertyID inID,
960 AudioUnitScope inScope,
961 AudioUnitElement inElement)
963 OSStatus err = noErr;
964 audio_osx_source* This = static_cast<audio_osx_source*>(inRefCon);
965 AudioStreamBasicDescription asbd;
967 fprintf (stderr, "a_o_s::UnitListener\n");
969 // get the converter's input ASBD (for printing)
971 UInt32 propertySize = sizeof (asbd);
972 err = AudioConverterGetProperty (This->d_AudioConverter,
973 kAudioConverterCurrentInputStreamDescription,
976 CheckErrorAndThrow (err, "AudioConverterGetProperty "
977 "CurrentInputStreamDescription",
978 "audio_osx_source::UnitListener");
980 fprintf (stderr, "UnitListener: Input Source changed.\n"
981 "Old Source Output Info:\n");
982 PrintStreamDesc (&asbd);
984 // get the new input unit's output ASBD
986 propertySize = sizeof (asbd);
987 err = AudioUnitGetProperty (This->d_InputAU,
988 kAudioUnitProperty_StreamFormat,
989 kAudioUnitScope_Output, 1,
990 &asbd, &propertySize);
991 CheckErrorAndThrow (err, "AudioUnitGetProperty StreamFormat",
992 "audio_osx_source::UnitListener");
994 fprintf (stderr, "New Source Output Info:\n");
995 PrintStreamDesc (&asbd);
997 // set the converter's input ASBD to this
999 err = AudioConverterSetProperty (This->d_AudioConverter,
1000 kAudioConverterCurrentInputStreamDescription,
1003 CheckErrorAndThrow (err, "AudioConverterSetProperty "
1004 "CurrentInputStreamDescription",
1005 "audio_osx_source::UnitListener");
1007 // reset the converter to tell it that the stream has changed
1009 err = AudioConverterReset (This->d_AudioConverter);
1010 CheckErrorAndThrow (err, "AudioConverterReset",
1011 "audio_osx_source::UnitListener");