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
|
//-*- C++ -*-
/*
* lingot, a musical instrument tuner.
*
* Copyright (C) 2004-2007 Ibán Cereijo Graña, Jairo Chapela Martínez.
*
* This file is part of lingot.
*
* lingot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* lingot 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with lingot; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <unistd.h>
#ifdef PORTAUDIO
#else
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/soundcard.h>
#endif
#include <stdlib.h>
#include "lingot-defs.h"
#include "lingot-audio.h"
#ifdef __WIN32__
#include "windows.h"
char _die_err[100];
#define DIE(...) \
do { snprintf(_die_err, sizeof(_die_err), __VA_ARGS__); \
MessageBox(NULL, _die_err, "Fatal error", MB_OK | MB_ICONEXCLAMATION); \
exit(1); } \
while (0)
#else
#define DIE(...) do { fprintf(stderr, __VA_ARGS__); exit(1); } while (0)
#endif
LingotAudio *
lingot_audio_new (int channels, int rate, char *fdsp)
{
LingotAudio *audio;
#ifdef ALSA
snd_pcm_hw_params_t *hw_params;
int err;
#elif defined PORTAUDIO
PaError err;
#else
int format = AFMT_S16_LE;
#endif
audio = malloc (sizeof (LingotAudio));
// int dma = 4;
#ifdef ALSA
if ((err =
snd_pcm_open (&audio->capture_handle, "default", SND_PCM_STREAM_CAPTURE,
0)) < 0)
{
fprintf (stderr, "cannot open audio device %s (%s)\n",
fdsp, snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
{
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_any (audio->capture_handle, hw_params)) < 0)
{
fprintf (stderr,
"cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err =
snd_pcm_hw_params_set_access (audio->capture_handle, hw_params,
SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
{
fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err));
exit (1);
}
if ((err =
snd_pcm_hw_params_set_format (audio->capture_handle, hw_params,
SND_PCM_FORMAT_S16_LE)) < 0)
{
fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err));
exit (1);
}
if ((err =
snd_pcm_hw_params_set_rate_near (audio->capture_handle, hw_params,
&rate, 0)) < 0)
{
fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err));
exit (1);
}
if ((err =
snd_pcm_hw_params_set_channels (audio->capture_handle, hw_params,
channels)) < 0)
{
fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params (audio->capture_handle, hw_params)) < 0)
{
fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err));
exit (1);
}
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (audio->capture_handle)) < 0)
{
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
#elif defined PORTAUDIO
if ((err = Pa_Initialize()) != paNoError)
{
DIE ("PortAudio error: %s\n", Pa_GetErrorText( err ) );
}
if ((err = Pa_OpenDefaultStream( &audio->stream, channels, 0, paInt16, rate,
paFramesPerBufferUnspecified, NULL,
NULL)) != paNoError)
{
DIE ("PortAudio error: %s\n", Pa_GetErrorText( err ) );
}
if ((err = Pa_StartStream(audio->stream)) != paNoError)
{
DIE ("PortAudio error: %s\n", Pa_GetErrorText( err ) );
}
audio->framesize = Pa_GetSampleSize( paInt16 ) * channels;
#else
audio->dsp = open (fdsp, O_RDONLY);
if (audio->dsp < 0)
{
char buff[80];
sprintf (buff, "Unable to open audio device %s", fdsp);
perror (buff);
exit (-1);
}
//if (ioctl(audio->dsp, SOUND_PCM_READ_CHANNELS, &channels) < 0)
if (ioctl (audio->dsp, SNDCTL_DSP_CHANNELS, &channels) < 0)
{
perror ("Error setting number of channels");
exit (-1);
}
/* if (ioctl(audio->dsp, SOUND_PCM_WRITE_CHANNELS, &channels) < 0)
{
perror("Error setting number of channels");
exit(-1);
} */
// sample size
//if (ioctl(audio->dsp, SOUND_PCM_SETFMT, &format) < 0)
if (ioctl (audio->dsp, SNDCTL_DSP_SETFMT, &format) < 0)
{
perror ("Error setting bits per sample");
exit (-1);
}
int fragment_size = 1;
int DMA_buffer_size = 512;
int param = 0;
for (param = 0; fragment_size < DMA_buffer_size; param++)
fragment_size <<= 1;
param |= 0x00ff0000;
if (ioctl (audio->dsp, SNDCTL_DSP_SETFRAGMENT, ¶m) < 0)
{
perror ("Error setting DMA buffer size");
exit (-1);
}
// DMA divisor
/* if (ioctl(audio->dsp, SNDCTL_DSP_SUBDIVIDE, &dma) < 0)
{
perror("Error setting DMA divisor");
exit(-1);
}
// Rate de muestreo / reproduccion
if (ioctl(audio->dsp, SOUND_PCM_WRITE_RATE, &rate) < 0)
{
perror("Error setting write rate");
exit(-1);
} */
// if (ioctl(audio->dsp, SOUND_PCM_READ_RATE, &rate) < 0)
if (ioctl (audio->dsp, SNDCTL_DSP_SPEED, &rate) < 0)
{
perror ("Error setting sample rate");
exit (-1);
}
/*
// non-blocking reads.
if (fcntl(audio->dsp, F_SETFL, O_NONBLOCK) < 0)
{
perror("Error setting non-blocking reads");
exit(-1);
}
*/
#endif
return audio;
}
void
lingot_audio_destroy (LingotAudio * audio)
{
#ifdef ALSA
snd_pcm_close (audio->capture_handle);
#elif defined PORTAUDIO
Pa_Terminate();
#else
close (audio->dsp);
free (audio);
#endif
}
int
lingot_audio_read (LingotAudio * audio, void *buffer, int size)
{
#ifdef ALSA
return snd_pcm_readi (audio->capture_handle, buffer, size);
#elif defined PORTAUDIO
PaError err;
if (((err = Pa_ReadStream(audio->stream, buffer, size / audio->framesize)) != paNoError) && (err != paInputOverflowed))
{
DIE ("PortAudio error: %s\n", Pa_GetErrorText( err ) );
}
return size;
#else
return read (audio->dsp, buffer, size);
#endif
}
|