/
sound.c
722 lines (677 loc) · 21.8 KB
/
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
/*
* Sound modules that do not depend on alsa or portaudio
*/
#include <Python.h>
#include <complex.h>
#include <math.h>
#include <sys/time.h>
#include <time.h>
#include "quisk.h"
#include "filter.h"
// Thanks to Franco Spinelli for this fix:
// The H101 hardware using the PCM2904 chip has a one-sample delay between
// channels that must be fixed in software. If you have this problem,
// set channel_delay in your config file. The FIX_H101 #define is obsolete
// but still works. It is equivalent to channel_delay = channel_q.
// The structure sound_dev represents a sound device to open. If portaudio_index
// is -1, it is an ALSA sound device; otherwise it is a portaudio device with that
// index. Portaudio devices have names that start with "portaudio". A device name
// can be the null string, meaning the device should not be opened. The sound_dev
// "handle" is either an alsa handle or a portaudio stream if the stream is open;
// otherwise it is NULL for a closed device.
// This sends the microphone samples to the FFT instead of the radio samples.
#define DEBUG_MIC 0
#if DEBUG_IO
static int debug_timer = 1; // count up number of samples
#endif
static struct sound_dev Capture, Playback, MicCapture, MicPlayback, DigitalInput, DigitalOutput;
// These are arrays of all capture and playback devices, and MUST end with NULL:
static struct sound_dev * CaptureDevices[] = {&Capture, &MicCapture, &DigitalInput, NULL};
static struct sound_dev * PlaybackDevices[] = {&Playback, &MicPlayback, &DigitalOutput, NULL};
struct sound_conf quisk_sound_state; // Current sound status
static char file_name_audio[QUISK_PATH_SIZE]; // file name for recording speaker output
static char file_name_samples[QUISK_PATH_SIZE]; // file name for recording samples
static int is_recording_audio;
static int is_recording_samples;
static int want_record;
static ty_sample_start pt_sample_start;
static ty_sample_stop pt_sample_stop;
static ty_sample_read pt_sample_read;
static complex cSamples[SAMP_BUFFER_SIZE]; // Complex buffer for samples
void ptimer(int counts) // used for debugging
{ // print the number of counts per second
static unsigned int calls=0, total=0;
static time_t time0=0;
time_t dt;
if (time0 == 0) {
time0 = (int)(QuiskTimeSec() * 1.e6);
return;
}
total += counts;
calls++;
if (calls % 1000 == 0) {
dt = (int)(QuiskTimeSec() * 1.e6) - time0;
printf("ptimer: %d counts in %d microseconds %.3f counts/sec\n",
total, (unsigned)dt, (double)total * 1E6 / dt);
}
}
static void delay_sample (struct sound_dev * dev, double * dSamp, int nSamples)
{ // Delay the I or Q data stream by one sample.
// cSamples is double D[nSamples][2]
double d;
double * first, * last;
if (nSamples < 1)
return;
if (dev->channel_Delay == dev->channel_I) {
first = dSamp;
last = dSamp + nSamples * 2 - 2;
}
else if (dev->channel_Delay == dev->channel_Q) {
first = dSamp + 1;
last = dSamp + nSamples * 2 - 1;
}
else {
return;
}
d = dev->save_sample;
dev->save_sample = *last;
while (--nSamples) {
*last = *(last - 2);
last -= 2;
}
*first = d;
}
static void correct_sample (struct sound_dev * dev, complex * cSamples, int nSamples)
{ // Correct the amplitude and phase
int i;
double re, im;
if (dev->doAmplPhase) { // amplitude and phase corrections
for (i = 0; i < nSamples; i++) {
re = creal(cSamples[i]);
im = cimag(cSamples[i]);
re = re * dev->AmPhAAAA;
im = re * dev->AmPhCCCC + im * dev->AmPhDDDD;
cSamples[i] = re + I * im;
}
}
}
static int record_audio(complex * cSamples, int nSamples)
{ // Record the speaker audio to a WAV file, PCM, 16 bits, one channel
static FILE * fp = NULL; // TODO: correct for big-endian byte order
static unsigned int samples=0, remain=0;
int j;
short samp; // must be 2 bytes
unsigned int u; // must be 4 bytes
unsigned short s; // must be 2 bytes
switch (nSamples) {
case -1: // Open the file
fp = fopen(file_name_audio, "wb");
if ( ! fp)
return 0;
if (fwrite("RIFF", 1, 4, fp) != 4) {
fclose(fp);
fp = NULL;
return 0;
}
// pcm data, 16-bit samples, one channel
u = 36;
fwrite(&u, 4, 1, fp);
fwrite("WAVE", 1, 4, fp);
fwrite("fmt ", 1, 4, fp);
u = 16;
fwrite(&u, 4, 1, fp);
s = 1; // wave_format_pcm
fwrite(&s, 2, 1, fp);
s = 1; // number of channels
fwrite(&s, 2, 1, fp);
u = Playback.sample_rate; // sample rate
fwrite(&u, 4, 1, fp);
u *= 2;
fwrite(&u, 4, 1, fp);
s = 2;
fwrite(&s, 2, 1, fp);
s = 16;
fwrite(&s, 2, 1, fp);
fwrite("data", 1, 4, fp);
u = 0;
fwrite(&u, 4, 1, fp);
samples = 0;
remain = 2147483000;
break;
case -2: // close the file
if (fp)
fclose(fp);
fp = NULL;
remain = 0;
break;
default: // write the sound data to the file
u = (unsigned int)nSamples;
if (u >= remain)
return 0;
samples += u;
remain -= u;
fseek(fp, 40, SEEK_SET); // seek from the beginning
u = 2 * samples;
fwrite(&u, 4, 1, fp);
fseek(fp, 4, SEEK_SET); // seek from the beginning
u += 36;
fwrite(&u, 4, 1, fp);
fseek(fp, 0, SEEK_END); // seek to the end
for (j = 0; j < nSamples; j++) {
samp = (short)(creal(cSamples[j]) / 65536.0);
fwrite(&samp, 2, 1, fp);
}
break;
}
return 1;
}
static int record_samples(complex * cSamples, int nSamples)
{ // Record the samples to a WAV file, two float samples I/Q
static FILE * fp = NULL; // TODO: correct for big-endian byte order
static unsigned int samples=0, remain=0;
int j;
float samp; // must be 4 bytes
unsigned int u; // must be 4 bytes
unsigned short s; // must be 2 bytes
switch (nSamples) {
case -1: // Open the file
fp = fopen(file_name_samples, "wb");
if ( ! fp)
return 0;
if (fwrite("RIFF", 1, 4, fp) != 4) {
fclose(fp);
fp = NULL;
return 0;
}
// IEEE float data, two channels
u = 50;
fwrite(&u, 4, 1, fp);
fwrite("WAVE", 1, 4, fp);
fwrite("fmt ", 1, 4, fp);
u = 18;
fwrite(&u, 4, 1, fp);
s = 3; // wave_format_ieee_float
fwrite(&s, 2, 1, fp);
s = 2; // number of channels
fwrite(&s, 2, 1, fp);
u = quisk_sound_state.sample_rate; // sample rate
fwrite(&u, 4, 1, fp);
u *= 8;
fwrite(&u, 4, 1, fp);
s = 8;
fwrite(&s, 2, 1, fp);
s = 32;
fwrite(&s, 2, 1, fp);
s = 0;
fwrite(&s, 2, 1, fp);
fwrite("fact", 1, 4, fp);
u = 4;
fwrite(&u, 4, 1, fp);
u = 0;
fwrite(&u, 4, 1, fp);
fwrite("data", 1, 4, fp);
u = 0;
fwrite(&u, 4, 1, fp);
samples = 0;
remain = 536870000;
break;
case -2: // close the file
if (fp)
fclose(fp);
fp = NULL;
remain = 0;
break;
default: // write the sound data to the file
u = (unsigned int)nSamples;
if (u >= remain)
return 0;
samples += u;
remain -= u;
fseek(fp, 54, SEEK_SET); // seek from the beginning
u = 8 * samples;
fwrite(&u, 4, 1, fp);
fseek(fp, 4, SEEK_SET); // seek from the beginning
u += 50 ;
fwrite(&u, 4, 1, fp);
fseek(fp, 46, SEEK_SET); // seek from the beginning
u = samples * 2;
fwrite(&u, 4, 1, fp);
fseek(fp, 0, SEEK_END); // seek to the end
for (j = 0; j < nSamples; j++) {
samp = creal(cSamples[j]) / CLIP32;
fwrite(&samp, 4, 1, fp);
samp = cimag(cSamples[j]) / CLIP32;
fwrite(&samp, 4, 1, fp);
}
break;
}
return 1;
}
void quisk_sample_source(ty_sample_start start, ty_sample_stop stop, ty_sample_read read)
{
pt_sample_start = start;
pt_sample_stop = stop;
pt_sample_read = read;
}
int quisk_read_sound(void) // Called from sound thread
{ // called in an infinite loop by the main program
int i, nSamples, mic_count, mic_interp, retval, is_cw, mic_sample_rate;
complex tx_mic_phase;
static double cwEnvelope=0;
static double cwCount=0;
static complex tuneVector = (double)CLIP32 / CLIP16; // Convert 16-bit to 32-bit samples
static struct quisk_cFilter filtInterp={NULL};
quisk_sound_state.interupts++;
#if DEBUG_IO > 1
QuiskPrintTime("Start read_sound", 0);
#endif
if (pt_sample_read) { // read samples from SDR-IQ
nSamples = (*pt_sample_read)(cSamples);
}
else if (quisk_using_udp) { // read samples from UDP port
nSamples = quisk_read_rx_udp(cSamples);
}
else if (Capture.handle) { // blocking read from soundcard
if (Capture.portaudio_index < 0)
nSamples = quisk_read_alsa(&Capture, cSamples);
else
nSamples = quisk_read_portaudio(&Capture, cSamples);
if (Capture.channel_Delay >= 0) // delay the I or Q channel by one sample
delay_sample(&Capture, (double *)cSamples, nSamples);
if (Capture.doAmplPhase) // amplitude and phase corrections
correct_sample(&Capture, cSamples, nSamples);
}
else {
nSamples = 0;
}
retval = nSamples; // retval remains the number of samples read
#if DEBUG_IO
debug_timer += nSamples;
if (debug_timer >= quisk_sound_state.sample_rate) // one second
debug_timer = 0;
#endif
#if DEBUG_IO > 2
ptimer (nSamples);
#endif
quisk_sound_state.latencyCapt = nSamples; // samples available
#if DEBUG_IO > 1
QuiskPrintTime(" read samples", 0);
#endif
// Perhaps record the samples to a file
if (want_record) {
if (is_recording_samples) {
record_samples(cSamples, nSamples); // Record samples
}
else if (file_name_samples[0]) {
if (record_samples(NULL, -1)) // Open file
is_recording_samples = 1;
}
}
else if (is_recording_samples) {
record_samples(NULL, -2); // Close file
is_recording_samples = 0;
}
#if ! DEBUG_MIC
nSamples = quisk_process_samples(cSamples, nSamples);
#endif
#if DEBUG_IO > 1
QuiskPrintTime(" process samples", 0);
#endif
if (Playback.portaudio_index < 0)
quisk_play_alsa(&Playback, nSamples, cSamples, 1);
else
quisk_play_portaudio(&Playback, nSamples, cSamples, 1);
if (rxMode == 7) {
if (DigitalOutput.portaudio_index < 0)
quisk_play_alsa(&DigitalOutput, nSamples, cSamples, 0);
else
quisk_play_portaudio(&DigitalOutput, nSamples, cSamples, 0);
}
// Perhaps record the speaker audio to a file
if (want_record) {
if (is_recording_audio) {
record_audio(cSamples, nSamples); // Record samples
}
else if (file_name_audio[0]) {
if (record_audio(NULL, -1)) // Open file
is_recording_audio = 1;
}
}
else if (is_recording_audio) {
record_audio(NULL, -2); // Close file
is_recording_audio = 0;
}
#if DEBUG_IO > 1
QuiskPrintTime(" play samples", 0);
#endif
// Read and process the microphone
mic_count = 0;
mic_sample_rate = quisk_sound_state.mic_sample_rate;
if (MicCapture.handle) {
if (MicCapture.portaudio_index < 0)
mic_count = quisk_read_alsa(&MicCapture, cSamples);
else
mic_count = quisk_read_portaudio(&MicCapture, cSamples);
}
if (quisk_record_state == PLAYBACK) { // Discard previous samples and replace with saved sound
quisk_tmp_microphone(cSamples, mic_count);
}
if (rxMode == 7) { // Discard previous samples and use digital samples
if (DigitalInput.handle) {
mic_sample_rate = DigitalInput.sample_rate;
if (DigitalInput.portaudio_index < 0)
mic_count = quisk_read_alsa(&DigitalInput, cSamples);
else
mic_count = quisk_read_portaudio(&DigitalInput, cSamples);
}
else {
mic_count = 0;
}
}
if (mic_count > 0) {
#if DEBUG_IO > 1
QuiskPrintTime(" mic-read", 0);
#endif
// quisk_process_microphone returns samples at the sample rate MIC_OUT_RATE
mic_count = quisk_process_microphone(mic_sample_rate, cSamples, mic_count);
#if DEBUG_MIC == 1
if ( ! quisk_is_key_down())
for (i = 0; i < mic_count; i++)
cSamples[i] = 0;
for (i = 0; i < mic_count; i++)
cSamples[i] *= (double)CLIP32 / CLIP16; // convert 16-bit samples to 32 bits
quisk_process_samples(cSamples, mic_count);
#endif
#if DEBUG_IO > 1
QuiskPrintTime(" mic-proc", 0);
#endif
}
// Mic playback without a mic is needed for CW
if (MicPlayback.handle) { // Mic playback: send mic I/Q samples to a sound card
if (rxMode == 0 || rxMode == 1) { // Transmit CW
is_cw = 1;
}
else {
is_cw = 0;
cwCount = 0;
cwEnvelope = 0.0;
}
tx_mic_phase = cexp(( -I * 2.0 * M_PI * quisk_tx_tune_freq) / MicPlayback.sample_rate);
if (is_cw) { // Transmit CW; use capture device for timing, not microphone
cwCount += (double)retval * MicPlayback.sample_rate / quisk_sound_state.sample_rate;
mic_count = 0;
if (quisk_is_key_down()) {
while (cwCount >= 1.0) {
if (cwEnvelope < 1.0) {
cwEnvelope += 1. / (MicPlayback.sample_rate * 5e-3); // 5 milliseconds
if (cwEnvelope > 1.0)
cwEnvelope = 1.0;
}
cSamples[mic_count++] = (CLIP16 - 1) * cwEnvelope * tuneVector * quisk_sound_state.mic_out_volume;
tuneVector *= tx_mic_phase;
cwCount -= 1;
}
}
else { // key is up
while (cwCount >= 1.0) {
if (cwEnvelope > 0.0) {
cwEnvelope -= 1.0 / (MicPlayback.sample_rate * 5e-3); // 5 milliseconds
if (cwEnvelope < 0.0)
cwEnvelope = 0.0;
}
cSamples[mic_count++] = (CLIP16 - 1) * cwEnvelope * tuneVector * quisk_sound_state.mic_out_volume;
tuneVector *= tx_mic_phase;
cwCount -= 1;
}
}
}
else { // Transmit SSB
if ( ! quisk_is_key_down()) {
for (i = 0; i < mic_count; i++)
cSamples[i] = 0.0;
}
}
// Perhaps interpolate the mic samples back to the mic play rate
mic_interp = MicPlayback.sample_rate / MIC_OUT_RATE;
if ( ! is_cw && mic_interp > 1) {
if (! filtInterp.dCoefs)
quisk_filt_cInit(&filtInterp, quiskFilt12_19Coefs, sizeof(quiskFilt12_19Coefs)/sizeof(double));
mic_count = quisk_cInterpolate(cSamples, mic_count, &filtInterp, mic_interp);
}
// Tune the samples to frequency
if ( ! is_cw) {
for (i = 0; i < mic_count; i++) {
cSamples[i] = conj(cSamples[i]) * tuneVector * quisk_sound_state.mic_out_volume;
tuneVector *= tx_mic_phase;
}
}
// delay the I or Q channel by one sample
if (MicPlayback.channel_Delay >= 0)
delay_sample(&MicPlayback, (double *)cSamples, mic_count);
// amplitude and phase corrections
if (MicPlayback.doAmplPhase)
correct_sample (&MicPlayback, cSamples, mic_count);
// play mic samples
if (MicPlayback.portaudio_index < 0)
quisk_play_alsa(&MicPlayback, mic_count, cSamples, 0);
else
quisk_play_portaudio(&MicPlayback, mic_count, cSamples, 0);
#if DEBUG_MIC == 2
quisk_process_samples(cSamples, mic_count);
#endif
}
#if DEBUG_IO > 1
QuiskPrintTime(" finished", 0);
#endif
// Return negative number for error
return retval;
}
int quisk_get_overrange(void) // Called from GUI thread
{ // Return the overrange (ADC clip) counter, then zero it
int i;
i = quisk_sound_state.overrange + Capture.overrange;
quisk_sound_state.overrange = 0;
Capture.overrange = 0;
return i;
}
void quisk_close_sound(void) // Called from sound thread
{
if (pt_sample_stop)
(*pt_sample_stop)();
quisk_close_sound_portaudio();
quisk_close_sound_alsa(CaptureDevices, PlaybackDevices);
strncpy (quisk_sound_state.err_msg, CLOSED_TEXT, QUISK_SC_SIZE);
}
static void set_num_channels(struct sound_dev * dev)
{ // Set num_channels to the maximum channel index plus one
dev->num_channels = dev->channel_I;
if (dev->num_channels < dev->channel_Q)
dev->num_channels = dev->channel_Q;
dev->num_channels++;
}
void quisk_open_sound(void) // Called from GUI thread
{
int i;
quisk_sound_state.read_error = 0;
quisk_sound_state.write_error = 0;
quisk_sound_state.underrun_error = 0;
quisk_sound_state.mic_read_error = 0;
quisk_sound_state.interupts = 0;
quisk_sound_state.rate_min = quisk_sound_state.rate_max = -99;
quisk_sound_state.chan_min = quisk_sound_state.chan_max = -99;
quisk_sound_state.msg1[0] = 0;
quisk_sound_state.err_msg[0] = 0;
strncpy(Capture.name, quisk_sound_state.dev_capt_name, QUISK_SC_SIZE);
strncpy(Playback.name, quisk_sound_state.dev_play_name, QUISK_SC_SIZE);
strncpy(MicCapture.name, quisk_sound_state.mic_dev_name, QUISK_SC_SIZE);
strncpy(MicPlayback.name, quisk_sound_state.name_of_mic_play, QUISK_SC_SIZE);
Playback.sample_rate = quisk_sound_state.playback_rate; // Radio sound play rate
MicPlayback.sample_rate = quisk_sound_state.mic_playback_rate;
MicCapture.sample_rate = quisk_sound_state.mic_sample_rate;
MicCapture.channel_I = quisk_sound_state.mic_channel_I; // Mic audio is here
MicCapture.channel_Q = quisk_sound_state.mic_channel_Q;
// Capture device for digital sound for DGTL
strncpy(DigitalInput.name, QuiskGetConfigString ("digital_input_name", ""), QUISK_SC_SIZE);
DigitalInput.sample_rate = 48000;
DigitalInput.channel_I = 0;
DigitalInput.channel_Q = 1;
// Playback device for digital sound for DGTL
strncpy(DigitalOutput.name, QuiskGetConfigString ("digital_output_name", ""), QUISK_SC_SIZE);
DigitalOutput.sample_rate = quisk_sound_state.playback_rate; // Radio sound play rate
DigitalOutput.channel_I = 0;
DigitalOutput.channel_Q = 1;
set_num_channels (&Capture);
set_num_channels (&Playback);
set_num_channels (&MicCapture);
set_num_channels (&MicPlayback);
set_num_channels (&DigitalInput);
set_num_channels (&DigitalOutput);
#ifdef FIX_H101
Capture.channel_Delay = Capture.channel_Q; // Obsolete; do not use.
#else
Capture.channel_Delay = QuiskGetConfigInt ("channel_delay", -1);
#endif
MicPlayback.channel_Delay = QuiskGetConfigInt ("tx_channel_delay", -1);
if (pt_sample_read) { // capture from SDR-IQ by Rf-Space
Capture.name[0] = 0; // zero the capture soundcard name
}
else if (quisk_using_udp) { // samples from UDP at multiple of 48 kHz
Capture.name[0] = 0; // zero the capture soundcard name
}
else { // sound card capture
Capture.sample_rate = quisk_sound_state.sample_rate;
}
// set read size for sound card capture
i = (int)(quisk_sound_state.data_poll_usec * 1e-6 * Capture.sample_rate + 0.5);
i = i / 64 * 64;
if (i > SAMP_BUFFER_SIZE / Capture.num_channels) // limit to buffer size
i = SAMP_BUFFER_SIZE / Capture.num_channels;
Capture.read_frames = i;
MicCapture.read_frames = 0; // Use non-blocking read for microphone
Playback.read_frames = 0;
MicPlayback.read_frames = 0;
// set sound card play latency
Playback.latency_frames = Playback.sample_rate * quisk_sound_state.latency_millisecs / 1000;
MicPlayback.latency_frames = MicPlayback.sample_rate * quisk_sound_state.latency_millisecs / 1000;
Capture.latency_frames = 0;
MicCapture.latency_frames = 0;
// set capture and playback for digital mode DGTL
DigitalInput.read_frames = 0; // Use non-blocking read
DigitalInput.latency_frames = 0;
DigitalOutput.read_frames = 0;
DigitalOutput.latency_frames = DigitalOutput.sample_rate * 500 / 1000; // 500 milliseconds
#if DEBUG_IO
printf("Sample buffer size %d, latency msec %d\n", SAMP_BUFFER_SIZE, quisk_sound_state.latency_millisecs);
#endif
}
void quisk_start_sound(void) // Called from sound thread
{
if (pt_sample_start)
(*pt_sample_start)();
quisk_start_sound_portaudio(CaptureDevices, PlaybackDevices);
quisk_start_sound_alsa(CaptureDevices, PlaybackDevices);
if (pt_sample_read || quisk_using_udp) {
quisk_sound_state.rate_min = Playback.rate_min;
quisk_sound_state.rate_max = Playback.rate_max;
quisk_sound_state.chan_min = Playback.chan_min;
quisk_sound_state.chan_max = Playback.chan_max;
}
else {
quisk_sound_state.rate_min = Capture.rate_min;
quisk_sound_state.rate_max = Capture.rate_max;
quisk_sound_state.chan_min = Capture.chan_min;
quisk_sound_state.chan_max = Capture.chan_max;
}
}
PyObject * quisk_set_ampl_phase(PyObject * self, PyObject * args) // Called from GUI thread
{ /* Set the sound card amplitude and phase corrections. See
S.W. Ellingson, Correcting I-Q Imbalance in Direct Conversion Receivers, February 10, 2003 */
struct sound_dev * dev;
double ampl, phase;
int is_tx; // Is this for Tx? Otherwise Rx.
if (!PyArg_ParseTuple (args, "ddi", &l, &phase, &is_tx))
return NULL;
if (is_tx)
dev = &MicPlayback;
else
dev = &Capture;
if (ampl == 0.0 && phase == 0.0) {
dev->doAmplPhase = 0;
}
else {
dev->doAmplPhase = 1;
ampl = ampl + 1.0; // Change factor 0.01 to 1.01
phase = (phase / 360.0) * 2.0 * M_PI; // convert to radians
dev->AmPhAAAA = 1.0 / ampl;
dev->AmPhCCCC = - dev->AmPhAAAA * tan(phase);
dev->AmPhDDDD = 1.0 / cos(phase);
}
Py_INCREF (Py_None);
return Py_None;
}
PyObject * quisk_capt_channels(PyObject * self, PyObject * args) // Called from GUI thread
{
if (!PyArg_ParseTuple (args, "ii", &Capture.channel_I, &Capture.channel_Q))
return NULL;
Py_INCREF (Py_None);
return Py_None;
}
PyObject * quisk_play_channels(PyObject * self, PyObject * args) // Called from GUI thread
{
if (!PyArg_ParseTuple (args, "ii", &Playback.channel_I, &Playback.channel_Q))
return NULL;
Py_INCREF (Py_None);
return Py_None;
}
PyObject * quisk_micplay_channels(PyObject * self, PyObject * args) // Called from GUI thread
{
if (!PyArg_ParseTuple (args, "ii", &MicPlayback.channel_I, &MicPlayback.channel_Q))
return NULL;
Py_INCREF (Py_None);
return Py_None;
}
static void AddCard(struct sound_dev * dev, PyObject * pylist, const char * txt)
{
PyObject * v;
if (dev->name[0]) {
v = Py_BuildValue("(ssiii)", txt, dev->name, dev->sample_rate, dev->dev_latency, dev->dev_error + dev->dev_underrun);
PyList_Append(pylist, v);
}
}
PyObject * quisk_sound_errors(PyObject * self, PyObject * args)
{ // return a list of strings with card names and error counts
PyObject * pylist;
if (!PyArg_ParseTuple (args, ""))
return NULL;
pylist = PyList_New(0);
AddCard(&Capture, pylist, "Capture radio samples");
AddCard(&MicCapture, pylist, "Capture microphone samples");
AddCard(&DigitalInput, pylist, "Capture digital Tx samples");
AddCard(&Playback, pylist, "Play radio sound");
AddCard(&MicPlayback, pylist, "Play microphone sound");
AddCard(&DigitalOutput, pylist, "Play digital mode sound");
return pylist;
}
PyObject * quisk_set_file_record(PyObject * self, PyObject * args) // called from GUI
{ /* set the names of the recording files and the recording state */
const char * name;
int which;
if (!PyArg_ParseTuple (args, "is", &which, &name))
return NULL;
switch (which) {
case 0: // change the name of the audio file
strncpy(file_name_audio, name, QUISK_PATH_SIZE);
break;
case 1: // change the name of the sample file
strncpy(file_name_samples, name, QUISK_PATH_SIZE);
break;
case 2: // the record button was pressed
want_record = 1;
break;
case 3: // the record button was un-pressed
want_record = 0;
break;
}
Py_INCREF (Py_None);
return Py_None;
}