3 * Copyright 2006,2010 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 #include <audio_osx_source.h>
28 #include <gr_io_signature.h>
30 #include <audio_osx.h>
32 #define _OSX_AU_DEBUG_ 0
33 #define _OSX_DO_LISTENERS_ 0
35 void PrintStreamDesc (AudioStreamBasicDescription *inDesc)
38 std::cerr << "PrintStreamDesc: Can't print a NULL desc!" << std::endl;
42 std::cerr << " Sample Rate : " << inDesc->mSampleRate << std::endl;
44 strncpy (format_id, (char*)(&inDesc->mFormatID), 4);
45 std::cerr << " Format ID : " << format_id << std::endl;
46 std::cerr << " Format Flags : " << inDesc->mFormatFlags << std::endl;
47 std::cerr << " Bytes per Packet : " << inDesc->mBytesPerPacket << std::endl;
48 std::cerr << " Frames per Packet : " << inDesc->mFramesPerPacket << std::endl;
49 std::cerr << " Bytes per Frame : " << inDesc->mBytesPerFrame << std::endl;
50 std::cerr << " Channels per Frame : " << inDesc->mChannelsPerFrame << std::endl;
51 std::cerr << " Bits per Channel : " << inDesc->mBitsPerChannel << std::endl;
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 std::cerr << "Invalid Sample Rate: " << sample_rate << std::endl;
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 std::cerr << "Invalid Channel Config: " << channel_config << std::endl;
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 std::cerr << "Error Converting Device Name: " << errno << std::endl;
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 std::cerr << "Invalid Max Sample Count: " << max_sample_count << std::endl;
111 throw std::invalid_argument ("audio_osx_source::audio_osx_source");
114 d_max_sample_count = max_sample_count;
117 std::cerr << "source(): max # samples = " << d_max_sample_count << std::endl;
120 OSStatus err = noErr;
122 // create the default AudioUnit for input
124 // Open the default input unit
125 #ifndef GR_USE_OLD_AUDIO_UNIT
126 AudioComponentDescription InputDesc;
128 ComponentDescription InputDesc;
132 InputDesc.componentType = kAudioUnitType_Output;
133 InputDesc.componentSubType = kAudioUnitSubType_HALOutput;
134 InputDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
135 InputDesc.componentFlags = 0;
136 InputDesc.componentFlagsMask = 0;
138 #ifndef GR_USE_OLD_AUDIO_UNIT
139 AudioComponent comp = AudioComponentFindNext (NULL, &InputDesc);
141 Component comp = FindNextComponent (NULL, &InputDesc);
145 #ifndef GR_USE_OLD_AUDIO_UNIT
146 std::cerr << "AudioComponentFindNext Error" << std::endl;
148 std::cerr << "FindNextComponent Error" << std::endl;
150 throw std::runtime_error ("audio_osx_source::audio_osx_source");
153 #ifndef GR_USE_OLD_AUDIO_UNIT
154 err = AudioComponentInstanceNew (comp, &d_InputAU);
155 CheckErrorAndThrow (err, "AudioComponentInstanceNew",
156 "audio_osx_source::audio_osx_source");
158 err = OpenAComponent (comp, &d_InputAU);
159 CheckErrorAndThrow (err, "OpenAComponent",
160 "audio_osx_source::audio_osx_source");
166 // must enable the AUHAL for input and disable output
167 // before setting the AUHAL's current device
169 // Enable input on the AUHAL
171 err = AudioUnitSetProperty (d_InputAU,
172 kAudioOutputUnitProperty_EnableIO,
173 kAudioUnitScope_Input,
177 CheckErrorAndThrow (err, "AudioUnitSetProperty Input Enable",
178 "audio_osx_source::audio_osx_source");
180 // Disable output on the AUHAL
182 err = AudioUnitSetProperty (d_InputAU,
183 kAudioOutputUnitProperty_EnableIO,
184 kAudioUnitScope_Output,
188 CheckErrorAndThrow (err, "AudioUnitSetProperty Output Disable",
189 "audio_osx_source::audio_osx_source");
191 // set the default input device for our input AU
193 SetDefaultInputDeviceAsCurrent ();
195 #if _OSX_DO_LISTENERS_
196 // set up a listener if default hardware input device changes
198 err = AudioHardwareAddPropertyListener
199 (kAudioHardwarePropertyDefaultInputDevice,
200 (AudioHardwarePropertyListenerProc) HardwareListener,
203 CheckErrorAndThrow (err, "AudioHardwareAddPropertyListener",
204 "audio_osx_source::audio_osx_source");
206 // Add a listener for any changes in the input AU's output stream
207 // the function "UnitListener" will be called if the stream format
208 // changes for whatever reason
210 err = AudioUnitAddPropertyListener
212 kAudioUnitProperty_StreamFormat,
213 (AudioUnitPropertyListenerProc) UnitListener,
215 CheckErrorAndThrow (err, "Adding Unit Property Listener",
216 "audio_osx_source::audio_osx_source");
219 // Now find out if it actually can do input.
222 UInt32 dataSize = sizeof (hasInput);
223 err = AudioUnitGetProperty (d_InputAU,
224 kAudioOutputUnitProperty_HasIO,
225 kAudioUnitScope_Input,
229 CheckErrorAndThrow (err, "AudioUnitGetProperty HasIO",
230 "audio_osx_source::audio_osx_source");
232 std::cerr << "Selected Audio Device does not support Input." << std::endl;
233 throw std::runtime_error ("audio_osx_source::audio_osx_source");
236 // Set up a callback function to retrieve input from the Audio Device
238 AURenderCallbackStruct AUCallBack;
240 AUCallBack.inputProc = (AURenderCallback)(audio_osx_source::AUInputCallback);
241 AUCallBack.inputProcRefCon = this;
243 err = AudioUnitSetProperty (d_InputAU,
244 kAudioOutputUnitProperty_SetInputCallback,
245 kAudioUnitScope_Global,
248 sizeof (AURenderCallbackStruct));
249 CheckErrorAndThrow (err, "AudioUnitSetProperty Input Callback",
250 "audio_osx_source::audio_osx_source");
253 AudioStreamBasicDescription asbd_device, asbd_client, asbd_user;
255 // asbd_device: ASBD of the device that is creating the input data stream
256 // asbd_client: ASBD of the client size (output) of the hardware device
257 // asbd_user: ASBD of the user's arguments
259 // Get the Stream Format (device side)
261 propertySize = sizeof (asbd_device);
262 err = AudioUnitGetProperty (d_InputAU,
263 kAudioUnitProperty_StreamFormat,
264 kAudioUnitScope_Input,
268 CheckErrorAndThrow (err, "AudioUnitGetProperty Device Input Stream Format",
269 "audio_osx_source::audio_osx_source");
272 std::cerr << std::endl << "---- Device Stream Format ----" << std::endl;
273 PrintStreamDesc (&asbd_device);
276 // Get the Stream Format (client side)
277 propertySize = sizeof (asbd_client);
278 err = AudioUnitGetProperty (d_InputAU,
279 kAudioUnitProperty_StreamFormat,
280 kAudioUnitScope_Output,
284 CheckErrorAndThrow (err, "AudioUnitGetProperty Device Ouput Stream Format",
285 "audio_osx_source::audio_osx_source");
288 std::cerr << std::endl << "---- Client Stream Format ----" << std::endl;
289 PrintStreamDesc (&asbd_client);
292 // Set the format of all the AUs to the input/output devices channel count
294 // get the max number of input (& thus output) channels supported by
296 d_n_max_channels = asbd_client.mChannelsPerFrame;
298 // create the output io signature;
299 // no input siganture to set (source is hardware)
300 set_output_signature (gr_make_io_signature (1,
304 // allocate the output circular buffer(s), one per channel
305 d_buffers = (circular_buffer<float>**) new
306 circular_buffer<float>* [d_n_max_channels];
307 UInt32 n_alloc = (UInt32) ceil ((double) d_max_sample_count);
308 for (UInt32 n = 0; n < d_n_max_channels; n++) {
309 d_buffers[n] = new circular_buffer<float> (n_alloc, false, false);
312 d_deviceSampleRate = asbd_device.mSampleRate;
313 d_n_deviceChannels = asbd_device.mChannelsPerFrame;
315 // create an ASBD for the user's wants
317 asbd_user.mSampleRate = d_outputSampleRate;
318 asbd_user.mFormatID = kAudioFormatLinearPCM;
319 asbd_user.mFormatFlags = (kLinearPCMFormatFlagIsFloat |
321 kLinearPCMFormatFlagIsPacked |
322 kAudioFormatFlagIsNonInterleaved);
323 asbd_user.mBytesPerPacket = 4;
324 asbd_user.mFramesPerPacket = 1;
325 asbd_user.mBytesPerFrame = 4;
326 asbd_user.mChannelsPerFrame = d_n_max_channels;
327 asbd_user.mBitsPerChannel = 32;
329 if (d_deviceSampleRate == d_outputSampleRate) {
330 // no need to do conversion if asbd_client matches user wants
331 d_passThrough = true;
332 d_leadSizeFrames = d_trailSizeFrames = 0L;
334 d_passThrough = false;
335 // Create the audio converter
337 err = AudioConverterNew (&asbd_client, &asbd_user, &d_AudioConverter);
338 CheckErrorAndThrow (err, "AudioConverterNew",
339 "audio_osx_source::audio_osx_source");
341 // Set the audio converter sample rate quality to "max" ...
342 // requires more samples, but should sound nicer
344 UInt32 ACQuality = kAudioConverterQuality_Max;
345 propertySize = sizeof (ACQuality);
346 err = AudioConverterSetProperty (d_AudioConverter,
347 kAudioConverterSampleRateConverterQuality,
350 CheckErrorAndThrow (err, "AudioConverterSetProperty "
351 "SampleRateConverterQuality",
352 "audio_osx_source::audio_osx_source");
354 // set the audio converter's prime method to "pre",
355 // which uses both leading and trailing frames
356 // from the "current input". All of this is handled
357 // internally by the AudioConverter; we just supply
358 // the frames for conversion.
360 // UInt32 ACPrimeMethod = kConverterPrimeMethod_None;
361 UInt32 ACPrimeMethod = kConverterPrimeMethod_Pre;
362 propertySize = sizeof (ACPrimeMethod);
363 err = AudioConverterSetProperty (d_AudioConverter,
364 kAudioConverterPrimeMethod,
367 CheckErrorAndThrow (err, "AudioConverterSetProperty PrimeMethod",
368 "audio_osx_source::audio_osx_source");
370 // Get the size of the I/O buffer(s) to allow for pre-allocated buffers
372 // lead frame info (trail frame info is ignored)
374 AudioConverterPrimeInfo ACPrimeInfo = {0, 0};
375 propertySize = sizeof (ACPrimeInfo);
376 err = AudioConverterGetProperty (d_AudioConverter,
377 kAudioConverterPrimeInfo,
380 CheckErrorAndThrow (err, "AudioConverterGetProperty PrimeInfo",
381 "audio_osx_source::audio_osx_source");
383 switch (ACPrimeMethod) {
384 case (kConverterPrimeMethod_None):
386 d_trailSizeFrames = 0L;
388 case (kConverterPrimeMethod_Normal):
389 d_leadSizeFrames = 0L;
390 d_trailSizeFrames = ACPrimeInfo.trailingFrames;
393 d_leadSizeFrames = ACPrimeInfo.leadingFrames;
394 d_trailSizeFrames = ACPrimeInfo.trailingFrames;
397 d_leadSizeBytes = d_leadSizeFrames * sizeof (Float32);
398 d_trailSizeBytes = d_trailSizeFrames * sizeof (Float32);
400 propertySize = sizeof (d_deviceBufferSizeFrames);
401 err = AudioUnitGetProperty (d_InputAU,
402 kAudioDevicePropertyBufferFrameSize,
403 kAudioUnitScope_Global,
405 &d_deviceBufferSizeFrames,
407 CheckErrorAndThrow (err, "AudioUnitGetProperty Buffer Frame Size",
408 "audio_osx_source::audio_osx_source");
410 d_deviceBufferSizeBytes = d_deviceBufferSizeFrames * sizeof (Float32);
411 d_inputBufferSizeBytes = d_deviceBufferSizeBytes + d_leadSizeBytes;
412 d_inputBufferSizeFrames = d_deviceBufferSizeFrames + d_leadSizeFrames;
414 // outBufSizeBytes = floor (inBufSizeBytes * rate_out / rate_in)
415 // since this is rarely exact, we need another buffer to hold
416 // "extra" samples not processed at any given sampling period
417 // this buffer must be at least 4 floats in size, but generally
418 // follows the rule that
419 // extraBufSize = ceil (rate_in / rate_out)*sizeof(float)
421 d_extraBufferSizeFrames = ((UInt32) ceil (d_deviceSampleRate
422 / d_outputSampleRate)
424 if (d_extraBufferSizeFrames < 4)
425 d_extraBufferSizeFrames = 4;
426 d_extraBufferSizeBytes = d_extraBufferSizeFrames * sizeof (float);
428 d_outputBufferSizeFrames = (UInt32) ceil (((Float64) d_inputBufferSizeFrames)
430 / d_deviceSampleRate);
431 d_outputBufferSizeBytes = d_outputBufferSizeFrames * sizeof (float);
432 d_inputBufferSizeFrames += d_extraBufferSizeFrames;
434 // pre-alloc all buffers
436 AllocAudioBufferList (&d_InputBuffer, d_n_deviceChannels,
437 d_inputBufferSizeBytes);
438 if (d_passThrough == false) {
439 AllocAudioBufferList (&d_OutputBuffer, d_n_max_channels,
440 d_outputBufferSizeBytes);
442 d_OutputBuffer = d_InputBuffer;
445 // create the stuff to regulate I/O
447 d_cond_data = new gruel::condition_variable ();
448 if (d_cond_data == NULL)
449 CheckErrorAndThrow (errno, "new condition (data)",
450 "audio_osx_source::audio_osx_source");
452 d_internal = new gruel::mutex ();
453 if (d_internal == NULL)
454 CheckErrorAndThrow (errno, "new mutex (internal)",
455 "audio_osx_source::audio_osx_source");
457 // initialize the AU for input
459 err = AudioUnitInitialize (d_InputAU);
460 CheckErrorAndThrow (err, "AudioUnitInitialize",
461 "audio_osx_source::audio_osx_source");
464 std::cerr << "audio_osx_source Parameters:" << std::endl;
465 std::cerr << " Device Sample Rate is " << d_deviceSampleRate << std::endl;
466 std::cerr << " User Sample Rate is " << d_outputSampleRate << std::endl;
467 std::cerr << " Max Sample Count is " << d_max_sample_count << std::endl;
468 std::cerr << " # Device Channels is " << d_n_deviceChannels << std::endl;
469 std::cerr << " # Max Channels is " << d_n_max_channels << std::endl;
470 std::cerr << " Device Buffer Size is Frames = " << d_deviceBufferSizeFrames << std::endl;
471 std::cerr << " Lead Size is Frames = " << d_leadSizeFrames << std::endl;
472 std::cerr << " Trail Size is Frames = " << d_trailSizeFrames << std::endl;
473 std::cerr << " Input Buffer Size is Frames = " << d_inputBufferSizeFrames << std::endl;
474 std::cerr << " Output Buffer Size is Frames = " << d_outputBufferSizeFrames << std::endl;
479 audio_osx_source::AllocAudioBufferList (AudioBufferList** t_ABL,
481 UInt32 bufferSizeBytes)
483 FreeAudioBufferList (t_ABL);
484 UInt32 propertySize = (offsetof (AudioBufferList, mBuffers[0]) +
485 (sizeof (AudioBuffer) * n_channels));
486 *t_ABL = (AudioBufferList*) calloc (1, propertySize);
487 (*t_ABL)->mNumberBuffers = n_channels;
489 int counter = n_channels;
491 while (--counter >= 0) {
492 (*t_ABL)->mBuffers[counter].mNumberChannels = 1;
493 (*t_ABL)->mBuffers[counter].mDataByteSize = bufferSizeBytes;
494 (*t_ABL)->mBuffers[counter].mData = calloc (1, bufferSizeBytes);
499 audio_osx_source::FreeAudioBufferList (AudioBufferList** t_ABL)
501 // free pre-allocated audio buffer, if it exists
502 if (*t_ABL != NULL) {
503 int counter = (*t_ABL)->mNumberBuffers;
504 while (--counter >= 0)
505 free ((*t_ABL)->mBuffers[counter].mData);
511 bool audio_osx_source::IsRunning ()
513 UInt32 AURunning = 0, AUSize = sizeof (UInt32);
515 OSStatus err = AudioUnitGetProperty (d_InputAU,
516 kAudioOutputUnitProperty_IsRunning,
517 kAudioUnitScope_Global,
521 CheckErrorAndThrow (err, "AudioUnitGetProperty IsRunning",
522 "audio_osx_source::IsRunning");
527 bool audio_osx_source::start ()
529 if (! IsRunning ()) {
530 OSStatus err = AudioOutputUnitStart (d_InputAU);
531 CheckErrorAndThrow (err, "AudioOutputUnitStart",
532 "audio_osx_source::start");
538 bool audio_osx_source::stop ()
541 OSStatus err = AudioOutputUnitStop (d_InputAU);
542 CheckErrorAndThrow (err, "AudioOutputUnitStart",
543 "audio_osx_source::stop");
544 for (UInt32 n = 0; n < d_n_user_channels; n++) {
545 d_buffers[n]->abort ();
552 audio_osx_source::~audio_osx_source ()
554 OSStatus err = noErr;
556 // stop the AudioUnit
559 #if _OSX_DO_LISTENERS_
560 // remove the listeners
562 err = AudioUnitRemovePropertyListener
564 kAudioUnitProperty_StreamFormat,
565 (AudioUnitPropertyListenerProc) UnitListener);
566 CheckError (err, "~audio_osx_source: AudioUnitRemovePropertyListener");
568 err = AudioHardwareRemovePropertyListener
569 (kAudioHardwarePropertyDefaultInputDevice,
570 (AudioHardwarePropertyListenerProc) HardwareListener);
571 CheckError (err, "~audio_osx_source: AudioHardwareRemovePropertyListener");
574 // free pre-allocated audio buffers
575 FreeAudioBufferList (&d_InputBuffer);
577 if (d_passThrough == false) {
578 err = AudioConverterDispose (d_AudioConverter);
579 CheckError (err, "~audio_osx_source: AudioConverterDispose");
580 FreeAudioBufferList (&d_OutputBuffer);
583 // remove the audio unit
584 err = AudioUnitUninitialize (d_InputAU);
585 CheckError (err, "~audio_osx_source: AudioUnitUninitialize");
587 #ifndef GR_USE_OLD_AUDIO_UNIT
588 err = AudioComponentInstanceDispose (d_InputAU);
589 CheckError (err, "~audio_osx_source: AudioComponentInstanceDispose");
591 err = CloseComponent (d_InputAU);
592 CheckError (err, "~audio_osx_source: CloseComponent");
595 // empty and delete the queues
596 for (UInt32 n = 0; n < d_n_max_channels; n++) {
603 // close and delete the control stuff
610 audio_osx_source_sptr
611 audio_osx_make_source (int sampling_freq,
612 const std::string device_name,
615 int max_sample_count)
617 return audio_osx_source_sptr (new audio_osx_source (sampling_freq,
625 audio_osx_source::check_topology (int ninputs, int noutputs)
627 // check # inputs to make sure it's valid
629 std::cerr << "audio_osx_source::check_topology(): number of input "
630 << "streams provided (" << ninputs
631 << ") should be 0." << std::endl;
632 throw std::runtime_error ("audio_osx_source::check_topology()");
635 // check # outputs to make sure it's valid
636 if ((noutputs < 1) | (noutputs > (int) d_n_max_channels)) {
637 std::cerr << "audio_osx_source::check_topology(): number of output "
638 << "streams provided (" << noutputs << ") should be in [1,"
639 << d_n_max_channels << "] for the selected audio device."
641 throw std::runtime_error ("audio_osx_source::check_topology()");
644 // save the actual number of output (user) channels
645 d_n_user_channels = noutputs;
648 std::cerr << "chk_topo: Actual # user output channels = "
649 << noutputs << std::endl;
656 audio_osx_source::work
658 gr_vector_const_void_star &input_items,
659 gr_vector_void_star &output_items)
661 // acquire control to do processing here only
662 gruel::scoped_lock l (*d_internal);
665 std::cerr << "work1: SC = " << d_queueSampleCount
666 << ", #OI = " << noutput_items
667 << ", #Chan = " << output_items.size() << std::endl;
670 // set the actual # of output items to the 'desired' amount then
671 // verify that data is available; if not enough data is available,
672 // either wait until it is (is "do_block" is true), return (0) is no
673 // data is available and "do_block" is false, or process the actual
674 // amount of available data.
676 UInt32 actual_noutput_items = noutput_items;
678 if (d_queueSampleCount < actual_noutput_items) {
679 if (d_queueSampleCount == 0) {
680 // no data; do_block decides what to do
681 if (d_do_block == true) {
682 while (d_queueSampleCount == 0) {
683 // release control so-as to allow data to be retrieved;
684 // block until there is data to return
685 d_cond_data->wait (l);
686 // the condition's 'notify' was called; acquire control to
690 // no data & not blocking; return nothing
694 // use the actual amount of available data
695 actual_noutput_items = d_queueSampleCount;
698 // number of channels
699 int l_counter = (int) output_items.size();
701 // copy the items from the circular buffer(s) to 'work's output buffers
702 // verify that the number copied out is as expected.
704 while (--l_counter >= 0) {
705 size_t t_n_output_items = actual_noutput_items;
706 d_buffers[l_counter]->dequeue ((float*) output_items[l_counter],
708 if (t_n_output_items != actual_noutput_items) {
709 std::cerr << "audio_osx_source::work(): ERROR: number of "
710 << "available items changing unexpectedly; expecting "
711 << actual_noutput_items << ", got "
712 << t_n_output_items << "." << std::endl;
713 throw std::runtime_error ("audio_osx_source::work()");
717 // subtract the actual number of items removed from the buffer(s)
718 // from the local accounting of the number of available samples
720 d_queueSampleCount -= actual_noutput_items;
723 std::cerr << "work2: SC = " << d_queueSampleCount
724 << ", act#OI = " << actual_noutput_items << std::endl
725 << "Returning." << std::endl;
728 return (actual_noutput_items);
732 audio_osx_source::ConverterCallback
733 (AudioConverterRef inAudioConverter,
734 UInt32* ioNumberDataPackets,
735 AudioBufferList* ioData,
736 AudioStreamPacketDescription** ioASPD,
739 // take current device buffers and copy them to the tail of the
740 // input buffers the lead buffer is already there in the first
741 // d_leadSizeFrames slots
743 audio_osx_source* This = static_cast<audio_osx_source*>(inUserData);
744 AudioBufferList* l_inputABL = This->d_InputBuffer;
745 UInt32 totalInputBufferSizeBytes = ((*ioNumberDataPackets) * sizeof (float));
746 int counter = This->d_n_deviceChannels;
747 ioData->mNumberBuffers = This->d_n_deviceChannels;
748 This->d_n_ActualInputFrames = (*ioNumberDataPackets);
751 std::cerr << "cc1: io#DP = " << (*ioNumberDataPackets)
752 << ", TIBSB = " << totalInputBufferSizeBytes
753 << ", #C = " << counter << std::endl;
756 while (--counter >= 0) {
757 AudioBuffer* l_ioD_AB = &(ioData->mBuffers[counter]);
758 l_ioD_AB->mNumberChannels = 1;
759 l_ioD_AB->mData = (float*)(l_inputABL->mBuffers[counter].mData);
760 l_ioD_AB->mDataByteSize = totalInputBufferSizeBytes;
764 std::cerr << "cc2: Returning." << std::endl;
771 audio_osx_source::AUInputCallback (void* inRefCon,
772 AudioUnitRenderActionFlags* ioActionFlags,
773 const AudioTimeStamp* inTimeStamp,
775 UInt32 inNumberFrames,
776 AudioBufferList* ioData)
778 OSStatus err = noErr;
779 audio_osx_source* This = static_cast<audio_osx_source*>(inRefCon);
781 gruel::scoped_lock l (*This->d_internal);
784 std::cerr << "cb0: in#F = " << inNumberFrames
785 << ", inBN = " << inBusNumber
786 << ", SC = " << This->d_queueSampleCount << std::endl;
789 // Get the new audio data from the input device
791 err = AudioUnitRender (This->d_InputAU,
796 This->d_InputBuffer);
797 CheckErrorAndThrow (err, "AudioUnitRender",
798 "audio_osx_source::AUInputCallback");
800 UInt32 AvailableInputFrames = inNumberFrames;
801 This->d_n_AvailableInputFrames = inNumberFrames;
803 // get the number of actual output frames,
804 // either via converting the buffer or not
806 UInt32 ActualOutputFrames;
808 if (This->d_passThrough == true) {
809 ActualOutputFrames = AvailableInputFrames;
811 UInt32 AvailableInputBytes = AvailableInputFrames * sizeof (float);
812 UInt32 AvailableOutputBytes = AvailableInputBytes;
813 UInt32 AvailableOutputFrames = AvailableOutputBytes / sizeof (float);
814 UInt32 propertySize = sizeof (AvailableOutputBytes);
815 err = AudioConverterGetProperty (This->d_AudioConverter,
816 kAudioConverterPropertyCalculateOutputBufferSize,
818 &AvailableOutputBytes);
819 CheckErrorAndThrow (err, "AudioConverterGetProperty CalculateOutputBufferSize", "audio_osx_source::audio_osx_source");
821 AvailableOutputFrames = AvailableOutputBytes / sizeof (float);
824 // when decimating too much, the output sounds warbly due to
825 // fluctuating # of output frames
826 // This should not be a surprise, but there's probably some
827 // clever programming that could lessed the effect ...
828 // like finding the "ideal" # of output frames, and keeping
829 // that number constant no matter the # of input frames
830 UInt32 l_InputBytes = AvailableOutputBytes;
831 propertySize = sizeof (AvailableOutputBytes);
832 err = AudioConverterGetProperty (This->d_AudioConverter,
833 kAudioConverterPropertyCalculateInputBufferSize,
836 CheckErrorAndThrow (err, "AudioConverterGetProperty CalculateInputBufferSize", "audio_osx_source::audio_osx_source");
838 if (l_InputBytes < AvailableInputBytes) {
839 // OK to zero pad the input a little
840 AvailableOutputFrames += 1;
841 AvailableOutputBytes = AvailableOutputFrames * sizeof (float);
846 std::cerr << "cb1: avail: #IF = " << AvailableInputFrames
847 << ", #OF = " << AvailableOutputFrames << std::endl;
849 ActualOutputFrames = AvailableOutputFrames;
851 // convert the data to the correct rate
852 // on input, ActualOutputFrames is the number of available output frames
854 err = AudioConverterFillComplexBuffer (This->d_AudioConverter,
855 (AudioConverterComplexInputDataProc)(This->ConverterCallback),
858 This->d_OutputBuffer,
860 CheckErrorAndThrow (err, "AudioConverterFillComplexBuffer",
861 "audio_osx_source::AUInputCallback");
863 // on output, ActualOutputFrames is the actual number of output frames
866 std::cerr << "cb2: actual: #IF = " << This->d_n_ActualInputFrames
867 << ", #OF = " << AvailableOutputFrames << std::endl;
868 if (This->d_n_ActualInputFrames != AvailableInputFrames)
869 std::cerr << "cb2.1: avail#IF = " << AvailableInputFrames
870 << ", actual#IF = " << This->d_n_ActualInputFrames << std::endl;
874 // add the output frames to the buffers' queue, checking for overflow
876 int l_counter = This->d_n_user_channels;
879 while (--l_counter >= 0) {
880 float* inBuffer = (float*) This->d_OutputBuffer->mBuffers[l_counter].mData;
883 std::cerr << "cb3: enqueuing audio data." << std::endl;
886 int l_res = This->d_buffers[l_counter]->enqueue (inBuffer, ActualOutputFrames);
892 // data coming in too fast
893 // drop oldest buffer
894 fputs ("aO", stderr);
896 // set the local number of samples available to the max
897 This->d_queueSampleCount = This->d_buffers[0]->buffer_length_items ();
899 // keep up the local sample count
900 This->d_queueSampleCount += ActualOutputFrames;
904 std::cerr << "cb4: #OI = " << ActualOutputFrames
905 << ", #Cnt = " << This->d_queueSampleCount
906 << ", mSC = " << This->d_max_sample_count << std::endl;
909 // signal that data is available, if appropraite
910 This->d_cond_data->notify_one ();
913 std::cerr << "cb5: returning." << std::endl;
920 audio_osx_source::SetDefaultInputDeviceAsCurrent
923 // set the default input device
924 AudioDeviceID deviceID;
925 UInt32 dataSize = sizeof (AudioDeviceID);
926 AudioHardwareGetProperty (kAudioHardwarePropertyDefaultInputDevice,
929 OSStatus err = AudioUnitSetProperty (d_InputAU,
930 kAudioOutputUnitProperty_CurrentDevice,
931 kAudioUnitScope_Global,
934 sizeof (AudioDeviceID));
935 CheckErrorAndThrow (err, "AudioUnitSetProperty Current Device",
936 "audio_osx_source::SetDefaultInputDeviceAsCurrent");
939 #if _OSX_DO_LISTENERS_
941 audio_osx_source::HardwareListener
942 (AudioHardwarePropertyID inPropertyID,
945 OSStatus err = noErr;
946 audio_osx_source* This = static_cast<audio_osx_source*>(inClientData);
948 std::cerr << "a_o_s::HardwareListener" << std::endl;
950 // set the new default hardware input device for use by our AU
952 This->SetDefaultInputDeviceAsCurrent ();
954 // reset the converter to tell it that the stream has changed
956 err = AudioConverterReset (This->d_AudioConverter);
957 CheckErrorAndThrow (err, "AudioConverterReset",
958 "audio_osx_source::UnitListener");
964 audio_osx_source::UnitListener
967 AudioUnitPropertyID inID,
968 AudioUnitScope inScope,
969 AudioUnitElement inElement)
971 OSStatus err = noErr;
972 audio_osx_source* This = static_cast<audio_osx_source*>(inRefCon);
973 AudioStreamBasicDescription asbd;
975 std::cerr << "a_o_s::UnitListener" << std::endl;
977 // get the converter's input ASBD (for printing)
979 UInt32 propertySize = sizeof (asbd);
980 err = AudioConverterGetProperty (This->d_AudioConverter,
981 kAudioConverterCurrentInputStreamDescription,
984 CheckErrorAndThrow (err, "AudioConverterGetProperty "
985 "CurrentInputStreamDescription",
986 "audio_osx_source::UnitListener");
988 std::cerr << "UnitListener: Input Source changed." << std::endl
989 << "Old Source Output Info:" << std::endl;
990 PrintStreamDesc (&asbd);
992 // get the new input unit's output ASBD
994 propertySize = sizeof (asbd);
995 err = AudioUnitGetProperty (This->d_InputAU,
996 kAudioUnitProperty_StreamFormat,
997 kAudioUnitScope_Output, 1,
998 &asbd, &propertySize);
999 CheckErrorAndThrow (err, "AudioUnitGetProperty StreamFormat",
1000 "audio_osx_source::UnitListener");
1002 std::cerr << "New Source Output Info:" << std::endl;
1003 PrintStreamDesc (&asbd);
1005 // set the converter's input ASBD to this
1007 err = AudioConverterSetProperty (This->d_AudioConverter,
1008 kAudioConverterCurrentInputStreamDescription,
1011 CheckErrorAndThrow (err, "AudioConverterSetProperty "
1012 "CurrentInputStreamDescription",
1013 "audio_osx_source::UnitListener");
1015 // reset the converter to tell it that the stream has changed
1017 err = AudioConverterReset (This->d_AudioConverter);
1018 CheckErrorAndThrow (err, "AudioConverterReset",
1019 "audio_osx_source::UnitListener");