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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
#include "cubeb/cubeb.h"
#include <atomic>
#include <cassert>
#include <cmath>
#include <cstdarg>
#include <cstring>
#include <iostream>
#ifdef _WIN32
#include <objbase.h> // Used by CoInitialize()
#endif
#ifndef M_PI
#define M_PI 3.14159263
#endif
// Default values if none specified
#define DEFAULT_RATE 44100
#define DEFAULT_CHANNELS 2
static const char* state_to_string(cubeb_state state) {
switch (state) {
case CUBEB_STATE_STARTED:
return "CUBEB_STATE_STARTED";
case CUBEB_STATE_STOPPED:
return "CUBEB_STATE_STOPPED";
case CUBEB_STATE_DRAINED:
return "CUBEB_STATE_DRAINED";
case CUBEB_STATE_ERROR:
return "CUBEB_STATE_ERROR";
default:
return "Undefined state";
}
}
void print_log(const char* msg, ...) {
va_list args;
va_start(args, msg);
vprintf(msg, args);
va_end(args);
}
class cubeb_client final {
public:
cubeb_client() {}
~cubeb_client() {}
bool init(char const * backend_name = nullptr);
bool init_stream();
bool start_stream() const;
bool stop_stream() const;
bool destroy_stream() const;
bool destroy();
bool activate_log(bool active);
long user_data_cb(cubeb_stream* stm, void* user, const void* input_buffer,
void* output_buffer, long nframes);
void user_state_cb(cubeb_stream* stm, void* user, cubeb_state state);
bool register_device_collection_changed(cubeb_device_type devtype) const;
bool unregister_device_collection_changed(cubeb_device_type devtype) const;
cubeb_stream_params output_params = {};
cubeb_stream_params input_params = {};
private:
bool has_input() { return input_params.rate != 0; }
bool has_output() { return output_params.rate != 0; }
cubeb* context = nullptr;
cubeb_stream* stream = nullptr;
cubeb_devid output_device = nullptr;
cubeb_devid input_device = nullptr;
/* Accessed only from client and audio thread. */
std::atomic<uint32_t> _rate = {0};
std::atomic<uint32_t> _channels = {0};
/* Accessed only from audio thread. */
uint32_t _total_frames = 0;
};
bool cubeb_client::init(char const * backend_name) {
int rv = cubeb_init(&context, "Cubeb Test Application", nullptr);
if (rv != CUBEB_OK) {
fprintf(stderr, "Could not init cubeb\n");
return false;
}
return true;
}
static long user_data_cb_s(cubeb_stream* stm, void* user,
const void* input_buffer, void* output_buffer,
long nframes) {
assert(user);
return static_cast<cubeb_client*>(user)->user_data_cb(stm, user, input_buffer,
output_buffer, nframes);
}
static void user_state_cb_s(cubeb_stream* stm, void* user, cubeb_state state) {
assert(user);
return static_cast<cubeb_client*>(user)->user_state_cb(stm, user, state);
}
void input_device_changed_callback_s(cubeb* context, void* user) {
fprintf(stderr, "input_device_changed_callback_s\n");
}
void output_device_changed_callback_s(cubeb* context, void* user) {
fprintf(stderr, "output_device_changed_callback_s\n");
}
void io_device_changed_callback_s(cubeb* context, void* user) {
fprintf(stderr, "io_device_changed_callback\n");
}
bool cubeb_client::init_stream() {
assert(has_input() || has_output());
_rate = has_output() ? output_params.rate : input_params.rate;
_channels = has_output() ? output_params.channels : input_params.channels;
int rv =
cubeb_stream_init(context, &stream, "Stream", input_device,
has_input() ? &input_params : nullptr, output_device,
has_output() ? &output_params : nullptr, 512,
user_data_cb_s, user_state_cb_s, this);
if (rv != CUBEB_OK) {
fprintf(stderr, "Could not open the stream\n");
return false;
}
return true;
}
bool cubeb_client::start_stream() const {
int rv = cubeb_stream_start(stream);
if (rv != CUBEB_OK) {
fprintf(stderr, "Could not start the stream\n");
return false;
}
return true;
}
bool cubeb_client::stop_stream() const {
int rv = cubeb_stream_stop(stream);
if (rv != CUBEB_OK) {
fprintf(stderr, "Could not stop the stream\n");
return false;
}
return true;
}
bool cubeb_client::destroy_stream() const {
cubeb_stream_destroy(stream);
return true;
}
bool cubeb_client::destroy() {
cubeb_destroy(context);
return true;
}
bool cubeb_client::activate_log(bool active) {
cubeb_log_level log_level = CUBEB_LOG_DISABLED;
cubeb_log_callback log_callback = nullptr;
if (active) {
log_level = CUBEB_LOG_NORMAL;
log_callback = print_log;
}
if (cubeb_set_log_callback(log_level, log_callback) != CUBEB_OK) {
fprintf(stderr, "Set log callback failed\n");
return false;
}
return true;
}
static void fill_with_sine_tone(float* buf, uint32_t num_of_frames,
uint32_t num_of_channels, uint32_t frame_rate,
uint32_t position) {
for (uint32_t i = 0; i < num_of_frames; ++i) {
for (uint32_t c = 0; c < num_of_channels; ++c) {
buf[i * num_of_channels + c] =
0.2 * sin(2 * M_PI * (i + position) * 350 / frame_rate);
buf[i * num_of_channels + c] +=
0.2 * sin(2 * M_PI * (i + position) * 440 / frame_rate);
}
}
}
long cubeb_client::user_data_cb(cubeb_stream* stm, void* user,
const void* input_buffer, void* output_buffer,
long nframes) {
if (input_buffer && output_buffer) {
const float* in = static_cast<const float*>(input_buffer);
float* out = static_cast<float*>(output_buffer);
memcpy(out, in, sizeof(float) * nframes * _channels);
} else if (output_buffer && !input_buffer) {
fill_with_sine_tone(static_cast<float*>(output_buffer), nframes, _channels,
_rate, _total_frames);
}
_total_frames += nframes;
return nframes;
}
void cubeb_client::user_state_cb(cubeb_stream* stm, void* user,
cubeb_state state) {
fprintf(stderr, "state is %s\n", state_to_string(state));
}
bool cubeb_client::register_device_collection_changed(
cubeb_device_type devtype) const {
cubeb_device_collection_changed_callback callback = nullptr;
if (devtype == static_cast<cubeb_device_type>(CUBEB_DEVICE_TYPE_INPUT |
CUBEB_DEVICE_TYPE_OUTPUT)) {
callback = io_device_changed_callback_s;
} else if (devtype & CUBEB_DEVICE_TYPE_OUTPUT) {
callback = output_device_changed_callback_s;
} else if (devtype & CUBEB_DEVICE_TYPE_INPUT) {
callback = input_device_changed_callback_s;
}
int r = cubeb_register_device_collection_changed(
context, devtype, callback, nullptr);
if (r != CUBEB_OK) {
return false;
}
return true;
}
bool cubeb_client::unregister_device_collection_changed(
cubeb_device_type devtype) const {
int r = cubeb_register_device_collection_changed(
context, devtype, nullptr, nullptr);
if (r != CUBEB_OK) {
return false;
}
return true;
}
enum play_mode {
RECORD,
PLAYBACK,
DUPLEX,
COLLECTION_CHANGE,
};
struct operation_data {
play_mode pm;
uint32_t rate;
cubeb_device_type collection_device_type;
};
void print_help() {
const char * msg =
"p: start a initialized stream\n"
"s: stop a started stream\n"
"i: change device type to input\n"
"o: change device type to output\n"
"a: change device type to input and output\n"
"k: change device type to unknown\n"
"r: register device collection changed callback for the current device type\n"
"u: unregister device collection changed callback for the current device type\n"
"q: quit\n"
"h: print this message\n";
fprintf(stderr, "%s\n", msg);
}
bool choose_action(const cubeb_client& cl, operation_data * op, char c) {
while (c == 10 || c == 32) {
// Consume "enter and "space"
c = getchar();
}
if (c == 'q') {
if (op->pm == PLAYBACK || op->pm == RECORD || op->pm == DUPLEX) {
bool res = cl.stop_stream();
if (!res) {
fprintf(stderr, "stop_stream failed\n");
}
res = cl.destroy_stream();
if (!res) {
fprintf(stderr, "destroy_stream failed\n");
}
} else if (op->pm == COLLECTION_CHANGE) {
bool res = cl.unregister_device_collection_changed(op->collection_device_type);
if (!res) {
fprintf(stderr, "unregister_device_collection_changed failed\n");
}
}
return false; // exit the loop
} else if (c == 'h') {
print_help();
} else if (c == 'p') {
bool res = cl.start_stream();
if (res) {
fprintf(stderr, "start_stream succeed\n");
} else {
fprintf(stderr, "start_stream failed\n");
}
} else if (c == 's') {
bool res = cl.stop_stream();
if (res) {
fprintf(stderr, "stop_stream succeed\n");
} else {
fprintf(stderr, "stop_stream failed\n");
}
} else if (c == 'i') {
op->collection_device_type = CUBEB_DEVICE_TYPE_INPUT;
fprintf(stderr, "collection device type changed to INPUT\n");
} else if (c == 'o') {
op->collection_device_type = CUBEB_DEVICE_TYPE_OUTPUT;
fprintf(stderr, "collection device type changed to OUTPUT\n");
} else if (c == 'a') {
op->collection_device_type = static_cast<cubeb_device_type>(CUBEB_DEVICE_TYPE_INPUT | CUBEB_DEVICE_TYPE_OUTPUT);
fprintf(stderr, "collection device type changed to INPUT | OUTPUT\n");
} else if (c == 'k') {
op->collection_device_type = CUBEB_DEVICE_TYPE_UNKNOWN;
fprintf(stderr, "collection device type changed to UNKNOWN\n");
} else if (c == 'r') {
bool res = cl.register_device_collection_changed(op->collection_device_type);
if (res) {
fprintf(stderr, "register_device_collection_changed succeed\n");
} else {
fprintf(stderr, "register_device_collection_changed failed\n");
}
} else if (c == 'u') {
bool res = cl.unregister_device_collection_changed(op->collection_device_type);
if (res) {
fprintf(stderr, "unregister_device_collection_changed succeed\n");
} else {
fprintf(stderr, "unregister_device_collection_changed failed\n");
}
} else {
fprintf(stderr, "Error: %c is not a valid entry\n", c);
}
return true; // Loop up
}
int main(int argc, char* argv[]) {
#ifdef _WIN32
CoInitialize(nullptr);
#endif
operation_data op;
op.pm = PLAYBACK;
if (argc > 1) {
if ('r' == argv[1][0]) {
op.pm = RECORD;
} else if ('p' == argv[1][0]) {
op.pm = PLAYBACK;
} else if ('d' == argv[1][0]) {
op.pm = DUPLEX;
} else if ('c' == argv[1][0]) {
op.pm = COLLECTION_CHANGE;
}
}
op.rate = DEFAULT_RATE;
if (argc > 2) {
op.rate = strtoul(argv[2], NULL, 0);
}
bool res = false;
cubeb_client cl;
cl.activate_log(true);
cl.init();
op.collection_device_type = CUBEB_DEVICE_TYPE_UNKNOWN;
fprintf(stderr, "collection device type is UNKNOWN\n");
if (op.pm == COLLECTION_CHANGE) {
op.collection_device_type = CUBEB_DEVICE_TYPE_OUTPUT;
fprintf(stderr, "collection device type changed to OUTPUT\n");
res = cl.register_device_collection_changed(op.collection_device_type);
if (res) {
fprintf(stderr, "register_device_collection_changed succeed\n");
} else {
fprintf(stderr, "register_device_collection_changed failed\n");
}
} else {
if (op.pm == PLAYBACK || op.pm == DUPLEX) {
cl.output_params = {CUBEB_SAMPLE_FLOAT32NE, op.rate, DEFAULT_CHANNELS,
CUBEB_LAYOUT_UNDEFINED, CUBEB_STREAM_PREF_NONE};
}
if (op.pm == RECORD || op.pm == DUPLEX) {
cl.input_params = {CUBEB_SAMPLE_FLOAT32NE, op.rate, DEFAULT_CHANNELS,
CUBEB_LAYOUT_UNDEFINED, CUBEB_STREAM_PREF_NONE};
}
res = cl.init_stream();
if (!res) {
fprintf(stderr, "stream_init failed\n");
return -1;
}
fprintf(stderr, "stream_init succeed\n");
res = cl.start_stream();
if (res) {
fprintf(stderr, "stream_start succeed\n");
} else {
fprintf(stderr, "stream_init failed\n");
}
}
// User input
do {
fprintf(stderr, "press `q` to abort or `h` for help\n");
} while (choose_action(cl, &op, getchar()));
cl.destroy();
return 0;
}
|