GNU Radio 3.5.3.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  * \ingroup audio_blk
00035  *
00036  * Input signature is one or two streams of floats.
00037  * Samples must be in the range [-1,1].
00038  */
00039 
00040 class audio_osx_source : public audio_source {
00041 
00042   Float64             d_deviceSampleRate, d_outputSampleRate;
00043   int                 d_channel_config;
00044   UInt32              d_inputBufferSizeFrames, d_inputBufferSizeBytes;
00045   UInt32              d_outputBufferSizeFrames, d_outputBufferSizeBytes;
00046   UInt32              d_deviceBufferSizeFrames, d_deviceBufferSizeBytes;
00047   UInt32              d_leadSizeFrames, d_leadSizeBytes;
00048   UInt32              d_trailSizeFrames, d_trailSizeBytes;
00049   UInt32              d_extraBufferSizeFrames, d_extraBufferSizeBytes;
00050   UInt32              d_queueSampleCount, d_max_sample_count;
00051   UInt32              d_n_AvailableInputFrames, d_n_ActualInputFrames;
00052   UInt32              d_n_user_channels, d_n_max_channels, d_n_deviceChannels;
00053   bool                d_do_block, d_passThrough, d_waiting_for_data;
00054   gruel::mutex*       d_internal;
00055   gruel::condition_variable* d_cond_data;
00056   circular_buffer<float>** d_buffers;
00057 
00058 // AudioUnits and Such
00059   AudioUnit           d_InputAU;
00060   AudioBufferList*    d_InputBuffer;
00061   AudioBufferList*    d_OutputBuffer;
00062   AudioConverterRef   d_AudioConverter;
00063 
00064 public:
00065   audio_osx_source (int sample_rate = 44100,
00066                     const std::string device_name = "",
00067                     bool do_block = true,
00068                     int channel_config = -1,
00069                     int max_sample_count = -1);
00070 
00071   ~audio_osx_source ();
00072 
00073   bool start ();
00074   bool stop ();
00075   bool IsRunning ();
00076 
00077   bool check_topology (int ninputs, int noutputs);
00078 
00079   int work (int noutput_items,
00080             gr_vector_const_void_star &input_items,
00081             gr_vector_void_star &output_items);
00082 
00083 private:
00084   void SetDefaultInputDeviceAsCurrent ();
00085 
00086   void AllocAudioBufferList (AudioBufferList** t_ABL,
00087                              UInt32 n_channels,
00088                              UInt32 inputBufferSizeBytes);
00089 
00090   void FreeAudioBufferList (AudioBufferList** t_ABL);
00091 
00092   static OSStatus ConverterCallback (AudioConverterRef inAudioConverter,
00093                                      UInt32* ioNumberDataPackets,
00094                                      AudioBufferList* ioData,
00095                                      AudioStreamPacketDescription** outASPD,
00096                                      void* inUserData);
00097 
00098   static OSStatus AUInputCallback (void *inRefCon,
00099                                    AudioUnitRenderActionFlags *ioActionFlags,
00100                                    const AudioTimeStamp *inTimeStamp,
00101                                    UInt32 inBusNumber,
00102                                    UInt32 inNumberFrames,
00103                                    AudioBufferList *ioData);
00104 #if _OSX_DO_LISTENERS_
00105   static OSStatus UnitListener (void *inRefCon,
00106                                 AudioUnit ci,
00107                                 AudioUnitPropertyID inID,
00108                                 AudioUnitScope inScope,
00109                                 AudioUnitElement inElement);
00110 
00111   static OSStatus HardwareListener (AudioHardwarePropertyID inPropertyID, 
00112                                     void *inClientData);
00113 #endif
00114 };
00115 
00116 #endif /* INCLUDED_AUDIO_OSX_SOURCE_H */