Imported Upstream version 3.0
[debian/gnuradio] / gr-audio-osx / src / audio_osx_source.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 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 2, 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 #ifndef INCLUDED_AUDIO_OSX_SOURCE_H
24 #define INCLUDED_AUDIO_OSX_SOURCE_H
25
26 #include <gr_sync_block.h>
27 #include <string>
28 #include <AudioToolbox/AudioToolbox.h>
29 #include <AudioUnit/AudioUnit.h>
30 #include <circular_buffer.h>
31
32 class audio_osx_source;
33 typedef boost::shared_ptr<audio_osx_source> audio_osx_source_sptr;
34
35 audio_osx_source_sptr
36 audio_osx_make_source (int sample_rate = 44100,
37                        const std::string device_name = "",
38                        bool do_block = true,
39                        int channel_config = -1,
40                        int max_sample_count = -1);
41
42 /*!
43  * \brief audio source using OSX
44  *
45  * Input signature is one or two streams of floats.
46  * Samples must be in the range [-1,1].
47  */
48
49 class audio_osx_source : public gr_sync_block {
50   friend audio_osx_source_sptr
51   audio_osx_make_source (int sample_rate,
52                          const std::string device_name,
53                          bool do_block,
54                          int channel_config,
55                          int max_sample_count);
56
57   Float64             d_deviceSampleRate, d_outputSampleRate;
58   int                 d_channel_config;
59   UInt32              d_inputBufferSizeFrames, d_inputBufferSizeBytes;
60   UInt32              d_outputBufferSizeFrames, d_outputBufferSizeBytes;
61   UInt32              d_deviceBufferSizeFrames, d_deviceBufferSizeBytes;
62   UInt32              d_leadSizeFrames, d_leadSizeBytes;
63   UInt32              d_trailSizeFrames, d_trailSizeBytes;
64   UInt32              d_extraBufferSizeFrames, d_extraBufferSizeBytes;
65   UInt32              d_queueSampleCount, d_max_sample_count;
66   UInt32              d_n_AvailableInputFrames, d_n_ActualInputFrames;
67   UInt32              d_n_user_channels, d_n_max_channels, d_n_deviceChannels;
68   bool                d_do_block, d_passThrough, d_waiting_for_data;
69   mld_mutex_ptr       d_internal;
70   mld_condition_ptr   d_cond_data;
71   circular_buffer<float>** d_buffers;
72
73 // AudioUnits and Such
74   AudioUnit           d_InputAU;
75   AudioBufferList*    d_InputBuffer;
76   AudioBufferList*    d_OutputBuffer;
77   AudioConverterRef   d_AudioConverter;
78
79 protected:
80   audio_osx_source (int sample_rate = 44100,
81                     const std::string device_name = "",
82                     bool do_block = true,
83                     int channel_config = -1,
84                     int max_sample_count = -1);
85
86 public:
87   ~audio_osx_source ();
88
89   bool start ();
90   bool stop ();
91   bool IsRunning ();
92
93   bool check_topology (int ninputs, int noutputs);
94
95   int work (int noutput_items,
96             gr_vector_const_void_star &input_items,
97             gr_vector_void_star &output_items);
98
99 private:
100   void SetDefaultInputDeviceAsCurrent ();
101
102   void AllocAudioBufferList (AudioBufferList** t_ABL,
103                              UInt32 n_channels,
104                              UInt32 inputBufferSizeBytes);
105
106   void FreeAudioBufferList (AudioBufferList** t_ABL);
107
108   static OSStatus ConverterCallback (AudioConverterRef inAudioConverter,
109                                      UInt32* ioNumberDataPackets,
110                                      AudioBufferList* ioData,
111                                      AudioStreamPacketDescription** outASPD,
112                                      void* inUserData);
113
114   static OSStatus AUInputCallback (void *inRefCon,
115                                    AudioUnitRenderActionFlags *ioActionFlags,
116                                    const AudioTimeStamp *inTimeStamp,
117                                    UInt32 inBusNumber,
118                                    UInt32 inNumberFrames,
119                                    AudioBufferList *ioData);
120 #if _OSX_DO_LISTENERS_
121   static OSStatus UnitListener (void *inRefCon,
122                                 AudioUnit ci,
123                                 AudioUnitPropertyID inID,
124                                 AudioUnitScope inScope,
125                                 AudioUnitElement inElement);
126
127   static OSStatus HardwareListener (AudioHardwarePropertyID inPropertyID, 
128                                     void *inClientData);
129 #endif
130 };
131
132 #endif /* INCLUDED_AUDIO_OSX_SOURCE_H */