-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsetad.c
6491 lines (6352 loc) · 169 KB
/
lsetad.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
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include "globdef.h"
#include "uidef.h"
#include "sigdef.h"
#include "fft1def.h"
#include "fft2def.h"
#include "fft3def.h"
#include "conf.h"
#include "lconf.h"
#include "screendef.h"
#include "sdrdef.h"
#include "vernr.h"
#include "rusage.h"
#include "thrdef.h"
#include "hwaredef.h"
#include "ldef.h"
#include "txdef.h"
#include "options.h"
#include "keyboard_def.h"
#include "padef.h"
#define ABOVE_MAX_SPEED 768000
#define MAX_COLUMN 80
#define DEVFLAG_R 1
#define DEVFLAG_W 2
#define DEVFLAG_RW 4
#if HAVE_OSS == 1
char dev_name[sizeof(SND_DEV)+2];
#endif
int devmodes[3]={O_RDONLY,O_WRONLY,O_RDWR};
char *devmode_txt[3]={"RDONLY","WRONLY","RDWR"};
#define DEVMODE_RDONLY 0
#define DEVMODE_WRONLY 1
#define DEVMODE_RDWR 2
double round(double x);
void thread_test_rxad(void);
void thread_test_rxda(void);
void thread_test_txda(void);
pthread_t thread_identifier_soundcard_test[MAX_IOTEST];
ROUTINE thread_iotest[MAX_IOTEST]={thread_test_rxad, //0
thread_test_rxda, //1
thread_test_txda}; //2
int dev_flag[MAX_DEVNAMES];
int dev_rd_channels[MAX_DEVNAMES];
int dev_max_rd_speed[MAX_DEVNAMES];
int dev_min_rd_speed[MAX_DEVNAMES];
int dev_rd_bytes[MAX_DEVNAMES];
int dev_wr_channels[MAX_DEVNAMES];
int dev_max_wr_speed[MAX_DEVNAMES];
int dev_min_wr_speed[MAX_DEVNAMES];
int dev_wr_bytes[MAX_DEVNAMES];
char blanks[60];
char *wish_anyway="Do you wish to try to use it anyway (Y/N)?";
char *intrate_string=" interrupt rate: ";
int low_speeds[MAX_LOWSPEED]={1,5000,6000,8000,11025,16000,
22050,24000,48000,96000};
int zrw; // untested return value for OSS
void fix_prio(int no);
#include "loadalsa.h"
#define MAX_ALSADEV 24
snd_pcm_t *rx_ad_handle, *rx_da_handle;
snd_pcm_t *tx_ad_handle, *tx_da_handle;
int alsa_dev_seq_nmbr;
char alsa_dev_soundcard_name [256];
char alsa_dev_name [64];
char alsa_dev_hw_pcm_name[MAX_ALSADEV];
char alsa_dev_plughw_pcm_name[MAX_ALSADEV];
unsigned int alsa_dev_min_rate[MAX_ALSADEV];
unsigned int alsa_dev_max_rate[MAX_ALSADEV];
int alsa_dev_min_channels;
int alsa_dev_max_channels;
int alsa_dev_min_bytes;
int alsa_dev_max_bytes;
void edit_soundcard_name(char *s)
{
int i,j,k,l;
// We are called with a string with a high number of blanks in the text.
// minimise the blanks and limit total text string length to max 51
i=(int)strlen(s);
j=0;
again:;
while(s[j] != ' ')j++;
if (j >= i) goto finish;
k=j;
while(s[k] == ' ')k++;
if (k >= i) goto finish;
l=k;
while(s[l] != ' ' && l < i)
{
j=j+1;
s[j]=s[l];
if (j != l)s[l]=' ';
l++;
}
if (l<i) goto again;
finish:;
j++;
if(j>51)j=51;
s[j]='\0';
}
int alsa_get_dev_names(int n)
{
//This routine returns alsa_dev_hw_pcm_name
// alsa_dev_plughw_pcm_name
// alsa_dev_soundcard_name
// alsa_dev_name
// alsa_dev_seq_nmbr
//for a valid sequence number n on input.
//This routine is not "thread safe"
//n must be positive and less than MAX_ALSADEV
//Returncode is negative if n invalid,
// if alsa errors
// if no device found for input sequence number.
snd_ctl_t *handle;
snd_ctl_card_info_t *info;
snd_pcm_info_t *pcminfo;
int err, idx, dev, dev_seq_nmbr;
char str[128];
int retcode;
alsa_dev_seq_nmbr=0;
sprintf(alsa_dev_hw_pcm_name ," ");
sprintf(alsa_dev_plughw_pcm_name," ");
sprintf(alsa_dev_soundcard_name," ");
sprintf(alsa_dev_name," ");
idx=-1;
dev_seq_nmbr=0;
retcode=-10;
if(n < 0 || n >= MAX_ALSADEV)
{
retcode=-6;
goto exit_3;
}
snd_ctl_card_info_malloc(&info);
snd_pcm_info_malloc(&pcminfo);
while (1) // loop trough all cards
{
err=snd_card_next(&idx);
if(err < 0)
{
SNDLOG"alsa_get_dev_names:snd_card_next error: %s\n", snd_strerror(err));
retcode=-5;
goto exit_2;
}
if(idx < 0) // no more cards available
{
SNDLOG"alsa_get_dev_pcm_name:end of available card/devices reached\n");
retcode=-1;
goto exit_2;
}
sprintf(str, "hw:CARD=%i", idx);
SNDLOG"Testing %s\n",str);
err=snd_ctl_open( &handle, str, 0);
if(err < 0)
{
SNDLOG"alsa_get_dev_names:snd_ctl_open error: %s\n", snd_strerror(err));
retcode=-4;
goto exit_2;
}
err=snd_ctl_card_info(handle, info);
if(err < 0)
{
SNDLOG"alsa_get_dev_names:HW info error: %s\n", snd_strerror(err));
retcode=-3;
goto exit_1;
}
dev=-1;
while (1) // loop trough all devices for this card
{
err=snd_ctl_pcm_next_device(handle, &dev);
if(err < 0)
{
SNDLOG"alsa_get_dev_names:PCM next device error: %s\n", snd_strerror(err));
retcode=-2;
goto exit_1;
}
if(dev < 0) // no more devices available for this card, get next card
{
snd_ctl_close(handle);
break;
}
// check first if device has valid pcminfo,
//if not, go to next device without incrementing dev seq nmbr
snd_pcm_info_set_device(pcminfo, (unsigned int)dev);
snd_pcm_info_set_subdevice(pcminfo, 0);
snd_pcm_info_set_stream(pcminfo, SND_PCM_STREAM_CAPTURE);
if((err=snd_ctl_pcm_info(handle, pcminfo))<0)
{
snd_pcm_info_set_stream(pcminfo, SND_PCM_STREAM_PLAYBACK);
if((err=snd_ctl_pcm_info(handle, pcminfo))<0) goto next_device;
}
//if valid, check if match with n
if(dev_seq_nmbr == n)
{
// if match with n, get all device name-info and exit loop
alsa_dev_seq_nmbr=dev_seq_nmbr;
sprintf(alsa_dev_hw_pcm_name ,"hw:%i,%i", idx,dev);
sprintf(alsa_dev_plughw_pcm_name,"plughw:%i,%i", idx,dev);
snd_pcm_info_set_device(pcminfo, (unsigned int)dev);
snd_pcm_info_set_subdevice(pcminfo, 0);
snd_pcm_info_set_stream(pcminfo, SND_PCM_STREAM_CAPTURE);
err=snd_ctl_pcm_info(handle, pcminfo);
if(err==0)
{
sprintf(alsa_dev_name," %s, %s",snd_ctl_card_info_get_name(info),snd_pcm_info_get_id(pcminfo));
}
else
{
snd_pcm_info_set_stream(pcminfo, SND_PCM_STREAM_PLAYBACK);
err=snd_ctl_pcm_info(handle, pcminfo);
if(err==0)
{
sprintf(alsa_dev_name," %s, %s",snd_ctl_card_info_get_name(info),snd_pcm_info_get_id(pcminfo));
}
else
{
SNDLOG"alsa_get_dev_names:snd_ctl_pcm_info error: %s\n", snd_strerror(err));
retcode=-7;
goto exit_1;
}
}
sprintf(alsa_dev_soundcard_name," %s",snd_ctl_card_info_get_longname(info));
edit_soundcard_name(alsa_dev_soundcard_name);
SNDLOG"device found: alsa_dev_plughw_pcm_name=%s \n", alsa_dev_plughw_pcm_name);
retcode=0;
goto exit_1;
}
// if no match with n, increment seq nbr of valid devices
dev_seq_nmbr= dev_seq_nmbr+1;
//get next device
next_device:;
} // end while loop through devices
} // end while loop through cards
exit_1:;
snd_ctl_close(handle);
exit_2:;
snd_ctl_card_info_free(info);
snd_pcm_info_free (pcminfo);
exit_3:;
SNDLOG"(alsa_get_dev_pcm_name: n=%i, retcode =%d) \n\n",n,retcode);
return retcode;
}
int alsar_get_dev_pcm_names (int n,
void *al_dev_plughw_pcm_name,
void *al_dev_hw_pcm_name )
{
//Threadsafe version of alsa_get_dev_names
//with limited functionality:
//This routine returns only dev_plughw_pcm_name and dev_hw_pcm_name
//for a valid sequence number n on input.
//n must be positive and less than MAX_ALSADEV
//Returncode is negative if n invalid,
// if alsa errors
// if no device found for input sequence number.
snd_ctl_t *handle;
snd_ctl_card_info_t *info;
snd_pcm_info_t *pcminfo;
int err, idx, dev, dev_seq_nmbr;
char str[128];
int retcode ;
retcode=-10;
alsa_dev_seq_nmbr=0;
sprintf((char *)(al_dev_plughw_pcm_name)," ");
sprintf((char *)(al_dev_hw_pcm_name)," ");
if(n < 0 || n >= MAX_ALSADEV)
{
retcode=-6;
goto exit_3;
}
idx=-1;
dev_seq_nmbr=0;
snd_ctl_card_info_malloc(&info);
snd_pcm_info_malloc(&pcminfo);
while (1) // loop through cards
{
err=snd_card_next(&idx);
if(err < 0)
{
SNDLOG"alsar_get_dev_pcm_name:snd_card_next error: %s\n", snd_strerror(err));
retcode=-5;
goto exit_2;
}
if(idx < 0) // no more cards available
{
SNDLOG"alsar_get_dev_pcm_name:end of available card/devices reached\n");
retcode=-1;
goto exit_2;
}
sprintf(str, "hw:CARD=%i", idx);
SNDLOG"Testing %s\n",str);
err=snd_ctl_open( &handle, str, 0);
if(err < 0)
{
SNDLOG"alsar_get_dev_pcm_name:snd_ctl_open error: %s\n", snd_strerror(err));
retcode=-4;
goto exit_2;
}
err=snd_ctl_card_info(handle, info);
if(err < 0)
{
SNDLOG"alsar_get_dev_pcm_name:HW info error: %s\n", snd_strerror(err));
retcode=-3;
goto exit_1;
}
dev=-1;
while (1) // loop through all devices for this card
{
err=snd_ctl_pcm_next_device(handle, &dev);
if(err < 0)
{
SNDLOG"alsar_get_dev_pcm_name:PCM next device error: %s\n", snd_strerror(err));
retcode=-2;
goto exit_1;
}
if(dev < 0) // no more devices available for this card
{
snd_ctl_close(handle);
break;
}
// check first if device has valid pcminfo,
//if not, go to next device without incrementing dev seq nmbr
snd_pcm_info_set_device(pcminfo, (unsigned int)dev);
snd_pcm_info_set_subdevice(pcminfo, 0);
snd_pcm_info_set_stream(pcminfo, SND_PCM_STREAM_CAPTURE);
err=snd_ctl_pcm_info(handle, pcminfo);
if(err<0)
{
snd_pcm_info_set_stream(pcminfo, SND_PCM_STREAM_PLAYBACK);
err=snd_ctl_pcm_info(handle, pcminfo);
if(err<0) goto next_device;
}
//if valid, check if match with n
if(dev_seq_nmbr == n)
{
// if match with n, get limited device name-info and exit loop
alsa_dev_seq_nmbr=dev_seq_nmbr;
sprintf((char*)(al_dev_hw_pcm_name) ,"hw:%i,%i", idx,dev);
sprintf((char *)(al_dev_plughw_pcm_name),"plughw:%i,%i", idx,dev);
SNDLOG"device found: dev_plughw_pcm_name=%s\n",((char *)(al_dev_plughw_pcm_name)));
retcode=0;
goto exit_1;
}
// if no match with n, increment seq nmbr of valid devices
dev_seq_nmbr= dev_seq_nmbr+1;
// get next_device
next_device:;
} // end while loop through devices
} // end while loop through cards
exit_1:;
snd_ctl_close(handle);
exit_2:;
snd_ctl_card_info_free(info);
snd_pcm_info_free (pcminfo);
exit_3:;
SNDLOG"(alsar_get_dev_pcm_name: n=%i, retcode =%d) \n\n",n,retcode);
return retcode;
}
int alsa_get_dev_native_capabilities(int n,int mode)
{
// This single threaded routine returns the native hw capabilities of a given
// soundcard/device n, in either capture or playback mode (mode input parameter).
// This is done by opening the device with a pcm_name=hw:n,0 and
// querying hw_params
// Returncode is negative if
// device does not exist
// alsa error
snd_pcm_t *wk_handle;
snd_pcm_hw_params_t *hw_params;
unsigned int val;
int err, dir;
dir=0;
alsa_dev_min_rate[n]=0;
alsa_dev_max_rate[n]=0;
alsa_dev_min_channels=0;
alsa_dev_max_channels=0;
alsa_dev_min_bytes=0;
alsa_dev_max_bytes=0;
if((err=alsa_get_dev_names (n)) < 0 ) {
SNDLOG"alsa_get_dev_native_capabilities: alsa_get_dev_id failed\n" );
return -1;
}
if((err=snd_pcm_open(&wk_handle, alsa_dev_hw_pcm_name, mode, 0)) < 0) {
SNDLOG"alsa_get_dev_native_capabilities: open failed,%s\n",alsa_dev_hw_pcm_name );
return -2;
}
if((err=snd_pcm_hw_params_malloc(&hw_params)) < 0) {
SNDLOG"alsa_get_dev_native_capabilities: snd_pcm_hw_params_malloc failed\n" );
snd_pcm_close(wk_handle);
return -3;
}
if((err=snd_pcm_hw_params_any(wk_handle, hw_params)) < 0) {
SNDLOG"alsa_get_dev_native_capabilities: snd_pcm_hw_params_any failed\n" );
snd_pcm_hw_params_free (hw_params);
snd_pcm_close(wk_handle);
return -4;
}
// get sampling rate
if((err= snd_pcm_hw_params_get_rate_max (hw_params, &val, &dir)) < 0){
SNDLOG"alsa_get_dev_native_capabilities: snd_pcm_hw_params_get_rate_max failed\n" );
snd_pcm_hw_params_free (hw_params);
snd_pcm_close(wk_handle);
return -5;
}
alsa_dev_max_rate[n]=(int)val;
if((err= snd_pcm_hw_params_get_rate_min (hw_params, &val, &dir)) < 0) {
SNDLOG"alsa_get_dev_native_capabilities: snd_pcm_hw_params_get_rate_min failed\n" );
snd_pcm_hw_params_free (hw_params);
snd_pcm_close(wk_handle);
return -6;
}
alsa_dev_min_rate[n]=(int)val;
// get # channels
if((err= snd_pcm_hw_params_get_channels_max(hw_params, &val)) < 0) {
SNDLOG"alsa_get_dev_native_capabilities: snd_pcm_hw_params_get_channels_max failed\n" );
snd_pcm_hw_params_free (hw_params);
snd_pcm_close(wk_handle);
return -7;
}
alsa_dev_max_channels=(int)val;
if((err= snd_pcm_hw_params_get_channels_min(hw_params, &val)) < 0){
SNDLOG"alsa_get_dev_native_capabilities: snd_pcm_hw_params_get_channels_min failed\n" );
snd_pcm_hw_params_free (hw_params);
snd_pcm_close(wk_handle);
return -8;
}
alsa_dev_min_channels=(int)val;
// get min # of bytes
if((err=snd_pcm_hw_params_test_format (wk_handle, hw_params,SND_PCM_FORMAT_U8)) == 0)
{
alsa_dev_min_bytes=1;
}
else
{
if((err=snd_pcm_hw_params_test_format (wk_handle, hw_params,SND_PCM_FORMAT_S16_LE)) == 0)
{
alsa_dev_min_bytes=2;
}
else
{
if((err=snd_pcm_hw_params_test_format (wk_handle, hw_params,SND_PCM_FORMAT_S32_LE)) == 0)
{
alsa_dev_min_bytes=4;
}
else {
SNDLOG"alsa_get_dev_native_capabilities: snd_pcm_hw_params_test_format failed\n" );
//probably an alsa error, assume 2 bytes
alsa_dev_min_bytes=2;
}
}
}
// get max # of bytes
if((err=snd_pcm_hw_params_test_format (wk_handle, hw_params,SND_PCM_FORMAT_S32_LE)) == 0)
{
alsa_dev_max_bytes=4;
}
else
{
if((err=snd_pcm_hw_params_test_format (wk_handle, hw_params,SND_PCM_FORMAT_S16_LE)) == 0)
{
alsa_dev_max_bytes=2;
}
else
{
if((err=snd_pcm_hw_params_test_format (wk_handle, hw_params,SND_PCM_FORMAT_U8)) == 0)
{
alsa_dev_max_bytes=1;
}
else{
SNDLOG"alsa_get_dev_native_capabilities: snd_pcm_hw_params_test_format failed\n" );
//probably an alsa error, assume 2 bytes
alsa_dev_max_bytes=2;
}
}
}
snd_pcm_hw_params_free (hw_params);
snd_pcm_close(wk_handle);
return 0;
}
int alsar_get_dev_native_capabilities(int n,
int mode,
int *al_dev_min_rate,
int *al_dev_max_rate,
int *al_dev_min_channels,
int *al_dev_max_channels,
int *al_dev_min_bytes,
int *al_dev_max_bytes
)
{
// threadsafe version of alsa_get_dev_native_capabilities
// This routine returns the native hw capabilities of a given
// soundcard/device n, in capture or playback mode (mode input parameter).
// This is done by opening the device with a pcm_name=hw:n,0 and
// querying hw_params
// Returncode is negative if
// device does not exist
// alsa error
snd_pcm_t *wk_handle;
snd_pcm_hw_params_t *hw_params;
unsigned int val;
int err, dir;
char dev_plughw_pcm_name[MAX_ALSADEV];
char dev_hw_pcm_name[MAX_ALSADEV];
dir=0;
*al_dev_min_rate=0;
*al_dev_max_rate=0;
*al_dev_min_channels=0;
*al_dev_max_channels=0;
*al_dev_min_bytes=0;
*al_dev_max_bytes=0;
//check if valid device
if (( alsar_get_dev_pcm_names(n,dev_plughw_pcm_name,dev_hw_pcm_name))< 0 )
{
SNDLOG"alsar_get_dev_native_capabilities: alsar_get_dev_plughw_pcm_name failed\n" );
return -1;
}
if((err=snd_pcm_open(&wk_handle, dev_hw_pcm_name, mode, 0)) < 0) {
SNDLOG"alsar_get_dev_native_capabilities: open failed,%s\n",dev_hw_pcm_name );
return -2;
}
if((err=snd_pcm_hw_params_malloc (&hw_params)) < 0) {
SNDLOG"alsar_get_dev_native_capabilities: snd_pcm_hw_params_malloc failed\n" );
snd_pcm_close(wk_handle);
return -3;
}
if((err=snd_pcm_hw_params_any(wk_handle, hw_params)) < 0) {
SNDLOG"alsar_get_dev_native_capabilities: snd_pcm_hw_params_any failed\n" );
snd_pcm_hw_params_free (hw_params);
snd_pcm_close(wk_handle);
return -4;
}
// get max sampling rate
if((err= snd_pcm_hw_params_get_rate_max (hw_params, &val, &dir)) < 0){
SNDLOG"alsar_get_dev_native_capabilities: snd_pcm_hw_params_get_rate_max failed\n" );
snd_pcm_hw_params_free (hw_params);
snd_pcm_close(wk_handle);
return -5;
}
*al_dev_max_rate=(int)val;
// get min sampling rate
if((err= snd_pcm_hw_params_get_rate_min (hw_params, &val, &dir)) < 0) {
SNDLOG"alsar_get_dev_native_capabilities: snd_pcm_hw_params_get_rate_min failed\n" );
snd_pcm_hw_params_free (hw_params);
snd_pcm_close(wk_handle);
return -6;
}
*al_dev_min_rate=(int)val;
// get max # channels
if((err= snd_pcm_hw_params_get_channels_max(hw_params, &val)) < 0) {
SNDLOG"alsar_get_dev_native_capabilities: snd_pcm_hw_params_get_channels_max failed\n" );
snd_pcm_hw_params_free (hw_params);
snd_pcm_close(wk_handle);
return -4;
}
*al_dev_max_channels=(int)val;
// get min # channels
if((err= snd_pcm_hw_params_get_channels_min(hw_params, &val)) < 0){
SNDLOG"alsar_get_dev_native_capabilities: snd_pcm_hw_params_get_channels_min failed\n" );
snd_pcm_hw_params_free (hw_params);
snd_pcm_close(wk_handle);
return -8;
}
*al_dev_min_channels=(int)val;
// get min # of bytes
if((err=snd_pcm_hw_params_test_format (wk_handle, hw_params,SND_PCM_FORMAT_U8)) == 0)
{
*al_dev_min_bytes=1;
}
else
{
if((err=snd_pcm_hw_params_test_format (wk_handle, hw_params,SND_PCM_FORMAT_S16_LE)) == 0)
{
*al_dev_min_bytes=2;
}
else
{
if((err=snd_pcm_hw_params_test_format (wk_handle, hw_params,SND_PCM_FORMAT_S32_LE)) == 0)
{
*al_dev_min_bytes=4;
}
else {
SNDLOG"alsar_get_dev_native_capabilities: snd_pcm_hw_params_test_format failed\n" );
//probably an alsa error, assume 2 bytes
*al_dev_min_bytes=2;
}
}
}
// get max # of bytes
if((err=snd_pcm_hw_params_test_format (wk_handle, hw_params,SND_PCM_FORMAT_S32_LE)) == 0)
{
*al_dev_max_bytes=4;
}
else
{
if((err=snd_pcm_hw_params_test_format (wk_handle, hw_params,SND_PCM_FORMAT_S16_LE)) == 0)
{
*al_dev_max_bytes=2;
}
else
{
if((err=snd_pcm_hw_params_test_format (wk_handle, hw_params,SND_PCM_FORMAT_U8)) == 0)
{
*al_dev_max_bytes=1;
}
else{
SNDLOG"alsar_get_dev_native_capabilities: snd_pcm_hw_params_test_format failed\n" );
//probably an alsa error, assume 2 bytes
*al_dev_max_bytes=2;
}
}
}
snd_pcm_hw_params_free (hw_params);
snd_pcm_close(wk_handle);
return 0;
}
int alsa_get_native_samplerate (int n,
int mode,
int *line,
unsigned int *returned_sampling_rate)
{
(void) line;
(void) returned_sampling_rate;
int err,i,k,id;
unsigned int val;
char s[80];
char dev_hw_pcm_name[MAX_ALSADEV];
char dev_plughw_pcm_name[MAX_ALSADEV];
unsigned int native_sampling_rate[MAX_ALSADEV];
snd_pcm_t *wk_handle;
snd_pcm_hw_params_t *hw_params;
// 0 terminated list
static unsigned int standardSampleRates[] = {8000, 9600,
11025, 12000, 16000, 22050, 24000, 32000,
44100, 48000, 50000,88200, 96000, 119466, 192000,
224000, 448000, 0};
unsigned int sample_rates[32];
unsigned int smin, smax;
err=alsar_get_dev_pcm_names(n,dev_plughw_pcm_name,dev_hw_pcm_name);
if (err!=0) return -1;
err=snd_pcm_hw_params_malloc (&hw_params);
if (err!=0) return -2;
err=snd_pcm_open(&wk_handle, dev_hw_pcm_name, mode, 0);
if (err!=0)
{
snd_pcm_hw_params_free (hw_params);
return -3;
}
clear_screen();
line[0]=2;
sprintf(s,"This is a native ALSA device.");
lir_text(1,line[0], s);
line[0]+=1;
sprintf(s,"Select sampling rate by line number from list:");
lir_text(1,line[0], s);
line[0]+=2;
i=0;
smin=alsa_dev_min_rate[n];
smax=alsa_dev_max_rate[n];
if(smin == smax)
{
smin=0;
}
while(standardSampleRates[i] != 0 )
{
if(smax == standardSampleRates[i])smax=0;
if(smin == standardSampleRates[i])smin=0;
sample_rates[i]=standardSampleRates[i];
i++;
}
if(smin != 0)
{
sample_rates[i]=smin;
i++;
}
if(smax > 0)
{
sample_rates[i]=smax;
i++;
}
sample_rates[i]=0;
i=0;
k=0;
while(sample_rates[i] != 0 )
{
val=(unsigned int)sample_rates[i];
err=snd_pcm_hw_params_any(wk_handle, hw_params);
if (err!=0)
{
snd_pcm_close(wk_handle);
snd_pcm_hw_params_free (hw_params);
return -4;
}
err=snd_pcm_hw_params_set_rate(wk_handle,hw_params,val, 0);
if(err == 0)
{
sprintf(s,"%2d -> %6d ",k,val);
lir_text(1,line[0],s);
native_sampling_rate[k]= val;
line[0]++;
k++;
}
i++;
}
line[0]++;
sprintf(s,"Enter line number >");
lir_text(1,line[0],s);
id=lir_get_integer(21,line[0],2, 0, k-1);
*returned_sampling_rate=native_sampling_rate[id];
snd_pcm_close(wk_handle);
snd_pcm_hw_params_free (hw_params);
return 0;
}
int select_alsadev(int open_mode, int *line)
{
//display available devices for a specific mode and return sequence number of selected device
char s[200];
int i, err;
line[0]++;
sprintf(s,"device soundcard-device-name device sample-rate channels bytes");
SNDLOG"%s\n",s);
lir_text(0,line[0],s);
line[0]++;
sprintf(s,"seq-no (soundcard-name) pcm-name min / max min/max min/max");
SNDLOG"%s\n",s);
lir_text(0,line[0],s);
line[0]++;
sprintf(s,"------ -------------------------------------------------- ---------- ----------- ------- ------- " );
SNDLOG"%s\n",s);
lir_text(0,line[0],s);
line[0]++;
for(i=0; i<MAX_ALSADEV; i++)
{
err=alsa_get_dev_names(i);
if(err == -1) goto end_alsa_get_dev_names;
if(err == 0)
{
err=alsa_get_dev_native_capabilities(i, open_mode);
if(err == 0)
{
line[0]++;
sprintf(s,"%i", alsa_dev_seq_nmbr);
lir_text(2,line[0],s);
sprintf(s,"%s", alsa_dev_name);
lir_text(6,line[0],s);
sprintf(s,"(%s)", alsa_dev_soundcard_name);
lir_text(20,line[0]+1,s);
sprintf(s,"%s", alsa_dev_plughw_pcm_name);
lir_text(59,line[0],s);
sprintf(s,"%i/%i", alsa_dev_min_rate[i], alsa_dev_max_rate[i]);
lir_text(72,line[0],s);
sprintf(s,"%i/%i ", alsa_dev_min_channels, alsa_dev_max_channels);
lir_text(86,line[0],s);
sprintf(s,"%i/%i ", alsa_dev_min_bytes, alsa_dev_max_bytes);
lir_text(96,line[0],s);
sprintf(s," %i %s (%s) %s %i/%i %i/%i %i/%i ",
alsa_dev_seq_nmbr, alsa_dev_name, alsa_dev_soundcard_name,
alsa_dev_plughw_pcm_name, alsa_dev_min_rate[i],
alsa_dev_max_rate[i],alsa_dev_min_channels,
alsa_dev_max_channels, alsa_dev_min_bytes,
alsa_dev_max_bytes );
line[0]+=2;
SNDLOG"%s\n",s);
}
}
}
end_alsa_get_dev_names:;
again:;
sprintf(s,"Enter selected device-seq-nmbr >" );
SNDLOG"%s\n",s);
line[0]=line[0]+2;
lir_text(0,line[0],s);
i=lir_get_integer(33, line[0], 2, 0,MAX_ALSADEV-1);
if(kill_all_flag)return -1;
sprintf(s,"User selected %i",i);
SNDLOG"%s\n",s);
err=alsa_get_dev_names (i);
if(err < 0)
{
lir_text(37,line[0],"invalid device-seq-nmbr, try again");
sprintf(s,"invalid device-seq-nmbr, try again");
SNDLOG"%s\n",s);
goto again;
}
err=alsa_get_dev_native_capabilities(i,open_mode);
if(err != 0)
{
lir_text(37,line[0],"incompatible capabilities, try again");
sprintf(s,"incompatible capabilities, try again");
SNDLOG"%s\n",s);
goto again;
}
lir_text(37,line[0],blanks);
line[0]++;
return i;
}
int chk_open_alsa(int no, int bytes, int speed, int channels, int mode)
{
int err;
char dev_plughw_pcm_name[MAX_ALSADEV];
char dev_hw_pcm_name[MAX_ALSADEV];
int al_dev_min_rate;
int al_dev_max_rate;
int al_dev_min_channels;
int al_dev_max_channels;
int al_dev_min_bytes;
int al_dev_max_bytes;
//check if device exists
err= alsar_get_dev_pcm_names(no,dev_plughw_pcm_name,dev_hw_pcm_name);
if( err < 0 )return -1;
//check capabilities of device
err=alsar_get_dev_native_capabilities( no,
mode,
&al_dev_min_rate,
&al_dev_max_rate,
&al_dev_min_channels,
&al_dev_max_channels,
&al_dev_min_bytes,
&al_dev_max_bytes);
if( err != 0 )return -2;
if( speed < al_dev_min_rate )return -4;
if( speed > al_dev_max_rate )return -5;
if( channels > al_dev_max_channels )return -7;
if( bytes < al_dev_min_bytes )return -8;
if( bytes > al_dev_max_bytes )return -9;
return 0;
}
void alsa_errors(int err)
{
switch (err)
{
case EIO:
lirerr(1169);
return;
case EPIPE:
lirerr(1108);
return;
default:
lirerr(1127);
return;
}
}
int alsaread(snd_pcm_t *handle, char *buf, int type)
{
snd_pcm_uframes_t frames,frames_ok;
int i, k, err, frame_time;
frames=(snd_pcm_uframes_t)snd[type].block_frames;
frame_time=1100000/snd[type].interrupt_rate;
if(frame_time < 1000)frame_time=1000;
k=0;
xx:;
err=snd_pcm_readi(handle, buf, frames);
if(kill_all_flag)return FALSE;
if((unsigned int)err != frames)
{
if(err == -EAGAIN)
{
// Nothing available yet.
lir_sleep(frame_time);
if(kill_all_flag)return FALSE;
goto xx;
}
if(err < 0)
{
xx1:;
k++;
err= snd_pcm_recover(handle, err, 0);
if(err == 0)goto xx;
lir_sleep(2000);
if(k < 3)goto xx1;
alsa_errors(-err);
return FALSE;
}
// We had a short read. Wait for the device to recover.
// Then read remaining bytes.
frames_ok=(snd_pcm_uframes_t)err;
k=0;
read_more:;
if(kill_all_flag)return FALSE;
i=(frame_time*(frames-frames_ok))/frames;
if(i<frame_time/2)i=frame_time/2;
lir_sleep(i);
err=snd_pcm_readi(handle, &buf[frames_ok*snd[type].framesize], frames-frames_ok);
if(err == -EAGAIN)
{
// Nothing available yet.
goto read_more;
}
if(err < 0)
{
read_more1:;
if(kill_all_flag)return FALSE;
err=snd_pcm_recover(handle, err, 0);
if(err == 0)goto read_more;
lir_sleep(2000);
if(k < 3)goto read_more1;
alsa_errors(-err);
return FALSE;
}
if((snd_pcm_uframes_t)err != frames-frames_ok)
{
frames_ok+=(snd_pcm_uframes_t)err;
goto read_more;
}
}
return TRUE;
}
void lir_tx_adread(char *buf)
{
int err;
char s[40];
int nread;
if( (ui.use_alsa&NATIVE_ALSA_USED)!=0)
{
err=alsaread(tx_ad_handle, buf, TXAD);
if(err == FALSE)
{
no_of_tx_overrun_errors++;
sprintf(s,"TX%s%d",overrun_error_msg,no_of_tx_overrun_errors);
wg_error(s,WGERR_TXIN);
}
}
else
{