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
|
#include <stdlib.h>
#include <audiotypes.h>
#include <string>
#define ALSA_PCM_NEW_HW_PARAMS_API
#include <asoundlib.h>
#define T(x, msg) if(x < 0) { printf("%s failed: %s\n", msg, snd_strerror(x)); fflush(stdout); return false; }
#define BUFSZ 40960
class Alsa {
public:
Alsa();
~Alsa();
bool init(int channels, char *cnames[]);
void setParm(std::string parm, std::string value);
bool start();
void stop();
void pre(size_t size);
void run(int channel, sample_t* data, size_t size);
void post(size_t size);
private:
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
sample_t *data;
size_t channels;
std::string device;
unsigned int srate;
snd_pcm_uframes_t frames;
};
Alsa::Alsa()
{
handle = NULL;
data = NULL;
device = "default";
srate = 44100;
frames = 32;
}
Alsa::~Alsa()
{
if(handle) snd_pcm_close(handle);
if(data) free(data);
}
bool Alsa::init(int channels, char *cnames[])
{
int rc;
rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
T(rc, "snd_pcm_open");
this->channels = channels;
if(!handle) {
printf("No handle!\n");
return false;
}
snd_pcm_hw_params_alloca(¶ms);
rc = snd_pcm_hw_params_any(handle, params);
T(rc, "snd_pcm_hw_params_any");
rc = snd_pcm_hw_params_set_access(handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED);
T(rc, "snd_pcm_hw_params_set_access");
rc = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_FLOAT);
T(rc, "snd_pcm_hw_params_set_format");
rc = snd_pcm_hw_params_set_channels(handle, params, channels);
T(rc, "snd_pcm_hw_params_set_channels");
rc = snd_pcm_hw_params_set_rate_near(handle, params, &srate, 0);
T(rc, "snd_pcm_hw_params_set_rate_near");
rc = snd_pcm_hw_params_set_period_size_near(handle, params, &frames, 0);
T(rc, "snd_pcm_hw_params_set_period_size_near");
rc = snd_pcm_hw_params(handle, params);
T(rc, "snd_pcm_hw_params");
data = (sample_t*)malloc(sizeof(sample_t) * BUFSZ * channels);
return true;
}
void Alsa::setParm(std::string parm, std::string value)
{
if(parm == "dev") device = value;
if(parm == "frames") frames = atoi(value.c_str());
if(parm == "srate") srate = atoi(value.c_str());
}
bool Alsa::start()
{
return true;
}
void Alsa::stop()
{
}
void Alsa::pre(size_t size)
{
}
void Alsa::run(int channel, sample_t* cdata, size_t csize)
{
for(size_t i = 0; i < csize; i++) {
data[i * channels + channel] = cdata[i];
}
}
void Alsa::post(size_t size)
{
snd_pcm_writei(handle, data, size);
}
extern "C" {
void *create()
{
return new Alsa();
}
void destroy(void *h)
{
Alsa *alsa = (Alsa*)h;
delete alsa;
}
bool init(void *h, int cs, char *cnames[])
{
Alsa *alsa = (Alsa*)h;
return alsa->init(cs, cnames);
}
void setparm(void *h, const char *parm, const char *value)
{
Alsa *alsa = (Alsa*)h;
alsa->setParm(parm, value);
}
bool start(void *h)
{
Alsa *alsa = (Alsa*)h;
return alsa->start();
}
void stop(void *h)
{
Alsa *alsa = (Alsa*)h;
alsa->stop();
}
void pre(void *h, size_t s)
{
Alsa *alsa = (Alsa*)h;
alsa->pre(s);
}
void run(void *h, int ch, sample_t *data, size_t size)
{
Alsa *alsa = (Alsa*)h;
alsa->run(ch, data, size);
}
void post(void *h, size_t s)
{
Alsa *alsa = (Alsa*)h;
alsa->post(s);
}
}
#ifdef TEST_AUDIOOUTPUTENGINEALSA
#include "test.h"
TEST_BEGIN;
TEST_END;
#endif
|