-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc_rtaudio.cpp
744 lines (476 loc) · 17.8 KB
/
gc_rtaudio.cpp
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
Copyright (C) 2019 BrerDawg
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//gc_rtaudio.cpp
//v1.01 03-jun-2019 //
#include "gc_rtaudio.h"
/*
//------------------------------------------------------------------------------------------------------------------------
//EXAMPLE CODE below - generate 2 tone outputs while mixing with incoming audio, see 'main()'
//has well worked with jack, also works with pulseaudio, pulseaudio seems to pop intermittently when an input is being used (mic)
mystr tim1;
struct st_osc_params_tag //a pointer to this is stored in stuct passed to audio proc callback, see 'st_rtaud_arg_tag.usr_ptr'
{
float freq0;
float gain0;
float freq1;
float gain1;
} st_osc_params;
rtaud rta;
int dbg_cnt = 0;
double theta0 = 0;
double theta1 = 0;
double theta0_inc;
double theta1_inc;
#define twopi (M_PI * 2)
//-------------------- realtime audio proc -----------------------------
int cb_inout( void *bf_out, void *bf_in, int frames, double streamTime, RtAudioStreamStatus status, void *arg_in )
{
double dt = tim1.time_passed( tim1.ns_tim_start );
tim1.time_start( tim1.ns_tim_start );
if( !(dbg_cnt%40) )
{
printf( "cb_inout() - dt: %f, frames: %d\n", dt, frames );
}
if ( status ) std::cout << "Stream over/underflow detected." << std::endl;
float *bfin = (float *) bf_in; //depends on which 'audio_format' data type, refer: 'RtAudioFormat'
float *bfout = (float *) bf_out;
st_rtaud_arg_tag *arg = (st_rtaud_arg_tag*) arg_in;
st_osc_params_tag *usr = (st_osc_params_tag*) arg->usr_ptr;
theta0_inc = usr->freq0 * twopi / arg->srate;
theta1_inc = usr->freq1 * twopi / arg->srate;
//printf( "inout()- frames %d\n ",frames );
//printf( "inout()- arg->channels %d\n ",arg->ch_outs );
//return 0;
dbg_cnt++;
if( !(dbg_cnt%40) )
{
//printf( "cb arg: %08x\n", (unsigned int)arg );
// printf( "cb_inout() - arg.bufsiz: %d\n", arg->bufsiz );
printf( "cb_inout() - arg.ch_ins: %d\n", arg->ch_ins );
printf( "cb_inout() - arg.ch_outs: %d\n",arg->ch_outs );
printf( "cb_inout() - arg.audio_format: 0x%08x\n\n",arg->audio_format );
}
int idx_in = 0;
int idx_out = 0;
//below handles a max 2 channels of either/or input and output
for( int i = 0; i < frames; i++ )
{
float in0, in1, tone;
in0 = in1 = 0;
if ( arg->ch_ins != 0 ) in0 = 0.2 * bfin[ idx_in++ ]; //input samples?
if ( arg->ch_ins == 2 ) in1 = 0.2 * bfin[ idx_in++ ];
if ( arg->ch_outs != 0 ) //output?
{
tone = in0 + sin( theta0 );
bfout[ idx_out++ ] = tone * usr->gain0;
theta0 += theta0_inc;
if( theta0 >= twopi ) theta0 -= twopi;
}
if ( arg->ch_outs == 2 ) //2nd output?
{
tone = in1 + sin( theta1 ); //mix
bfout[ idx_out++ ] = tone * usr->gain1;
theta1 += theta1_inc;
if( theta1 >= twopi ) theta1 -= twopi;
}
}
// unsigned int *bytes = (unsigned int *) data;
// memcpy( outputBuffer, inputBuffer, *bytes );
return 0;
}
//----------------------------------------------------------------------
int rtaudio_probe()
{
vector<RtAudio::DeviceInfo> vinfo;
rta.get_avail_devices( vinfo );
for( int i = 0; i < vinfo.size(); i++ )
{
printf("---- device number: %02d -----\n", i );
printf("RtAudio::DeviceInfo.name: '%s'\n", vinfo[i].name.c_str() );
printf("RtAudio::DeviceInfo.inputChannels: %d\n", vinfo[i].inputChannels );
printf("RtAudio::DeviceInfo.outputChannels: %d\n", vinfo[i].outputChannels );
printf("RtAudio::DeviceInfo.duplexChannels: %d\n", vinfo[i].duplexChannels );
printf("RtAudio::DeviceInfo.isDefaultInput: %d\n", vinfo[i].isDefaultInput );
printf("RtAudio::DeviceInfo.isDefaultOutput: %d\n", vinfo[i].isDefaultOutput );
printf("RtAudio::DeviceInfo.preferredSampleRate: %d\n", vinfo[i].preferredSampleRate );
printf("RtAudio::DeviceInfo.nativeFormats: 0x%04x\n", (int)vinfo[i].nativeFormats );
for( int j = 0; j < vinfo[i].sampleRates.size(); j++ )
{
printf("supports samplerate: %d\n", vinfo[i].sampleRates[j] );
}
printf("----------------------------\n\n" );
}
}
int main( int argc, char *argv[] )
{
rta.verbose = 1;
rtaudio_probe(); //dump a list of devices to console
st_rtaud_arg_tag st_arg; //this is used in audio proc callback to work out chan in/out counts
RtAudio::StreamOptions options;
options.streamName = "myrt";
options.numberOfBuffers = 2; //for jack (at least) this is hard set by jack's settings and can't be changed via this parameter
options.flags = 0; //0 means interleaved, use oring options, refer 'RtAudioStreamFlags': RTAUDIO_NONINTERLEAVED, RTAUDIO_MINIMIZE_LATENCY, RTAUDIO_HOG_DEVICE, RTAUDIO_SCHEDULE_REALTIME, RTAUDIO_ALSA_USE_DEFAULT, RTAUDIO_JACK_DONT_CONNECT
// !!! when using RTAUDIO_JACK_DONT_CONNECT, you can create any number of channels, you can't do this if auto connecting as 'openStream()' will fail if there is not enough channel mating ports
options.priority = 0; //only used with flag 'RTAUDIO_SCHEDULE_REALTIME'
uint16_t device_num_in = 0; //use 0 for default device to be used
int channels_in = 2; //if auto connecting (not RTAUDIO_JACK_DONT_CONNECT), there must be enough mathing ports or 'openStream()' will fail
int first_chan_in = 0;
uint16_t device_num_out = 0; //use 0 for default device to be used
int channels_out = 2; //if auto connecting (not RTAUDIO_JACK_DONT_CONNECT), there must be enough mathing ports or 'openStream()' will fail
int first_chan_out = 0;
int srate = 48000;
uint16_t frames = 2048;
unsigned int audio_format = RTAUDIO_FLOAT32; //see below for supported format types, adj audio proc code to suit
// RTAUDIO_SINT8: 8-bit signed integer.
// RTAUDIO_SINT16: 16-bit signed integer.
// RTAUDIO_SINT24: 24-bit signed integer.
// RTAUDIO_SINT32: 32-bit signed integer.
// RTAUDIO_FLOAT32: Normalized between plus/minus 1.0.
// RTAUDIO_FLOAT64: Normalized between plus/minus 1.0.
st_osc_params.freq0 = 200; //set up some audio proc callback user params
st_osc_params.gain0 = 0.1;
st_osc_params.freq1 = 600;
st_osc_params.gain1 = 0.1;
st_arg.usr_ptr = (void*)&st_osc_params;
// --- un-comment to test each possibility ---
//if( !rta.start_stream_in( device_num_in, channels_in, first_chan_in, srate, frames, audio_format, &options, &st_arg, (void*)cb_inout ) ) //input only
//if( !rta.start_stream_out( device_num_out, channels_out, first_chan_out, srate, frames, audio_format, &options, &st_arg, (void*)cb_inout ) ) //output only
if( !rta.start_stream_duplex( device_num_in, device_num_out, channels_in, first_chan_in, channels_out, first_chan_out, srate, frames, audio_format, &options, &st_arg, (void*)cb_inout ) ) //input and output
{
printf("failed to open audio device!!!!\n" );
}
else{
// printf("audio in device %d opened, srate is: %d, framecnt: %d\n", device_num_in, rta.get_srate(), rta.get_framecnt() ); //input only
// printf("audio out device %d opened, srate is: %d, framecnt: %d\n", device_num_out, rta.get_srate(), rta.get_framecnt() ); //output only
printf("audio devices opened in: %d, out: %d, srate is: %d, framecnt: %d\n", device_num_in, device_num_out, rta.get_srate(), rta.get_framecnt() ); //(duplex)
}
printf("\npress a key\n" );
getchar(); //wait here and do nothing, audio proc will be generating audio samples
return 0;
}
//------------------------------------------------------------------------------------------------------------------------
*/
rtaud::rtaud()
{
device_num_in = -1;
device_num_out = -1;
verbose = 0;
samplerate = 0;
framecnt = 0;
adac.showWarnings( true );
}
rtaud::~rtaud()
{
if( ( device_num_in >= 0 ) || ( device_num_out >= 0 ) ) stop_stream();
}
int rtaud::get_avail_devices( vector< RtAudio::DeviceInfo> &vinfo )
{
int cnt = adac.getDeviceCount();
vinfo.clear();
if( cnt == 0 ) return 0;
for( int i = 0; i < cnt; i++ )
{
RtAudio::DeviceInfo o = adac.getDeviceInfo( i );
vinfo.push_back( o );
}
return vinfo.size();
}
int rtaud::get_srate()
{
if( ( device_num_in < 0 ) && ( device_num_out < 0 ) )
{
printf( "rtaud::get_srate() - failed, no audio device is opened\n" );
samplerate = 0;
return samplerate;
}
samplerate = adac.getStreamSampleRate();
return samplerate;
}
int rtaud::get_framecnt()
{
if( ( device_num_in < 0 ) && ( device_num_out < 0 ) )
{
printf( "rtaud::get_framecnt() - failed, no audio device is opened\n" );
return 0;
}
return framecnt;
}
//input and output
bool rtaud::start_stream_duplex( uint16_t start_in_device_num, uint16_t start_out_device_num, uint16_t channels_in, uint16_t first_chan_in, uint16_t channels_out, uint16_t first_chan_out, uint16_t srate, unsigned int frames, unsigned int audio_format, RtAudio::StreamOptions *options, st_rtaud_arg_tag *st_arg, void *cb )
{
if( verbose ) printf( "rtaud::start_stream_duplex()\n" );
if( device_num_in >= 0 )
{
printf( "rtaud::start_stream_duplex() - failed, 'device_num_in' %d stream already started\n", device_num_in );
return 0;
}
if( device_num_out >= 0 )
{
printf( "rtaud::start_stream_duplex() - failed, 'device_num_out' %d stream already started\n", device_num_out );
return 0;
}
if( channels_in == 0 )
{
printf( "rtaud::start_stream_duplex() - failed, 0 for 'channels_in' is invalid\n" );
return 0;
}
if( channels_out == 0 )
{
printf( "rtaud::start_stream_duplex() - failed, 0 for 'channels_out' is invalid\n" );
return 0;
}
if( cb == 0 )
{
printf( "rtaud::start_stream_duplex() - failed, a callback must be specified\n" );
return 0;
}
if( st_arg == 0 )
{
printf( "rtaud::start_stream_duplex() - failed, 'st_arg' is zero\n" );
return 0;
}
if ( adac.getDeviceCount() < 1 )
{
printf( "\nrtaud::start_stream_duplex() - no audio devices found\n" );
return 0;
}
if ( start_in_device_num >= adac.getDeviceCount() )
{
printf( "\nrtaud::start_stream_duplex() - 'start_in_device_num' %d not found\n", start_in_device_num );
return 0;
}
if ( start_out_device_num >= adac.getDeviceCount() )
{
printf( "\nrtaud::start_stream_duplex() - 'start_out_device_num' %d not found\n", start_out_device_num );
return 0;
}
device_num_in = start_in_device_num;
if ( start_in_device_num == 0 ) device_num_in = adac.getDefaultInputDevice();
device_num_out = start_out_device_num;
if ( start_in_device_num == 0 ) device_num_out = adac.getDefaultOutputDevice();
st_arg->device_num_in = device_num_in;
st_arg->device_num_out = device_num_out;
st_arg->srate = srate;
st_arg->ch_ins = channels_in;
st_arg->ch_outs = channels_out;
st_arg->audio_format = audio_format;
RtAudio::StreamParameters iparams, oparams;
try {
iparams.deviceId = start_in_device_num;
iparams.nChannels = channels_in;
iparams.firstChannel = first_chan_in;
oparams.deviceId = start_out_device_num;
oparams.nChannels = channels_out;
oparams.firstChannel = first_chan_out;
unsigned int srate2 = srate;
adac.openStream( &oparams, &iparams, audio_format, srate2, &frames, (RtAudioCallback)cb, (void *)st_arg, options );
st_arg->srate = srate2;
samplerate = srate2;
st_arg->framecnt = frames;
framecnt = frames;
if( verbose) printf( "rtaud::start_stream_duplex() - opened stream: srate %d, frame size: %d\n", st_arg->srate , st_arg->framecnt );
}
catch ( RtAudioError& e )
{
string s1 = e.getMessage();
printf( "rtaud::start_stream_duplex() - error while opening stream:-\n'%s'\n", s1.c_str() );
// std::cout << '\n' << e.getMessage() << '\n' << std::endl;
device_num_in = -1;
device_num_out = -1;
return 0;
}
st_arg->latency_in_bytes = adac.getStreamLatency();
st_arg->srate = samplerate = adac.getStreamSampleRate();
if( verbose) printf( "rtaud::start_stream_duplex() - stream latency %d\n", st_arg->latency_in_bytes );
try {
adac.startStream();
}
catch( RtAudioError& e )
{
string s1 = e.getMessage();
printf( "rtaud::start_stream_duplex() - error while starting stream:-\n'%s'\n", s1.c_str() );
if ( adac.isStreamOpen() ) adac.closeStream();
return 0;
}
return 1;
}
//output only, no input
bool rtaud::start_stream_out( uint16_t start_device_num, uint16_t channels_out, uint16_t first_chan, uint16_t srate, unsigned int frames, unsigned int audio_format, RtAudio::StreamOptions *options, st_rtaud_arg_tag *st_arg, void *cb )
{
if( verbose ) printf( "rtaud::start_stream_out()\n" );
if( device_num_out >= 0 )
{
printf( "rtaud::start_stream_out() - failed, 'device_num_out' %d stream already started\n", device_num_out );
return 0;
}
if( channels_out == 0 )
{
printf( "rtaud::start_stream_out() - failed, 0 for 'channels_out' is invalid\n" );
return 0;
}
if( cb == 0 )
{
printf( "rtaud::start_stream_out() - failed, a callback must be specified\n" );
return 0;
}
if( st_arg == 0 )
{
printf( "rtaud::start_stream_out() - failed, 'st_arg' is zero\n" );
return 0;
}
if ( adac.getDeviceCount() < 1 )
{
printf( "\nrtaud::start_stream_out() - no audio devices found\n" );
return 0;
}
if ( start_device_num >= adac.getDeviceCount() )
{
printf( "\nrtaud::start_stream_out() - device %d not found\n", start_device_num );
return 0;
}
device_num_out = start_device_num;
if ( start_device_num == 0 ) device_num_out = adac.getDefaultOutputDevice();;
st_arg->device_num_out = device_num_out;
st_arg->srate = srate;
st_arg->ch_outs = channels_out;
st_arg->ch_ins = 0;
st_arg->audio_format = audio_format;
RtAudio::StreamParameters params;
try {
params.deviceId = st_arg->device_num_out ;
params.nChannels = channels_out;
params.firstChannel = first_chan;
unsigned int srate2 = srate;
adac.openStream( ¶ms, 0, audio_format, srate2, &frames, (RtAudioCallback)cb, (void *)st_arg, options );
st_arg->srate = srate2;
samplerate = srate2;
st_arg->framecnt = frames;
framecnt = frames;
if( verbose) printf( "rtaud::start_stream_out() - opened stream: srate %d, frame size: %d\n", st_arg->srate , st_arg->framecnt );
}
catch ( RtAudioError& e )
{
string s1 = e.getMessage();
printf( "rtaud::start_stream_out() - error while opening stream:-\n'%s'\n", s1.c_str() );
// std::cout << '\n' << e.getMessage() << '\n' << std::endl;
device_num_out = -1;
return 0;
}
st_arg->latency_in_bytes = adac.getStreamLatency();
st_arg->srate = samplerate = adac.getStreamSampleRate();
if( verbose) printf( "rtaud::start_stream_out() - stream latency %d\n", st_arg->latency_in_bytes );
try {
adac.startStream();
}
catch( RtAudioError& e )
{
string s1 = e.getMessage();
printf( "rtaud::start_stream_out() - error while starting stream:-\n'%s'\n", s1.c_str() );
if ( adac.isStreamOpen() ) adac.closeStream();
return 0;
}
return 1;
}
//input only, no output
bool rtaud::start_stream_in( uint16_t start_device_num, uint16_t channels_in, uint16_t first_chan, uint16_t srate, unsigned int frames, unsigned int audio_format, RtAudio::StreamOptions *options, st_rtaud_arg_tag *st_arg, void *cb )
{
if( verbose ) printf( "rtaud::start_stream_in()\n" );
if( device_num_in >= 0 )
{
printf( "rtaud::start_stream_in() - failed, 'device_num_in' %d stream already started\n", device_num_out );
return 0;
}
if( channels_in == 0 )
{
printf( "rtaud::start_stream_in() - failed, 0 for 'channels_in' is invalid\n" );
return 0;
}
if( cb == 0 )
{
printf( "rtaud::start_stream_in() - failed, a callback must be specified\n" );
return 0;
}
if( st_arg == 0 )
{
printf( "rtaud::start_stream_in() - failed, 'st_arg' is zero\n" );
return 0;
}
if ( adac.getDeviceCount() < 1 )
{
printf( "\nrtaud::start_stream_in() - no audio devices found\n" );
return 0;
}
if ( start_device_num >= adac.getDeviceCount() )
{
printf( "\nrtaud::start_stream_in() - device %d not found\n", start_device_num );
return 0;
}
device_num_in = start_device_num;
if ( start_device_num == 0 ) device_num_in = adac.getDefaultInputDevice();;
st_arg->device_num_in = device_num_in;
st_arg->srate = srate;
st_arg->ch_outs = 0;
st_arg->ch_ins = channels_in;
st_arg->audio_format = audio_format;
RtAudio::StreamParameters params;
try {
params.deviceId = st_arg->device_num_in;
params.nChannels = channels_in;
params.firstChannel = first_chan;
unsigned int srate2 = srate;
adac.openStream( 0, ¶ms, audio_format, srate2, &frames, (RtAudioCallback)cb, (void *)st_arg, options );
st_arg->srate = srate2;
samplerate = srate2;
st_arg->framecnt = frames;
framecnt = frames;
if( verbose) printf( "rtaud::start_stream_in() - opened stream: srate %d, frame size: %d\n", st_arg->srate , st_arg->framecnt );
}
catch ( RtAudioError& e )
{
string s1 = e.getMessage();
printf( "rtaud::start_stream_in() - error while opening stream:-\n'%s'\n", s1.c_str() );
// std::cout << '\n' << e.getMessage() << '\n' << std::endl;
device_num_in = -1;
return 0;
}
st_arg->latency_in_bytes = adac.getStreamLatency();
st_arg->srate = samplerate = adac.getStreamSampleRate();
if( verbose) printf( "rtaud::start_stream_in() - stream latency %d\n", st_arg->latency_in_bytes );
try {
adac.startStream();
}
catch( RtAudioError& e )
{
string s1 = e.getMessage();
printf( "rtaud::start_stream_in() - error while starting stream:-\n'%s'\n", s1.c_str() );
if ( adac.isStreamOpen() ) adac.closeStream();
return 0;
}
return 1;
}
bool rtaud::stop_stream()
{
if( verbose ) printf( "rtaud::stop_stream()\n" );
if( ( device_num_in < 0 ) && ( device_num_out < 0 ) )
{
printf( "rtaud::stop_stream() - failed, already stopped\n" );
return 0;
}
adac.stopStream();
if ( adac.isStreamOpen() ) adac.closeStream();
device_num_in = -1;
device_num_out = -1;
return 0;
}