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