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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
|
/*
DeaDBeeF - ultimate music player for GNU/Linux systems with X11
Copyright (C) 2009-2010 Alexey Yakovenko <waker@users.sourceforge.net>
This program 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.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/ioctl.h>
#include <alsa/asoundlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/prctl.h>
#include "../../deadbeef.h"
#include "../../config.h"
#define trace(...) { fprintf(stderr, __VA_ARGS__); }
//#define trace(fmt,...)
#define min(x,y) ((x)<(y)?(x):(y))
static DB_output_t plugin;
DB_functions_t *deadbeef;
static snd_pcm_t *audio;
static int alsa_terminate;
static int requested_rate = -1;
static int alsa_rate = 44100;
static int state; // one of output_state_t
static uintptr_t mutex;
static intptr_t alsa_tid;
static snd_pcm_uframes_t buffer_size;
static snd_pcm_uframes_t period_size;
static snd_pcm_uframes_t req_buffer_size;
static snd_pcm_uframes_t req_period_size;
static int conf_alsa_resample = 0;
static char conf_alsa_soundcard[100] = "default";
//static snd_async_handler_t *pcm_callback;
static void
palsa_callback (char *stream, int len);
#if 0
static void
alsa_callback (snd_async_handler_t *pcm_callback) {
snd_pcm_t *pcm_handle = snd_async_handler_get_pcm(pcm_callback);
snd_pcm_sframes_t avail;
int err;
printf ("alsa_callback\n");
avail = snd_pcm_avail_update(pcm_handle);
while (avail >= period_size) {
char buf[avail * 4];
palsa_callback (buf, avail * 4);
if ((err = snd_pcm_writei (pcm_handle, buf, period_size)) < 0) {
perror ("snd_pcm_writei");
}
avail = snd_pcm_avail_update(pcm_handle);
}
}
#endif
static void
palsa_thread (void *context);
static int
palsa_init (void);
static int
palsa_free (void);
static int
palsa_change_rate (int rate);
static int
palsa_play (void);
static int
palsa_stop (void);
static int
palsa_pause (void);
static int
palsa_unpause (void);
static int
palsa_get_rate (void);
static int
palsa_get_bps (void);
static int
palsa_get_channels (void);
static int
palsa_get_endianness (void);
static void
palsa_enum_soundcards (void (*callback)(const char *name, const char *desc, void*), void *userdata);
static int
palsa_set_hw_params (int samplerate) {
snd_pcm_hw_params_t *hw_params = NULL;
// int alsa_resample = conf_get_int ("alsa.resample", 0);
int err = 0;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
trace ("cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
goto error;
}
if ((err = snd_pcm_hw_params_any (audio, hw_params)) < 0) {
trace ("cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
goto error;
}
if ((err = snd_pcm_hw_params_set_access (audio, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
trace ("cannot set access type (%s)\n",
snd_strerror (err));
goto error;
}
snd_pcm_format_t fmt;
#if WORDS_BIGENDIAN
fmt = SND_PCM_FORMAT_S16_BE;
#else
fmt = SND_PCM_FORMAT_S16_LE;
#endif
if ((err = snd_pcm_hw_params_set_format (audio, hw_params, fmt)) < 0) {
trace ("cannot set sample format (%s)\n",
snd_strerror (err));
goto error;
}
snd_pcm_hw_params_get_format (hw_params, &fmt);
trace ("chosen sample format: %04Xh\n", (int)fmt);
int val = samplerate;
int ret = 0;
if ((err = snd_pcm_hw_params_set_rate_resample (audio, hw_params, conf_alsa_resample)) < 0) {
trace ("cannot setup resampling (%s)\n",
snd_strerror (err));
goto error;
}
if ((err = snd_pcm_hw_params_set_rate_near (audio, hw_params, &val, &ret)) < 0) {
trace ("cannot set sample rate (%s)\n",
snd_strerror (err));
goto error;
}
alsa_rate = val;
trace ("chosen samplerate: %d Hz\n", alsa_rate);
if ((err = snd_pcm_hw_params_set_channels (audio, hw_params, 2)) < 0) {
trace ("cannot set channel count (%s)\n",
snd_strerror (err));
goto error;
}
int nchan;
snd_pcm_hw_params_get_channels (hw_params, &nchan);
trace ("alsa channels: %d\n", nchan);
req_buffer_size = deadbeef->conf_get_int ("alsa.buffer", 1024);
req_period_size = deadbeef->conf_get_int ("alsa.period", 512);
trace ("trying buffer size: %d frames\n", req_buffer_size);
trace ("trying period size: %d frames\n", req_period_size);
snd_pcm_hw_params_set_buffer_size_near (audio, hw_params, &buffer_size);
snd_pcm_hw_params_set_period_size_near (audio, hw_params, &period_size, NULL);
trace ("alsa buffer size: %d frames\n", buffer_size);
trace ("alsa period size: %d frames\n", period_size);
if ((err = snd_pcm_hw_params (audio, hw_params)) < 0) {
trace ("cannot set parameters (%s)\n",
snd_strerror (err));
goto error;
}
error:
if (hw_params) {
snd_pcm_hw_params_free (hw_params);
}
return err;
}
int
palsa_init (void) {
int err;
alsa_tid = 0;
mutex = 0;
// get and cache conf variables
strcpy (conf_alsa_soundcard, deadbeef->conf_get_str ("alsa_soundcard", "default"));
conf_alsa_resample = deadbeef->conf_get_int ("alsa.resample", 0);
trace ("alsa_soundcard: %s\n", conf_alsa_soundcard);
trace ("alsa.resample: %d\n", conf_alsa_resample);
snd_pcm_sw_params_t *sw_params = NULL;
state = OUTPUT_STATE_STOPPED;
//const char *conf_alsa_soundcard = conf_get_str ("alsa_soundcard", "default");
if ((err = snd_pcm_open (&audio, conf_alsa_soundcard, SND_PCM_STREAM_PLAYBACK, 0))) {
trace ("could not open audio device (%s)\n",
snd_strerror (err));
return -1;
}
mutex = deadbeef->mutex_create ();
if (requested_rate != -1) {
alsa_rate = requested_rate;
}
if (palsa_set_hw_params (alsa_rate) < 0) {
goto open_error;
}
if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
trace ("cannot allocate software parameters structure (%s)\n",
snd_strerror (err));
goto open_error;
}
if ((err = snd_pcm_sw_params_current (audio, sw_params)) < 0) {
trace ("cannot initialize software parameters structure (%s)\n",
snd_strerror (err));
goto open_error;
}
snd_pcm_sw_params_set_start_threshold (audio, sw_params, buffer_size - period_size);
if ((err = snd_pcm_sw_params_set_avail_min (audio, sw_params, period_size)) < 0) {
trace ("cannot set minimum available count (%s)\n",
snd_strerror (err));
goto open_error;
}
snd_pcm_uframes_t av;
if ((err = snd_pcm_sw_params_get_avail_min (sw_params, &av)) < 0) {
trace ("snd_pcm_sw_params_get_avail_min failed (%s)\n",
snd_strerror (err));
goto open_error;
}
trace ("alsa avail_min: %d frames\n", (int)av);
// if ((err = snd_pcm_sw_params_set_start_threshold (audio, sw_params, 0U)) < 0) {
// trace ("cannot set start mode (%s)\n",
// snd_strerror (err));
// goto open_error;
// }
if ((err = snd_pcm_sw_params (audio, sw_params)) < 0) {
trace ("cannot set software parameters (%s)\n",
snd_strerror (err));
goto open_error;
}
snd_pcm_sw_params_free (sw_params);
sw_params = NULL;
/* the interface will interrupt the kernel every N frames, and ALSA
will wake up this program very soon after that.
*/
if ((err = snd_pcm_prepare (audio)) < 0) {
trace ("cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
goto open_error;
}
snd_pcm_start (audio);
alsa_terminate = 0;
alsa_tid = deadbeef->thread_start (palsa_thread, NULL);
return 0;
open_error:
if (sw_params) {
snd_pcm_sw_params_free (sw_params);
}
if (audio != NULL) {
palsa_free ();
}
return -1;
}
int
palsa_change_rate (int rate) {
trace ("palsa_change_rate: %d\n", rate);
requested_rate = rate;
if (!audio) {
return alsa_rate;
}
if (rate == alsa_rate) {
trace ("palsa_change_rate: ignored\n", rate);
return rate;
}
state = OUTPUT_STATE_STOPPED;
snd_pcm_drop (audio);
deadbeef->mutex_lock (mutex);
int ret = palsa_set_hw_params (rate);
deadbeef->mutex_unlock (mutex);
if (ret < 0) {
trace ("palsa_change_rate: impossible to set samplerate to %d\n", rate);
return alsa_rate;
}
trace ("chosen samplerate: %d\n", alsa_rate);
return alsa_rate;
}
int
palsa_free (void) {
trace ("palsa_free\n");
if (audio && !alsa_terminate) {
deadbeef->mutex_lock (mutex);
alsa_terminate = 1;
deadbeef->mutex_unlock (mutex);
printf ("waiting for alsa thread to finish\n");
if (alsa_tid) {
deadbeef->thread_join (alsa_tid);
alsa_tid = 0;
}
snd_pcm_close(audio);
audio = NULL;
if (mutex) {
deadbeef->mutex_free (mutex);
mutex = 0;
}
state = OUTPUT_STATE_STOPPED;
alsa_terminate = 0;
}
return 0;
}
static void
palsa_hw_pause (int pause) {
if (!audio) {
return;
}
if (state == OUTPUT_STATE_STOPPED) {
return;
}
if (pause == 1) {
snd_pcm_drop (audio);
}
else {
snd_pcm_prepare (audio);
snd_pcm_start (audio);
}
}
int
palsa_play (void) {
int err;
trace ("palsa_play\n");
if (state == OUTPUT_STATE_STOPPED) {
if (!audio) {
if (palsa_init () < 0) {
state = OUTPUT_STATE_STOPPED;
return -1;
}
}
else {
if ((err = snd_pcm_prepare (audio)) < 0) {
trace ("cannot prepare audio interface for use (%d, %s)\n",
err, snd_strerror (err));
return -1;
}
}
}
if (state != OUTPUT_STATE_PLAYING) {
deadbeef->mutex_lock (mutex);
// trace ("alsa: installing async handler\n");
// if (snd_async_add_pcm_handler (&pcm_callback, audio, alsa_callback, NULL) < 0) {
// perror ("snd_async_add_pcm_handler");
// }
// trace ("pcm_callback=%p\n", pcm_callback);
snd_pcm_start (audio);
deadbeef->mutex_unlock (mutex);
state = OUTPUT_STATE_PLAYING;
}
return 0;
}
int
palsa_stop (void) {
if (!audio) {
return 0;
}
state = OUTPUT_STATE_STOPPED;
deadbeef->mutex_lock (mutex);
snd_pcm_drop (audio);
#if 0
if (pcm_callback) {
snd_async_del_handler (pcm_callback);
pcm_callback = NULL;
}
#endif
deadbeef->mutex_unlock (mutex);
deadbeef->streamer_reset (1);
if (deadbeef->conf_get_int ("alsa.freeonstop", 0)) {
palsa_free ();
}
return 0;
}
int
palsa_pause (void) {
if (state == OUTPUT_STATE_STOPPED || !audio) {
return -1;
}
// set pause state
deadbeef->mutex_lock (mutex);
palsa_hw_pause (1);
deadbeef->mutex_unlock (mutex);
state = OUTPUT_STATE_PAUSED;
return 0;
}
int
palsa_unpause (void) {
// unset pause state
if (state == OUTPUT_STATE_PAUSED) {
state = OUTPUT_STATE_PLAYING;
deadbeef->mutex_lock (mutex);
palsa_hw_pause (0);
deadbeef->mutex_unlock (mutex);
}
return 0;
}
int
palsa_get_rate (void) {
if (!audio) {
palsa_init ();
}
return alsa_rate;
}
int
palsa_get_bps (void) {
return 16;
}
int
palsa_get_channels (void) {
return 2;
}
static int
palsa_get_endianness (void) {
#if WORDS_BIGENDIAN
return 1;
#else
return 0;
#endif
}
static void
palsa_thread (void *context) {
prctl (PR_SET_NAME, "deadbeef-alsa", 0, 0, 0, 0);
int err;
for (;;) {
if (alsa_terminate) {
break;
}
if (state != OUTPUT_STATE_PLAYING || !deadbeef->streamer_ok_to_read (-1)) {
usleep (10000);
continue;
}
deadbeef->mutex_lock (mutex);
if ((err = snd_pcm_wait (audio, 1000)) < 0) {
if (err == -ESTRPIPE) {
trace ("alsa: trying to recover from suspend... (error=%d, %s)\n", err, snd_strerror (err));
deadbeef->sendmessage (M_REINIT_SOUND, 0, 0, 0);
deadbeef->mutex_unlock (mutex);
break;
}
else if (err == -EPIPE) {
// this pretty frequent condition, no spam here
// trace ("alsa: snd_pcm_wait error=%d, %s\n", err, snd_strerror (err));
snd_pcm_prepare (audio);
snd_pcm_start (audio);
deadbeef->mutex_unlock (mutex);
continue;
}
else {
trace ("alsa: snd_pcm_wait error=%d, %s\n", err, snd_strerror (err));
deadbeef->mutex_unlock (mutex);
continue;
}
}
/* find out how much space is available for playback data */
int written = 0;
snd_pcm_sframes_t frames_to_deliver = snd_pcm_avail_update (audio);
while (frames_to_deliver >= period_size) {
char buf[period_size * 4];
palsa_callback (buf, period_size * 4);
if ((err = snd_pcm_writei (audio, buf, period_size)) < 0) {
break;
}
written += period_size;
frames_to_deliver = snd_pcm_avail_update (audio);
}
// trace ("wrote %d frames\n", written);
deadbeef->mutex_unlock (mutex);
// usleep (1000); // this must be here to prevent mutex deadlock
}
}
static void
palsa_callback (char *stream, int len) {
int bytesread = deadbeef->streamer_read (stream, len);
// FIXME: move volume control to streamer_read for copy optimization
#if 0
int16_t vol[4];
vol[0] = volume_get_amp () * 255; // that will be extra 8 bits
// pack 4 times
vol[1] = vol[2] = vol[3] = vol[0];
// apply volume with mmx
__asm__ volatile(
" mov %0, %%ecx\n\t"
" shr $4, %%ecx\n\t"
" mov %1, %%eax\n\t"
" movq %2, %mm1\n\t"
"1:\n\t"
" movq [%%eax], %mm0\n\t"
" movq %mm0, %mm2\n\t"
" movq %mm0, %mm3\n\t"
" pmullw %mm1, %mm2\n\t"
" pmulhw %mm1, %mm3\n\t"
" psrlw $8, %mm2\n\t" // discard lowest 8 bits
" psllw $8, %mm3\n\t" // shift left 8 lsbs of hiwords
" por %mm3, %mm2\n\t" // OR them together
" movq %mm3, [%%eax]\n\t" // load back to memory
" add $8, %%eax\n\t"
" dec %%ecx\n\t"
" jnz 1b\n\t"
:
: "r"(len), "r"(stream), "r"(vol)
: "%ecx", "%eax"
);
#else
int16_t ivolume = deadbeef->volume_get_amp () * 1000;
for (int i = 0; i < bytesread/2; i++) {
((int16_t*)stream)[i] = (int16_t)(((int32_t)(((int16_t*)stream)[i])) * ivolume / 1000);
}
#endif
if (bytesread < len) {
memset (stream + bytesread, 0, len-bytesread);
}
}
static int
palsa_configchanged (DB_event_t *ev, uintptr_t data) {
int alsa_resample = deadbeef->conf_get_int ("alsa.resample", 0);
const char *alsa_soundcard = deadbeef->conf_get_str ("alsa_soundcard", "default");
int buffer = deadbeef->conf_get_int ("alsa.buffer", 1024);
int period = deadbeef->conf_get_int ("alsa.period", 512);
if (audio &&
(alsa_resample != conf_alsa_resample
|| strcmp (alsa_soundcard, conf_alsa_soundcard)
|| buffer != req_buffer_size
|| period != req_period_size)) {
trace ("alsa: config option changed, restarting\n");
deadbeef->sendmessage (M_REINIT_SOUND, 0, 0, 0);
}
return 0;
}
// derived from alsa-utils/aplay.c
void
palsa_enum_soundcards (void (*callback)(const char *name, const char *desc, void *), void *userdata) {
void **hints, **n;
char *name, *descr, *io;
const char *filter = "Output";
if (snd_device_name_hint(-1, "pcm", &hints) < 0)
return;
n = hints;
while (*n != NULL) {
name = snd_device_name_get_hint(*n, "NAME");
descr = snd_device_name_get_hint(*n, "DESC");
io = snd_device_name_get_hint(*n, "IOID");
if (io == NULL || !strcmp(io, filter)) {
if (name && descr && callback) {
callback (name, descr, userdata);
}
}
if (name != NULL)
free(name);
if (descr != NULL)
free(descr);
if (io != NULL)
free(io);
n++;
}
snd_device_name_free_hint(hints);
}
int
palsa_get_state (void) {
return state;
}
int
alsa_start (void) {
deadbeef->ev_subscribe (DB_PLUGIN (&plugin), DB_EV_CONFIGCHANGED, DB_CALLBACK (palsa_configchanged), 0);
return 0;
}
int
alsa_stop (void) {
deadbeef->ev_unsubscribe (DB_PLUGIN (&plugin), DB_EV_CONFIGCHANGED, DB_CALLBACK (palsa_configchanged), 0);
return 0;
}
DB_plugin_t *
alsa_load (DB_functions_t *api) {
deadbeef = api;
return DB_PLUGIN (&plugin);
}
static const char settings_dlg[] =
"property \"Use ALSA resampling\" checkbox alsa.resample 0;\n"
"property \"Release device while stopped\" checkbox alsa.freeonstop 0;\n"
"property \"Preferred buffer size\" entry alsa.buffer 1024;\n"
"property \"Preferred period size\" entry alsa.period 512;\n"
;
// define plugin interface
static DB_output_t plugin = {
DB_PLUGIN_SET_API_VERSION
.plugin.version_major = 0,
.plugin.version_minor = 1,
.plugin.nostop = 1,
.plugin.type = DB_PLUGIN_OUTPUT,
.plugin.name = "ALSA output plugin",
.plugin.descr = "plays sound through linux standard alsa library",
.plugin.author = "Alexey Yakovenko",
.plugin.email = "waker@users.sourceforge.net",
.plugin.website = "http://deadbeef.sf.net",
.plugin.start = alsa_start,
.plugin.stop = alsa_stop,
.plugin.configdialog = settings_dlg,
.init = palsa_init,
.free = palsa_free,
.change_rate = palsa_change_rate,
.play = palsa_play,
.stop = palsa_stop,
.pause = palsa_pause,
.unpause = palsa_unpause,
.state = palsa_get_state,
.samplerate = palsa_get_rate,
.bitspersample = palsa_get_bps,
.channels = palsa_get_channels,
.endianness = palsa_get_endianness,
.enum_soundcards = palsa_enum_soundcards,
};
|