-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRtAudio.m
More file actions
238 lines (206 loc) · 7.35 KB
/
RtAudio.m
File metadata and controls
238 lines (206 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
classdef RtAudio < handle
% CLASS RtAudio
%
% This class provides a uniform interface within Matlab for audio IO
% support using RtAudio. RtAudio functionality is provided via underlying
% MEX functions.
%
% Gary Scavone, McGill University, 2018-2022.
properties (SetObservable)
isValid = false;
streamIsOpen = 0;
version = 1.1;
api_ = '';
end
methods(Static)
%-----------------------------------------------------------
function apis = getAudioApis( nOutputs, nInputs )
apis = '';
if exist( ['mxGetAudioApis.' mexext], 'file' )
apis = mxGetAudioApis( nOutputs, nInputs, 0 );
else
error( 'RtAudio: mxGetAudioApis mex function not found!' );
end
end
end % static methods
methods
%-----------------------------------------------------------
function obj = RtAudio( api )
if exist( 'mxGetAudioDeviceCount', 'file' ) ...
&& exist( 'mxGetAudioApis', 'file' ) ...
&& exist( 'mxGetAudioDeviceName', 'file' ) ...
&& exist( 'mxGetAudioDeviceInfo', 'file' ) ...
&& exist( 'mxGetAudioDeviceIds', 'file' ) ...
&& exist( 'mxControlAudioStream', 'file' )
obj.isValid = true;
else
error( 'RtAudio: Some or all mex functions not found!' );
end
% Check that we have compiled APIs
apis = obj.getAudioApis(0, 0);
if isempty( apis )
error( 'RtAudio: No compiled audio APIs found!' );
end
if nargin == 0
% If no api specified, use the first one compiled
obj.api_ = char( apis( 1 ) );
else
if ischar( api )
if any( strcmp( apis, api ))
obj.api_ = char( api );
else
error( 'RtAudio: Api input argument is invalid!' );
end
else
error( 'RtAudio: Api input argument must be a string!' );
end
end
end
%-----------------------------------------------------------
function delete( obj )
if obj.streamIsOpen
mxControlAudioStream( 'close', true );
end
clear mxControlAudioStream;
end
%-----------------------------------------------------------
function list = listDevices( obj, show, api )
if nargin < 2, show = false; end
if nargin < 3, api = obj.api_; end
inputIds = obj.getAudioDeviceIds( 0, 1, api );
outputIds = obj.getAudioDeviceIds( 1, 0, api );
list = '';
for n = 1:length( outputIds )
if n == 1
list = sprintf( 'Output Devices: \n' );
end
name = sprintf( '\t%d: %s\n', n, obj.getAudioDeviceName( outputIds( n ) ) );
list = [list name]; %#ok<*AGROW>
end
for n = 1:length( inputIds )
if n == 1
list = [list sprintf( 'Input Devices: \n' ) ];
end
name = sprintf( '\t%d: %s\n', n, obj.getAudioDeviceName( inputIds( n ) ) );
list = [list name];
end
if show
fprintf( '%s', list );
end
end
%-----------------------------------------------------------
function count = getAudioDeviceCount( obj, nOutputs, nInputs, api )
if nargin < 4, api = obj.api_; end
if nargin < 3, nInputs = 0; end
count = 0;
try
count = mxGetAudioDeviceCount( nOutputs, nInputs, 0, api );
catch
error('RtAudio.m: error calling mxGetAudioDeviceCount.');
end
end
%-----------------------------------------------------------
function ids = getAudioDeviceIds( obj, nOutputs, nInputs, api )
if nargin < 4, api = obj.api_; end
ids = [];
try
ids = mxGetAudioDeviceIds( nOutputs, nInputs, 0, api );
catch
error('RtAudio.m: error calling mxGetAudioDeviceIds.');
end
end
%-----------------------------------------------------------
function name = getAudioDeviceName( obj, deviceId, api )
if nargin < 3, api = obj.api_; end
name = '';
try
name = mxGetAudioDeviceName( deviceId, api );
catch
error('RtAudio.m: error calling mxGetAudioDeviceName.');
end
end
%-----------------------------------------------------------
function info = getAudioDeviceInfo( obj, deviceId, api )
if nargin < 3, api = obj.api_; end
info = struct( 'name', [], 'outputChannels', [], ...
'inputChannels', [], 'duplexChannels', [], 'sampleRates', [] );
try
info = mxGetAudioDeviceInfo( deviceId, api );
catch
error('RtAudio.m: error calling mxGetAudioDeviceInfo.');
end
end
%-----------------------------------------------------------
function startAudioStream(obj, oChannels, iChannels, sampleRate, ...
iDuration, oSignal, nRepetitions, ...
oDevice, iDevice, triggerThreshold, ...
triggerChannel, triggerTimeout, triggerBackup, ...
oChannelOffset, iChannelOffset, nBufferFrames, api)
if obj.streamIsOpen
warning('RtAudio::startAudioStream: a stream is already open!');
return;
end
if nargin < 6, oSignal = []; end
if nargin < 7, nRepetitions = 0; end
if nargin < 8, oDevice = 0; end
if nargin < 9, iDevice = 0; end
if nargin < 10, triggerThreshold = 0; end
if nargin < 11, triggerChannel = 0; end
if nargin < 12, triggerTimeout = 0; end
if nargin < 13, triggerBackup = 0; end
if nargin < 14, oChannelOffset = 0; end
if nargin < 15, iChannelOffset = 0; end
if nargin < 16, nBufferFrames = 512; end
if nargin < 17, api = obj.api_; end
[ochans, ~] = size( oSignal );
if ochans ~= oChannels
warning('RtAudio::startAudioStream: oSignal must have oChannels rows!');
return;
end
try
mxControlAudioStream( 'start', oChannels, iChannels, sampleRate, ...
iDuration, oSignal, nRepetitions, ...
oDevice, iDevice, triggerThreshold, ...
triggerChannel, triggerTimeout, triggerBackup, ...
oChannelOffset, iChannelOffset, nBufferFrames, api );
catch ME
error(['RtAudio.m: error calling startAudioStream. ', ME.message]);
end
obj.streamIsOpen = 1;
end
%-----------------------------------------------------------
function closeAudioStream(obj, force)
if obj.streamIsOpen
if nargin < 2, force = 0; end
mxControlAudioStream( 'close', force );
obj.streamIsOpen = 0;
else
warning('RtAudio::closeAudioStream: no stream open!');
end
end
%-----------------------------------------------------------
function stopAudioStream(obj)
if obj.streamIsOpen
mxControlAudioStream( 'stop' );
else
warning('RtAudio::stopAudioStream: no stream open!');
end
end
%-----------------------------------------------------------
function restartAudioStream(obj)
if obj.streamIsOpen
mxControlAudioStream( 'restart' );
else
warning('RtAudio::restartAudioStream: no stream open!');
end
end
%-----------------------------------------------------------
function retval = getAudioData(obj, y)
if obj.streamIsOpen
retval = mxControlAudioStream( 'getData', y );
else
warning('RtAudio::getAudioData: no stream open!');
end
end
end % methods
end