-
Notifications
You must be signed in to change notification settings - Fork 16
/
CNFA_sf.h
1977 lines (1593 loc) · 54 KB
/
CNFA_sf.h
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
//This file was automatically generated by Makefile at https://github.com/cnlohr/cnfa
//Generated from files git hash dbe638e0c72e599cab6bc71c2bf0b6da216f50e6 on Thu Jun 13 11:55:47 UTC 2024 (This is not the git hash of this file)
//Copyright <>< 2010-2020 Charles Lohr (And other authors as cited)
//CNFA is licensed under the MIT/x11, ColorChord or NewBSD Licenses. You choose.
//
// CN's Platform-agnostic, foundational sound driver subsystem.
// Easily output and input sound on a variety of platforms.
//
// Options:
// * #define CNFA_IMPLEMENTATION before this header and it will build all
// definitions in.
//
#ifndef _CNFA_H
#define _CNFA_H
//this #define is per-platform. For instance on Linux, you have ALSA, Pulse and null
#define MAX_CNFA_DRIVERS 4
struct CNFADriver;
#ifdef __cplusplus
extern "C" {
#endif
#ifdef BUILD_DLL
#ifdef WINDOWS
#define DllExport __declspec( dllexport )
#else
#define DllExport extern
#endif
#else
#define DllExport
#endif
//NOTE: Some drivers have synchronous duplex mode, other drivers will use two different callbacks. If ether is unavailable, it will be NULL.
//I.e. if `out` is null, only use in to read. If in is null, only place samples in out.
typedef void(*CNFACBType)( struct CNFADriver * sd, short * out, short * in, int framesp, int framesr );
typedef void*(CNFAInitFn)( CNFACBType cb, const char * your_name, int reqSPSPlay, int reqSPSRec, int reqChannelsPlay, int reqChannelsRec, int sugBufferSize, const char * outputSelect, const char * inputSelect, void * opaque );
struct CNFADriver
{
void (*CloseFn)( void * object );
int (*StateFn)( void * object );
CNFACBType callback;
short channelsPlay;
short channelsRec;
int spsPlay;
int spsRec;
void * opaque;
//More fields may exist on a per-sound-driver basis
};
//Accepts:
//If DriverName = 0 or empty, will try to find best driver.
//
// our_source_name is an optional argument, but on some platforms controls the name of your endpoint.
// reqSPSPlay = 44100 is guaranteed on many platforms.
// reqSPSRec = 44100 is guaranteed on many platforms.
// NOTE: Some platforms do not allow SPS play and REC to deviate from each other.
// reqChannelsRec = 1 or 2 guaranteed on many platforms.
// reqChannelsPlay = 1 or 2 guaranteedon many platforms. NOTE: Some systems require ChannelsPlay == ChannelsRec!
// sugBufferSize = No promises.
// outputSelect = No standardization, NULL is OK for default.
// inputSelect = No standardization, NULL is OK for default.
DllExport struct CNFADriver * CNFAInit( const char * driver_name, const char * your_name, CNFACBType cb, int reqSPSPlay, int reqSPSRec, int reqChannelsPlay,
int reqChannelsRec, int sugBufferSize, const char * outputSelect, const char * inputSelect, void * opaque );
DllExport int CNFAState( struct CNFADriver * cnfaobject ); //returns bitmask. 1 if mic recording, 2 if play back running, 3 if both running.
DllExport void CNFAClose( struct CNFADriver * cnfaobject );
//Called by various sound drivers. Notice priority must be greater than 0. Priority of 0 or less will not register.
//This is an internal function. Applications shouldnot call it.
void RegCNFADriver( int priority, const char * name, CNFAInitFn * fn );
#if defined(_MSC_VER) && !defined(__clang__)
#define REGISTER_CNFA( cnfadriver, priority, name, function ) \
void REGISTER##cnfadriver() { RegCNFADriver( priority, name, function ); }
#else
#define REGISTER_CNFA( cnfadriver, priority, name, function ) \
void __attribute__((constructor)) REGISTER##cnfadriver() { RegCNFADriver( priority, name, function ); }
#endif
#if defined(WINDOWS) || defined(__WINDOWS__) || defined(_WINDOWS) \
|| defined(_WIN32) || defined(_WIN64) \
|| defined(WIN32) || defined(WIN64) \
|| defined(__WIN32__) || defined(__CYGWIN__) \
|| defined(__MINGW32__) || defined(__MINGW64__) \
|| defined(__TOS_WIN__)
#define CNFA_WINDOWS 1
#elif defined( ANDROID ) || defined( __android__ ) || defined(ANDROID)
#define CNFA_ANDROID 1
#elif defined(__NetBSD__) || defined(__NetBSD) || defined(__sun) || defined(sun)
#define CNFA_SUN 1
#elif defined(__linux) || defined(__linux__) || defined(linux) || defined(__LINUX__)
#define CNFA_LINUX 1
#endif
#if defined(PULSEAUDIO)
#define CNFA_PULSE 1
#endif
#ifdef __TINYC__
#ifndef TCC
#define TCC
#endif
#endif
#ifdef CNFA_IMPLEMENTATION
//Copyright <>< 2010-2020 Charles Lohr (And other authors as cited)
//CNFA is licensed under the MIT/x11, ColorChord or NewBSD Licenses. You choose.
#ifndef _CNFA_C
#define _CNFA_C
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#if defined(_MSC_VER)
#if CNFA_WINDOWS
#ifndef strdup
#define strdup _strdup
#endif
#endif
#endif
static CNFAInitFn * CNFADrivers[MAX_CNFA_DRIVERS];
static char * CNFADriverNames[MAX_CNFA_DRIVERS];
static int CNFADriverPriorities[MAX_CNFA_DRIVERS];
void RegCNFADriver( int priority, const char * name, CNFAInitFn * fn )
{
int j;
if( priority <= 0 )
{
return;
}
printf("[CNFA] Registering Driver: %s\n", name);
for( j = MAX_CNFA_DRIVERS-1; j >= 0; j-- )
{
//Cruise along, find location to insert
if( j > 0 && ( !CNFADrivers[j-1] || CNFADriverPriorities[j-1] < priority ) )
{
CNFADrivers[j] = CNFADrivers[j-1];
CNFADriverNames[j] = CNFADriverNames[j-1];
CNFADriverPriorities[j] = CNFADriverPriorities[j-1];
}
else
{
CNFADrivers[j] = fn;
CNFADriverNames[j] = strdup( name );
CNFADriverPriorities[j] = priority;
break;
}
}
}
struct CNFADriver * CNFAInit( const char * driver_name, const char * your_name, CNFACBType cb, int reqSPSPlay, int reqSPSRec,
int reqChannelsPlay, int reqChannelsRec, int sugBufferSize, const char * outputSelect, const char * inputSelect, void * opaque)
{
#if CNFA_ANDROID
//Android can't run static-time code.
void REGISTERAndroidCNFA();
REGISTERAndroidCNFA();
#endif
int i;
struct CNFADriver * ret = 0;
int minprio = 0;
CNFAInitFn * bestinit = 0;
if( driver_name == 0 || strlen( driver_name ) == 0 )
{
//Search for a driver.
for( i = 0; i < MAX_CNFA_DRIVERS; i++ )
{
if( CNFADrivers[i] == 0 )
{
break;
}
if( CNFADriverPriorities[i] > minprio )
{
minprio = CNFADriverPriorities[i];
bestinit = CNFADrivers[i];
}
}
if( bestinit )
{
ret = (struct CNFADriver *)bestinit( cb, your_name, reqSPSPlay, reqSPSRec, reqChannelsPlay, reqChannelsRec, sugBufferSize, outputSelect, inputSelect, opaque );
}
if( ret )
{
return ret;
}
}
else
{
for( i = 0; i < MAX_CNFA_DRIVERS; i++ )
{
if( CNFADrivers[i] == 0 )
{
break;
}
if( strcmp( CNFADriverNames[i], driver_name ) == 0 )
{
return (struct CNFADriver *)CNFADrivers[i]( cb, your_name, reqSPSPlay, reqSPSRec, reqChannelsPlay, reqChannelsRec, sugBufferSize, outputSelect, inputSelect, opaque );
}
}
}
printf( "CNFA Driver not found.\n" );
return 0;
}
int CNFAState( struct CNFADriver * cnfaobject )
{
if( cnfaobject )
{
return cnfaobject->StateFn( cnfaobject );
}
return -1;
}
void CNFAClose( struct CNFADriver * cnfaobject )
{
if( cnfaobject )
{
cnfaobject->CloseFn( cnfaobject );
}
}
#endif
//Copyright 2015-2020 <>< Charles Lohr under the ColorChord License.
#include "os_generic.h"
#include <stdlib.h>
struct CNFADriverNull
{
void (*CloseFn)( void * object );
int (*StateFn)( void * object );
CNFACBType callback;
short channelsPlay;
short channelsRec;
int spsPlay;
int spsRec;
void * opaque;
};
void CloseCNFANull( void * object )
{
free( object );
}
int CNFAStateNull( void * object )
{
return 0;
}
void * InitCNFANull( CNFACBType cb, const char * your_name, int reqSPSPlay, int reqSPSRec, int reqChannelsPlay, int reqChannelsRec, int sugBufferSize, const char * outputSelect, const char * inputSelect, void * opaque )
{
struct CNFADriverNull * r = (struct CNFADriverNull *)malloc( sizeof( struct CNFADriverNull ) );
r->CloseFn = CloseCNFANull;
r->StateFn = CNFAStateNull;
r->callback = cb;
r->spsPlay = reqSPSPlay;
r->spsRec = reqSPSRec;
r->opaque = opaque;
r->channelsPlay = reqChannelsPlay;
r->channelsRec = reqChannelsRec;
return r;
}
REGISTER_CNFA( NullCNFA, 1, "NULL", InitCNFANull );
#if CNFA_WINDOWS
#include "CNFA_winmm.c"
#include <ntverp.h> // This probably won't work on pre-NT systems
#if VER_PRODUCTBUILD >= 7601
#include "CNFA_wasapi.c"
#endif
#elif CNFA_ANDROID
//Copyright 2019-2020 <>< Charles Lohr under the ColorChord License, MIT/x11 license or NewBSD Licenses.
// This was originally to be used with rawdrawandroid
#include "os_generic.h"
#include <pthread.h> //Using android threads not os_generic threads.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//based on https://github.com/android/ndk-samples/blob/master/native-audio/app/src/main/cpp/native-audio-jni.c
// for native audio
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include <android_native_app_glue.h>
#include <jni.h>
#include <native_activity.h>
struct CNFADriverAndroid
{
//Standard header - must remain.
void (*CloseFn)( void * object );
int (*StateFn)( void * object );
CNFACBType callback;
short channelsPlay;
short channelsRec;
int spsPlay;
int spsRec;
void * opaque;
int buffsz;
SLObjectItf engineObject;
SLEngineItf engineEngine;
SLRecordItf recorderRecord;
SLObjectItf recorderObject;
SLPlayItf playerPlay;
SLObjectItf playerObject;
SLObjectItf outputMixObject;
SLAndroidSimpleBufferQueueItf recorderBufferQueue;
SLAndroidSimpleBufferQueueItf playerBufferQueue;
//unsigned recorderSize;
int recorderBufferSizeBytes;
int playerBufferSizeBytes;
short * recorderBuffer;
short * playerBuffer;
};
void bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
{
struct CNFADriverAndroid * r = (struct CNFADriverAndroid*)context;
r->callback( (struct CNFADriver*)r, 0, r->recorderBuffer, 0, r->buffsz/(sizeof(short)*r->channelsRec) );
(*r->recorderBufferQueue)->Enqueue( r->recorderBufferQueue, r->recorderBuffer, r->recorderBufferSizeBytes/(r->channelsRec*sizeof(short)) );
}
void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
{
struct CNFADriverAndroid * r = (struct CNFADriverAndroid*)context;
r->callback( (struct CNFADriver*)r, r->playerBuffer, 0, r->buffsz/(sizeof(short)*r->channelsPlay), 0 );
(*r->playerBufferQueue)->Enqueue( r->playerBufferQueue, r->playerBuffer, r->playerBufferSizeBytes/(r->channelsPlay*sizeof(short)));
}
static struct CNFADriverAndroid* InitAndroidDriver( struct CNFADriverAndroid * r )
{
SLresult result;
printf( "Starting InitAndroidDriver\n" );
// create engine
result = slCreateEngine(&r->engineObject, 0, NULL, 0, NULL, NULL);
assert(SL_RESULT_SUCCESS == result);
(void)result;
// realize the engine
result = (*r->engineObject)->Realize(r->engineObject, SL_BOOLEAN_FALSE);
assert(SL_RESULT_SUCCESS == result);
(void)result;
// get the engine interface, which is needed in order to create other objects
result = (*r->engineObject)->GetInterface(r->engineObject, SL_IID_ENGINE, &r->engineEngine);
assert(SL_RESULT_SUCCESS == result);
(void)result;
///////////////////////////////////////////////////////////////////////////////////////////////////////
if( r->channelsPlay )
{
printf("create output mix");
SLDataFormat_PCM format_pcm ={
SL_DATAFORMAT_PCM,
r->channelsPlay,
r->spsPlay*1000,
SL_PCMSAMPLEFORMAT_FIXED_16,
SL_PCMSAMPLEFORMAT_FIXED_16,
(r->channelsPlay==1)?SL_SPEAKER_FRONT_CENTER:3,
SL_BYTEORDER_LITTLEENDIAN,
};
SLDataLocator_AndroidSimpleBufferQueue loc_bq_play = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
SLDataSource source = {&loc_bq_play, &format_pcm};
const SLInterfaceID ids[1] = {SL_IID_VOLUME};
const SLboolean req[1] = {SL_BOOLEAN_TRUE};
const SLInterfaceID id[1] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE};
result = (*r->engineEngine)->CreateOutputMix(r->engineEngine, &r->outputMixObject, 0, ids, req);
result = (*r->outputMixObject)->Realize(r->outputMixObject, SL_BOOLEAN_FALSE);
SLDataLocator_OutputMix loc_outmix = { SL_DATALOCATOR_OUTPUTMIX, r->outputMixObject };
SLDataSink sink;
sink.pFormat = &format_pcm;
sink.pLocator = &loc_outmix;
// create audio player
result = (*r->engineEngine)->CreateAudioPlayer(r->engineEngine, &r->playerObject, &source, &sink, 1, id, req);
if (SL_RESULT_SUCCESS != result) {
printf( "CreateAudioPlayer failed\n" );
return JNI_FALSE;
}
// realize the audio player
result = (*r->playerObject)->Realize(r->playerObject, SL_BOOLEAN_FALSE);
if (SL_RESULT_SUCCESS != result) {
printf( "AudioPlayer Realize failed: %d\n", result );
return JNI_FALSE;
}
// get the player interface
result = (*r->playerObject)->GetInterface(r->playerObject, SL_IID_PLAY, &r->playerPlay);
assert(SL_RESULT_SUCCESS == result);
(void)result;
// get the buffer queue interface
result = (*r->playerObject)->GetInterface(r->playerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &r->playerBufferQueue);
assert(SL_RESULT_SUCCESS == result);
(void)result;
// register callback on the buffer queue
result = (*r->playerBufferQueue)->RegisterCallback(r->playerBufferQueue, bqPlayerCallback, r);
assert(SL_RESULT_SUCCESS == result);
(void)result;
printf( "===================== Player init ok.\n" );
}
if( r->channelsRec )
{
// configure audio source
SLDataLocator_IODevice loc_devI = {SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT, SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
SLDataSource audioSrc = {&loc_devI, NULL};
// configure audio sink
SLDataFormat_PCM format_pcm ={
SL_DATAFORMAT_PCM,
r->channelsRec,
r->spsRec*1000,
SL_PCMSAMPLEFORMAT_FIXED_16,
SL_PCMSAMPLEFORMAT_FIXED_16,
(r->channelsRec==1)?SL_SPEAKER_FRONT_CENTER:3,
SL_BYTEORDER_LITTLEENDIAN,
};
SLDataLocator_AndroidSimpleBufferQueue loc_bq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
SLDataSink audioSnk = {&loc_bq, &format_pcm};
const SLInterfaceID id[1] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE};
const SLboolean req[1] = {SL_BOOLEAN_TRUE};
result = (*r->engineEngine)->CreateAudioRecorder(r->engineEngine, &r->recorderObject, &audioSrc, &audioSnk, 1, id, req);
if (SL_RESULT_SUCCESS != result) {
printf( "CreateAudioRecorder failed\n" );
return JNI_FALSE;
}
// realize the audio recorder
result = (*r->recorderObject)->Realize(r->recorderObject, SL_BOOLEAN_FALSE);
if (SL_RESULT_SUCCESS != result) {
printf( "AudioRecorder Realize failed: %d\n", result );
return JNI_FALSE;
}
// get the record interface
result = (*r->recorderObject)->GetInterface(r->recorderObject, SL_IID_RECORD, &r->recorderRecord);
assert(SL_RESULT_SUCCESS == result);
(void)result;
// get the buffer queue interface
result = (*r->recorderObject)->GetInterface(r->recorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &r->recorderBufferQueue);
assert(SL_RESULT_SUCCESS == result);
(void)result;
// register callback on the buffer queue
result = (*r->recorderBufferQueue)->RegisterCallback(r->recorderBufferQueue, bqRecorderCallback, r);
assert(SL_RESULT_SUCCESS == result);
(void)result;
}
if( r->playerPlay )
{
result = (*r->playerPlay)->SetPlayState(r->playerPlay, SL_PLAYSTATE_STOPPED);
assert(SL_RESULT_SUCCESS == result); (void)result;
result = (*r->playerBufferQueue)->Clear(r->playerBufferQueue);
assert(SL_RESULT_SUCCESS == result); (void)result;
r->playerBuffer = malloc( r->playerBufferSizeBytes );
memset( r->playerBuffer, 0, r->playerBufferSizeBytes );
result = (*r->playerBufferQueue)->Enqueue(r->playerBufferQueue, r->playerBuffer, r->playerBufferSizeBytes );
assert(SL_RESULT_SUCCESS == result); (void)result;
result = (*r->playerPlay)->SetPlayState(r->playerPlay, SL_PLAYSTATE_PLAYING);
assert(SL_RESULT_SUCCESS == result); (void)result;
}
if( r->recorderRecord )
{
result = (*r->recorderRecord)->SetRecordState(r->recorderRecord, SL_RECORDSTATE_STOPPED);
assert(SL_RESULT_SUCCESS == result); (void)result;
result = (*r->recorderBufferQueue)->Clear(r->recorderBufferQueue);
assert(SL_RESULT_SUCCESS == result); (void)result;
// the buffer is not valid for playback yet
r->recorderBuffer = malloc( r->recorderBufferSizeBytes );
// enqueue an empty buffer to be filled by the recorder
// (for streaming recording, we would enqueue at least 2 empty buffers to start things off)
result = (*r->recorderBufferQueue)->Enqueue(r->recorderBufferQueue, r->recorderBuffer, r->recorderBufferSizeBytes );
// the most likely other result is SL_RESULT_BUFFER_INSUFFICIENT,
// which for this code example would indicate a programming error
assert(SL_RESULT_SUCCESS == result); (void)result;
// start recording
result = (*r->recorderRecord)->SetRecordState(r->recorderRecord, SL_RECORDSTATE_RECORDING);
assert(SL_RESULT_SUCCESS == result); (void)result;
}
printf( "Complete Init Sound Android\n" );
return r;
}
int CNFAStateAndroid( void * v )
{
struct CNFADriverAndroid * soundobject = (struct CNFADriverAndroid *)v;
return ((soundobject->recorderObject)?1:0) | ((soundobject->playerObject)?2:0);
}
void CloseCNFAAndroid( void * v )
{
struct CNFADriverAndroid * r = (struct CNFADriverAndroid *)v;
// destroy audio recorder object, and invalidate all associated interfaces
if (r->recorderObject != NULL) {
(*r->recorderObject)->Destroy(r->recorderObject);
r->recorderObject = NULL;
r->recorderRecord = NULL;
r->recorderBufferQueue = NULL;
if( r->recorderBuffer ) free( r->recorderBuffer );
}
if (r->playerObject != NULL) {
(*r->playerObject)->Destroy(r->playerObject);
r->playerObject = NULL;
r->playerPlay = NULL;
r->playerBufferQueue = NULL;
if( r->playerBuffer ) free( r->playerBuffer );
}
// destroy engine object, and invalidate all associated interfaces
if (r->engineObject != NULL) {
(*r->engineObject)->Destroy(r->engineObject);
r->engineObject = NULL;
r->engineEngine = NULL;
}
}
int AndroidHasPermissions(const char* perm_name);
void AndroidRequestAppPermissions(const char * perm);
void * InitCNFAAndroid( CNFACBType cb, const char * your_name, int reqSPSPlay, int reqSPSRec, int reqChannelsPlay, int reqChannelsRec, int sugBufferSize, const char * outputSelect, const char * inputSelect, void * opaque )
{
struct CNFADriverAndroid * r = (struct CNFADriverAndroid *)malloc( sizeof( struct CNFADriverAndroid ) );
memset( r, 0, sizeof( *r) );
r->CloseFn = CloseCNFAAndroid;
r->StateFn = CNFAStateAndroid;
r->callback = cb;
r->opaque = opaque;
r->channelsPlay = reqChannelsPlay;
r->channelsRec = reqChannelsRec;
r->spsRec = reqSPSRec;
r->spsPlay = reqSPSPlay;
r->recorderBufferSizeBytes = sugBufferSize * 2 * r->channelsRec;
r->playerBufferSizeBytes = sugBufferSize * 2 * r->channelsPlay;
int hasperm = AndroidHasPermissions( "RECORD_AUDIO" );
if( !hasperm )
{
AndroidRequestAppPermissions( "RECORD_AUDIO" );
}
r->buffsz = sugBufferSize;
return InitAndroidDriver(r);
}
//Tricky: On Android, this can't actually run before main. Have to manually execute it.
REGISTER_CNFA( AndroidCNFA, 10, "ANDROID", InitCNFAAndroid );
#elif CNFA_SUN
//Copyright 2015-2020 <>< Charles Lohr under the MIT/x11, NewBSD or ColorChord License. You choose.
#include "os_generic.h"
#include <sys/audioio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
struct CNFADriverSun
{
void (*CloseFn)( void * object );
int (*StateFn)( void * object );
CNFACBType callback;
short channelsPlay;
short channelsRec;
int spsPlay;
int spsRec;
void * opaque;
char * devRec;
char * devPlay;
short * samplesRec;
short * samplesPlay;
og_thread_t threadPlay;
og_thread_t threadRec;
int bufsize;
int playback_handle;
int record_handle;
char playing;
char recording;
};
int CNFAStateSun( void * v )
{
struct CNFADriverSun * r = (struct CNFADriverSun *)v;
return ((r->playing)?2:0) | ((r->recording)?1:0);
}
void CloseCNFASun( void * v )
{
struct CNFADriverSun * r = (struct CNFADriverSun *)v;
if( r )
{
if( r->threadPlay ) OGJoinThread( r->threadPlay );
if( r->threadRec ) OGJoinThread( r->threadRec );
OGUSleep(2000);
if( r->playback_handle != -1 ) close (r->playback_handle);
if( r->record_handle != -1 ) close (r->record_handle);
OGUSleep(2000);
free( r->devRec );
free( r->devPlay );
free( r->samplesRec );
free( r->samplesPlay );
free( r );
}
}
void * RecThread( void * v )
{
struct CNFADriverSun * r = (struct CNFADriverSun *)v;
size_t nbytes = r->bufsize * (2 * r->channelsRec);
do
{
int nread = read( r->record_handle, r->samplesRec, nbytes );
if( nread < 0 )
{
fprintf( stderr, "Warning: Sun Recording Failed\n" );
break;
}
r->recording = 1;
r->callback( (struct CNFADriver *)r, NULL, r->samplesRec, 0, (nread / 2) / r->channelsRec);
} while( 1 );
r->recording = 0;
fprintf( stderr, "Sun Recording Stopped\n" );
return 0;
}
void * PlayThread( void * v )
{
struct CNFADriverSun * r = (struct CNFADriverSun *)v;
size_t nbytes = r->bufsize * (2 * r->channelsPlay);
int err;
r->callback( (struct CNFADriver *)r, r->samplesPlay, NULL, r->bufsize, 0 );
err = write( r->playback_handle, r->samplesPlay, nbytes );
while( err >= 0 )
{
r->callback( (struct CNFADriver *)r, r->samplesPlay, NULL, r->bufsize, 0 );
err = write( r->playback_handle, r->samplesPlay, nbytes );
r->playing = 1;
}
r->playing = 0;
fprintf( stderr, "Sun Playback Stopped\n" );
return 0;
}
static struct CNFADriverSun * InitSun( struct CNFADriverSun * r )
{
const char * devPlay = r->devPlay;
const char * devRec = r->devRec;
struct audio_info rinfo, pinfo;
if( devRec == NULL || strcmp ( devRec, "default" ) == 0 )
{
devRec = "/dev/audio";
}
if( devPlay == NULL || strcmp ( devPlay , "default" ) == 0 )
{
devPlay = "/dev/audio";
}
printf( "CNFA Sun Init -> devPlay: %s, channelsPlay: %d, spsPlay: %d, devRec: %s, channelsRec: %d, spsRec: %d\n", devPlay, r->channelsPlay, r->spsPlay, devRec, r->channelsRec, r->spsRec);
if( r->channelsPlay && r->channelsRec && strcmp (devPlay, devRec) == 0 )
{
if ( (r->playback_handle = r->record_handle = open (devPlay, O_RDWR)) < 0 )
{
fprintf (stderr, "cannot open audio device (%s)\n",
strerror (errno));
goto fail;
}
}
else
{
if( r->channelsPlay )
{
if ( (r->playback_handle = open (devPlay, O_WRONLY)) < 0 )
{
fprintf (stderr, "cannot open output audio device %s (%s)\n",
r->devPlay, strerror (errno));
goto fail;
}
}
if( r->channelsRec )
{
if ( (r->record_handle = open (devRec, O_RDONLY)) < 0 )
{
fprintf (stderr, "cannot open input audio device %s (%s)\n",
r->devRec, strerror (errno));
goto fail;
}
}
}
if( r->playback_handle )
{
AUDIO_INITINFO(&pinfo);
pinfo.play.precision = 16;
pinfo.play.encoding = AUDIO_ENCODING_LINEAR;
pinfo.play.sample_rate = r->spsPlay;
pinfo.play.channels = r->channelsPlay;
if ( ioctl(r->playback_handle, AUDIO_SETINFO, &pinfo) < 0 )
{
fprintf (stderr, "cannot set audio playback format (%s)\n",
strerror (errno));
goto fail;
}
if ( ioctl(r->playback_handle, AUDIO_GETINFO, &pinfo) < 0 )
{
fprintf (stderr, "cannot get audio record format (%s)\n",
strerror (errno));
goto fail;
}
r->spsPlay = pinfo.play.sample_rate;
r->channelsPlay = pinfo.play.channels;
if ( (r->samplesPlay = calloc(2 * r->channelsPlay, r->bufsize)) == NULL )
{
goto fail;
}
}
if( r->record_handle )
{
AUDIO_INITINFO(&rinfo);
rinfo.record.precision = 16;
rinfo.record.encoding = AUDIO_ENCODING_LINEAR;
rinfo.record.sample_rate = r->spsRec;
rinfo.record.channels = r->channelsRec;
if ( ioctl(r->record_handle, AUDIO_SETINFO, &rinfo) < 0 )
{
fprintf (stderr, "cannot set audio record format (%s)\n",
strerror (errno));
goto fail;
}
if ( ioctl(r->record_handle, AUDIO_GETINFO, &rinfo) < 0 )
{
fprintf (stderr, "cannot get audio record format (%s)\n",
strerror (errno));
goto fail;
}
r->spsRec = rinfo.record.sample_rate;
r->channelsRec = rinfo.record.channels;
if ( (r->samplesRec = calloc(2 * r->channelsRec, r->bufsize)) == NULL )
{
goto fail;
}
}
if( r->playback_handle )
{
r->threadPlay = OGCreateThread( PlayThread, r );
}
if( r->record_handle )
{
r->threadRec = OGCreateThread( RecThread, r );
}
printf( "CNFA Sun Init Out -> channelsPlay: %d, spsPlay: %d, channelsRec: %d, spsRec: %d\n", r->channelsPlay, r->spsPlay, r->channelsRec, r->spsRec);
return r;
fail:
if( r )
{
if( r->playback_handle != -1 ) close (r->playback_handle);
if( r->record_handle != -1 ) close (r->record_handle);
free( r->samplesPlay );
free( r->samplesRec );
free( r );
}
return 0;
}
void * InitSunDriver( CNFACBType cb, const char * your_name, int reqSPSPlay, int reqSPSRec, int reqChannelsPlay, int reqChannelsRec, int sugBufferSize, const char * outputSelect, const char * inputSelect, void * opaque )
{
struct CNFADriverSun * r = (struct CNFADriverSun *)malloc( sizeof( struct CNFADriverSun ) );
r->CloseFn = CloseCNFASun;
r->StateFn = CNFAStateSun;
r->callback = cb;
r->opaque = opaque;
r->spsPlay = reqSPSPlay;
r->spsRec = reqSPSRec;
r->channelsPlay = reqChannelsPlay;
r->channelsRec = reqChannelsRec;
r->devRec = (inputSelect)?strdup(inputSelect):0;
r->devPlay = (outputSelect)?strdup(outputSelect):0;
r->samplesPlay = NULL;
r->samplesRec = NULL;
r->playback_handle = -1;
r->record_handle = -1;
r->bufsize = sugBufferSize;
return InitSun(r);
}
REGISTER_CNFA( SUN, 10, "Sun", InitSunDriver );
#elif CNFA_LINUX
//Copyright 2015-2020 <>< Charles Lohr under the MIT/x11, NewBSD or ColorChord License. You choose.
#include "os_generic.h"
#include <alsa/asoundlib.h>
#include <string.h>
struct CNFADriverAlsa
{
void (*CloseFn)( void * object );
int (*StateFn)( void * object );
CNFACBType callback;
short channelsPlay;
short channelsRec;
int spsPlay;
int spsRec;
void * opaque;
char * devRec;
char * devPlay;
snd_pcm_uframes_t bufsize;
og_thread_t threadPlay;
og_thread_t threadRec;
snd_pcm_t *playback_handle;
snd_pcm_t *record_handle;
char playing;
char recording;
};
int CNFAStateAlsa( void * v )
{
struct CNFADriverAlsa * r = (struct CNFADriverAlsa *)v;
return ((r->playing)?2:0) | ((r->recording)?1:0);
}
void CloseCNFAAlsa( void * v )
{
struct CNFADriverAlsa * r = (struct CNFADriverAlsa *)v;
if( r )
{
if( r->threadPlay ) OGJoinThread( r->threadPlay );
if( r->threadRec ) OGJoinThread( r->threadRec );
OGUSleep(2000);
if( r->playback_handle ) snd_pcm_close (r->playback_handle);
if( r->record_handle ) snd_pcm_close (r->record_handle);
OGUSleep(2000);
if( r->devRec ) free( r->devRec );
if( r->devPlay ) free( r->devPlay );
free( r );
}
}
static int SetHWParams( snd_pcm_t * handle, int * samplerate, short * channels, snd_pcm_uframes_t * bufsize, struct CNFADriverAlsa * a )
{
int err;
int bufs;
int dir;
snd_pcm_hw_params_t *hw_params;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
return -1;
}
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
goto fail;
}
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf (stderr, "cannot set access type (%s)\n",
snd_strerror (err));
goto fail;
}
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE )) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
goto fail;
}
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, (unsigned int*)samplerate, 0)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
goto fail;
}
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, *channels)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
goto fail;
}
dir = 0;
if( (err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, bufsize, &dir)) < 0 )
{
fprintf( stderr, "cannot set period size. (%s)\n",