switch source package format to 3.0 quilt
[debian/gnuradio] / gr-audio-osx / src / audio_osx_source.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2010 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio.
6  *
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)
10  * any later version.
11  * 
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.
16  * 
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <audio_osx_source.h>
28 #include <gr_io_signature.h>
29 #include <stdexcept>
30 #include <audio_osx.h>
31
32 #define _OSX_AU_DEBUG_ 0
33 #define _OSX_DO_LISTENERS_ 0
34
35 void PrintStreamDesc (AudioStreamBasicDescription *inDesc)
36 {
37   if (inDesc == NULL) {
38     std::cerr << "PrintStreamDesc: Can't print a NULL desc!" << std::endl;
39     return;
40   }
41
42   std::cerr << "  Sample Rate        : " << inDesc->mSampleRate << std::endl;
43   char format_id[4];
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;
52 }
53
54 // FIXME these should query some kind of user preference
55
56 audio_osx_source::audio_osx_source (int sample_rate,
57                                     const std::string device_name,
58                                     bool do_block,
59                                     int channel_config,
60                                     int max_sample_count)
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),
65     d_channel_config (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),
77     d_buffers (0),
78     d_InputAU (0), d_InputBuffer (0), d_OutputBuffer (0),
79     d_AudioConverter (0)
80 {
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");
84   } else
85     d_outputSampleRate = (Float64) sample_rate;
86
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");
96     }
97     if (l_n_channels <= 0)
98       channel_config = 2;
99     else
100       channel_config = l_n_channels;
101   }
102
103   d_channel_config = channel_config;
104
105 // check that the max # of samples to store is valid
106
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");
112   }
113
114   d_max_sample_count = max_sample_count;
115
116 #if _OSX_AU_DEBUG_
117   std::cerr << "source(): max # samples = " << d_max_sample_count << std::endl;
118 #endif
119
120   OSStatus err = noErr;
121
122 // create the default AudioUnit for input
123
124 // Open the default input unit
125 #ifndef GR_USE_OLD_AUDIO_UNIT
126   AudioComponentDescription InputDesc;
127 #else
128   ComponentDescription InputDesc;
129 #endif
130   
131
132   InputDesc.componentType = kAudioUnitType_Output;
133   InputDesc.componentSubType = kAudioUnitSubType_HALOutput;
134   InputDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
135   InputDesc.componentFlags = 0;
136   InputDesc.componentFlagsMask = 0;
137
138 #ifndef GR_USE_OLD_AUDIO_UNIT
139   AudioComponent comp = AudioComponentFindNext (NULL, &InputDesc);
140 #else
141   Component comp = FindNextComponent (NULL, &InputDesc);
142 #endif
143   
144   if (comp == NULL) {
145 #ifndef GR_USE_OLD_AUDIO_UNIT
146     std::cerr << "AudioComponentFindNext Error" << std::endl;
147 #else
148     std::cerr << "FindNextComponent Error" << std::endl;
149 #endif
150     throw std::runtime_error ("audio_osx_source::audio_osx_source");
151   }
152
153 #ifndef GR_USE_OLD_AUDIO_UNIT
154   err = AudioComponentInstanceNew (comp, &d_InputAU);
155   CheckErrorAndThrow (err, "AudioComponentInstanceNew",
156                       "audio_osx_source::audio_osx_source");
157 #else
158   err = OpenAComponent (comp, &d_InputAU);
159   CheckErrorAndThrow (err, "OpenAComponent",
160                       "audio_osx_source::audio_osx_source");
161 #endif
162   
163
164   UInt32 enableIO;
165
166 // must enable the AUHAL for input and disable output 
167 // before setting the AUHAL's current device
168
169 // Enable input on the AUHAL
170   enableIO = 1;
171   err = AudioUnitSetProperty (d_InputAU,
172                               kAudioOutputUnitProperty_EnableIO,
173                               kAudioUnitScope_Input,
174                               1, // input element
175                               &enableIO,
176                               sizeof (UInt32));
177   CheckErrorAndThrow (err, "AudioUnitSetProperty Input Enable",
178                       "audio_osx_source::audio_osx_source");
179
180 // Disable output on the AUHAL
181   enableIO = 0;
182   err = AudioUnitSetProperty (d_InputAU,
183                               kAudioOutputUnitProperty_EnableIO,
184                               kAudioUnitScope_Output,
185                               0, // output element
186                               &enableIO,
187                               sizeof (UInt32));
188   CheckErrorAndThrow (err, "AudioUnitSetProperty Output Disable",
189                       "audio_osx_source::audio_osx_source");
190
191 // set the default input device for our input AU
192
193   SetDefaultInputDeviceAsCurrent ();
194
195 #if _OSX_DO_LISTENERS_
196 // set up a listener if default hardware input device changes
197
198   err = AudioHardwareAddPropertyListener
199     (kAudioHardwarePropertyDefaultInputDevice,
200      (AudioHardwarePropertyListenerProc) HardwareListener,
201      this);
202
203   CheckErrorAndThrow (err, "AudioHardwareAddPropertyListener",
204                       "audio_osx_source::audio_osx_source");
205
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
209
210   err = AudioUnitAddPropertyListener
211     (d_InputAU,
212      kAudioUnitProperty_StreamFormat,
213      (AudioUnitPropertyListenerProc) UnitListener,
214      this);
215   CheckErrorAndThrow (err, "Adding Unit Property Listener",
216                       "audio_osx_source::audio_osx_source");
217 #endif
218
219 // Now find out if it actually can do input.
220
221   UInt32 hasInput = 0;
222   UInt32 dataSize = sizeof (hasInput);
223   err = AudioUnitGetProperty (d_InputAU,
224                               kAudioOutputUnitProperty_HasIO,
225                               kAudioUnitScope_Input,
226                               1,
227                               &hasInput,
228                               &dataSize);
229   CheckErrorAndThrow (err, "AudioUnitGetProperty HasIO",
230                       "audio_osx_source::audio_osx_source");
231   if (hasInput == 0) {
232     std::cerr << "Selected Audio Device does not support Input." << std::endl;
233     throw std::runtime_error ("audio_osx_source::audio_osx_source");
234   }
235
236 // Set up a callback function to retrieve input from the Audio Device
237
238   AURenderCallbackStruct AUCallBack;
239
240   AUCallBack.inputProc = (AURenderCallback)(audio_osx_source::AUInputCallback);
241   AUCallBack.inputProcRefCon = this;
242
243   err = AudioUnitSetProperty (d_InputAU,
244                               kAudioOutputUnitProperty_SetInputCallback,
245                               kAudioUnitScope_Global,
246                               0,
247                               &AUCallBack,
248                               sizeof (AURenderCallbackStruct));
249   CheckErrorAndThrow (err, "AudioUnitSetProperty Input Callback",
250                       "audio_osx_source::audio_osx_source");
251
252   UInt32 propertySize;
253   AudioStreamBasicDescription asbd_device, asbd_client, asbd_user;
254
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
258
259 // Get the Stream Format (device side)
260
261   propertySize = sizeof (asbd_device);
262   err = AudioUnitGetProperty (d_InputAU,
263                               kAudioUnitProperty_StreamFormat,
264                               kAudioUnitScope_Input,
265                               1,
266                               &asbd_device,
267                               &propertySize);
268   CheckErrorAndThrow (err, "AudioUnitGetProperty Device Input Stream Format",
269                       "audio_osx_source::audio_osx_source");
270
271 #if _OSX_AU_DEBUG_
272   std::cerr << std::endl << "---- Device Stream Format ----" << std::endl;
273   PrintStreamDesc (&asbd_device);
274 #endif
275
276 // Get the Stream Format (client side)
277   propertySize = sizeof (asbd_client);
278   err = AudioUnitGetProperty (d_InputAU,
279                               kAudioUnitProperty_StreamFormat,
280                               kAudioUnitScope_Output,
281                               1,
282                               &asbd_client,
283                               &propertySize);
284   CheckErrorAndThrow (err, "AudioUnitGetProperty Device Ouput Stream Format",
285                       "audio_osx_source::audio_osx_source");
286
287 #if _OSX_AU_DEBUG_
288   std::cerr << std::endl << "---- Client Stream Format ----" << std::endl;
289   PrintStreamDesc (&asbd_client);
290 #endif
291
292 // Set the format of all the AUs to the input/output devices channel count
293
294 // get the max number of input (& thus output) channels supported by
295 // this device
296   d_n_max_channels = asbd_client.mChannelsPerFrame;
297
298 // create the output io signature;
299 // no input siganture to set (source is hardware)
300   set_output_signature (gr_make_io_signature (1,
301                                               d_n_max_channels,
302                                               sizeof (float)));
303
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);
310   }
311
312   d_deviceSampleRate = asbd_device.mSampleRate;
313   d_n_deviceChannels = asbd_device.mChannelsPerFrame;
314
315 // create an ASBD for the user's wants
316
317   asbd_user.mSampleRate = d_outputSampleRate;
318   asbd_user.mFormatID = kAudioFormatLinearPCM;
319   asbd_user.mFormatFlags = (kLinearPCMFormatFlagIsFloat |
320                             GR_PCM_ENDIANNESS |
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;
328
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;
333   } else {
334     d_passThrough = false;
335 // Create the audio converter
336
337     err = AudioConverterNew (&asbd_client, &asbd_user, &d_AudioConverter);
338     CheckErrorAndThrow (err, "AudioConverterNew",
339                         "audio_osx_source::audio_osx_source");
340
341 // Set the audio converter sample rate quality to "max" ...
342 // requires more samples, but should sound nicer
343
344     UInt32 ACQuality = kAudioConverterQuality_Max;
345     propertySize = sizeof (ACQuality);
346     err = AudioConverterSetProperty (d_AudioConverter,
347                                      kAudioConverterSampleRateConverterQuality,
348                                      propertySize,
349                                      &ACQuality);
350     CheckErrorAndThrow (err, "AudioConverterSetProperty "
351                         "SampleRateConverterQuality",
352                         "audio_osx_source::audio_osx_source");
353
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.
359
360 //   UInt32 ACPrimeMethod = kConverterPrimeMethod_None;
361     UInt32 ACPrimeMethod = kConverterPrimeMethod_Pre;
362     propertySize = sizeof (ACPrimeMethod);
363     err = AudioConverterSetProperty (d_AudioConverter, 
364                                      kAudioConverterPrimeMethod,
365                                      propertySize,
366                                      &ACPrimeMethod);
367     CheckErrorAndThrow (err, "AudioConverterSetProperty PrimeMethod",
368                         "audio_osx_source::audio_osx_source");
369
370 // Get the size of the I/O buffer(s) to allow for pre-allocated buffers
371       
372 // lead frame info (trail frame info is ignored)
373
374     AudioConverterPrimeInfo ACPrimeInfo = {0, 0};
375     propertySize = sizeof (ACPrimeInfo);
376     err = AudioConverterGetProperty (d_AudioConverter, 
377                                      kAudioConverterPrimeInfo,
378                                      &propertySize,
379                                      &ACPrimeInfo);
380     CheckErrorAndThrow (err, "AudioConverterGetProperty PrimeInfo",
381                         "audio_osx_source::audio_osx_source");
382
383     switch (ACPrimeMethod) {
384     case (kConverterPrimeMethod_None):
385       d_leadSizeFrames =
386         d_trailSizeFrames = 0L;
387       break;
388     case (kConverterPrimeMethod_Normal):
389       d_leadSizeFrames = 0L;
390       d_trailSizeFrames = ACPrimeInfo.trailingFrames;
391       break;
392     default:
393       d_leadSizeFrames = ACPrimeInfo.leadingFrames;
394       d_trailSizeFrames = ACPrimeInfo.trailingFrames;
395     }
396   }
397   d_leadSizeBytes = d_leadSizeFrames * sizeof (Float32);
398   d_trailSizeBytes = d_trailSizeFrames * sizeof (Float32);
399
400   propertySize = sizeof (d_deviceBufferSizeFrames);
401   err = AudioUnitGetProperty (d_InputAU,
402                               kAudioDevicePropertyBufferFrameSize,
403                               kAudioUnitScope_Global,
404                               0,
405                               &d_deviceBufferSizeFrames,
406                               &propertySize);
407   CheckErrorAndThrow (err, "AudioUnitGetProperty Buffer Frame Size",
408                       "audio_osx_source::audio_osx_source");
409
410   d_deviceBufferSizeBytes = d_deviceBufferSizeFrames * sizeof (Float32);
411   d_inputBufferSizeBytes = d_deviceBufferSizeBytes + d_leadSizeBytes;
412   d_inputBufferSizeFrames = d_deviceBufferSizeFrames + d_leadSizeFrames;
413
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)
420
421   d_extraBufferSizeFrames = ((UInt32) ceil (d_deviceSampleRate
422                                             / d_outputSampleRate)
423                              * sizeof (float));
424   if (d_extraBufferSizeFrames < 4)
425     d_extraBufferSizeFrames = 4;
426   d_extraBufferSizeBytes = d_extraBufferSizeFrames * sizeof (float);
427
428   d_outputBufferSizeFrames = (UInt32) ceil (((Float64) d_inputBufferSizeFrames)
429                                             * d_outputSampleRate
430                                             / d_deviceSampleRate);
431   d_outputBufferSizeBytes = d_outputBufferSizeFrames * sizeof (float);
432   d_inputBufferSizeFrames += d_extraBufferSizeFrames;
433
434 // pre-alloc all buffers
435
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);
441   } else {
442     d_OutputBuffer = d_InputBuffer;
443   }
444
445 // create the stuff to regulate I/O
446
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");
451
452   d_internal = new gruel::mutex ();
453   if (d_internal == NULL)
454     CheckErrorAndThrow (errno, "new mutex (internal)",
455                         "audio_osx_source::audio_osx_source");
456
457 // initialize the AU for input
458
459   err = AudioUnitInitialize (d_InputAU);
460   CheckErrorAndThrow (err, "AudioUnitInitialize",
461                       "audio_osx_source::audio_osx_source");
462
463 #if _OSX_AU_DEBUG_
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;
475 #endif
476 }
477
478 void
479 audio_osx_source::AllocAudioBufferList (AudioBufferList** t_ABL,
480                                         UInt32 n_channels,
481                                         UInt32 bufferSizeBytes)
482 {
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;
488
489   int counter = n_channels;
490
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);
495   }
496 }
497
498 void
499 audio_osx_source::FreeAudioBufferList (AudioBufferList** t_ABL)
500 {
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);
506     free (*t_ABL);
507     (*t_ABL) = 0;
508   }
509 }
510
511 bool audio_osx_source::IsRunning ()
512 {
513   UInt32 AURunning = 0, AUSize = sizeof (UInt32);
514
515   OSStatus err = AudioUnitGetProperty (d_InputAU,
516                                        kAudioOutputUnitProperty_IsRunning,
517                                        kAudioUnitScope_Global,
518                                        0,
519                                        &AURunning,
520                                        &AUSize);
521   CheckErrorAndThrow (err, "AudioUnitGetProperty IsRunning",
522                       "audio_osx_source::IsRunning");
523
524   return (AURunning);
525 }
526
527 bool audio_osx_source::start ()
528 {
529   if (! IsRunning ()) {
530     OSStatus err = AudioOutputUnitStart (d_InputAU);
531     CheckErrorAndThrow (err, "AudioOutputUnitStart",
532                         "audio_osx_source::start");
533   }
534
535   return (true);
536 }
537
538 bool audio_osx_source::stop ()
539 {
540   if (IsRunning ()) {
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 ();
546     }
547   }
548
549   return (true);
550 }
551
552 audio_osx_source::~audio_osx_source ()
553 {
554   OSStatus err = noErr;
555
556 // stop the AudioUnit
557   stop();
558
559 #if _OSX_DO_LISTENERS_
560 // remove the listeners
561
562   err = AudioUnitRemovePropertyListener
563     (d_InputAU,
564      kAudioUnitProperty_StreamFormat,
565      (AudioUnitPropertyListenerProc) UnitListener);
566   CheckError (err, "~audio_osx_source: AudioUnitRemovePropertyListener");
567
568   err = AudioHardwareRemovePropertyListener
569     (kAudioHardwarePropertyDefaultInputDevice,
570      (AudioHardwarePropertyListenerProc) HardwareListener);
571   CheckError (err, "~audio_osx_source: AudioHardwareRemovePropertyListener");
572 #endif
573
574 // free pre-allocated audio buffers
575   FreeAudioBufferList (&d_InputBuffer);
576
577   if (d_passThrough == false) {
578     err = AudioConverterDispose (d_AudioConverter);
579     CheckError (err, "~audio_osx_source: AudioConverterDispose");
580     FreeAudioBufferList (&d_OutputBuffer);
581   }
582
583 // remove the audio unit
584   err = AudioUnitUninitialize (d_InputAU);
585   CheckError (err, "~audio_osx_source: AudioUnitUninitialize");
586
587 #ifndef GR_USE_OLD_AUDIO_UNIT
588   err = AudioComponentInstanceDispose (d_InputAU);
589   CheckError (err, "~audio_osx_source: AudioComponentInstanceDispose");
590 #else
591   err = CloseComponent (d_InputAU);
592   CheckError (err, "~audio_osx_source: CloseComponent");
593 #endif
594
595 // empty and delete the queues
596   for (UInt32 n = 0; n < d_n_max_channels; n++) {
597     delete d_buffers[n];
598     d_buffers[n] = 0;
599   }
600   delete [] d_buffers;
601   d_buffers = 0;
602
603 // close and delete the control stuff
604   delete d_cond_data;
605   d_cond_data = 0;
606   delete d_internal;
607   d_internal = 0;
608 }
609
610 audio_osx_source_sptr
611 audio_osx_make_source (int sampling_freq,
612                        const std::string device_name,
613                        bool do_block,
614                        int channel_config,
615                        int max_sample_count)
616 {
617   return audio_osx_source_sptr (new audio_osx_source (sampling_freq,
618                                                       device_name,
619                                                       do_block,
620                                                       channel_config,
621                                                       max_sample_count));
622 }
623
624 bool
625 audio_osx_source::check_topology (int ninputs, int noutputs)
626 {
627 // check # inputs to make sure it's valid
628   if (ninputs != 0) {
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()");
633   }
634
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."
640               << std::endl;
641     throw std::runtime_error ("audio_osx_source::check_topology()");
642   }
643
644 // save the actual number of output (user) channels
645   d_n_user_channels = noutputs;
646
647 #if _OSX_AU_DEBUG_
648   std::cerr << "chk_topo: Actual # user output channels = "
649             << noutputs << std::endl;
650 #endif
651
652   return (true);
653 }
654
655 int
656 audio_osx_source::work
657 (int noutput_items,
658  gr_vector_const_void_star &input_items,
659  gr_vector_void_star &output_items)
660 {
661   // acquire control to do processing here only
662   gruel::scoped_lock l (*d_internal);
663
664 #if _OSX_AU_DEBUG_
665   std::cerr << "work1: SC = " << d_queueSampleCount
666             << ", #OI = " << noutput_items
667             << ", #Chan = " << output_items.size() << std::endl;
668 #endif
669
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.
675
676   UInt32 actual_noutput_items = noutput_items;
677
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
687           // keep thread safe
688         }
689       } else {
690         // no data & not blocking; return nothing
691         return (0);
692       }
693     }
694     // use the actual amount of available data
695     actual_noutput_items = d_queueSampleCount;
696   }
697
698   // number of channels
699   int l_counter = (int) output_items.size();
700
701   // copy the items from the circular buffer(s) to 'work's output buffers
702   // verify that the number copied out is as expected.
703
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],
707                                    &t_n_output_items);
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()");
714     }
715   }
716
717   // subtract the actual number of items removed from the buffer(s)
718   // from the local accounting of the number of available samples
719
720   d_queueSampleCount -= actual_noutput_items;
721
722 #if _OSX_AU_DEBUG_
723   std::cerr << "work2: SC = " << d_queueSampleCount
724             << ", act#OI = " << actual_noutput_items << std::endl
725             << "Returning." << std::endl;
726 #endif
727
728   return (actual_noutput_items);
729 }
730
731 OSStatus
732 audio_osx_source::ConverterCallback
733 (AudioConverterRef inAudioConverter,
734  UInt32* ioNumberDataPackets,
735  AudioBufferList* ioData,
736  AudioStreamPacketDescription** ioASPD,
737  void* inUserData)
738 {
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
742
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);
749
750 #if _OSX_AU_DEBUG_
751   std::cerr << "cc1: io#DP = " << (*ioNumberDataPackets)
752             << ", TIBSB = " << totalInputBufferSizeBytes
753             << ", #C = " << counter << std::endl;
754 #endif
755
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;
761   }
762
763 #if _OSX_AU_DEBUG_
764   std::cerr << "cc2: Returning." << std::endl;
765 #endif
766
767   return (noErr);
768 }
769
770 OSStatus
771 audio_osx_source::AUInputCallback (void* inRefCon,
772                                    AudioUnitRenderActionFlags* ioActionFlags,
773                                    const AudioTimeStamp* inTimeStamp,
774                                    UInt32 inBusNumber,
775                                    UInt32 inNumberFrames,
776                                    AudioBufferList* ioData)
777 {
778   OSStatus err = noErr;
779   audio_osx_source* This = static_cast<audio_osx_source*>(inRefCon);
780
781   gruel::scoped_lock l (*This->d_internal);
782
783 #if _OSX_AU_DEBUG_
784   std::cerr << "cb0: in#F = " << inNumberFrames
785             << ", inBN = " << inBusNumber
786             << ", SC = " << This->d_queueSampleCount << std::endl;
787 #endif
788
789 // Get the new audio data from the input device
790
791   err = AudioUnitRender (This->d_InputAU,
792                          ioActionFlags,
793                          inTimeStamp,
794                          1, //inBusNumber,
795                          inNumberFrames,
796                          This->d_InputBuffer);
797   CheckErrorAndThrow (err, "AudioUnitRender",
798                       "audio_osx_source::AUInputCallback");
799
800   UInt32 AvailableInputFrames = inNumberFrames;
801   This->d_n_AvailableInputFrames = inNumberFrames;
802
803 // get the number of actual output frames,
804 // either via converting the buffer or not
805
806   UInt32 ActualOutputFrames;
807
808   if (This->d_passThrough == true) {
809     ActualOutputFrames = AvailableInputFrames;
810   } else {
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,
817                                      &propertySize,
818                                      &AvailableOutputBytes);
819     CheckErrorAndThrow (err, "AudioConverterGetProperty CalculateOutputBufferSize", "audio_osx_source::audio_osx_source");
820
821     AvailableOutputFrames = AvailableOutputBytes / sizeof (float);
822
823 #if 0
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,
834                                      &propertySize,
835                                      &l_InputBytes);
836     CheckErrorAndThrow (err, "AudioConverterGetProperty CalculateInputBufferSize", "audio_osx_source::audio_osx_source");
837
838     if (l_InputBytes < AvailableInputBytes) {
839 // OK to zero pad the input a little
840       AvailableOutputFrames += 1;
841       AvailableOutputBytes = AvailableOutputFrames * sizeof (float);
842     }
843 #endif
844
845 #if _OSX_AU_DEBUG_
846     std::cerr << "cb1:  avail: #IF = " << AvailableInputFrames
847               << ", #OF = " << AvailableOutputFrames << std::endl;
848 #endif
849     ActualOutputFrames = AvailableOutputFrames;
850
851 // convert the data to the correct rate
852 // on input, ActualOutputFrames is the number of available output frames
853
854     err = AudioConverterFillComplexBuffer (This->d_AudioConverter,
855            (AudioConverterComplexInputDataProc)(This->ConverterCallback),
856                                            inRefCon,
857                                            &ActualOutputFrames,
858                                            This->d_OutputBuffer,
859                                            NULL);
860     CheckErrorAndThrow (err, "AudioConverterFillComplexBuffer",
861                         "audio_osx_source::AUInputCallback");
862
863 // on output, ActualOutputFrames is the actual number of output frames
864
865 #if _OSX_AU_DEBUG_
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;
871 #endif
872   }
873
874 // add the output frames to the buffers' queue, checking for overflow
875
876   int l_counter = This->d_n_user_channels;
877   int res = 0;
878
879   while (--l_counter >= 0) {
880     float* inBuffer = (float*) This->d_OutputBuffer->mBuffers[l_counter].mData;
881
882 #if _OSX_AU_DEBUG_
883     std::cerr << "cb3: enqueuing audio data." << std::endl;
884 #endif
885
886     int l_res = This->d_buffers[l_counter]->enqueue (inBuffer, ActualOutputFrames);
887     if (l_res == -1)
888       res = -1;
889   }
890
891   if (res == -1) {
892 // data coming in too fast
893 // drop oldest buffer
894     fputs ("aO", stderr);
895     fflush (stderr);
896 // set the local number of samples available to the max
897     This->d_queueSampleCount = This->d_buffers[0]->buffer_length_items ();
898   } else {
899 // keep up the local sample count
900     This->d_queueSampleCount += ActualOutputFrames;
901   }
902
903 #if _OSX_AU_DEBUG_
904   std::cerr << "cb4: #OI = " << ActualOutputFrames
905             << ", #Cnt = " << This->d_queueSampleCount
906             << ", mSC = " << This->d_max_sample_count << std::endl;
907 #endif
908
909 // signal that data is available, if appropraite
910   This->d_cond_data->notify_one ();
911
912 #if _OSX_AU_DEBUG_
913   std::cerr << "cb5: returning." << std::endl;
914 #endif
915
916   return (err);
917 }
918
919 void
920 audio_osx_source::SetDefaultInputDeviceAsCurrent
921 ()
922 {
923 // set the default input device
924   AudioDeviceID deviceID;
925   UInt32 dataSize = sizeof (AudioDeviceID);
926   AudioHardwareGetProperty (kAudioHardwarePropertyDefaultInputDevice,
927                             &dataSize,
928                             &deviceID);
929   OSStatus err = AudioUnitSetProperty (d_InputAU,
930                                        kAudioOutputUnitProperty_CurrentDevice,
931                                        kAudioUnitScope_Global,
932                                        0,
933                                        &deviceID,
934                                        sizeof (AudioDeviceID));
935   CheckErrorAndThrow (err, "AudioUnitSetProperty Current Device",
936                       "audio_osx_source::SetDefaultInputDeviceAsCurrent");
937 }
938
939 #if _OSX_DO_LISTENERS_
940 OSStatus
941 audio_osx_source::HardwareListener
942 (AudioHardwarePropertyID inPropertyID, 
943  void *inClientData)
944 {
945   OSStatus err = noErr;
946   audio_osx_source* This = static_cast<audio_osx_source*>(inClientData);
947
948   std::cerr << "a_o_s::HardwareListener" << std::endl;
949
950 // set the new default hardware input device for use by our AU
951
952   This->SetDefaultInputDeviceAsCurrent ();
953
954 // reset the converter to tell it that the stream has changed
955
956   err = AudioConverterReset (This->d_AudioConverter);
957   CheckErrorAndThrow (err, "AudioConverterReset",
958                       "audio_osx_source::UnitListener");
959
960   return (err);
961 }
962
963 OSStatus
964 audio_osx_source::UnitListener
965 (void *inRefCon,
966  AudioUnit ci,
967  AudioUnitPropertyID inID,
968  AudioUnitScope inScope,
969  AudioUnitElement inElement)
970 {
971   OSStatus err = noErr;
972   audio_osx_source* This = static_cast<audio_osx_source*>(inRefCon);
973   AudioStreamBasicDescription asbd;                     
974
975   std::cerr << "a_o_s::UnitListener" << std::endl;
976
977 // get the converter's input ASBD (for printing)
978
979   UInt32 propertySize = sizeof (asbd);
980   err = AudioConverterGetProperty (This->d_AudioConverter,
981                                    kAudioConverterCurrentInputStreamDescription,
982                                    &propertySize,
983                                    &asbd);
984   CheckErrorAndThrow (err, "AudioConverterGetProperty "
985                       "CurrentInputStreamDescription",
986                       "audio_osx_source::UnitListener");
987
988   std::cerr << "UnitListener: Input Source changed." << std::endl
989             << "Old Source Output Info:" << std::endl;
990   PrintStreamDesc (&asbd);
991
992 // get the new input unit's output ASBD
993
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");
1001
1002   std::cerr << "New Source Output Info:" << std::endl;
1003   PrintStreamDesc (&asbd);
1004
1005 // set the converter's input ASBD to this
1006
1007   err = AudioConverterSetProperty (This->d_AudioConverter,
1008                                    kAudioConverterCurrentInputStreamDescription,
1009                                    propertySize,
1010                                    &asbd);
1011   CheckErrorAndThrow (err, "AudioConverterSetProperty "
1012                       "CurrentInputStreamDescription",
1013                       "audio_osx_source::UnitListener");
1014
1015 // reset the converter to tell it that the stream has changed
1016
1017   err = AudioConverterReset (This->d_AudioConverter);
1018   CheckErrorAndThrow (err, "AudioConverterReset",
1019                       "audio_osx_source::UnitListener");
1020
1021   return (err);
1022 }
1023 #endif