forked from mlang/linux-si470x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux-si470x.c
1333 lines (1179 loc) · 38.1 KB
/
linux-si470x.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
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
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include <linux/videodev2.h>
#define DEFAULT_RADIO_DEVICE "/dev/radio0"
#define DEFAULT_AUDIO_DEVICE "hw:Music"
#define MAX_VOLUME 100
#define MAX_CHANNELS 2
#define HAVE_JACK 1
static int verbose = 0;
static int frequencyDivider;
static float minFrequency, currentFrequency, maxFrequency;
static void
setTunerVolume(int fd, unsigned int volume) {
struct v4l2_control control = {
.id = V4L2_CID_AUDIO_MUTE,
.value = (volume==0? 1 : 0)
};
if (ioctl(fd, VIDIOC_S_CTRL, &control) != -1) {
struct v4l2_queryctrl queryctrl = {
.id = V4L2_CID_AUDIO_VOLUME
};
if (ioctl(fd, VIDIOC_QUERYCTRL, &queryctrl) != -1) {
if (volume > MAX_VOLUME) volume = MAX_VOLUME;
memset(&control, 0, sizeof(control));
control.id = V4L2_CID_AUDIO_VOLUME;
control.value =
queryctrl.minimum
+ volume * (queryctrl.maximum - queryctrl.minimum) / MAX_VOLUME;
if (ioctl(fd, VIDIOC_S_CTRL, &control) != -1) {
return;
} else {
perror("ioctl VIDIOC_S_CTRL");
}
} else {
perror("ioctl VIDIOC_QUERYCTRL");
}
} else {
perror("ioctl VIDIOC_S_CTRL");
}
}
static void
setTunerFrequency(int fd, struct v4l2_tuner *tuner, float newFrequency) {
if (newFrequency < maxFrequency && newFrequency > minFrequency) {
struct v4l2_frequency freq;
memset(&freq, 0, sizeof(freq));
freq.tuner = 0;
freq.type = V4L2_TUNER_RADIO;
freq.frequency = newFrequency * frequencyDivider;
if (ioctl(fd, VIDIOC_S_FREQUENCY, &freq) != -1) {
return;
} else {
perror("ioctl VIDIOC_S_FREQUENCY");
}
} else {
printf("%.2f is not in range (%.2f - %.2f)\n",
newFrequency, minFrequency, maxFrequency);
}
}
static float
getTunerFrequency(int fd) {
struct v4l2_frequency freq;
memset(&freq, 0, sizeof(freq));
freq.tuner = 0;
freq.type = V4L2_TUNER_RADIO;
if (ioctl(fd, VIDIOC_G_FREQUENCY, &freq) != -1) {
return freq.frequency / (float)frequencyDivider;
} else {
perror("ioctl VIDIOC_G_FREQUENCY");
}
return 0;
}
static float
seekTunerFrequency(int fd, int up) {
struct v4l2_hw_freq_seek freqSeek;
memset(&freqSeek, 0, sizeof(freqSeek));
freqSeek.tuner = 0;
freqSeek.type = V4L2_TUNER_RADIO;
freqSeek.seek_upward = up? 1 : 0;
freqSeek.wrap_around = 1;
if (ioctl(fd, VIDIOC_S_HW_FREQ_SEEK, &freqSeek) != -1) {
return getTunerFrequency(fd);
} else {
perror("ioctl VIDIOC_S_HW_FREQ_SEEK");
}
return 0;
}
/* Radio (Broadcast) Data System */
static const char *programTypes[30] = {
"News", "Current affairs", "Information", "Sport",
"Education", "Drama", "Culture", "Science", "Varied", "Pop music",
"Rock music", "Easy listening", "Light classical", "Serious classical",
"Other music", "Weather", "Finance", "Children's programmes",
"Social affairs", "Religion" "Phone-in", "Travel", "Leisure", "Jazz music",
"Country music", "National music", "Oldies music", "Folk music",
"Documentary", "Alarm test", "Alarm"
};
typedef struct {
uint16_t id;
float freq;
char name[8+1];
unsigned char tp;
unsigned char ta;
char type;
} ProgramData;
static ProgramData *programs = NULL;
static int programCount = 0;
static ProgramData *
getProgram(uint16_t id) {
for (int i = 0; i < programCount; i++) {
ProgramData *pd = &programs[i];
if (pd->id == id) return pd;
}
programs = realloc(programs, ++programCount*sizeof(*programs));
{
ProgramData *pd = &programs[programCount - 1];
memset(pd, 0, sizeof(*pd));
pd->id = id;
return pd;
}
}
/* For restoring cannonical mode upon exit */
static struct termios savedTerminalSettings;
static void
parent_sigterm_handler(int signal) {
tcsetattr(0, TCSAFLUSH, &savedTerminalSettings);
kill(0, signal);
}
static void
disableCannonicalMode() {
struct termios termios_p;
const int fd = 0;
tcgetattr(fd, &termios_p);
savedTerminalSettings = termios_p;
termios_p.c_lflag &= ~(ECHO|ICANON);
tcsetattr(fd, TCSAFLUSH, &termios_p);
signal(SIGTERM, parent_sigterm_handler);
signal(SIGINT, parent_sigterm_handler);
}
static int
EONAF_handleFrequencyPair(ProgramData *this, ProgramData *other, float f1, float f2) {
if (this->freq >= minFrequency) {
if (f1 >= (this->freq-.04) && f1 <= (this->freq+.04)) {
other->freq = f2;
return 1;
}
}
return 0;
}
static void
nextProgram(int fd, struct v4l2_tuner *tuner) {
int i;
if (programCount <= 1) return;
for (i = 0; i < programCount; i++) {
if (currentFrequency >= programs[i].freq-.09
&& currentFrequency <= programs[i].freq+.09) {
int next = (i == programCount - 1)? 0 : i + 1;
while (next != i) {
float freq = programs[next].freq;
if (freq >= minFrequency) {
if (programs[next].name[0])
printf("Switching to %s (%.2f)\n", programs[next].name, freq);
setTunerFrequency(fd, tuner, freq);
currentFrequency = freq;
return;
}
if (next == programCount - 1) next = 0; else next += 1;
}
printf("No other stations known\n");
return;
}
}
}
static inline void
decodeRds(int fd, struct v4l2_tuner *tuner) {
typedef enum {
TYPE_0A = 0, TYPE_0B, /* Basic tuning and switching information */
TYPE_1A, TYPE_1B, /* Program-item number and slow labeling codes */
TYPE_2A, TYPE_2B, /* Radiotext */
TYPE_3A, /* Applications Identification for Open Data */
TYPE_3B, /* Open data application */
TYPE_4A, /* Clock-time and date */
TYPE_4B, /* Open data application */
TYPE_5A, TYPE_5B, /* Transparent data channels or ODA */
TYPE_6A, TYPE_6B, /* In house applications or ODA */
TYPE_7A, /* Radio paging or ODA */
TYPE_7B, /* Open data application */
TYPE_8A, TYPE_8B, /* Traffic Message Channel or ODA */
TYPE_9A, TYPE_9B, /* Emergency warning systems or ODA */
TYPE_10A, /* Program Type Name */
TYPE_10B, /* Open data */
TYPE_11A, TYPE_11B, /* Open data application */
TYPE_12A, TYPE_12B, /* Open data application */
TYPE_13A, /* Enhanced Radio paging or ODA */
TYPE_13B, /* Open data application */
TYPE_14A, TYPE_14B, /* Enhanced Other Networks information */
TYPE_15A,
TYPE_15B /* Fast tuning and switching information */
} RDS_GroupType;
struct rds_data {
uint8_t lsb;
uint8_t msb;
uint8_t block;
} __attribute__((packed)) rdsData;
ssize_t count;
int blockCount = 0;
int errorCount = 0;
RDS_GroupType groupType;
unsigned char groupData[2*4];
unsigned char lastGroupData[2*4];
ProgramData *thisProgram = NULL;
char programName[8+1] = {0};
char *lastProgramName = NULL;
char stereoKnown = 0;
char isStereo;
char ta = 0;
int freqCounter = 0;
char radioText[4*0X10 + 1] = {0};
char radioTextabFlag = 0;
memset(radioText, ' ', 4*0X10);
radioText[4*0X10] = 0;
if (isatty(STDIN_FILENO)) {
disableCannonicalMode();
}
while (1) {
struct pollfd fds[] = {
{ .fd = fd, .events = POLLIN },
{ .fd = STDIN_FILENO, .events = POLLIN }
};
const int fdCount = sizeof(fds)/sizeof(*fds);
int pollval = poll(fds, fdCount, 1000);
if (pollval == 0) {
if (verbose) printf("No RDS data\n");
continue;
} else if (pollval == -1) {
perror("poll");
break;
}
for (int i = 0; i < fdCount; i++) {
if (fds[i].revents & fds[i].events) {
if (fds[i].fd == fd) {
count = read(fd, &rdsData, sizeof(rdsData));
if (count == 0) break;
if (count != sizeof(rdsData)) {
printf("ERR: Incomplete RDS block, count was %d\n", (int)count);
continue;
}
} else if (fds[i].fd == STDIN_FILENO) {
uint8_t c;
count = read(STDIN_FILENO, &c, 1);
if (count == 1) {
switch (c) {
case 'n': nextProgram(fd, tuner); break;
case '+': {
currentFrequency += .05;
if (currentFrequency > maxFrequency)
currentFrequency = minFrequency;
setTunerFrequency(fd, tuner, currentFrequency);
printf("Frequency tuned to %.2f\n", currentFrequency);
break;
}
case '-': {
currentFrequency -= .05;
if (currentFrequency < minFrequency)
currentFrequency = maxFrequency;
setTunerFrequency(fd, tuner, currentFrequency);
printf("Frequency tuned to %.2f\n", currentFrequency);
break;
}
default:
printf("Keyboard: %d (%X)\n", c, c);
}
continue;
} else if (count == 0) {
break;
}
}
}
}
int blockNumber = rdsData.block & 0X07;
int error = (rdsData.block&0X80)==0X80;
blockCount += 1;
if (error) {
errorCount += 1;
if (verbose) printf("%d errors in %d blocks so far\n",
errorCount, blockCount);
continue;
}
if (blockNumber == 0) {
thisProgram = getProgram(rdsData.msb<<8|rdsData.lsb);
thisProgram->freq = currentFrequency;
}
if (blockNumber == 1) {
int ptyCode = ((rdsData.msb << 3) & 0X18) | ((rdsData.lsb >> 5) & 0X07);
if (thisProgram != NULL && ptyCode != 0) {
if (thisProgram->type != ptyCode) {
thisProgram->type = ptyCode;
if (ptyCode > 0) printf("Program type: %s\n",
programTypes[ptyCode-1]);
}
}
groupType = (RDS_GroupType)rdsData.msb>>3;
}
groupData[2*blockNumber] = rdsData.msb;
groupData[2*blockNumber+1] = rdsData.lsb;
if (blockNumber == 3) {
if (memcmp(groupData, lastGroupData, sizeof(groupData)) == 0)
continue;
switch (groupType) {
case TYPE_0A: {
char TP = (groupData[2] & 0x04) == 0X04;
char isTrafficAnnouncement = (groupData[3] & 0x10) == 0X10;
char isMusic = (groupData[3] & 0x08) == 0X08;
int index = (groupData[3] & 0x03) << 1;
if (TP && isTrafficAnnouncement != ta) {
ta = isTrafficAnnouncement;
printf("Traffic announcement %s\n", ta? "on" : "off");
}
programName[index] = groupData[6];
programName[index+1] = groupData[7];
if (strlen(programName) && index == 6) {
if (lastProgramName == NULL
|| strcmp(programName, lastProgramName) != 0) {
printf("Program: %s\n", programName);
if (lastProgramName != NULL) free(lastProgramName);
lastProgramName = strdup(programName);
}
programName[0] = 0;
}
switch (groupData[3]&0X03) {
case 3:
if (!stereoKnown) {
isStereo = ((groupData[3]&0X04)==0X04);
stereoKnown = 1;
printf("Program is %s\n", isStereo? "stereo" : "mono");
}
if (isStereo != ((groupData[3]&0X04)==0X04)) {
isStereo = ((groupData[3]&0X04)==0X04);
printf("Program is %s\n", isStereo? "stereo" : "mono");
}
break;
}
if ((groupData[4]>=224)&&(groupData[4]<=249)) {
freqCounter = groupData[4] - 224;
if (freqCounter) {
if ((groupData[5]>=1) && ((groupData[5]<=204))) {
float f = ((100*(groupData[5]-1))+87600)/1000.0;
freqCounter -= 1;
}
}
} else if (freqCounter > 0) {
float f1 = ((100*(groupData[4]-1))+87600)/1000.0;
float f2 = ((100*(groupData[5]-1))+87600)/1000.0;
freqCounter -= 2;
if (freqCounter == 0) {
//printf("AFlist done\n");
}
}
break;
}
case TYPE_2A: {
int index = groupData[3]&0X0F;
int newabFlag = (groupData[3]&0X10)==0X10;
if (newabFlag != radioTextabFlag) {
radioTextabFlag = newabFlag;
{
int i = 63;
while (i >= 0) {
if (radioText[i] == 0) {
i -= 1;
continue;
}
if ((radioText[i] == ' ') || (radioText[i] == '\r')) {
radioText[i] = 0;
i -= 1;
continue;
}
break;
}
}
if (strlen(radioText) > 0) {
printf("Text: %s\n", radioText);
}
memset(radioText, ' ', 4*0X10);
}
for (int i = 0; i < 4; i++) {
radioText[4*index + i] = groupData[4+i];
}
}
break;
case TYPE_4A: {
const int monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
const int julianDate = ((groupData[3]&0X03)<<15)
| (groupData[4]<<7)
| (groupData[5]>>1);
int year = (int)(((double)julianDate - 15078.2)/365.25);
int month = (int)(((julianDate - 14956.1)-(int)(year*365.25))/30.6001);
int day = julianDate-14956-(int)(year*365.25)-(int)(month*30.6001);
int utcHour = ((groupData[5]&0X01)<<4)
| ((groupData[6]&0XF0)>>4);
int utcMinute = ((groupData[6]&0X0F)<<2)
| ((groupData[7]&0XC0)>>6);
int utcOffset = groupData[7]&0X1F;
int K = ((month == 14)||(month == 15))? 1 : 0;
if (groupData[7]&0X20) utcOffset = -utcOffset;
year = year + K + 1900;
month = month - 1 - (K*12);
{ /* Calculate local time */
int localHour = utcHour;
int localMinute = utcMinute + (utcOffset*30);
while (localMinute < 0) { localMinute += 60, localHour -= 1; }
while (localMinute >= 60) { localMinute -= 60, localHour += 1; }
if (localHour < 0) {
localHour += 24, day -= 1;
if (day < 1) {
month -= 1;
if (month < 1) {
month = 12;
year -= 1;
}
day = monthDays[month-1];
if (((year % 4) == 0) && (month == 2)) day = 29;
}
}
if (localHour >= 24) {
localHour -= 24, day += 1;
int maxDay = (((year%4)==0)&&(month==2))? 29 : monthDays[month-1];
if (day > maxDay) {
month += 1;
if (month > 12) {
month = 1, year += 1;
}
}
}
printf("Date: %04d-%02d-%02d %02d:%02d (%c%02d:%02d)\n",
year, month, day, localHour, localMinute,
(utcOffset > 0)? '+' : '-',
utcOffset*30 / 60, (utcOffset*30) % 60);
}
break;
}
case TYPE_8A: {
typedef enum {TMC_GROUP=0, TMC_SINGLE, TMC_SYSTEM, TMC_TUNING} TMC_Type;
TMC_Type tmctype = (groupData[3]&0X18)>>3;
int CI = groupData[3]&0X07;
int extent = (groupData[4]&0X38)>>3;
int event = ((groupData[4]&0X07)<<8)|groupData[5];
uint16_t location = groupData[6]<<8|groupData[7];
switch (tmctype) {
case TMC_SINGLE: {
int duration = CI;
char *durStr = NULL;
switch (duration) {
case 0: durStr = "unknown"; break;
case 1: durStr = "15 minutes"; break;
case 2: durStr = "30 minutes"; break;
case 3: durStr = "1 hour"; break;
case 4: durStr = "2 hours"; break;
case 5: durStr = "3 hour"; break;
case 6: durStr = "4 hour"; break;
case 7: durStr = "rest of the day"; break;
}
printf("TMC(single): evt=%X, loc=%X, extent=%X, dur=%s\n",
event, location, extent, durStr);
break;
}
default:
if (verbose) printf("TMC: Type=%X, CI=%X, event=%X, loc=%X\n",
tmctype, CI, event, location);
}
break;
}
case TYPE_14A: {
int TPON = (groupData[3]&0X10)==0X10;
int variantType = groupData[3]&0X0F;
int info = (groupData[4]<<8)|(groupData[5]);
int PION = (groupData[6]<<8)|(groupData[7]);
ProgramData *otherProgram = getProgram(PION);
switch (variantType) {
case 0:
case 1:
case 2:
case 3: {
otherProgram->name[2*variantType] = groupData[4];
otherProgram->name[2*variantType+1] = groupData[5];
break;
}
case 5: {
uint8_t lsb = groupData[5];
uint8_t msb = groupData[4];
if (thisProgram != NULL
&& EONAF_handleFrequencyPair(thisProgram, otherProgram,
((100*(msb-1))+87600)/1000.0,
((100*(lsb-1))+87600)/1000.0)) {
if (verbose && otherProgram->name && otherProgram->name[0])
printf("%s is on %.2fMHz\n", otherProgram->name, otherProgram->freq);
}
break;
}
case 0XD: {
int TAON = groupData[5]&0X01;
if (TPON && TAON) {
if (TAON != otherProgram->ta) {
if (otherProgram->name && otherProgram->name[0])
printf("Traffic Announcement on %s is %s\n",
otherProgram->name, TAON? "on" : "off");
else
printf("Traffic Announcement on %X is %s\n",
PION, TAON? "on" : "off");
otherProgram->ta = TAON;
}
}
break;
}
default:
if (verbose) printf("EON: TPON=%d, v=%X, info=%X, PION=%X\n",
TPON, variantType, info, PION);
break;
}
}
default:
if (verbose > 1) {
printf("Group(%X): %02X%02X-%02X%02X-%02X%02X-%02X%02X\n",
groupType,
groupData[0], groupData[1], groupData[2], groupData[3],
groupData[4], groupData[5], groupData[6], groupData[7]);
}
}
memcpy(lastGroupData, groupData, sizeof(groupData));
memset(groupData, 0, sizeof(groupData));
}
}
if (lastProgramName != NULL) free(lastProgramName);
tcsetattr(0, TCSAFLUSH, &savedTerminalSettings);
}
/* Audio I/O */
#include <alsa/asoundlib.h>
#include <samplerate.h>
static snd_pcm_t *pcmIn;
static unsigned int inputSampleRate = 96000;
static char num_channels = 2;
static unsigned int period_size = 2048, num_periods = 4; /* 85ms */
static unsigned int resample_quality = 3;
#ifdef HAVE_JACK
#include <alloca.h>
#include <math.h>
#include <jack/jack.h>
static jack_client_t *jackClient;
static jack_port_t *jackPorts[MAX_CHANNELS];
static SRC_STATE *srcs[MAX_CHANNELS];
static int jackSampleRate, jackBufferSize;
static int quit = 0;
static double resample_mean = 1.0;
static double static_resample_factor = 1.0;
static double *offset_array;
static double *window_array;
static int offset_differential_index = 0;
static double offset_integral = 0;
static int target_delay = 0; /* the delay which the program should try to approach. */
static int max_diff = 0; /* the diff value, when a hard readpointer skip should occur */
static int catch_factor = 100000, catch_factor2 = 10000;
static double pclamp = 15.0;
static double controlquant = 10000.0;
static const int smooth_size = 512;
// Debug stuff:
volatile float output_resampling_factor = 1.0;
volatile int output_new_delay = 0;
volatile float output_offset = 0.0;
volatile float output_integral = 0.0;
volatile float output_diff = 0.0;
typedef struct {
snd_pcm_format_t format_id;
size_t sample_size;
void (*soundcard_to_jack) (jack_default_audio_sample_t *dst, char *src,
unsigned long nsamples, unsigned long src_skip);
} alsa_format_t;
#define SAMPLE_16BIT_SCALING 32767.0f
static void
sample_move_dS_s16(jack_default_audio_sample_t *dst, char *src,
unsigned long nsamples, unsigned long src_skip)
{
while (nsamples--) {
*dst = (*((short *)src)) / SAMPLE_16BIT_SCALING;
dst += 1, src += src_skip;
}
}
static alsa_format_t formats[] = {
{ SND_PCM_FORMAT_S16, 2, sample_move_dS_s16 }
};
#define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
static int format = 0;
static int
set_hwformat(snd_pcm_t *handle, snd_pcm_hw_params_t *params) {
int err;
for (int i=0; i<NUMFORMATS; i++) {
err = snd_pcm_hw_params_set_format(handle, params, formats[i].format_id);
if (err == 0) {
format = i;
break;
}
}
return err;
}
static int
xrun_recovery(snd_pcm_t *handle, int err) {
if (err == -EPIPE) { /* under-run */
err = snd_pcm_prepare(handle);
if (err < 0)
printf("Can't recovery from underrun, prepare failed: %s\n",
snd_strerror(err));
} else if (err == -EAGAIN) {
while ((err = snd_pcm_resume(handle)) == -EAGAIN)
usleep(100); /* wait until the suspend flag is released */
if (err < 0) {
err = snd_pcm_prepare(handle);
if (err < 0)
printf("Can't recovery from suspend, prepare failed: %s\n",
snd_strerror(err));
}
}
return err;
}
static inline int
set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params,
snd_pcm_access_t access, unsigned int rate, unsigned int channels,
unsigned int period, unsigned int nperiods) {
int err, dir=0;
unsigned int buffer_time;
unsigned int period_time;
unsigned int rrate;
/* choose all parameters */
if ((err = snd_pcm_hw_params_any(handle, params)) == 0) {
if ((err = snd_pcm_hw_params_set_access(handle, params, access)) == 0) {
if ((err = set_hwformat(handle, params)) == 0) {
unsigned int rchannels = channels;
if ((err = snd_pcm_hw_params_set_channels_near(handle, params,
&rchannels)) == 0) {
if (rchannels != channels) {
printf("WARNING: channel count does not match (requested %d got %d)\n",
channels, rchannels);
num_channels = rchannels;
}
rrate = rate;
if ((err = snd_pcm_hw_params_set_rate_near(handle, params,
&rrate, NULL)) >= 0) {
if (rrate != rate) {
printf("WARNING: Rate doesn't match (requested %iHz, get %iHz)\n", rate,
rrate);
inputSampleRate = rrate;
}
buffer_time = 1000000*(uint64_t)period*nperiods/rrate;
printf("buffer_time = %d\n", buffer_time);
if ((err = snd_pcm_hw_params_set_buffer_time_near(handle, params,
&buffer_time,
&dir)) >= 0) {
snd_pcm_uframes_t real_buffer_size;
if ((err = snd_pcm_hw_params_get_buffer_size(params,
&real_buffer_size))
>= 0) {
printf("Buffer size: %d\n", (int)real_buffer_size);
if (real_buffer_size != nperiods * period) {
printf("WARNING: buffer size does not match: "
"requested %d, got %d\n",
nperiods * period, (int) real_buffer_size);
}
/* set the period time */
printf("period_time = %d\n", period_time = 1000000U*(uint64_t)period/rrate);
if ((err = snd_pcm_hw_params_set_period_time_near(handle,
params,
&period_time,
&dir))
== 0) {
snd_pcm_uframes_t real_period_size;
if ((err = snd_pcm_hw_params_get_period_size(params,
&real_period_size, NULL))
== 0) {
printf("Period size: %d\n", (int)real_period_size);
if (real_period_size != period) {
printf("WARNING: period size does not match: "
"requested %i, got %i\n",
period, (int)real_period_size);
}
/* write the parameters to device */
if ((err = snd_pcm_hw_params(handle, params)) == 0) {
if (verbose)
printf("Input buffer time: %.1fms\n",
1000.0/(rrate/(float)real_buffer_size));
return 0;
} else {
printf("Unable to set hw params for capture: %s\n",
snd_strerror(err));
}
} else {
printf("Unable to get period size back: %s\n",
snd_strerror(err));
}
} else {
printf("Unable to set period time %i for capture: %s\n",
(int)(1000000*(uint64_t)period/rate),
snd_strerror(err));
}
} else {
printf("Unable to get buffer size back: %s\n",
snd_strerror(err));
}
} else {
printf("Unable to set buffer time %i for capture: %s\n",
(int)(1000000*(uint64_t)period*nperiods/rate),
snd_strerror(err));
}
} else {
printf("Rate %iHz not available for playback: %s\n",
rate, snd_strerror(err));
}
} else {
printf("Channels count (%i) not available for record: %s\n",
channels, snd_strerror(err));
}
} else {
printf("Sample format not available for playback: %s\n",
snd_strerror(err));
}
} else {
printf("Access type not available for capture: %s\n", snd_strerror(err));
}
} else {
printf("No configurations available for capture: %s\n", snd_strerror(err));
}
return err;
}
static inline int
set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams, int period) {
int err;
if ((err = snd_pcm_sw_params_current(handle, swparams)) == 0) {
/* start the transfer when the buffer is full */
if ((err = snd_pcm_sw_params_set_start_threshold(handle, swparams,
period)) >= 0) {
if ((err = snd_pcm_sw_params_set_stop_threshold(handle, swparams,
-1)) >= 0) {
if ((err = snd_pcm_sw_params_set_avail_min(handle, swparams,
2*period)) >= 0) {
if ((err = snd_pcm_sw_params(handle, swparams)) == 0) {
return 0;
} else {
printf("Unable to set sw params for capture: %s\n",
snd_strerror(err));
}
} else {
printf("Unable to set avail min for capture: %s\n",
snd_strerror(err));
}
} else {
printf("Unable to set start threshold mode for capture: %s\n",
snd_strerror(err));
}
} else {
printf("Unable to set start threshold mode for capture: %s\n",
snd_strerror(err));
}
} else {
printf("Unable to determine current sw params for capture: %s\n",
snd_strerror(err));
}
return err;
}
static inline snd_pcm_t *
openAudioIn(char *device, int rate, int channels, int period, int nperiods) {
int err;
snd_pcm_t *handle;
if ((err = snd_pcm_open(&(handle), device,
SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) == 0) {
snd_pcm_hw_params_t *hwparams;
snd_pcm_hw_params_alloca(&hwparams);
if ((err = set_hwparams(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED,
rate, channels, period, nperiods)) == 0) {
snd_pcm_sw_params_t *swparams;
snd_pcm_sw_params_alloca(&swparams);
if ((err = set_swparams(handle, swparams, period)) == 0) {
snd_pcm_start(handle);
snd_pcm_wait(handle, 100);
return handle;
} else {
printf("Setting of swparams failed: %s\n", snd_strerror(err));
}
} else {
printf("Setting of hwparams failed: %s\n", snd_strerror(err));
}
} else {
printf("AudioIn open error: %s\n", snd_strerror(err));
}
return NULL;
}
#define MIN_RESAMPLE_FACTOR 0.25
#define MAX_RESAMPLE_FACTOR 4.0
static int process(jack_nframes_t nframes, void *arg) {
int err;
snd_pcm_sframes_t delay = snd_pcm_avail(pcmIn);
int i;
delay -= jack_frames_since_cycle_start(jackClient);
if (delay > (target_delay+max_diff)) {
const int skipFrames = delay-target_delay;
char tmp[skipFrames * formats[format].sample_size * num_channels];
printf("Skipping %d frames\n", skipFrames);
int count = skipFrames;
while (count > 0) {
int amount = snd_pcm_readi(pcmIn, tmp, skipFrames);
if (amount == -EAGAIN) continue;
if (amount < 0) {
xrun_recovery(pcmIn, amount);
continue;
}
count -= amount;
}
output_new_delay = (int)delay;
delay += skipFrames;
// Set the resample_rate... we need to adjust the offset integral, to do this.
// first look at the PI controller, this code is just a special case, which should never execute once
// everything is swung in.
offset_integral = - (resample_mean - static_resample_factor)
* catch_factor * catch_factor2;
// Also clear the array. we are beginning a new control cycle.
for (i=0; i<smooth_size; i++) offset_array[i] = 0.0;
}
if (delay < (target_delay-max_diff)) {
int rewound = snd_pcm_rewind(pcmIn, target_delay - delay);
printf("Rewound %d, delay was %d\n", rewound, (int)delay);
output_new_delay = (int)delay;
delay += rewound;
// Set the resample_rate... we need to adjust the offset integral, to do this.
offset_integral = - (resample_mean - static_resample_factor)
* catch_factor * catch_factor2;
for (i=0; i<smooth_size; i++) offset_array[i] = 0.0;
}
/* ok... now we should have target_delay +- max_diff on the alsa side.
*
* calculate the number of frames, we want to get.
*/
double offset = delay - target_delay;
// Save offset.
offset_array[(offset_differential_index++)%smooth_size] = offset;
// Build the mean of the windowed offset array
// basically fir lowpassing.
double smooth_offset = 0.0;
for (i=0; i<smooth_size; i++)
smooth_offset += offset_array[(i + offset_differential_index-1)
% smooth_size] * window_array[i];
smooth_offset /= (double)smooth_size;
// this is the integral of the smoothed_offset
offset_integral += smooth_offset;
// Clamp offset.
// the smooth offset still contains unwanted noise
// which would go straigth onto the resample coeff.
// it only used in the P component and the I component is used for the fine tuning anyways.
if (fabs(smooth_offset) < pclamp) smooth_offset = 0.0;
// ok. now this is the PI controller.
// u(t) = K * ( e(t) + 1/T \int e(t') dt' )
// K = 1/catch_factor and T = catch_factor2
double current_resample_factor = static_resample_factor
- smooth_offset / (double)catch_factor
- offset_integral / (double)catch_factor
/ (double)catch_factor2;
// quantize around resample_mean, so that noise in the integral component doesnt hurt.
current_resample_factor = floor((current_resample_factor - resample_mean)
* controlquant + 0.5)
/ controlquant + resample_mean;
output_resampling_factor = (float)current_resample_factor;
output_diff = (float) smooth_offset;
output_integral = (float) offset_integral;
output_offset = (float) offset;
// Clamp a bit.
if (current_resample_factor < MIN_RESAMPLE_FACTOR)
current_resample_factor = MIN_RESAMPLE_FACTOR;
else if (current_resample_factor > MAX_RESAMPLE_FACTOR)
current_resample_factor = MAX_RESAMPLE_FACTOR;
// Calculate resample_mean so we can init ourselves to saner values.
resample_mean = 0.9999 * resample_mean + 0.0001 * current_resample_factor;
{
char *outbuf;
float *resampbuf;
int rlen = ceil(((double)nframes) / current_resample_factor)+2;
int framesToRead = rlen;