-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathsbitx_sound.c
796 lines (642 loc) · 23.4 KB
/
sbitx_sound.c
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
#include <stdio.h>
#include <alsa/asoundlib.h>
#include <pthread.h>
#include <complex.h>
#include <fftw3.h>
#include <time.h>
#include "sound.h"
#include "sdr.h"
/* follows the tutorial at http://alsamodular.sourceforge.net/alsa_programming_howto.html
Next thing to try is http://www.saunalahti.fi/~s7l/blog/2005/08/21/Full%20Duplex%20ALSA
We are using 4 bytes per sample,
each frame is consists of two channels of audio, hence 8 bytes
We are shooting for 1024x2 = 2048 samples per period. that is 8K
At two periods in the buffer, the buffer has to be 16K
To simply the work, we are picking up some settings for the Wolfson codec
as it connects to a raspberry pi. These values are interdependent
and they will work out of the box. It takes the guess work out of
configuring the Raspberry Pi with Wolfson codec.
*/
/*
MIXER api
https://alsa.opensrc.org/HowTo_access_a_mixer_control
https://android.googlesource.com/platform/hardware/qcom/audio/+/jb-mr1-dev/alsa_sound/ALSAMixer.cpp
https://github.com/bear24rw/alsa-utils/blob/master/amixer/amixer.c
There are six kinds of controls:
playback volume
playback switch
playback enumeration
capture volume
capture switch
capture enumeration
examples of using amixer to mute and unmute:
amixer -c 1 set 'Output Mixer Mic Sidetone' unmute
amixer -c 1 set 'Output Mixer Mic Sidetone' mute
examples of using sound_mixer function:
'Mic' 0/1 = mute/unmute the mic
'Line' 0/1= mute/unmute the line in
'Master' 0-100 controls the earphone volume only, line out remains unaffected
'Input Mux' 1/0 take the input either from the Mic or Line In
*/
void sound_volume(char *card_name, char *element, int volume)
{
long min, max;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
char *card;
card = card_name;
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, card);
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_index(sid, 0);
snd_mixer_selem_id_set_name(sid, element);
snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid);
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
snd_mixer_selem_set_playback_volume_all(elem, volume * max / 100);
snd_mixer_close(handle);
}
void sound_mixer(char *card_name, char *element, int make_on)
{
long min, max;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
char *card = card_name;
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, card);
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_index(sid, 0);
snd_mixer_selem_id_set_name(sid, element);
snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid);
/* if (elem)
puts("Element found.");
*/
//find out if the his element is capture side or plaback
if(snd_mixer_selem_has_capture_switch(elem)){
//puts("this is a capture switch.");
snd_mixer_selem_set_capture_switch_all(elem, make_on);
}
else if (snd_mixer_selem_has_playback_switch(elem)){
// puts("this is a playback switch.");
snd_mixer_selem_set_playback_switch_all(elem, make_on);
}
else if (snd_mixer_selem_has_playback_volume(elem)){
//puts("this is playback volume");
long volume = make_on;
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
snd_mixer_selem_set_playback_volume_all(elem, volume * max / 100);
}
else if (snd_mixer_selem_has_capture_volume(elem)){
// puts("this is a capture volume");
long volume = make_on;
snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
snd_mixer_selem_set_capture_volume_all(elem, volume * max / 100);
}
else if (snd_mixer_selem_is_enumerated(elem)){
// puts("TBD: this is an enumerated capture element");
snd_mixer_selem_set_enum_item(elem, 0, make_on);
}
snd_mixer_close(handle);
}
int rate = 96000; /* Sample rate */
static snd_pcm_uframes_t buff_size = 8192; /* Periodsize (bytes) */
static int n_periods_per_buffer = 2; /* Number of periods */
//static int n_periods_per_buffer = 1024; /* Number of periods */
static snd_pcm_t *pcm_play_handle=0; //handle for the pcm device
static snd_pcm_t *pcm_capture_handle=0; //handle for the pcm device
static snd_pcm_t *loopback_play_handle=0; //handle for the pcm device
static snd_pcm_t *loopback_capture_handle=0; //handle for the pcm device
static snd_pcm_stream_t play_stream = SND_PCM_STREAM_PLAYBACK; //playback stream
static snd_pcm_stream_t capture_stream = SND_PCM_STREAM_CAPTURE; //playback stream
static char *pcm_play_name, *pcm_capture_name;
static snd_pcm_hw_params_t *hwparams;
static snd_pcm_sw_params_t *swparams;
static snd_pcm_hw_params_t *hloop_params;
static snd_pcm_sw_params_t *sloop_params;
static int exact_rate; /* Sample rate returned by */
static int sound_thread_continue = 0;
pthread_t sound_thread, loopback_thread;
int use_virtual_cable = 0;
unsigned long sound_millis = 0;
struct Queue qloop;
/* this function should be called just once in the application process.
Calling it frequently will result in more allocation of hw_params memory blocks
without releasing them.
The list of PCM devices available on any platform can be found by running
aplay -L
We have to pass the id of one of those devices to this function.
The sequence of the alsa functions must be maintained for this to work consistently
It returns a -1 if the device didn't open. The error message is on stderr.
IMPORTANT:
The sound is playback is carried on in a non-blocking way
*/
int sound_start_play(char *device){
//found out the correct device through aplay -L (for pcm devices)
snd_pcm_hw_params_alloca(&hwparams); //more alloc
//puts a playback handle into the pointer to the pointer
int e = snd_pcm_open(&pcm_play_handle, device, play_stream, SND_PCM_NONBLOCK);
if (e < 0) {
fprintf(stderr, "Error opening PCM playback device %s: %s\n", device, snd_strerror(e));
return -1;
}
//fills up the hwparams with values, hwparams was allocated above
e = snd_pcm_hw_params_any(pcm_play_handle, hwparams);
if (e < 0) {
fprintf(stderr, "*Error getting hw playback params (%d)\n", e);
return(-1);
}
// set the pcm access to interleaved
e = snd_pcm_hw_params_set_access(pcm_play_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
if (e < 0) {
fprintf(stderr, "*Error setting playback access.\n");
return(-1);
}
/* Set sample format */
e = snd_pcm_hw_params_set_format(pcm_play_handle, hwparams, SND_PCM_FORMAT_S32_LE);
if (e < 0) {
fprintf(stderr, "*Error setting plyaback format.\n");
return(-1);
}
/* Set sample rate. If the exact rate is not supported */
/* by the hardware, use nearest possible rate. */
exact_rate = rate;
e = snd_pcm_hw_params_set_rate_near(pcm_play_handle, hwparams, &exact_rate, 0);
if ( e< 0) {
fprintf(stderr, "Error setting playback rate.\n");
return(-1);
}
if (rate != exact_rate)
fprintf(stderr, "*The playback rate %d changed to %d Hz\n", rate, exact_rate);
/* else
fprintf(stderr, "Playback sampling rate is set to %d\n", exact_rate);
*/
/* Set number of channels */
if ((e = snd_pcm_hw_params_set_channels(pcm_play_handle, hwparams, 2)) < 0) {
fprintf(stderr, "*Error setting playback channels.\n");
return(-1);
}
// frame = bytes_per_sample x n_channel
// period = frames transfered at a time (160 for voip, etc.)
// we use two periods per buffer.
if ((e = snd_pcm_hw_params_set_periods(pcm_play_handle, hwparams, n_periods_per_buffer, 0)) < 0) {
fprintf(stderr, "*Error setting playback periods.\n");
return(-1);
}
// the buffer size is each periodsize x n_periods
snd_pcm_uframes_t n_frames= (buff_size * n_periods_per_buffer)/8;
//printf("trying for buffer size of %ld\n", n_frames);
e = snd_pcm_hw_params_set_buffer_size_near(pcm_play_handle, hwparams, &n_frames);
if (e < 0) {
fprintf(stderr, "*Error setting playback buffersize.\n");
return(-1);
}
if (snd_pcm_hw_params(pcm_play_handle, hwparams) < 0) {
fprintf(stderr, "*Error setting playback HW params.\n");
return(-1);
}
// puts("All hw params set to play sound");
return 0;
}
int sound_start_loopback_capture(char *device){
snd_pcm_hw_params_alloca(&hloop_params);
//printf ("opening audio tx stream to %s\n", device);
int e = snd_pcm_open(&loopback_capture_handle, device, capture_stream, 0);
if (e < 0) {
fprintf(stderr, "Err: Opening loop capture %s: %s\n", device, snd_strerror(e));
return -1;
}
e = snd_pcm_hw_params_any(loopback_capture_handle, hloop_params);
if (e < 0) {
fprintf(stderr, "*Error setting capture access (%d)\n", e);
return(-1);
}
e = snd_pcm_hw_params_set_access(loopback_capture_handle, hloop_params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (e < 0) {
fprintf(stderr, "*Error setting capture access.\n");
return(-1);
}
/* Set sample format */
e = snd_pcm_hw_params_set_format(loopback_capture_handle, hloop_params, SND_PCM_FORMAT_S32_LE);
if (e < 0) {
fprintf(stderr, "*Error setting loopback capture format.\n");
return(-1);
}
/* Set sample rate. If the exact rate is not supported */
/* by the hardware, use nearest possible rate. */
exact_rate = 48000;
//printf("Setting loopback capture rate to %d\n", exact_rate);
e = snd_pcm_hw_params_set_rate_near(loopback_capture_handle, hloop_params, &exact_rate, 0);
if ( e< 0) {
fprintf(stderr, "*Error setting loopback capture rate.\n");
return(-1);
}
if (48000 != exact_rate)
fprintf(stderr, "#The loopback capture rate set to %d Hz\n", exact_rate);
/* Set number of channels */
if ((e = snd_pcm_hw_params_set_channels(loopback_capture_handle, hloop_params, 2)) < 0) {
fprintf(stderr, "*Error setting loopback capture channels.\n");
return(-1);
}
//printf("%d: set the #channels\n", __LINE__, 2);
/* Set number of periods. Periods used to be called fragments. */
if ((e = snd_pcm_hw_params_set_periods(loopback_capture_handle, hloop_params, n_periods_per_buffer, 0)) < 0) {
fprintf(stderr, "*Error setting loopback capture periods.\n");
return(-1);
}
// the buffer size is each periodsize x n_periods
snd_pcm_uframes_t n_frames= (buff_size * n_periods_per_buffer)/ 8;
//printf("trying for buffer size of %ld\n", n_frames);
e = snd_pcm_hw_params_set_buffer_size_near(loopback_capture_handle, hloop_params, &n_frames);
if (e < 0) {
fprintf(stderr, "*Error setting loopback capture buffersize.\n");
return(-1);
}
//printf("%d: set buffer to \n", __LINE__, n_frames);
if (snd_pcm_hw_params(loopback_capture_handle, hloop_params) < 0) {
fprintf(stderr, "*Error setting capture HW params.\n");
return(-1);
}
//printf("%d: set hwparams\n", __LINE__);
/* set some parameters in the driver to handle the latencies */
snd_pcm_sw_params_malloc(&sloop_params);
if((e = snd_pcm_sw_params_current(loopback_capture_handle, sloop_params)) < 0){
fprintf(stderr, "Error getting current loopback capture sw params : %s\n", snd_strerror(e));
return (-1);
}
if ((e = snd_pcm_sw_params_set_start_threshold(loopback_capture_handle, sloop_params, 15)) < 0){
fprintf(stderr, "Unable to set threshold mode for loopback capture\n");
}
if ((e = snd_pcm_sw_params_set_stop_threshold(loopback_capture_handle, sloop_params, 1)) < 0){
fprintf(stderr, "Unable to set stop threshold for loopback capture\n");
}
return 0;
}
/*
The capture is opened in a blocking mode, the read function will block until
there are enough samples to return a block.
This ensures that the blocks are returned in perfect timing with the codec's clock
Once you process these captured samples and send them to the playback device, you
just wait for the next block to arrive
*/
int sound_start_capture(char *device){
snd_pcm_hw_params_alloca(&hwparams);
int e = snd_pcm_open(&pcm_capture_handle, device, capture_stream, 0);
if (e < 0) {
fprintf(stderr, "Error opening PCM capture device %s: %s\n", pcm_capture_name, snd_strerror(e));
return -1;
}
e = snd_pcm_hw_params_any(pcm_capture_handle, hwparams);
if (e < 0) {
fprintf(stderr, "*Error setting capture access (%d)\n", e);
return(-1);
}
e = snd_pcm_hw_params_set_access(pcm_capture_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
if (e < 0) {
fprintf(stderr, "*Error setting capture access.\n");
return(-1);
}
/* Set sample format */
e = snd_pcm_hw_params_set_format(pcm_capture_handle, hwparams, SND_PCM_FORMAT_S32_LE);
if (e < 0) {
fprintf(stderr, "*Error setting capture format.\n");
return(-1);
}
/* Set sample rate. If the exact rate is not supported */
/* by the hardware, use nearest possible rate. */
exact_rate = rate;
e = snd_pcm_hw_params_set_rate_near(pcm_capture_handle, hwparams, &exact_rate, 0);
if ( e< 0) {
fprintf(stderr, "*Error setting capture rate.\n");
return(-1);
}
if (rate != exact_rate)
fprintf(stderr, "#The capture rate %d changed to %d Hz\n", rate, exact_rate);
/* Set number of channels */
if ((e = snd_pcm_hw_params_set_channels(pcm_capture_handle, hwparams, 2)) < 0) {
fprintf(stderr, "*Error setting capture channels.\n");
return(-1);
}
/* Set number of periods. Periods used to be called fragments. */
if ((e = snd_pcm_hw_params_set_periods(pcm_capture_handle, hwparams, n_periods_per_buffer, 0)) < 0) {
fprintf(stderr, "*Error setting capture periods.\n");
return(-1);
}
// the buffer size is each periodsize x n_periods
snd_pcm_uframes_t n_frames= (buff_size * n_periods_per_buffer)/ 8;
//printf("trying for buffer size of %ld\n", n_frames);
e = snd_pcm_hw_params_set_buffer_size_near(pcm_play_handle, hwparams, &n_frames);
if (e < 0) {
fprintf(stderr, "*Error setting capture buffersize.\n");
return(-1);
}
if (snd_pcm_hw_params(pcm_capture_handle, hwparams) < 0) {
fprintf(stderr, "*Error setting capture HW params.\n");
return(-1);
}
return 0;
}
int sound_start_loopback_play(char *device){
//found out the correct device through aplay -L (for pcm devices)
snd_pcm_hw_params_alloca(&hwparams); //more alloc
//printf ("opening audio rx stream to %s\n", device);
int e = snd_pcm_open(&loopback_play_handle, device, play_stream, SND_PCM_NONBLOCK);
if (e < 0) {
fprintf(stderr, "Error opening loopback playback device %s: %s\n", device, snd_strerror(e));
return -1;
}
e = snd_pcm_hw_params_any(loopback_play_handle, hwparams);
if (e < 0) {
fprintf(stderr, "*Error getting loopback playback params (%d)\n", e);
return(-1);
}
e = snd_pcm_hw_params_set_access(loopback_play_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
if (e < 0) {
fprintf(stderr, "*Error setting loopback access.\n");
return(-1);
}
/* Set sample format */
e = snd_pcm_hw_params_set_format(loopback_play_handle, hwparams, SND_PCM_FORMAT_S32_LE);
if (e < 0) {
fprintf(stderr, "*Error setting loopback format.\n");
return(-1);
}
/* Set sample rate. If the exact rate is not supported */
/* by the hardware, use nearest possible rate. */
exact_rate = 48000;
e = snd_pcm_hw_params_set_rate_near(loopback_play_handle, hwparams, &exact_rate, 0);
if ( e< 0) {
fprintf(stderr, "Error setting playback rate.\n");
return(-1);
}
if (48000 != exact_rate)
fprintf(stderr, "*The loopback playback rate %d changed to %d Hz\n", rate, exact_rate);
/* Set number of channels */
if ((e = snd_pcm_hw_params_set_channels(loopback_play_handle, hwparams, 2)) < 0) {
fprintf(stderr, "*Error setting playback channels.\n");
return(-1);
}
// frame = bytes_per_sample x n_channel
// period = frames transfered at a time (160 for voip, etc.)
// we use two periods per buffer.
if ((e = snd_pcm_hw_params_set_periods(loopback_play_handle, hwparams, 8, 0)) < 0) {
fprintf(stderr, "*Error setting playback periods.\n");
return(-1);
}
// the buffer size is each periodsize x n_periods
snd_pcm_uframes_t n_frames= (buff_size * n_periods_per_buffer)/8;
//lets pump it up to see if we can reduce the dropped frames
n_frames *= 4;
//printf("trying for loopback buffer size of %ld\n", n_frames);
e = snd_pcm_hw_params_set_buffer_size_near(loopback_play_handle, hwparams, &n_frames);
if (e < 0) {
fprintf(stderr, "*Error setting loopback playback buffersize.\n");
return(-1);
}
//printf("loopback playback buffer size is set to %d\n", n_frames);
if (snd_pcm_hw_params(loopback_play_handle, hwparams) < 0) {
fprintf(stderr, "*Error setting loopback playback HW params.\n");
return(-1);
}
return 0;
}
// this is only a test process to be substituted to try loopback
// it was used to debug timing errors
void sound_process2(int32_t *input_i, int32_t *input_q, int32_t *output_i, int32_t *output_q, int n_samples){
for (int i= 0; i < n_samples; i++){
output_i[i] = input_q[i];
output_q[i] = 0;
}
}
//check that we haven't free()-ed up the hwparams block
//don't call this function at all until that is fixed
//you don't have to call it anyway
void sound_stop(){
snd_pcm_drop(pcm_play_handle);
snd_pcm_drain(pcm_play_handle);
snd_pcm_drop(pcm_capture_handle);
snd_pcm_drain(pcm_capture_handle);
}
static int count = 0;
static struct timespec gettime_now;
static long int last_time = 0;
static long int last_sec = 0;
static int nframes = 0;
int32_t resample_in[10000];
int32_t resample_out[10000];
int last_second = 0;
int nsamples = 0;
int played_samples = 0;
unsigned long sbitx_millis(){
return sound_millis;
}
int sound_loop(){
int32_t *line_in, *line_out, *data_in, *data_out,
*input_i, *output_i, *input_q, *output_q;
int pcmreturn, i, j, loopreturn;
short s1, s2;
int frames;
//we allocate enough for two channels of int32_t sized samples
data_in = (int32_t *)malloc(buff_size * 2);
line_in = (int32_t *)malloc(buff_size * 2);
line_out = (int32_t *)malloc(buff_size * 2);
data_out = (int32_t *)malloc(buff_size * 2);
input_i = (int32_t *)malloc(buff_size * 2);
output_i = (int32_t *)malloc(buff_size * 2);
input_q = (int32_t *)malloc(buff_size * 2);
output_q = (int32_t *)malloc(buff_size * 2);
frames = buff_size / 8;
snd_pcm_prepare(pcm_play_handle);
snd_pcm_prepare(loopback_play_handle);
snd_pcm_writei(pcm_play_handle, data_out, frames);
snd_pcm_writei(pcm_play_handle, data_out, frames);
//Note: the virtual cable samples queue should be flushed at the start of tx
qloop.stall = 1;
while(sound_thread_continue) {
//restart the pcm capture if there is an error reading the samples
//this is opened as a blocking device, hence we derive accurate timing
last_time = gettime_now.tv_nsec/1000;
while ((pcmreturn = snd_pcm_readi(pcm_capture_handle, data_in, frames)) < 0){
snd_pcm_prepare(pcm_capture_handle);
//putchar('=');
}
i = 0;
j = 0;
int ret_card = pcmreturn;
if (use_virtual_cable){
//printf(" we have %d in qloop, writing now\n", q_length(&qloop));
// if don't we have enough to last two iterations loop back...
if (q_length(&qloop) < pcmreturn){
// puts(" skipping");
continue;
}
//copy 1024 samples from the queue.
i = 0;
j = 0;
for (int samples = 0; samples < 1024; samples++){
int32_t s = q_read(&qloop);
input_i[j] = input_q[j] = s;
j++;
}
played_samples += 1024;
}
else {
while (i < pcmreturn){
input_i[i] = data_in[j++]/2;
input_q[i] = data_in[j++]/2;
i++;
}
}
clock_gettime(CLOCK_MONOTONIC, &gettime_now);
sound_millis = (gettime_now.tv_sec * 1000) + (gettime_now.tv_nsec/1000000);
//printf("%d %ld %d\n", count++, nsamples, pcmreturn);
sound_process(input_i, input_q, output_i, output_q, pcmreturn);
i = 0;
j = 0;
while (i < pcmreturn){
data_out[j++] = output_i[i];
data_out[j++] = output_q[i++];
}
while ((pcmreturn = snd_pcm_writei(pcm_play_handle,
data_out, frames)) < 0) {
snd_pcm_prepare(pcm_play_handle);
}
//decimate the line out to half, ie from 96000 to 48000
//play the recevied data (from left channel) to both of line out
int jj = 0;
int ii = 0;
while (ii < pcmreturn){
line_out[jj++] = output_i[ii];
line_out[jj++] = output_i[ii];
ii += 2;
}
while((pcmreturn = snd_pcm_writei(loopback_play_handle,
line_out, jj)) < 0){
//printf("loopback rx error: %s\n", snd_strerror(pcmreturn));
snd_pcm_prepare(loopback_play_handle);
//puts("preparing loopback");
}
//played_samples += pcmreturn;
}
//fclose(pf);
printf("********Ending sound thread\n");
}
int loopback_loop(){
int32_t *line_in, *line_out, *data_in, *data_out,
*input_i, *output_i, *input_q, *output_q;
int pcmreturn, i, j, loopreturn;
short s1, s2;
int frames;
//we allocate enough for two channels of int32_t sized samples
data_in = (int32_t *)malloc(buff_size * 2);
frames = buff_size / 8;
snd_pcm_prepare(loopback_capture_handle);
while(sound_thread_continue) {
//restart the pcm capture if there is an error reading the samples
//this is opened as a blocking device, hence we derive accurate timing
last_time = gettime_now.tv_nsec/1000;
while ((pcmreturn = snd_pcm_readi(loopback_capture_handle, data_in, frames/2)) < 0){
snd_pcm_prepare(loopback_capture_handle);
//putchar('=');
}
i = 0;
j = 0;
int ret_card = pcmreturn;
//fill up a local buffer, take only the left channel
i = 0;
j = 0;
for (int i = 0; i < pcmreturn; i++){
q_write(&qloop, data_in[j]/64);
q_write(&qloop, data_in[j]/64);
j += 2;
}
nsamples += j;
if (gettime_now.tv_sec != last_sec){
if(use_virtual_cable)
// printf("######sampling rate %d/%d\n", played_samples, nsamples);
last_sec = gettime_now.tv_sec;
nsamples = 0;
played_samples = 0;
count = 0;
}
}
printf("********Ending loopback thread\n");
}
/*
We process the sound in a background thread.
It will call the user-supplied function sound_process()
*/
void *sound_thread_function(void *ptr){
char *device = (char *)ptr;
struct sched_param sch;
//switch to maximum priority
sch.sched_priority = sched_get_priority_max(SCHED_FIFO);
pthread_setschedparam(sound_thread, SCHED_FIFO, &sch);
//printf("opening %s sound card\n", device);
if (sound_start_play(device)){
fprintf(stderr, "*Error opening play device");
return NULL;
}
if (sound_start_capture(device)){
fprintf(stderr, "*Error opening capture device");
return NULL;
}
// printf("opening loopback on plughw:1,0 sound card\n");
if(sound_start_loopback_play("plughw:1,0")){
fprintf(stderr, "*Error opening loopback play device");
return NULL;
}
sound_thread_continue = 1;
sound_loop();
sound_stop();
}
void *loopback_thread_function(void *ptr){
struct sched_param sch;
//switch to maximum priority
sch.sched_priority = sched_get_priority_max(SCHED_FIFO);
pthread_setschedparam(loopback_thread, SCHED_FIFO, &sch);
// printf("loopback thread is %x\n", loopback_thread);
// printf("opening loopback on plughw:1,0 sound card\n");
if (sound_start_loopback_capture("plughw:2,1")){
fprintf(stderr, "*Error opening loopback capture device");
return NULL;
}
sound_thread_continue = 1;
loopback_loop();
sound_stop();
}
int sound_thread_start(char *device){
q_init(&qloop, 10240);
qloop.stall = 1;
pthread_create( &sound_thread, NULL, sound_thread_function, (void*)device);
sleep(1);
pthread_create( &loopback_thread, NULL, loopback_thread_function, (void*)device);
}
void sound_thread_stop(){
sound_thread_continue = 0;
}
void sound_input(int loop){
if (loop){
use_virtual_cable = 1;
}
else{
use_virtual_cable = 0;
}
}
//demo, uncomment it to test it out
/*
void sound_process(int32_t *input_i, int32_t *input_q, int32_t *output_i, int32_t *output_q, int n_samples){
for (int i= 0; i < n_samples; i++){
output_i[i] = input_i[i];
output_q[i] = input_q[i];
}
}
void main(int argc, char **argv){
sound_thread_start("plughw:0,0");
sleep(10);
sound_thread_stop();
sleep(10);
}
*/