-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalsa.c
128 lines (107 loc) · 3.55 KB
/
alsa.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
/*
* Copyright (c) 2014 Thierry Leconte (f4dwv)
*
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <alsa/asoundlib.h>
#include "acarsdec.h"
#define MAXNBFRAMES 1024
static snd_pcm_t *capture_handle;
int initAlsa(char **argv,int optind)
{
snd_pcm_hw_params_t *hw_params;
int err,n;
unsigned int Fs;
if ((err = snd_pcm_open(&capture_handle, argv[optind],
SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf(stderr, "Alsa cannot open audio device %s (%s)\n",argv[optind], snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
fprintf(stderr,
"Alsa cannot allocate hardware parameter structure (%s)\n",snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_any(capture_handle, hw_params)) < 0) {
fprintf(stderr,
"Alsa cannot initialize hardware parameter structure (%s)\n",snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_set_access(capture_handle, hw_params,SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf(stderr, "Alsa cannot set access type (%s)\n",snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_set_format(capture_handle, hw_params,SND_PCM_FORMAT_S16)) < 0) {
fprintf(stderr, "Alsa cannot set sample format (%s)\n",snd_strerror(err));
return 1;
}
snd_pcm_hw_params_set_rate_resample(capture_handle, hw_params,0);
Fs=19200;n=1;
if ((err = snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &Fs,&n)) < 0) {
fprintf(stderr, "Alsa cannot set sample rate (%s)\n",snd_strerror(err));
return 1;
}
fprintf(stderr, "Alsa sample rate %d\n",Fs);
if(snd_pcm_hw_params_get_channels (hw_params, &nbch)!=0) {
fprintf(stderr, "Alsa cannot get number of channels\n");
return 1;
}
if(nbch>4) {
fprintf(stderr, "Alsa too much channels\n");
return 1;
}
if ((err = snd_pcm_hw_params(capture_handle, hw_params)) < 0) {
fprintf(stderr, "Alsa cannot set parameters (%s)\n",snd_strerror(err));
return 1;
}
snd_pcm_hw_params_free(hw_params);
if ((err = snd_pcm_prepare(capture_handle)) < 0) {
fprintf(stderr,
"Alsa cannot prepare audio interface for use (%s)\n",snd_strerror(err));
return 1;
}
for(n=0;n<nbch;n++) {
channel[n].chn=n;
channel[n].Infs=Fs;
channel[n].InBuff=malloc(MAXNBFRAMES*sizeof(sample_t));
}
for(;n<MAXNBCHANNELS;n++) channel[n].Infs=0;
return (0);
}
int getAlsaSample(void)
{
int r,n,i;
signed short sndbuff[MAXNBFRAMES*MAXNBCHANNELS];
r = snd_pcm_readi(capture_handle, sndbuff,MAXNBFRAMES );
if (r <= 0) {
fprintf(stderr,"Alsa cannot read from interface (%s)\n", snd_strerror(r));
return -1;
}
pthread_mutex_lock(&datamtx);
while(wrkmask) pthread_cond_wait(&datawcd, &datamtx);
pthread_mutex_unlock(&datamtx);
for(n=0;n<nbch;n++)
if(channel[n].Infs) {
int len=r;
for(i=0;i<len;i++) channel[n].InBuff[i]=(float)(sndbuff[n+i*nbch])/32768.0;
channel[n].lenIn=len;
}
return 0;
}