GNU Radio 3.4.2 C++ API
audio_osx_source.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2006-2011 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio.
00006  *
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_AUDIO_OSX_SOURCE_H
00024 #define INCLUDED_AUDIO_OSX_SOURCE_H
00025 
00026 #include <gr_audio_source.h>
00027 #include <string>
00028 #include <AudioToolbox/AudioToolbox.h>
00029 #include <AudioUnit/AudioUnit.h>
00030 #include <circular_buffer.h>
00031 
00032 /*!
00033  * \brief audio source using OSX
00034  *
00035  * Input signature is one or two streams of floats.
00036  * Samples must be in the range [-1,1].
00037  */
00038 
00039 class audio_osx_source : public audio_source {
00040 
00041   Float64             d_deviceSampleRate, d_outputSampleRate;
00042   int                 d_channel_config;
00043   UInt32              d_inputBufferSizeFrames, d_inputBufferSizeBytes;
00044   UInt32              d_outputBufferSizeFrames, d_outputBufferSizeBytes;
00045   UInt32              d_deviceBufferSizeFrames, d_deviceBufferSizeBytes;
00046   UInt32              d_leadSizeFrames, d_leadSizeBytes;
00047   UInt32              d_trailSizeFrames, d_trailSizeBytes;
00048   UInt32              d_extraBufferSizeFrames, d_extraBufferSizeBytes;
00049   UInt32              d_queueSampleCount, d_max_sample_count;
00050   UInt32              d_n_AvailableInputFrames, d_n_ActualInputFrames;
00051   UInt32              d_n_user_channels, d_n_max_channels, d_n_deviceChannels;
00052   bool                d_do_block, d_passThrough, d_waiting_for_data;
00053   gruel::mutex*       d_internal;
00054   gruel::condition_variable* d_cond_data;
00055   circular_buffer<float>** d_buffers;
00056 
00057 // AudioUnits and Such
00058   AudioUnit           d_InputAU;
00059   AudioBufferList*    d_InputBuffer;
00060   AudioBufferList*    d_OutputBuffer;
00061   AudioConverterRef   d_AudioConverter;
00062 
00063 public:
00064   audio_osx_source (int sample_rate = 44100,
00065                     const std::string device_name = "",
00066                     bool do_block = true,
00067                     int channel_config = -1,
00068                     int max_sample_count = -1);
00069 
00070   ~audio_osx_source ();
00071 
00072   bool start ();
00073   bool stop ();
00074   bool IsRunning ();
00075 
00076   bool check_topology (int ninputs, int noutputs);
00077 
00078   int work (int noutput_items,
00079             gr_vector_const_void_star &input_items,
00080             gr_vector_void_star &output_items);
00081 
00082 private:
00083   void SetDefaultInputDeviceAsCurrent ();
00084 
00085   void AllocAudioBufferList (AudioBufferList** t_ABL,
00086                              UInt32 n_channels,
00087                              UInt32 inputBufferSizeBytes);
00088 
00089   void FreeAudioBufferList (AudioBufferList** t_ABL);
00090 
00091   static OSStatus ConverterCallback (AudioConverterRef inAudioConverter,
00092                                      UInt32* ioNumberDataPackets,
00093                                      AudioBufferList* ioData,
00094                                      AudioStreamPacketDescription** outASPD,
00095                                      void* inUserData);
00096 
00097   static OSStatus AUInputCallback (void *inRefCon,
00098                                    AudioUnitRenderActionFlags *ioActionFlags,
00099                                    const AudioTimeStamp *inTimeStamp,
00100                                    UInt32 inBusNumber,
00101                                    UInt32 inNumberFrames,
00102                                    AudioBufferList *ioData);
00103 #if _OSX_DO_LISTENERS_
00104   static OSStatus UnitListener (void *inRefCon,
00105                                 AudioUnit ci,
00106                                 AudioUnitPropertyID inID,
00107                                 AudioUnitScope inScope,
00108                                 AudioUnitElement inElement);
00109 
00110   static OSStatus HardwareListener (AudioHardwarePropertyID inPropertyID, 
00111                                     void *inClientData);
00112 #endif
00113 };
00114 
00115 #endif /* INCLUDED_AUDIO_OSX_SOURCE_H */