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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
|
/* -*- c++ -*- */
/*
* Copyright 2006-2011,2013-2014 Free Software Foundation, Inc.
*
* This file is part of GNU Radio.
*
* GNU Radio 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 3, or (at your option)
* any later version.
*
* GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "audio_registry.h"
#include "osx_source.h"
#include <gnuradio/io_signature.h>
#include <gnuradio/prefs.h>
#include <stdexcept>
namespace gr {
namespace audio {
source::sptr
osx_source_fcn(int sampling_rate,
const std::string& device_name,
bool ok_to_block)
{
return source::sptr
(new osx_source(sampling_rate, device_name, ok_to_block));
}
static std::string
default_device_name()
{
return prefs::singleton()->get_string
("audio_osx", "default_input_device", "built-in");
}
osx_source::osx_source
(int sample_rate,
const std::string& device_name,
bool ok_to_block)
: sync_block("audio_osx_source",
io_signature::make(0, 0, 0),
io_signature::make(0, 0, 0)),
d_device_sample_rate(0.0), d_output_sample_rate(0.0),
d_input_buffer_size_frames(0), d_input_buffer_size_bytes(0),
d_output_buffer_size_frames(0), d_output_buffer_size_bytes(0),
d_device_buffer_size_frames(0), d_device_buffer_size_bytes(0),
d_lead_size_frames(0), d_lead_size_bytes(0),
d_trail_size_frames(0), d_trail_size_bytes(0),
d_extra_buffer_size_frames(0), d_extra_buffer_size_bytes(0),
d_queue_sample_count(0), d_buffer_sample_count(0),
d_n_available_input_frames(0), d_n_actual_input_frames(0),
d_n_user_channels(0), d_n_dev_channels(0),
d_ok_to_block(ok_to_block), d_pass_through(false),
d_waiting_for_data(false), d_do_reset(false),
d_hardware_changed(false), d_using_default_device(false),
d_desired_name(device_name.empty() ? default_device_name()
: device_name),
d_input_ad_id(0), d_input_au(0), d_input_buffer(0),
d_output_buffer(0), d_audio_converter(0)
{
// set the desired output sample rate
if(sample_rate <= 0) {
GR_LOG_ERROR(d_logger, boost::format
("Invalid Sample Rate: %d")
% sample_rate);
throw std::invalid_argument("audio_osx_source");
}
else {
d_output_sample_rate = (Float64)sample_rate;
}
// set up for audio input using the stored desired parameters
setup();
}
void osx_source::setup()
{
OSStatus err = noErr;
// set the default input audio device id to "unknown"
d_input_ad_id = kAudioDeviceUnknown;
// try to find the input audio device, if specified
std::vector < AudioDeviceID > all_ad_ids;
std::vector < std::string > all_names;
osx::find_audio_devices
(d_desired_name, true,
&all_ad_ids, &all_names);
// check number of device(s) returned
if (d_desired_name.length() != 0) {
if (all_ad_ids.size() == 1) {
// exactly 1 match was found; see if it was partial
if (all_names[0].compare(d_desired_name) != 0) {
// yes: log the full device name
GR_LOG_INFO(d_logger, boost::format
("Using input audio device '%s'.")
% all_names[0]);
}
// store info on this device
d_input_ad_id = all_ad_ids[0];
d_selected_name = all_names[0];
} else {
// either 0 or more than 1 device was found; get all input
// device names, print those, and error out.
osx::find_audio_devices("", true, NULL, &all_names);
std::string err_str("\n\nA unique input audio device name "
"matching the string '");
err_str += d_desired_name;
err_str += "' was not found.\n\n";
err_str += "The current known input audio device name";
err_str += ((all_names.size() > 1) ? "s are" : " is");
err_str += ":\n";
for (UInt32 nn = 0; nn < all_names.size(); ++nn) {
err_str += " " + all_names[nn] + "\n";
}
GR_LOG_ERROR(d_logger, boost::format(err_str));
throw std::runtime_error("audio_osx_source::setup");
}
}
// if no input audio device id was found or no specific device
// name was provided, use the default input audio device id as
// set in System Preferences.
if (d_input_ad_id == kAudioDeviceUnknown) {
UInt32 prop_size = (UInt32)sizeof(AudioDeviceID);
AudioObjectPropertyAddress ao_address = {
kAudioHardwarePropertyDefaultInputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
err = AudioObjectGetPropertyData
(kAudioObjectSystemObject, &ao_address,
0, NULL, &prop_size, &d_input_ad_id);
check_error_and_throw
(err, "Getting the default input audio device ID failed",
"audio_osx_source::setup");
{
// retrieve the device name; max name length is 64 characters.
UInt32 prop_size = 65;
char c_name_buf[prop_size];
memset((void*)c_name_buf, 0, (size_t)prop_size);
--prop_size;
AudioObjectPropertyAddress ao_address = {
kAudioDevicePropertyDeviceName,
kAudioDevicePropertyScopeInput, 0
};
if ((err = AudioObjectGetPropertyData
(d_input_ad_id, &ao_address, 0, NULL,
&prop_size, (void*)c_name_buf)) != noErr) {
check_error(err, "Unable to retrieve input audio device name");
} else {
GR_LOG_INFO(d_logger, boost::format
("\n\nUsing input audio device '%s'.\n ... "
"which is the current default input audio"
" device.\n Changing the default input"
" audio device in the System Preferences"
" will \n result in changing it here, too "
"(with an internal reconfiguration).\n") %
std::string(c_name_buf));
}
d_selected_name = c_name_buf;
}
d_using_default_device = true;
}
// retrieve the total number of channels for the selected input
// audio device
osx::get_num_channels_for_audio_device_id
(d_input_ad_id, &d_n_dev_channels, NULL);
// set the block output signature, if not already set
// (d_n_user_channels is set in check_topology, which is called
// before the flow-graph is running)
if (d_n_user_channels == 0) {
set_output_signature(io_signature::make
(1, d_n_dev_channels, sizeof(float)));
}
// set the interim buffer size; to work with the GR scheduler,
// must be at least 16kB. Pick 50 kB since that's plenty yet
// not very much.
d_buffer_sample_count = (d_output_sample_rate < 50000.0 ?
50000 : (UInt32)d_output_sample_rate);
#if _OSX_AU_DEBUG_
std::cerr << "source(): max # samples = "
<< d_buffer_sample_count << std::endl;
#endif
// create the default AudioUnit for input
// Open the default input unit
#ifndef GR_USE_OLD_AUDIO_UNIT
AudioComponentDescription desc;
#else
ComponentDescription desc;
#endif
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_HALOutput;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
#ifndef GR_USE_OLD_AUDIO_UNIT
AudioComponent comp = AudioComponentFindNext(NULL, &desc);
if(!comp) {
GR_LOG_FATAL(d_logger, boost::format
("AudioComponentFindNext Failed"));
throw std::runtime_error("audio_osx_source::setup");
}
err = AudioComponentInstanceNew(comp, &d_input_au);
check_error_and_throw(err, "AudioComponentInstanceNew Failed",
"audio_osx_source::setup");
#else
Component comp = FindNextComponent(NULL, &desc);
if(!comp) {
GR_LOG_FATAL(d_logger, boost::format
("FindNextComponent Failed"));
throw std::runtime_error("audio_osx_source::setup");
}
err = OpenAComponent(comp, &d_input_au);
check_error_and_throw(err, "OpenAComponent Failed",
"audio_osx_source::setup");
#endif
// must enable the AUHAL for input and disable output
// before setting the AUHAL's current device
// Enable input on the AUHAL
UInt32 enable_io = 1;
err = AudioUnitSetProperty
(d_input_au,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input, 1, // input element
&enable_io, sizeof(enable_io));
check_error_and_throw
(err, "AudioUnitSetProperty Input Enable",
"audio_osx_source::setup");
// Disable output on the AUHAL
enable_io = 0;
err = AudioUnitSetProperty
(d_input_au,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Output, 0, // output element
&enable_io, sizeof(enable_io));
check_error_and_throw
(err, "AudioUnitSetProperty Output Disable",
"audio_osx_source::setup");
// set the selected device ID as the current input device
err = AudioUnitSetProperty
(d_input_au, kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global, 0,
&d_input_ad_id, sizeof(d_input_ad_id));
check_error_and_throw
(err, "Setting selected input device as current failed",
"audio_osx_source::setup");
// Set up a callback function to retrieve input from the Audio Device
AURenderCallbackStruct au_callback = {
reinterpret_cast<AURenderCallback>
(&osx_source::au_input_callback),
reinterpret_cast<void*>(this)
};
UInt32 prop_size = (UInt32)sizeof(au_callback);
err = AudioUnitSetProperty
(d_input_au,
kAudioOutputUnitProperty_SetInputCallback,
kAudioUnitScope_Global, 0,
&au_callback, prop_size);
check_error_and_throw
(err, "Set Input Callback",
"audio_osx_source::setup");
// Get the Stream Format (device side; cannot generally be changed)
prop_size = (UInt32)sizeof(d_asbd_device);
memset((void*)(&d_asbd_device), 0, (size_t)prop_size);
err = AudioUnitGetProperty
(d_input_au,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, 1,
&d_asbd_device, &prop_size);
check_error_and_throw
(err, "Get Device Input Stream Format (before) failed",
"audio_osx_source::setup");
#if _OSX_AU_DEBUG_
std::cerr << std::endl << "---- Device Stream Format (before) ----"
<< std::endl << d_asbd_device << std::endl << std::endl;
#endif
// try to set the device (input) side of the audio device to the
// sample rate of this source. This will likely fail, and
// that's OK; just ignore the error since we can accomplish
// audio input in other ways.
prop_size = (UInt32)sizeof(d_asbd_device);
d_asbd_device.mSampleRate = d_output_sample_rate;
err = AudioUnitSetProperty
(d_input_au,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, 1,
&d_asbd_device, prop_size);
#if _OSX_AU_DEBUG_
check_error
(err, "Set Device Input Stream Format failed (expected)");
#endif
memset((void*)(&d_asbd_device), 0, (size_t)prop_size);
err = AudioUnitGetProperty
(d_input_au,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, 1,
&d_asbd_device, &prop_size);
check_error_and_throw
(err, "Get Device Input Stream Format (after) failed",
"audio_osx_source::setup");
#if _OSX_AU_DEBUG_
std::cerr << std::endl << "---- Device Stream Format (after) ----"
<< std::endl << d_asbd_device << std::endl << std::endl;
#endif
d_device_sample_rate = d_asbd_device.mSampleRate;
// Get the Stream Format (client side; might be changeable)
prop_size = (UInt32)sizeof(d_asbd_client);
memset((void*)(&d_asbd_client), 0, (size_t)prop_size);
err = AudioUnitGetProperty
(d_input_au,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output, 1,
&d_asbd_client, &prop_size);
check_error_and_throw
(err, "Get Device Output Stream Format (before) failed",
"audio_osx_source::setup");
#if _OSX_AU_DEBUG_
std::cerr << "---- Client Stream Format (Before) ----"
<< std::endl << d_asbd_client << std::endl << std::endl;
#endif
// Set the format of all the AUs to the
// input/output devices channel count
d_asbd_client.mFormatID = kAudioFormatLinearPCM;
d_asbd_client.mFormatFlags = (kAudioFormatFlagIsFloat |
kAudioFormatFlagIsPacked |
kAudioFormatFlagIsNonInterleaved);
if((d_asbd_client.mFormatID == kAudioFormatLinearPCM) &&
(d_n_dev_channels == 1)) {
d_asbd_client.mFormatFlags &= ~kLinearPCMFormatFlagIsNonInterleaved;
}
d_asbd_client.mBytesPerFrame = (UInt32)sizeof(float);
d_asbd_client.mFramesPerPacket = 1;
d_asbd_client.mBitsPerChannel = d_asbd_client.mBytesPerFrame * 8;
d_asbd_client.mChannelsPerFrame = d_n_dev_channels;
d_asbd_client.mBytesPerPacket = d_asbd_client.mBytesPerFrame;
// according to Apple docs [see, e.g., Apple Technical Note
// TN2091 "Device input using the HAL Output Audio Unit"], the
// device input and output sample rate must be the same; do
// sample rate conversion elsewhere.
d_asbd_client.mSampleRate = d_asbd_device.mSampleRate;
err = AudioUnitSetProperty
(d_input_au,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output, 1,
&d_asbd_client, prop_size);
check_error_and_throw
(err, "Set Device Ouput Stream Format failed",
"audio_osx_source::setup");
// Get the Stream Format (client side), again
prop_size = (UInt32)sizeof(d_asbd_client);
memset((void*)(&d_asbd_client), 0, (size_t)prop_size);
err = AudioUnitGetProperty
(d_input_au,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output, 1,
&d_asbd_client, &prop_size);
check_error_and_throw
(err, "Get Device Output Stream Format (after) failed",
"audio_osx_source::setup");
#if _OSX_AU_DEBUG_
std::cerr << "---- Client Stream Format (After) ----"
<< std::endl << d_asbd_client << std::endl << std::endl;
#endif
d_pass_through = (d_asbd_client.mSampleRate == d_output_sample_rate);
if (d_pass_through) {
// no need to do conversion if d_asbd_client matches user wants
d_lead_size_frames = d_trail_size_frames = 0L;
}
else {
// create an ASBD for the user's wants
memset((void*)(&d_asbd_user), 0, sizeof(d_asbd_user));
d_asbd_user.mSampleRate = d_output_sample_rate;
d_asbd_user.mFormatID = kAudioFormatLinearPCM;
d_asbd_user.mFormatFlags = (kLinearPCMFormatFlagIsFloat |
GR_PCM_ENDIANNESS |
kLinearPCMFormatFlagIsPacked |
kAudioFormatFlagIsNonInterleaved);
d_asbd_user.mBytesPerPacket = (UInt32)sizeof(float);
d_asbd_user.mFramesPerPacket = 1;
d_asbd_user.mBytesPerFrame = d_asbd_user.mBytesPerPacket;
d_asbd_user.mChannelsPerFrame = d_n_dev_channels;
d_asbd_user.mBitsPerChannel = d_asbd_user.mBytesPerPacket * 8;
// Create the audio converter
err = AudioConverterNew(&d_asbd_client,
&d_asbd_user,
&d_audio_converter);
check_error_and_throw
(err, "AudioConverterNew failed",
"audio_osx_source::setup");
// Set the audio converter sample rate quality to "max" ...
// requires more samples, but should sound nicer
UInt32 ac_quality = kAudioConverterQuality_Max;
prop_size = (UInt32)sizeof(ac_quality);
err = AudioConverterSetProperty
(d_audio_converter,
kAudioConverterSampleRateConverterQuality,
prop_size, &ac_quality);
check_error_and_throw
(err, "Set Sample Rate Converter Quality failed",
"audio_osx_source::setup");
// set the audio converter's prime method to "pre",
// which uses both leading and trailing frames
// from the "current input". All of this is handled
// internally by the AudioConverter; we just supply
// the frames for conversion.
UInt32 ac_prime_method = kConverterPrimeMethod_Pre;
prop_size = (UInt32)sizeof(ac_prime_method);
err = AudioConverterSetProperty
(d_audio_converter,
kAudioConverterPrimeMethod,
prop_size, &ac_prime_method);
check_error_and_throw
(err, "Set Prime Method failed",
"audio_osx_source::setup");
// Get the size of the priming I/O buffer space to allow for
// pre-allocated buffers
AudioConverterPrimeInfo ac_prime_info = {0, 0};
prop_size = (UInt32)sizeof(ac_prime_info);
err = AudioConverterGetProperty
(d_audio_converter,
kAudioConverterPrimeInfo,
&prop_size, &ac_prime_info);
check_error_and_throw
(err, "Get Prime Info failed",
"audio_osx_source::setup");
d_lead_size_frames = ac_prime_info.leadingFrames;
d_trail_size_frames = ac_prime_info.trailingFrames;
}
d_lead_size_bytes = d_lead_size_frames * sizeof(float);
d_trail_size_bytes = d_trail_size_frames * sizeof(float);
prop_size = (UInt32)sizeof(d_device_buffer_size_frames);
err = AudioUnitGetProperty
(d_input_au,
kAudioDevicePropertyBufferFrameSize,
kAudioUnitScope_Global, 0,
&d_device_buffer_size_frames, &prop_size);
check_error_and_throw
(err, "Get Buffer Frame Size failed",
"audio_osx_source::setup");
d_device_buffer_size_bytes = (d_device_buffer_size_frames *
sizeof(float));
d_input_buffer_size_bytes = (d_device_buffer_size_bytes +
d_lead_size_bytes);
d_input_buffer_size_frames = (d_device_buffer_size_frames +
d_lead_size_frames);
// outBufSizeBytes = floor (inBufSizeBytes * rate_out / rate_in)
// since this is rarely exact, we need another buffer to hold
// "extra" samples not processed at any given sampling period
// this buffer must be at least 4 floats in size, but generally
// follows the rule that
// extraBufSize = ceil (rate_in / rate_out)*sizeof(float)
d_extra_buffer_size_frames =
((UInt32)ceil(d_device_sample_rate /
d_output_sample_rate) *
sizeof(float));
if(d_extra_buffer_size_frames < 4)
d_extra_buffer_size_frames = 4;
d_extra_buffer_size_bytes =
d_extra_buffer_size_frames * sizeof(float);
d_output_buffer_size_frames =
(UInt32)ceil(((Float64)d_input_buffer_size_frames) *
d_output_sample_rate / d_device_sample_rate);
d_output_buffer_size_bytes =
d_output_buffer_size_frames * sizeof(float);
d_input_buffer_size_frames += d_extra_buffer_size_frames;
// pre-alloc all CoreAudio buffers
alloc_audio_buffer_list
(&d_input_buffer, d_n_dev_channels,
d_input_buffer_size_bytes);
if(!d_pass_through) {
alloc_audio_buffer_list
(&d_output_buffer, d_n_dev_channels,
d_output_buffer_size_bytes);
}
else {
d_output_buffer = d_input_buffer;
}
// allocate the output circular buffer(s), one per device
// channel (the user may select fewer channels; those buffers
// just won't get used).
d_buffers.resize(d_n_dev_channels);
for(UInt32 nn = 0; nn < d_n_dev_channels; ++nn) {
d_buffers[nn] = new circular_buffer<float>
(d_buffer_sample_count, false, false);
}
// clear the RunLoop (whatever that is); needed, for some
// reason, before a listener will work.
{
CFRunLoopRef the_run_loop = NULL;
AudioObjectPropertyAddress property = {
kAudioHardwarePropertyRunLoop,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
prop_size = (UInt32)sizeof(the_run_loop);
err = AudioObjectSetPropertyData
(kAudioObjectSystemObject, &property, 0, NULL,
prop_size, &the_run_loop);
check_error(err, "Clearing RunLoop failed; "
"Audio Input Device Listener might not work.");
}
// set up listeners
#ifndef GR_USE_OLD_AUDIO_UNIT
// 10.4 and newer
{
// set up a listener if hardware changes (at all)
AudioObjectPropertyAddress property = {
kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
err = AudioObjectAddPropertyListener
(kAudioObjectSystemObject, &property,
reinterpret_cast<AudioObjectPropertyListenerProc>
(&osx_source::hardware_listener),
reinterpret_cast<void*>(this));
check_error(err, "Adding Audio Hardware Listener failed");
}
if (d_using_default_device) {
// set up a listener if default hardware input device changes
AudioObjectPropertyAddress property = {
kAudioHardwarePropertyDefaultInputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
err = AudioObjectAddPropertyListener
(kAudioObjectSystemObject, &property,
reinterpret_cast<AudioObjectPropertyListenerProc>
(&osx_source::default_listener),
reinterpret_cast<void*>(this));
check_error(err, "Adding Default Input Audio Listener failed");
}
#else
// 10.5 and older
err = AudioHardwareAddPropertyListener
(kAudioHardwarePropertyDevices,
reinterpret_cast<AudioHardwarePropertyListenerProc>
(&osx_source::hardware_listener),
reinterpret_cast<void*>(this));
check_error(err, "Adding Audio Hardware Listener failed");
if (d_using_default_device) {
err = AudioHardwareAddPropertyListener
(kAudioHardwarePropertyDefaultInputDevice,
reinterpret_cast<AudioHardwarePropertyListenerProc>
(&osx_source::default_listener),
reinterpret_cast<void*>(this));
check_error(err, "Adding Default Input Audio Listener failed");
}
#endif
// initialize the AU for input, so that it is ready to be used
err = AudioUnitInitialize(d_input_au);
check_error_and_throw
(err, "AudioUnitInitialize",
"audio_osx_source::check_channels");
#if _OSX_AU_DEBUG_
std::cerr << std::endl << "audio_osx_source Parameters:"
<< std::endl << " Device Sample Rate is "
<< d_device_sample_rate << std::endl
<< " Client Sample Rate is "
<< (d_pass_through ? d_output_sample_rate :
d_device_sample_rate) << std::endl
<< " User Sample Rate is "
<< d_output_sample_rate << std::endl
<< " Do Passthrough is "
<< (d_pass_through ? "true" : "false") << std::endl
<< " Max Sample Count is "
<< d_buffer_sample_count << std::endl
<< " # Device Channels is "
<< d_n_dev_channels << std::endl
<< " Device Buffer Size in Frames = "
<< d_device_buffer_size_frames << std::endl
<< " Lead Size in Frames = "
<< d_lead_size_frames << std::endl
<< " Trail Size in Frames = "
<< d_trail_size_frames << std::endl
<< " Input Buffer Size in Frames = "
<< d_input_buffer_size_frames << std::endl
<< " Output Buffer Size in Frames = "
<< d_output_buffer_size_frames << std::endl
<< std::endl;
#endif
}
void
osx_source::alloc_audio_buffer_list
(AudioBufferList** t_abl,
UInt32 n_channels,
UInt32 input_buffer_size_bytes)
{
free_audio_buffer_list(t_abl);
UInt32 prop_size = (offsetof(AudioBufferList, mBuffers[0]) +
(sizeof(AudioBuffer) * n_channels));
*t_abl = (AudioBufferList*)calloc(1, prop_size);
(*t_abl)->mNumberBuffers = n_channels;
int counter = n_channels;
#if _OSX_AU_DEBUG_
std::cerr << "alloc_audio_buffer_list: (#chan, #bytes) == ("
<< n_channels << ", " << input_buffer_size_bytes
<< ")" << std::endl;
#endif
while(--counter >= 0) {
AudioBuffer* t_ab = &((*t_abl)->mBuffers[counter]);
t_ab->mNumberChannels = 1;
t_ab->mDataByteSize = input_buffer_size_bytes;
t_ab->mData = calloc(1, input_buffer_size_bytes);
}
}
void
osx_source::free_audio_buffer_list(AudioBufferList** t_abl)
{
// free pre-allocated audio buffer, if it exists
if(*t_abl) {
int counter = (*t_abl)->mNumberBuffers;
while(--counter >= 0) {
AudioBuffer* t_ab = &((*t_abl)->mBuffers[counter]);
free(t_ab->mData);
t_ab->mData = 0;
}
free(*t_abl);
(*t_abl) = 0;
}
}
bool
osx_source::is_running()
{
UInt32 au_running = 0;
if (d_input_au) {
UInt32 prop_size = (UInt32)sizeof(au_running);
OSStatus err = AudioUnitGetProperty
(d_input_au,
kAudioOutputUnitProperty_IsRunning,
kAudioUnitScope_Global, 0,
&au_running, &prop_size);
check_error_and_throw
(err, "AudioUnitGetProperty IsRunning",
"audio_osx_source::is_running");
}
return(au_running != 0);
}
bool
osx_source::start()
{
#if _OSX_AU_DEBUG_
std::cerr << ((void*)(pthread_self()))
<< " : audio_osx_source::start: Starting." << std::endl;
#endif
if((!is_running ()) && d_input_au) {
#if _OSX_AU_DEBUG_
std::cerr << ((void*)(pthread_self()))
<< " : audio_osx_source::start: Starting Audio Unit."
<< std::endl;
#endif
// reset buffers before starting
for(UInt32 nn = 0; nn < d_buffers.size(); ++nn) {
d_buffers[nn]->reset();
}
// start the audio unit
OSStatus err = AudioOutputUnitStart(d_input_au);
check_error_and_throw(err, "AudioOutputUnitStart",
"audio_osx_source::start");
// clear reset (will sometimes be necessary, and it has to
// happen after AudioOutputUnitStart)
d_do_reset = false;
}
#if _OSX_AU_DEBUG_
std::cerr << ((void*)(pthread_self()))
<< " : audio_osx_source::start: Returning." << std::endl;
#endif
return (true);
}
bool
osx_source::stop()
{
#if _OSX_AU_DEBUG_
std::cerr << ((void*)(pthread_self()))
<< " : audio_osx_source::stop: Starting." << std::endl;
#endif
if(is_running ()) {
#if _OSX_AU_DEBUG_
std::cerr << ((void*)(pthread_self()))
<< " : audio_osx_source::stop: stopping audio unit."
<< std::endl;
#endif
// stop the audio unit
OSStatus err = AudioOutputUnitStop(d_input_au);
check_error_and_throw(err, "AudioOutputUnitStart",
"audio_osx_source::stop");
// abort all buffers
for(UInt32 nn = 0; nn < d_n_user_channels; ++nn) {
d_buffers[nn]->abort ();
}
}
#if _OSX_AU_DEBUG_
std::cerr << ((void*)(pthread_self()))
<< " : audio_osx_source::stop: Returning." << std::endl;
#endif
return (true);
}
void
osx_source::teardown()
{
#if _OSX_AU_DEBUG_
std::cerr << ((void*)(pthread_self()))
<< " : audio_osx_source::teardown: Starting." << std::endl;
#endif
OSStatus err = noErr;
// stop the AudioUnit
stop();
// remove the listeners
#ifndef GR_USE_OLD_AUDIO_UNIT
// 10.4 and newer
{
AudioObjectPropertyAddress property = {
kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
err = AudioObjectRemovePropertyListener
(kAudioObjectSystemObject, &property,
reinterpret_cast<AudioObjectPropertyListenerProc>
(&osx_source::hardware_listener),
reinterpret_cast<void*>(this));
#if _OSX_AU_DEBUG_
check_error(err, "teardown: AudioObjectRemovePropertyListener "
"hardware failed");
#endif
}
if (d_using_default_device) {
AudioObjectPropertyAddress property = {
kAudioHardwarePropertyDefaultInputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
err = AudioObjectRemovePropertyListener
(kAudioObjectSystemObject, &property,
reinterpret_cast<AudioObjectPropertyListenerProc>
(&osx_source::default_listener),
reinterpret_cast<void*>(this));
#if _OSX_AU_DEBUG_
check_error(err, "AudioObjectRemovePropertyListener default");
#endif
d_using_default_device = false;
}
#else
// 10.5 and older
err = AudioHardwareRemovePropertyListener
(kAudioHardwarePropertyDevices,
reinterpret_cast<AudioHardwarePropertyListenerProc>
(&osx_source::hardware_listener));
#if _OSX_AU_DEBUG_
check_error(err, "AudioObjectRemovePropertyListener hardware");
#endif
if (d_using_default_device) {
err = AudioHardwareRemovePropertyListener
(kAudioHardwarePropertyDefaultInputDevice,
reinterpret_cast<AudioHardwarePropertyListenerProc>
(&osx_source::default_listener));
#if _OSX_AU_DEBUG_
check_error(err, "AudioObjectRemovePropertyListener default");
#endif
d_using_default_device = false;
}
#endif // GR_USE_OLD_AUDIO_UNIT
// free pre-allocated audio buffers
free_audio_buffer_list(&d_input_buffer);
if(!d_pass_through) {
err = AudioConverterDispose(d_audio_converter);
#if _OSX_AU_DEBUG_
check_error(err, "~audio_osx_source: AudioConverterDispose");
#endif
free_audio_buffer_list(&d_output_buffer);
}
// remove the audio unit
err = AudioUnitUninitialize(d_input_au);
#if _OSX_AU_DEBUG_
check_error(err, "~audio_osx_source: AudioUnitUninitialize");
#endif
#ifndef GR_USE_OLD_AUDIO_UNIT
err = AudioComponentInstanceDispose(d_input_au);
#if _OSX_AU_DEBUG_
check_error(err, "~audio_osx_source: AudioComponentInstanceDispose");
#endif
#else
err = CloseComponent(d_input_au);
#if _OSX_AU_DEBUG_
check_error(err, "~audio_osx_source: CloseComponent");
#endif
#endif
// empty and delete the queues
for(UInt32 nn = 0; nn < d_buffers.size(); ++nn) {
delete d_buffers[nn];
d_buffers[nn] = 0;
}
d_buffers.resize(0);
// clear important variables; not # user channels
d_queue_sample_count = 0;
d_device_sample_rate = 0;
d_n_dev_channels = 0;
d_input_ad_id = 0;
d_input_au = 0;
d_input_buffer = d_output_buffer = 0;
d_audio_converter = 0;
d_using_default_device = false;
#if _OSX_AU_DEBUG_
std::cerr << ((void*)(pthread_self()))
<< " : audio_osx_source::teardown: Returning." << std::endl;
#endif
}
bool
osx_source::check_topology(int ninputs, int noutputs)
{
// check # inputs to make sure it's valid
if(ninputs != 0) {
GR_LOG_FATAL(d_logger, boost::format
("check_topology(): number of input "
"streams provided (%d) should be 0.")
% ninputs);
throw std::runtime_error
("audio_osx_source::check_topology");
}
// check # outputs to make sure it's valid
if((noutputs < 1) | (noutputs > (int) d_n_dev_channels)) {
GR_LOG_FATAL(d_logger, boost::format
("check_topology(): number of output "
"streams provided (%d) should be in [1,%d] "
"for the selected input audio device.")
% noutputs % d_n_dev_channels);
throw std::runtime_error
("audio_osx_source::check_topology");
}
// save the actual number of output (user) channels
d_n_user_channels = noutputs;
#if _OSX_AU_DEBUG_
std::cerr << "chk_topo: Actual # user output channels = "
<< noutputs << std::endl;
#endif
return(true);
}
int
osx_source::work(int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
#if _OSX_AU_DEBUG_RENDER_
std::cerr << ((void*)(pthread_self()))
<< " : audio_osx_source::work: Starting." << std::endl;
#endif
if (d_do_reset) {
if (d_hardware_changed) {
// see if the current AudioDeviceID is still available
std::vector < AudioDeviceID > all_ad_ids;
osx::find_audio_devices
(d_desired_name, true,
&all_ad_ids, NULL);
bool found = false;
for (UInt32 nn = 0; (nn < all_ad_ids.size()) && (!found);
++nn) {
found = (all_ad_ids[nn] == d_input_ad_id);
}
if (!found) {
GR_LOG_FATAL(d_logger, boost::format
("The selected input audio device ('%s') "
"is no longer available.\n")
% d_selected_name);
return(gr::block::WORK_DONE);
}
d_do_reset = d_hardware_changed = false;
} else {
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "audio_osx_source::work: doing reset."
<< std::endl;
#endif
GR_LOG_WARN(d_logger, boost::format
("\n\nThe default input audio device has "
"changed; resetting audio.\nThere may "
"be a sound glitch while resetting.\n"));
// for any changes, just tear down the current
// configuration, then set it up again using the user's
// parameters to try to make selections.
teardown();
gr::thread::scoped_lock l(d_internal);
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "audio_osx_source::work: mutex locked."
<< std::endl;
#endif
setup();
start();
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "audio_osx_source::work: returning after reset."
<< std::endl;
#endif
return(0);
}
}
gr::thread::scoped_lock l(d_internal);
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "audio_osx_source::work: mutex locked." << std::endl;
#endif
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "work1: SC = " << d_queue_sample_count
<< ", #OI = " << noutput_items
<< ", #Chan = " << output_items.size() << std::endl;
#endif
// set the actual # of output items to the 'desired' amount then
// verify that data is available; if not enough data is
// available, either wait until it is (is "ok_to_block" is
// true), return (0) is no data is available and "ok_to_block"
// is false, or process the actual amount of available data.
UInt32 actual_noutput_items = noutput_items;
if(d_queue_sample_count < actual_noutput_items) {
if(d_queue_sample_count == 0) {
// no data; ok_to_block decides what to do
if(d_ok_to_block == true) {
// block until there is data to return, or on reset
while(d_queue_sample_count == 0) {
// release control so-as to allow data to be retrieved;
// block until there is data to return
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "audio_osx_source::work: waiting."
<< std::endl;
#endif
d_waiting_for_data = true;
d_cond_data.wait(l);
d_waiting_for_data = false;
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "audio_osx_source::work: done waiting."
<< std::endl;
#endif
// the condition's 'notify' was called; acquire control to
// keep thread safe
// if doing a reset, just return here; reset will pick
// up the next time this method is called.
if (d_do_reset) {
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "audio_osx_source::work: "
"returning for reset." << std::endl;
#endif
return(0);
}
}
}
else {
// no data & not blocking; return nothing
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "audio_osx_source::work: no data "
"& not blocking; returning 0." << std::endl;
#endif
return (0);
}
}
// use the actual amount of available data
actual_noutput_items = d_queue_sample_count;
}
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "audio_osx_source::work: copying "
<< actual_noutput_items << " items per channel"
<< std::endl;
#endif
// number of channels
int l_counter = (int)output_items.size();
// copy the items from the circular buffer(s) to 'work's output
// buffers; verify that the number copied out is as expected.
while(--l_counter >= 0) {
size_t t_n_output_items = actual_noutput_items;
d_buffers[l_counter]->dequeue
((float*)output_items[l_counter],
&t_n_output_items);
if(t_n_output_items != actual_noutput_items) {
GR_LOG_FATAL(d_logger, boost::format
("work(): ERROR: number of available "
"items changing unexpectedly; expecting %d"
", got %d.")
% actual_noutput_items % t_n_output_items);
throw std::runtime_error("audio_osx_source::work()");
}
}
// subtract the actual number of items removed from the buffer(s)
// from the local accounting of the number of available samples
d_queue_sample_count -= actual_noutput_items;
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "work2: SC = " << d_queue_sample_count
<< ", act#OI = " << actual_noutput_items << std::endl
<< "Returning." << std::endl;
#endif
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "audio_osx_source::work: returning." << std::endl;
#endif
return (actual_noutput_items);
}
OSStatus
osx_source::converter_callback
(AudioConverterRef in_audio_converter,
UInt32* io_number_data_packets,
AudioBufferList* io_data,
AudioStreamPacketDescription** out_aspd,
void* in_user_data)
{
// This callback is for us to provide the buffers to CoreAudio
// for conversion. We need to set the buffers in the provided
// buffer list (io_data) to the buffers we know about and use to
// do data input (d_input_buffers).
osx_source* This = static_cast<osx_source*>(in_user_data);
AudioBufferList* l_input_abl = This->d_input_buffer;
UInt32 total_input_buffer_size_bytes =
((*io_number_data_packets) * sizeof(float));
int counter = This->d_n_dev_channels;
io_data->mNumberBuffers = This->d_n_dev_channels;
This->d_n_actual_input_frames = (*io_number_data_packets);
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "cc1: io#DP = " << (*io_number_data_packets)
<< ", TIBSB = " << total_input_buffer_size_bytes
<< ", #C = " << counter << std::endl;
#endif
while(--counter >= 0) {
AudioBuffer* t_ab = &(io_data->mBuffers[counter]);
t_ab->mNumberChannels = 1;
t_ab->mData = l_input_abl->mBuffers[counter].mData;
t_ab->mDataByteSize = total_input_buffer_size_bytes;
}
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "cc2: Returning." << std::endl;
#endif
return (noErr);
}
OSStatus
osx_source::au_input_callback
(void *in_ref_con,
AudioUnitRenderActionFlags *io_action_flags,
const AudioTimeStamp *in_time_stamp,
UInt32 in_bus_number,
UInt32 in_number_frames,
AudioBufferList *io_data)
{
#if _OSX_AU_DEBUG_RENDER_
std::cerr << ((void*)(pthread_self()))
<< " : audio_osx_source::au_input_callback: Starting."
<< std::endl;
#endif
osx_source* This = reinterpret_cast
<osx_source*>(in_ref_con);
gr::thread::scoped_lock l(This->d_internal);
gr::logger_ptr d_logger = This->d_logger;
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "audio_osx_source::au_input_callback: mutex locked."
<< std::endl;
#endif
OSStatus err = noErr;
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "cb0: in#Fr = " << in_number_frames
<< ", inBus# = " << in_bus_number
<< ", idD = " << ((void*)io_data)
<< ", d_ib = " << ((void*)(This->d_input_buffer))
<< ", d_ib#c = " << This->d_input_buffer->mNumberBuffers
<< ", SC = " << This->d_queue_sample_count << std::endl;
#endif
if (This->d_do_reset) {
// clear audio data; do not render since it will generate an error
AudioBufferList* t_abl = This->d_input_buffer;
for (UInt32 nn = 0; nn < t_abl->mNumberBuffers; ++nn) {
AudioBuffer* t_ab = &(t_abl->mBuffers[nn]);
memset(t_ab->mData, 0, (size_t)((t_ab->mDataByteSize) *
(t_ab->mNumberChannels)));
}
} else {
// Get the new audio data from the input device
err = AudioUnitRender
(This->d_input_au, io_action_flags,
in_time_stamp, 1, //inBusNumber,
in_number_frames, This->d_input_buffer);
check_error_and_throw
(err, "AudioUnitRender",
"audio_osx_source::au_input_callback");
}
UInt32 available_input_frames =
This->d_n_available_input_frames = in_number_frames;
// get the number of actual output frames,
// either via converting the buffer or not
UInt32 actual_output_frames = available_input_frames;
if(!This->d_pass_through) {
UInt32 available_input_bytes =
available_input_frames * sizeof(float);
UInt32 available_output_bytes = available_input_bytes;
UInt32 prop_size = sizeof(available_output_bytes);
err = AudioConverterGetProperty
(This->d_audio_converter,
kAudioConverterPropertyCalculateOutputBufferSize,
&prop_size,
&available_output_bytes);
check_error_and_throw
(err, "Get Output Buffer Size failed",
"audio_osx_source::au_input_callback");
UInt32 available_output_frames =
available_output_bytes / sizeof(float);
#if 0
// when decimating too much, the output sounds warbly due to
// fluctuating # of output frames
// This should not be a surprise, but there's probably some
// clever programming that could lessed the effect ...
// like finding the "ideal" # of output frames, and keeping
// that number constant no matter the # of input frames
UInt32 l_input_bytes = available_output_bytes;
prop_size = sizeof(available_output_bytes);
err = AudioConverterGetProperty
(This->d_audio_converter,
kAudioConverterPropertyCalculateInputBufferSize,
&prop_size, &l_input_bytes);
check_error_and_throw
(err, "Get Input Buffer Size failed",
"audio_osx_source::au_input_callback");
if(l_input_bytes < available_input_bytes) {
// OK to zero pad the input a little
++available_output_frames;
available_output_bytes = available_output_frames * sizeof(float);
}
#endif
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "cb1: avail: #IF = " << available_input_frames
<< ", #OF = " << available_output_frames << std::endl;
#endif
actual_output_frames = available_output_frames;
// convert the data to the correct rate; on input,
// actual_output_frames is the number of available output frames
err = AudioConverterFillComplexBuffer
(This->d_audio_converter,
reinterpret_cast<AudioConverterComplexInputDataProc>
(&(This->converter_callback)),
in_ref_con,
&actual_output_frames,
This->d_output_buffer,
NULL);
check_error_and_throw
(err, "AudioConverterFillComplexBuffer failed",
"audio_osx_source::au_input_callback");
// on output, actual_output_frames is the actual number of
// output frames
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "cb2: actual: #IF = " << This->d_n_actual_input_frames
<< ", #OF = " << actual_output_frames << std::endl;
if(This->d_n_actual_input_frames != available_input_frames)
std::cerr << "cb2.1: avail#IF = " << available_input_frames
<< ", actual#IF = " << This->d_n_actual_input_frames << std::endl;
#endif
}
// add the output frames to the buffers' queue, checking for overflow
int counter = This->d_n_user_channels;
int res = 0;
while(--counter >= 0) {
float* in_buffer = (float*)
This->d_output_buffer->mBuffers[counter].mData;
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "cb3: enqueuing audio data." << std::endl;
#endif
int l_res = This->d_buffers[counter]->enqueue
(in_buffer, actual_output_frames);
if(l_res == -1)
res = -1;
}
if(res == -1) {
// data coming in too fast
// drop oldest buffer
fputs("aO", stderr);
fflush(stderr);
// set the local number of samples available to the max
This->d_queue_sample_count =
This->d_buffers[0]->buffer_length_items();
}
else {
// keep up the local sample count
This->d_queue_sample_count += actual_output_frames;
}
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "cb4: #OI = " << actual_output_frames
<< ", #Cnt = " << This->d_queue_sample_count
<< ", mSC = " << This->d_buffer_sample_count << std::endl;
#endif
// signal that data is available, if appropraite
if (This->d_waiting_for_data) {
This->d_cond_data.notify_one();
}
#if _OSX_AU_DEBUG_RENDER_
std::cerr << "cb5: returning." << std::endl;
#endif
return(err);
}
#ifndef GR_USE_OLD_AUDIO_UNIT
OSStatus
osx_source::hardware_listener
(AudioObjectID in_object_id,
UInt32 in_num_addresses,
const AudioObjectPropertyAddress in_addresses[],
void* in_client_data)
#else
OSStatus
osx_source::hardware_listener
(AudioHardwarePropertyID in_property_id,
void* in_client_data)
#endif
{
osx_source* This = reinterpret_cast
<osx_source*>(in_client_data);
This->reset(true);
return(noErr);
}
#ifndef GR_USE_OLD_AUDIO_UNIT
OSStatus
osx_source::default_listener
(AudioObjectID in_object_id,
UInt32 in_num_addresses,
const AudioObjectPropertyAddress in_addresses[],
void* in_client_data)
#else
OSStatus
osx_source::default_listener
(AudioHardwarePropertyID in_property_id,
void* in_client_data)
#endif
{
osx_source* This = reinterpret_cast
<osx_source*>(in_client_data);
This->reset(false);
return(noErr);
}
} /* namespace audio */
} /* namespace gr */
|