Skip to content

Commit 6e0895c

Browse files
committed
fix gr-audio osx:
+ use GNU Radio preferences file to set default input and output audio device, if provided; + use gr::logger for all non-debug messages; + case-insensitive string find with desired audio device name; + fixes buffer allocation bug with low sample rates; + allows using a specific (named) audio device, or the default; + handles the case when the selected audio device becomes unavailable (e.g., a USB stick is removed while in use); + if no audio device name is provided, uses the default audio device as found in System Preferences::Sound; + handles the case when the default audio device is in use, and the user changes that audio device in System Preferences::Sound, by internally resetting to use the newly selected audio device; + all non-Apple names are now lower_case, not CamelCase; + move osx_impl functions to gr::audio::osx, and use them correctly; + install osx_impl.h to expose gr::audio::osx functions, but iff OSX audio is enabled.
1 parent ca69ec5 commit 6e0895c

12 files changed

+2648
-1141
lines changed

gr-audio/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ CPACK_COMPONENT("audio_swig"
8686
########################################################################
8787
# Add subdirectories
8888
########################################################################
89-
add_subdirectory(include/gnuradio/audio)
9089
add_subdirectory(lib)
90+
add_subdirectory(include/gnuradio/audio)
9191
add_subdirectory(doc)
9292
if(ENABLE_PYTHON)
9393
add_subdirectory(swig)

gr-audio/include/gnuradio/audio/CMakeLists.txt

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2011,2013 Free Software Foundation, Inc.
1+
# Copyright 2011,2013-2014 Free Software Foundation, Inc.
22
#
33
# This file is part of GNU Radio
44
#
@@ -20,10 +20,14 @@
2020
########################################################################
2121
# Install header files
2222
########################################################################
23-
install(FILES
24-
api.h
25-
source.h
26-
sink.h
23+
24+
SET(gr_audio_install_files api.h source.h sink.h)
25+
26+
if(OSX_AUDIO_VALID)
27+
list(APPEND gr_audio_install_files osx_impl.h)
28+
endif(OSX_AUDIO_VALID)
29+
30+
install(FILES ${gr_audio_install_files}
2731
DESTINATION ${GR_INCLUDE_DIR}/gnuradio/audio
2832
COMPONENT "audio_devel"
2933
)
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* -*- c++ -*- */
2+
/*
3+
* Copyright 2006, 2013-2014 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_IMPL_H
24+
#define INCLUDED_AUDIO_OSX_IMPL_H
25+
26+
#include <gnuradio/audio/api.h>
27+
28+
#include <iostream>
29+
#include <vector>
30+
31+
#include <string.h>
32+
33+
#include <AudioToolbox/AudioToolbox.h>
34+
#include <AudioUnit/AudioUnit.h>
35+
36+
namespace gr {
37+
namespace audio {
38+
namespace osx {
39+
40+
// Check the version of MacOSX being used
41+
#ifdef __APPLE_CC__
42+
#include <AvailabilityMacros.h>
43+
#ifndef MAC_OS_X_VERSION_10_6
44+
#define MAC_OS_X_VERSION_10_6 1060
45+
#endif
46+
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
47+
#define GR_USE_OLD_AUDIO_UNIT
48+
#endif
49+
#endif
50+
51+
// helper function to print an ASBD
52+
53+
extern std::ostream& GR_AUDIO_API
54+
operator<<
55+
(std::ostream& s,
56+
const AudioStreamBasicDescription& asbd);
57+
58+
// returns the number of channels for the provided AudioDeviceID,
59+
// input and/or output depending on if the pointer is valid.
60+
61+
extern void GR_AUDIO_API
62+
get_num_channels_for_audio_device_id
63+
(AudioDeviceID ad_id,
64+
UInt32* n_input,
65+
UInt32* n_output);
66+
67+
// search all known audio devices, input or output, for all that
68+
// match the provided device_name string (in part or in whole).
69+
// Returns a vector of all matching IDs, and another of all
70+
// matching names. If the device name is empty, then match all
71+
// input or output devices.
72+
73+
extern void GR_AUDIO_API
74+
find_audio_devices
75+
(const std::string& device_name,
76+
bool is_input,
77+
std::vector < AudioDeviceID >* all_ad_ids,
78+
std::vector < std::string >* all_names);
79+
80+
} /* namespace osx */
81+
} /* namespace audio */
82+
} /* namespace gr */
83+
84+
#endif /* INCLUDED_AUDIO_OSX_IMPL_H */

