-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathslaudio.c
More file actions
1661 lines (1349 loc) · 35.2 KB
/
Copy pathslaudio.c
File metadata and controls
1661 lines (1349 loc) · 35.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
/**
* @file slaudio.c SLAUDIO (libsoundio)
*
* Copyright (C) 2020 studio-link.de
*/
#include <stdlib.h>
#include <string.h>
#include <re.h>
#include <rem.h>
#include <baresip.h>
#include <samplerate.h>
#include <soundio/soundio.h>
#include "slaudio.h"
#include "convert.h"
#ifdef WIN32
#include <windows.h>
#endif
#ifdef DARWIN
#include <IOKit/pwr_mgt/IOPMLib.h>
#endif
#include <webapp.h>
#define BUFFER_LEN 19200 /* max buffer_len = 192kHz*2ch*25ms*2frames */
#define FLOAT_FRAME_BYTES 8 /* sizeof(float) * 2ch */
enum
{
MAX_REMOTE_CHANNELS = 6,
};
static enum SoundIoFormat prioritized_formats[] = {
SoundIoFormatFloat32LE,
SoundIoFormatS32LE,
SoundIoFormatS24LE,
SoundIoFormatS16LE,
SoundIoFormatInvalid,
};
struct slaudio_st
{
int16_t *inBuffer;
int16_t *outBufferTmp;
float *inBufferFloat;
float *inBufferOutFloat;
float *outBufferFloat;
struct SoundIo *soundio;
struct SoundIoDevice *dev_in;
struct SoundIoDevice *dev_out;
struct SoundIoInStream *instream;
struct SoundIoOutStream *outstream;
};
static struct slaudio_st *slaudio;
int16_t *empty_buffer;
struct auplay_st
{
const struct auplay *ap; /* pointer to base-class (inheritance) */
bool run;
void *write;
int16_t *sampv;
size_t sampc;
auplay_write_h *wh;
void *arg;
struct auplay_prm prm;
char *device;
struct session *sess;
};
struct ausrc_st
{
const struct ausrc *as; /* pointer to base-class (inheritance) */
bool run;
void *read;
int16_t *sampv;
size_t sampc;
ausrc_read_h *rh;
void *arg;
struct ausrc_prm prm;
char *device;
struct session *sess;
};
static struct ausrc *ausrc;
static struct auplay *auplay;
static enum SoundIoBackend backend;
static int driver = -1;
static int input = -1;
static int first_input_channel = 0;
static int preferred_sample_rate_in = 0;
static int preferred_sample_rate_out = 0;
static int output = -1;
static struct odict *interfaces = NULL;
struct list sessionl;
static SRC_STATE *src_state_in;
static SRC_STATE *src_state_out;
static int slaudio_stop(void);
static int slaudio_start(void);
static int slaudio_devices(void);
static int slaudio_drivers(void);
static struct SoundIoRingBuffer *ring_buffer = NULL;
void webapp_ws_rtaudio_sync(void);
static int16_t *playmix;
static bool monorecord = true;
static bool monostream = true;
static bool mute = false;
static bool monitor = true;
static bool fatal_error = false;
char error_msg[512] = {0};
static int16_t startup_count = 0;
static struct sl_debug {
uint64_t out_ur;
uint64_t out_or;
uint64_t in_ur;
uint64_t in_or;
uint64_t ring_ur;
uint64_t ring_or;
bool changed;
} sl_debug = {0, 0, 0, 0, 0, 0, false};
static struct tmr sl_debug_tmr;
/* webapp/jitter.c */
void webapp_jitter(struct session *sess, int16_t *sampv,
auplay_write_h *wh, unsigned int sampc, void *arg);
void slaudio_monorecord_set(bool status)
{
monorecord = status;
}
void slaudio_monostream_set(bool status)
{
monostream = status;
}
void slaudio_mute_set(bool status)
{
mute = status;
}
void slaudio_monitor_set(bool status)
{
monitor = status;
}
struct list* sl_sessions(void);
struct list* sl_sessions(void)
{
return &sessionl;
}
static void sess_destruct(void *arg)
{
struct session *sess = arg;
if (sess->run_record)
{
sess->run_record = false;
(void)pthread_join(sess->record_thread, NULL);
}
if (sess->flac)
{
FLAC__stream_encoder_finish(sess->flac);
FLAC__stream_encoder_delete(sess->flac);
sess->flac = NULL;
}
mem_deref(sess->sampv);
mem_deref(sess->pcm);
mem_deref(sess->aubuf);
mem_deref(sess->vumeter);
list_unlink(&sess->le);
}
const struct odict *slaudio_get_interfaces(void)
{
return (const struct odict *)interfaces;
}
int slaudio_reset(void)
{
int err;
if (interfaces)
mem_deref(interfaces);
err = odict_alloc(&interfaces, DICT_BSIZE);
if (err)
return ENOMEM;
info("slaudio/reset\n");
slaudio_stop();
startup_count = 0;
slaudio_drivers();
slaudio_devices();
webapp_ws_rtaudio_sync();
slaudio_start();
return err;
}
void slaudio_set_driver(int value)
{
driver = value;
output = -1;
input = -1;
first_input_channel = 0;
slaudio_reset();
}
void slaudio_set_input(int value)
{
input = value;
first_input_channel = 0;
slaudio_reset();
}
void slaudio_set_first_input_channel(int value)
{
first_input_channel = value;
slaudio_reset();
}
void slaudio_set_output(int value)
{
output = value;
slaudio_reset();
}
static void convert_float(int16_t *sampv, float *f_sampv, size_t sampc)
{
const float scaling = 1.0 / 32767.0f;
while (sampc--)
{
f_sampv[0] = sampv[0] * scaling;
++f_sampv;
++sampv;
}
}
static void stereo2mono(int16_t *buf, size_t sampc)
{
while (sampc--) {
buf[0] = buf[0]/2 + buf[1]/2;
buf[1] = buf[0];
buf += 2;
}
}
static float format_float(char *src, enum SoundIoFormat fmt)
{
float value = 0.0;
if (fmt == SoundIoFormatS32LE) {
value = (*((int32_t*)src)) / SAMPLE_32BIT_SCALING;
}
else if (fmt == SoundIoFormatS24LE) {
int x;
memcpy ((char*)&x + 1, src, 3);
x >>= 8;
value = x / SAMPLE_24BIT_SCALING;
}
else if (fmt == SoundIoFormatS16LE) {
value = (*((int16_t*)src)) / SAMPLE_16BIT_SCALING;
}
else {
value = *((float*)src);
}
return value;
}
static void downsample_first_ch(float *outv, struct SoundIoChannelArea *areas,
int nframes, struct SoundIoInStream *instream)
{
float left;
float right;
for (int frame = 0; frame < nframes; frame++) {
left = 0.0;
right = 0.0;
for (int ch = 0; ch < instream->layout.channel_count; ch++) {
if (ch == first_input_channel) {
left = format_float(areas[ch].ptr,
instream->format);
}
if (ch == first_input_channel + 1) {
right = format_float(areas[ch].ptr,
instream->format);
}
if (first_input_channel == 99) {
left += format_float(areas[ch].ptr,
instream->format);
right = left;
}
if (monorecord)
right = left;
areas[ch].ptr += areas[ch].step;
}
/*stereo ch left*/
*outv = left;
outv += 1;
/*stereo ch right*/
*outv = right;
outv += 1;
}
}
static void ausrc_destructor(void *arg)
{
struct ausrc_st *st = arg;
struct session *sess = st->sess;
info("slaudio: destruct ausrc\n");
sess->run_src = false;
sys_msleep(20);
mem_deref(st->sampv);
}
static void auplay_destructor(void *arg)
{
struct auplay_st *st = arg;
struct session *sess = st->sess;
info("slaudio: destruct auplay\n");
sess->run_play = false;
sys_msleep(20);
mem_deref(st->sampv);
mem_deref(sess->dstmix);
}
static int src_alloc(struct ausrc_st **stp, const struct ausrc *as,
struct media_ctx **ctx,
struct ausrc_prm *prm, const char *device,
ausrc_read_h *rh, ausrc_error_h *errh, void *arg)
{
(void)ctx;
(void)errh;
(void)device;
int err = 0;
struct ausrc_st *st_src = NULL;
struct le *le;
if (!stp || !as || !prm)
return EINVAL;
struct session *sess = NULL;
for (le = sessionl.head; le; le = le->next)
{
sess = le->data;
if (!sess->run_src && !sess->local && sess->call)
{
sess->st_src = mem_zalloc(sizeof(*st_src),
ausrc_destructor);
if (!sess->st_src)
return ENOMEM;
st_src = sess->st_src;
st_src->sess = sess;
break;
}
}
if (!st_src || !sess)
return EINVAL;
st_src->run = false;
st_src->as = as;
st_src->rh = rh;
st_src->arg = arg;
st_src->sampc = prm->srate * prm->ch * prm->ptime / 1000;
st_src->sampv = mem_zalloc(sizeof(int16_t) * st_src->sampc * 10, NULL);
if (!st_src->sampv)
{
err = ENOMEM;
}
if (err)
{
mem_deref(st_src);
return err;
}
sess->run_src = true;
st_src->run = true;
*stp = st_src;
return err;
}
static int play_alloc(struct auplay_st **stp, const struct auplay *ap,
struct auplay_prm *prm, const char *device,
auplay_write_h *wh, void *arg)
{
int err = 0;
struct auplay_st *st_play = NULL;
struct le *le;
if (!stp || !ap || !prm)
return EINVAL;
size_t sampc = prm->srate * prm->ch * prm->ptime / 1000;
struct session *sess;
for (le = sessionl.head; le; le = le->next)
{
sess = le->data;
if (!sess->run_play && !sess->local && sess->call)
{
sess->st_play = mem_zalloc(sizeof(*st_play),
auplay_destructor);
sess->dstmix = mem_zalloc(sizeof(int16_t) * sampc * 10,
NULL);
if (!sess->st_play)
return ENOMEM;
st_play = sess->st_play;
st_play->sess = sess;
break;
}
}
if (!st_play || !sess)
return EINVAL;
st_play->run = false;
st_play->ap = ap;
st_play->wh = wh;
st_play->arg = arg;
st_play->sampc = sampc;
st_play->sampv = mem_zalloc(sizeof(int16_t) * sampc * 10, NULL);
if (!st_play->sampv)
{
err = ENOMEM;
}
if (err)
{
mem_deref(st_play);
return err;
}
sess->run_play = true;
st_play->run = true;
*stp = st_play;
return err;
}
static void underflow_callback(struct SoundIoOutStream *outstream) {
sl_debug.out_ur++;
sl_debug.changed = true;
}
static void outstream_error_callback(struct SoundIoOutStream *os, int err) {
warning("slaudio/out_err_call: %s\n", soundio_strerror(err));
fatal_error = true;
slaudio_reset();
}
#ifdef DARWIN
static void instream_error_callback(struct SoundIoInStream *is, int err,
const char *msg) {
warning("slaudio/in_err_call: %s %s\n", soundio_strerror(err), msg);
fatal_error = true;
slaudio_reset();
}
#else
static void instream_error_callback(struct SoundIoInStream *is, int err) {
warning("slaudio/in_err_call: %s\n", soundio_strerror(err));
fatal_error = true;
slaudio_reset();
}
#endif
static int slaudio_drivers(void)
{
int err = 0;
static enum SoundIoBackend backend_tmp;
struct SoundIo *soundio;
struct odict *o;
struct odict *array;
char backend_name[30];
char idx[2];
err = odict_alloc(&array, DICT_BSIZE);
if (err)
return ENOMEM;
if (!(soundio = soundio_create()))
return ENOMEM;
for (int i = 0; i < soundio_backend_count(soundio); i++)
{
(void)re_snprintf(idx, sizeof(idx), "%d", i);
backend_tmp = soundio_get_backend(soundio, i);
(void)re_snprintf(backend_name, sizeof(backend_name), "%s",
soundio_backend_name(backend_tmp));
if (!str_cmp(backend_name, "Dummy"))
continue;
err = odict_alloc(&o, DICT_BSIZE);
if (err)
return ENOMEM;
odict_entry_add(o, "display", ODICT_STRING,
backend_name);
info("slaudio/drivers detected: %s\n", backend_name);
if (driver == -1 && !str_cmp(backend_name, "PulseAudio"))
driver = i;
if (driver == -1 && !str_cmp(backend_name, "WASAPI"))
driver = i;
if (driver == -1 && !str_cmp(backend_name, "CoreAudio"))
driver = i;
if (driver == i) {
odict_entry_add(o, "selected", ODICT_BOOL, true);
backend = backend_tmp;
info("slaudio/drivers: %s selected\n", backend_name);
}
else {
odict_entry_add(o, "selected", ODICT_BOOL, false);
}
odict_entry_add(o, "id", ODICT_INT, (int64_t)i);
odict_entry_add(array, idx, ODICT_OBJECT, o);
mem_deref(o);
}
odict_entry_add(interfaces, "drivers", ODICT_ARRAY, array);
mem_deref(array);
soundio_destroy(soundio);
return err;
}
static void slaudio_destruct(void *arg)
{
if (slaudio) {
if (!fatal_error) {
soundio_instream_destroy(slaudio->instream);
soundio_outstream_destroy(slaudio->outstream);
}
soundio_device_unref(slaudio->dev_in);
soundio_device_unref(slaudio->dev_out);
soundio_destroy(slaudio->soundio);
sys_msleep(25);
mem_deref(slaudio->inBuffer);
mem_deref(slaudio->inBufferFloat);
mem_deref(slaudio->inBufferOutFloat);
mem_deref(slaudio->outBufferFloat);
slaudio = NULL;
}
if (fatal_error) {
output = -1;
input = -1;
first_input_channel = 0;
sys_msleep(400);
fatal_error = false;
}
}
static int slaudio_construct(void) {
int err = 0;
slaudio = mem_zalloc(sizeof(*slaudio), slaudio_destruct);
slaudio->inBuffer = mem_zalloc(sizeof(int16_t) * BUFFER_LEN,
NULL);
slaudio->inBufferFloat = mem_zalloc(sizeof(float) * BUFFER_LEN,
NULL);
slaudio->inBufferOutFloat = mem_zalloc(sizeof(float) * BUFFER_LEN,
NULL);
slaudio->outBufferFloat = mem_zalloc(sizeof(float) * BUFFER_LEN,
NULL);
/** Initialize the sample rate converter for input */
if ((src_state_in = src_new(SRC_SINC_FASTEST, 2, &err)) == NULL)
{
warning("slaudio/start: Samplerate::src_new failed : %s.\n",
src_strerror(err));
return err;
};
/** Initialize the sample rate converter for output */
if ((src_state_out = src_new(SRC_SINC_FASTEST, 2, &err)) == NULL)
{
warning("slaudio/start: Samplerate::src_new failed : %s.\n",
src_strerror(err));
return err;
};
return err;
}
static int slaudio_devices(void)
{
int err = 0;
int output_count, input_count, default_output, default_input;
bool default_input_err;
bool default_output_err;
struct odict *o;
struct odict *array_in;
struct odict *array_out;
struct SoundIoDevice *device;
char idx[2];
char device_name[256];
err = odict_alloc(&array_in, DICT_BSIZE);
if (err)
return ENOMEM;
err = odict_alloc(&array_out, DICT_BSIZE);
if (err)
return ENOMEM;
err = slaudio_construct();
if (err)
return err;
if (!(slaudio->soundio = soundio_create()))
return ENOMEM;
slaudio->soundio->app_name = "StudioLink";
err = soundio_connect_backend(slaudio->soundio, backend);
if (err) {
warning("slaudio/soundio_connect_backend err: %s\n",
soundio_strerror(err));
goto out;
}
soundio_flush_events(slaudio->soundio);
output_count = soundio_output_device_count(slaudio->soundio);
input_count = soundio_input_device_count(slaudio->soundio);
default_output = soundio_default_output_device_index(slaudio->soundio);
default_output_err = false;
default_input = soundio_default_input_device_index(slaudio->soundio);
default_input_err = false;
for (int i = 0; i < input_count; i += 1) {
device = soundio_get_input_device(slaudio->soundio, i);
(void)re_snprintf(idx, sizeof(idx), "%d", i);
if (backend == SoundIoBackendAlsa && !device->is_raw) {
soundio_device_unref(device);
if (i == default_input)
default_input_err = true;
continue;
}
if (backend == SoundIoBackendWasapi && device->is_raw) {
soundio_device_unref(device);
if (i == default_input)
default_input_err = true;
continue;
}
if (device->probe_error) {
info("slaudio/input device %s probe error\n",
device->name);
soundio_device_unref(device);
if (i == default_input)
default_input_err = true;
continue;
}
debug("slaudio/input device %s formats:\n", device->name);
for (int y = 0; y < device->format_count; y += 1) {
const char *comma =
(y == device->format_count - 1) ? "\n" : ", ";
debug("%s%s",
soundio_format_string(device->formats[y]),
comma);
}
err = odict_alloc(&o, DICT_BSIZE);
if (err)
goto out;
odict_entry_add(o, "id", ODICT_INT, (int64_t)i);
if (device->is_raw) {
(void)re_snprintf(device_name, sizeof(device_name),
"%s (Exclusiv)", device->name);
}
else {
(void)re_snprintf(device_name,
sizeof(device_name), "%s",
device->name);
}
odict_entry_add(o, "display", ODICT_STRING, device_name);
info("slaudio: device: %s\n", device_name);
int64_t channels = device->current_layout.channel_count;
info("slaudio/in: default channels: %d\n", channels);
if (!channels) {
const struct SoundIoChannelLayout *layout =
&device->layouts[0];
channels = layout->channel_count;
info("slaudio: fallback channels: %d\n", channels);
}
odict_entry_add(o, "channels", ODICT_INT, channels);
odict_entry_add(o, "first_input_channel",
ODICT_INT,
(int64_t)first_input_channel);
if (input == -1 && default_input == i)
{
info("slaudio: set input %s (%d)\n", device_name, i);
input = i;
}
if (input == -1 && default_input_err) {
info("slaudio: set fallback input %s (%d)\n",
device_name, i);
input = i;
}
if (input == i)
{
odict_entry_add(o, "selected", ODICT_BOOL, true);
}
else
{
odict_entry_add(o, "selected", ODICT_BOOL, false);
}
odict_entry_add(array_in, idx, ODICT_OBJECT, o);
soundio_device_unref(device);
mem_deref(o);
}
for (int i = 0; i < output_count; i += 1) {
device = soundio_get_output_device(slaudio->soundio, i);
(void)re_snprintf(idx, sizeof(idx), "%d", i);
if (backend == SoundIoBackendAlsa && !device->is_raw) {
soundio_device_unref(device);
if (i == default_output)
default_output_err = true;
continue;
}
if (backend == SoundIoBackendWasapi && device->is_raw) {
soundio_device_unref(device);
if (i == default_output)
default_output_err = true;
continue;
}
if (device->probe_error) {
info("slaudio/input device %s probe error\n",
device->name);
soundio_device_unref(device);
if (i == default_output)
default_output_err = true;
continue;
}
debug("slaudio/output device %s formats:\n", device->name);
for (int y = 0; y < device->format_count; y += 1) {
const char *comma =
(y == device->format_count - 1) ? "\n" : ", ";
debug("%s%s",
soundio_format_string(device->formats[y]),
comma);
}
err = odict_alloc(&o, DICT_BSIZE);
if (err)
goto out;
odict_entry_add(o, "id", ODICT_INT, (int64_t)i);
if (device->is_raw) {
(void)re_snprintf(device_name, sizeof(device_name),
"%s (Exclusiv)", device->name);
}
else {
(void)re_snprintf(device_name, sizeof(device_name),
"%s", device->name);
}
odict_entry_add(o, "display", ODICT_STRING, device_name);
info("slaudio: output device %s\n", device_name);
if (output == -1 && default_output == i)
{
info("slaudio: set output %s (%d)\n", device_name, i);
output = i;
}
if (input == -1 && default_output_err) {
info("slaudio: set fallback ouput %s (%d)\n",
device_name, i);
output = i;
}
if (output == i)
{
odict_entry_add(o, "selected", ODICT_BOOL, true);
}
else
{
odict_entry_add(o, "selected", ODICT_BOOL, false);
}
odict_entry_add(array_out, idx, ODICT_OBJECT, o);
soundio_device_unref(device);
mem_deref(o);
}
odict_entry_add(interfaces, "input", ODICT_ARRAY, array_in);
odict_entry_add(interfaces, "output", ODICT_ARRAY, array_out);
out:
mem_deref(array_in);
mem_deref(array_out);
return err;
}
static void read_callback(struct SoundIoInStream *instream,
int frame_count_min, int frame_count_max)
{
int err;
struct SoundIoChannelArea *areas;
int nframes = frame_count_max;
int samples;
struct le *le;
struct le *mle;
struct session *sess;
struct auplay_st *st_play;
struct auplay_st *mst_play;
struct ausrc_st *st_src;
int cntplay = 0, msessplay = 0, sessplay = 0;
SRC_DATA src_data_in;
SRC_DATA src_data_out;
struct auframe af;
static uint64_t frames = 0;
if ((err = soundio_instream_begin_read(instream, &areas, &nframes))) {
warning("slaudio/read_callback:"
"begin read error: %s\n", soundio_strerror(err));
return; /*@TODO handle error */
}
if (!nframes)
return;
if (startup_count < 16) {
/*
* drop first frames (keeps buffers small)
*/
debug("slaudio: drop %d frames due startup %d\n", nframes,
startup_count);
if ((err = soundio_instream_end_read(instream))) {
warning("slaudio/read_callback:"
"end read error: %s\n", soundio_strerror(err));
}
++startup_count;
return;
}
samples = nframes * 2; /* Stereo (2 ch) only */
if (!areas) {
/* Due to an overflow there is a hole. Fill with silence */
memset(slaudio->inBufferFloat, 0, samples * sizeof(float));
sl_debug.in_or++;
sl_debug.changed = true;
}
else {
downsample_first_ch(slaudio->inBufferFloat, areas, nframes,
instream);
}
if ((err = soundio_instream_end_read(instream))) {
warning("slaudio/read_callback:"
"end read error: %s\n", soundio_strerror(err));
return; /*@TODO handle error */
}
if (mute)
{
memset(slaudio->inBufferFloat, 0, samples * sizeof(float));
}
ws_meter_process(0, slaudio->inBufferFloat, (unsigned long)samples);
if (!monorecord)
ws_meter_process(2, slaudio->inBufferFloat+1,
(unsigned long)samples-1);
/**<-- Input Samplerate conversion */
if (preferred_sample_rate_in != 48000)
{
if (!src_state_in)
return;
src_data_in.data_in = slaudio->inBufferFloat;
src_data_in.data_out = slaudio->inBufferOutFloat;
src_data_in.input_frames = nframes;
src_data_in.output_frames = BUFFER_LEN / 2;
src_data_in.src_ratio =
48000 / (double)preferred_sample_rate_in;
src_data_in.end_of_input = 0;
if ((err = src_process(src_state_in, &src_data_in)) != 0)
{
warning("slaudio: Samplerate::src_process_in :"
"returned error : %s %f\n",
src_strerror(err),
src_data_in.src_ratio);
return;
};
samples = src_data_in.output_frames_gen * 2;
auconv_to_s16(slaudio->inBuffer, AUFMT_FLOAT,
slaudio->inBufferOutFloat,
samples);
}
else {
auconv_to_s16(slaudio->inBuffer, AUFMT_FLOAT,
slaudio->inBufferFloat, samples);
}
/** Input Samplerate conversion -->*/
for (le = sessionl.head; le; le = le->next)
{
sess = le->data;
if (sess->local)
{
/* write local audio to flac record buffer */