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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
* device.cc
*
* Wed Sep 24 08:54:47 CEST 2014
* Copyright 2014 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of LibAudioIO.
*
* LibAudioIO is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* LibAudioIO 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with LibAudioIO; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "device.h"
Device::Device(std::string device)
{
card = device;
}
Device::~Device()
{
/*
if(handle) {
snd_pcm_drain(handle);
snd_pcm_close(handle);
}
*/
}
std::vector<std::string> Device::mixerNames()
{
std::vector<std::string> mlist;
int err;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
snd_mixer_elem_t *elem;
snd_mixer_selem_id_alloca(&sid);
if((err = snd_mixer_open(&handle, 0)) < 0) {
printf("Mixer %s open error: %s", card.c_str(), snd_strerror(err));
return mlist;
}
if((err = snd_mixer_attach(handle, card.c_str())) < 0) {
printf("Mixer attach %s error: %s", card.c_str(), snd_strerror(err));
snd_mixer_close(handle);
return mlist;
}
if((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
printf("Mixer register error: %s", snd_strerror(err));
snd_mixer_close(handle);
return mlist;
}
err = snd_mixer_load(handle);
if (err < 0) {
printf("Mixer %s load error: %s", card.c_str(), snd_strerror(err));
snd_mixer_close(handle);
return mlist;
}
for(elem = snd_mixer_first_elem(handle);
elem;
elem = snd_mixer_elem_next(elem)) {
snd_mixer_selem_get_id(elem, sid);
if(snd_mixer_selem_is_active(elem) == 0) continue;
char cname[256];
snprintf(cname, sizeof(cname), "'%s',%i",
snd_mixer_selem_id_get_name(sid),
snd_mixer_selem_id_get_index(sid));
mlist.push_back(cname);
}
snd_mixer_close(handle);
return mlist;
}
Mixer *Device::getMixer(std::string name)
{
int err;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
snd_mixer_elem_t *elem;
snd_mixer_selem_id_alloca(&sid);
if((err = snd_mixer_open(&handle, 0)) < 0) {
printf("Mixer %s open error: %s", card.c_str(), snd_strerror(err));
return NULL;
}
if((err = snd_mixer_attach(handle, card.c_str())) < 0) {
printf("Mixer attach %s error: %s", card.c_str(), snd_strerror(err));
snd_mixer_close(handle);
return NULL;
}
if((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
printf("Mixer register error: %s", snd_strerror(err));
snd_mixer_close(handle);
return NULL;
}
err = snd_mixer_load(handle);
if (err < 0) {
printf("Mixer %s load error: %s", card.c_str(), snd_strerror(err));
snd_mixer_close(handle);
return NULL;
}
for(elem = snd_mixer_first_elem(handle);
elem;
elem = snd_mixer_elem_next(elem)) {
snd_mixer_selem_get_id(elem, sid);
if(snd_mixer_selem_is_active(elem) == 0) continue;
char cname[256];
snprintf(cname, sizeof(cname), "'%s',%i",
snd_mixer_selem_id_get_name(sid),
snd_mixer_selem_id_get_index(sid));
if(std::string(cname) == name) {
// NOTE: The Mixer object takes ownership of 'handle' which closed in
// its destructor.
return new Mixer(handle, elem);
}
}
snd_mixer_close(handle);
printf("Mixer element \"%s\" not found.\n", name.c_str());
return NULL;
}
static int pcm_init(snd_pcm_t *handle, unsigned int *samplerate,
unsigned int channels, snd_pcm_uframes_t *frames)
{
int err;
snd_pcm_hw_params_t *params;
// Allocate a hardware parameters object.
snd_pcm_hw_params_alloca(¶ms);
// Fill it in with default values.
err = snd_pcm_hw_params_any(handle, params);
if(err) {
printf("snd_pcm_hw_params_any: %s, %d\n", snd_strerror(err), err);
return 1;
}
// Interleaved mode
err = snd_pcm_hw_params_set_access(handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED);
if(err) {
printf("snd_pcm_hw_params_set_access: %s, %d\n", snd_strerror(err), err);
return 1;
}
// Signed 16-bit little-endian format
err = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
if(err) {
printf("snd_pcm_hw_params_set_format: %s, %d\n", snd_strerror(err), err);
return 1;
}
// Set channels (stereo/mono)
err = snd_pcm_hw_params_set_channels(handle, params, channels);
if(err) {
printf("snd_pcm_hw_params_set_channels: %s, %d\n", snd_strerror(err), err);
return 1;
}
// dir: -1 := exact or first below
// 0 := exact (error if not?)
// 1 := exact or first above
int dir = 1;
// Set sampling rate
err = snd_pcm_hw_params_set_rate_near(handle, params, samplerate, &dir);
if(err) {
printf("snd_pcm_hw_params_set_rate_near: %s, %d\n", snd_strerror(err), err);
return 1;
}
// printf("Actual samplerate: %d\n", samplerate);
err = snd_pcm_hw_params_set_period_size_near(handle, params, frames, &dir);
if(err) {
printf("snd_pcm_hw_params_set_period_size_near: %s, %d\n",
snd_strerror(err), err);
return 1;
}
// printf("Actual buffersize: %d\n", (int)frames);
// Write the parameters to the driver
err = snd_pcm_hw_params(handle, params);
if(err) {
printf("snd_pcm_hw_params: %s, %d\n",
snd_strerror(err), err);
return 1;
}
return 0;
}
Source *Device::getSource(std::string name, unsigned int samplerate,
unsigned int channels)
{
int open_mode = 0;
//open_mode |= SND_PCM_NONBLOCK;
//open_mode |= SND_PCM_NO_AUTO_RESAMPLE;
//open_mode |= SND_PCM_NO_AUTO_CHANNELS;
//open_mode |= SND_PCM_NO_AUTO_FORMAT;
//open_mode |= SND_PCM_NO_SOFTVOL;
int err;
snd_pcm_t *handle;
// Open PCM device for recording (capture).
err = snd_pcm_open(&handle, name.c_str(), SND_PCM_STREAM_CAPTURE,
open_mode);
if(err) {
printf("snd_pcm_open: %s, %d\n", snd_strerror(err), err);
return NULL;
}
snd_pcm_uframes_t frames = 512;
if(pcm_init(handle, &samplerate, channels, &frames)) return NULL;
return new Source(handle, samplerate, channels, frames);
}
Sink *Device::getSink(std::string name, unsigned int samplerate,
unsigned int channels)
{
int open_mode = 0;
//open_mode |= SND_PCM_NONBLOCK;
//open_mode |= SND_PCM_NO_AUTO_RESAMPLE;
//open_mode |= SND_PCM_NO_AUTO_CHANNELS;
//open_mode |= SND_PCM_NO_AUTO_FORMAT;
//open_mode |= SND_PCM_NO_SOFTVOL;
int err;
snd_pcm_t *handle;
// Open PCM device for recording (capture).
err = snd_pcm_open(&handle, name.c_str(), SND_PCM_STREAM_PLAYBACK,
open_mode);
if(err) {
printf("snd_pcm_open: %s, %d\n", snd_strerror(err), err);
return NULL;
}
snd_pcm_uframes_t frames = 512;
if(pcm_init(handle, &samplerate, channels, &frames)) return NULL;
return new Sink(handle, samplerate, channels, frames);
}
|