gr-audio/lib/CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ CHECK_INCLUDE_FILE_CXX(AudioToolbox/AudioToolbox.h AUDIO_TOOLBOX_H)
106106

107107
if(AUDIO_UNIT_H AND AUDIO_TOOLBOX_H)
108108

109+
set(OSX_AUDIO_VALID 1 CACHE INTERNAL "OSX Audio is valid")
110+
109111
include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/osx)
110112
list(APPEND gr_audio_libs
111113
"-framework AudioUnit"
@@ -114,10 +116,17 @@ if(AUDIO_UNIT_H AND AUDIO_TOOLBOX_H)
114116
"-framework Carbon"
115117
)
116118
list(APPEND gr_audio_sources
119+
${CMAKE_CURRENT_SOURCE_DIR}/osx/osx_impl.cc
117120
${CMAKE_CURRENT_SOURCE_DIR}/osx/osx_source.cc
118121
${CMAKE_CURRENT_SOURCE_DIR}/osx/osx_sink.cc
119122
)
120123

124+
list(APPEND gr_audio_confs ${CMAKE_CURRENT_SOURCE_DIR}/osx/gr-audio-osx.conf)
125+
126+
else(AUDIO_UNIT_H AND AUDIO_TOOLBOX_H)
127+
128+
set(OSX_AUDIO_VALID 0 CACHE INTERNAL "OSX Audio is not valid")
129+
121130
endif(AUDIO_UNIT_H AND AUDIO_TOOLBOX_H)
122131

123132
########################################################################

gr-audio/lib/osx/gr-audio-osx.conf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file contains system wide configuration data for GNU Radio.
2+
# You may override any setting on a per-user basis by editing
3+
# ~/.gnuradio/config.conf. For OSX audio only, you can use a unique
4+
# subset of the actual device name to set these variables. By this
5+
# default, the OSX audio will use whatever devices are selected in
6+
# System Preferences::Sound for input and output.
7+
8+
[audio_osx]
9+
10+
default_input_device =
11+
default_output_device =

gr-audio/lib/osx/osx_common.h

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* -*- c++ -*- */
2+
/*
3+
* Copyright 2014 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_COMMON_H
24+
#define INCLUDED_AUDIO_OSX_COMMON_H
25+
26+
#include <gnuradio/audio/osx_impl.h>
27+
28+
namespace gr {
29+
namespace audio {
30+
namespace osx {
31+
32+
#define _OSX_AU_DEBUG_ 0
33+
#define _OSX_AU_DEBUG_RENDER_ 0
34+
35+
#define check_error_and_throw(err,what,throw_str) \
36+
if(err) { \
37+
OSStatus error = static_cast<OSStatus>(err); \
38+
char err_str[5]; \
39+
*((UInt32*)err_str) = error; \
40+
err_str[4] = 0; \
41+
GR_LOG_FATAL(d_logger, boost::format(what)); \
42+
GR_LOG_FATAL(d_logger, boost::format(" Error# %u ('%s')") \
43+
% error % err_str); \
44+
GR_LOG_FATAL(d_logger, boost::format(" %s:%d") \
45+
% __FILE__ %__LINE__); \
46+
throw std::runtime_error(throw_str); \
47+
}
48+
49+
#define check_error(err,what) \
50+
if(err) { \
51+
OSStatus error = static_cast<OSStatus>(err); \
52+
char err_str[5]; \
53+
*((UInt32*)err_str) = error; \
54+
err_str[4] = 0; \
55+
GR_LOG_WARN(d_logger, boost::format(what)); \
56+
GR_LOG_WARN(d_logger, boost::format(" Error# %u ('%s')") \
57+
% error % err_str); \
58+
GR_LOG_WARN(d_logger, boost::format(" %s:%d") \
59+
% __FILE__ %__LINE__); \
60+
}
61+
62+
#include <boost/detail/endian.hpp> //BOOST_BIG_ENDIAN
63+
#ifdef BOOST_BIG_ENDIAN
64+
#define GR_PCM_ENDIANNESS kLinearPCMFormatFlagIsBigEndian
65+
#else
66+
#define GR_PCM_ENDIANNESS 0
67+
#endif
68+
69+
} /* namespace osx */
70+
} /* namespace audio */
71+
} /* namespace gr */
72+
73+
#endif /* INCLUDED_AUDIO_OSX_COMMON_H */

0 commit comments

Comments
 (0)