forked from TLeconte/acarsdec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alsa.c
129 lines (111 loc) · 3.23 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
127
128
129
/*
* Copyright (c) 2015 Thierry Leconte
*
*
* 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.
*
*/
#ifdef WITH_ALSA
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <alsa/asoundlib.h>
#include "acarsdec.h"
#define MAXNBFRAMES 4096
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_FLOAT)) < 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 = INTRATE;
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;
}
if (snd_pcm_hw_params_get_channels(hw_params, &nbch) != 0) {
fprintf(stderr, "Alsa cannot get number of channels\n");
return 1;
}
if (nbch > 1) {
fprintf(stderr, "Alsa too much channels : %d\n", nbch);
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;
}
channel[0].chn = 0;
channel[0].dm_buffer=malloc(MAXNBFRAMES*sizeof(float));
return (0);
}
int runAlsaSample(void)
{
int r, n, i;
do {
r = snd_pcm_readi(capture_handle, channel[0].dm_buffer,MAXNBFRAMES);
if (r <= 0) {
fprintf(stderr,
"Alsa cannot read from interface (%s)\n",
snd_strerror(r));
return -1;
}
demodMSK(&(channel[0]),r);
} while (1);
return 0;
}
#endif