-
Notifications
You must be signed in to change notification settings - Fork 6
/
input.c
166 lines (146 loc) · 3.8 KB
/
input.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
/*
* Copyright (c) 2007 by Thierry Leconte (F4DWV)
*
* $Id: input.c,v 1.3 2007/03/29 16:21:49 f4dwv Exp $
*
* This library 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 <sndfile.h>
#include <alsa/asoundlib.h>
#include "acarsdec.h"
static int source = 0;
static int nbch = 0;
static SNDFILE *inwav;
static int initsnd(char *filename)
{
SF_INFO infwav;
/* open wav input file */
infwav.format = 0;
inwav = sf_open(filename, SFM_READ, &infwav);
if (inwav == NULL) {
fprintf(stderr, "could not open %s\n", filename);
return (0);
}
if (infwav.samplerate != 48000) {
fprintf(stderr,
"Bad Input File sample rate: %d. Must be 48000\n",
infwav.samplerate);
return (0);
}
nbch=infwav.channels;
return (infwav.channels);
}
static snd_pcm_t *capture_handle;
static int initalsa(char *filename)
{
snd_pcm_hw_params_t *hw_params;
int err;
if ((err =
snd_pcm_open(&capture_handle, filename,
SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf(stderr, "cannot open audio device %s (%s)\n",
filename, snd_strerror(err));
return 0;
}
if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
fprintf(stderr,
"cannot allocate hardware parameter structure (%s)\n",
snd_strerror(err));
return 0;
}
if ((err = snd_pcm_hw_params_any(capture_handle, hw_params)) < 0) {
fprintf(stderr,
"cannot initialize hardware parameter structure (%s)\n",
snd_strerror(err));
return 0;
}
if ((err =
snd_pcm_hw_params_set_access(capture_handle, hw_params,
SND_PCM_ACCESS_RW_INTERLEAVED)) <
0) {
fprintf(stderr, "cannot set access type (%s)\n",
snd_strerror(err));
return 0;
}
if ((err =
snd_pcm_hw_params_set_format(capture_handle, hw_params,
SND_PCM_FORMAT_S16)) < 0) {
fprintf(stderr, "cannot set sample format (%s)\n",
snd_strerror(err));
return 0;
}
if ((err =
snd_pcm_hw_params_set_rate(capture_handle, hw_params, 48000,
0)) < 0) {
fprintf(stderr, "cannot set sample rate (%s)\n",
snd_strerror(err));
return 0;
}
for(nbch=2;nbch>0;nbch--) {
if (snd_pcm_hw_params_set_channels(capture_handle, hw_params, nbch)==0)
break;
}
if (nbch ==0) {
fprintf(stderr, "cannot set number of channels\n");
return 0;
}
if ((err = snd_pcm_hw_params(capture_handle, hw_params)) < 0) {
fprintf(stderr, "cannot set parameters (%s)\n",
snd_strerror(err));
return 0;
}
snd_pcm_hw_params_free(hw_params);
if ((err = snd_pcm_prepare(capture_handle)) < 0) {
fprintf(stderr,
"cannot prepare audio interface for use (%s)\n",
snd_strerror(err));
return 0;
}
return nbch;
}
/* open input source*/
int initsample(char *sourcename, int src)
{
source = src;
if (src)
return initsnd(sourcename);
else
return initalsa(sourcename);
}
int getsample(short *sample, int nb)
{
int r;
if (source) {
r = sf_read_short(inwav, sample, nb);
if (r == 0)
r = -1; /* this is the end */
} else {
r = snd_pcm_readi(capture_handle, sample, nb/nbch);
if (r <= 0)
fprintf(stderr,
"cannot read from interface (%s)\n",
snd_strerror(r));
r=r*nbch;
}
return r;
}
void endsample(void)
{
if (source)
sf_close(inwav);
else
snd_pcm_close(capture_handle);
}