2019-04-05 19:05:18 +00:00
|
|
|
#include "oslib/audiostream.h"
|
2015-04-16 11:59:48 +00:00
|
|
|
#if USE_ALSA
|
2013-12-19 17:10:14 +00:00
|
|
|
#include <alsa/asoundlib.h>
|
2019-01-16 18:04:32 +00:00
|
|
|
#include "cfg/cfg.h"
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2018-04-27 10:49:23 +00:00
|
|
|
static snd_pcm_t *handle;
|
|
|
|
static bool pcm_blocking = true;
|
2018-05-01 17:25:42 +00:00
|
|
|
static snd_pcm_uframes_t buffer_size;
|
|
|
|
static snd_pcm_uframes_t period_size;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2015-04-14 19:54:26 +00:00
|
|
|
// We're making these functions static - there's no need to pollute the global namespace
|
|
|
|
static void alsa_init()
|
2013-12-19 17:10:14 +00:00
|
|
|
{
|
2013-12-24 00:56:44 +00:00
|
|
|
snd_pcm_hw_params_t *params;
|
|
|
|
unsigned int val;
|
2016-10-18 20:09:00 +00:00
|
|
|
int dir=-1;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2019-01-16 18:04:32 +00:00
|
|
|
string device = cfgLoadStr("alsa", "device", "");
|
2013-12-24 00:56:44 +00:00
|
|
|
|
2019-01-16 18:04:32 +00:00
|
|
|
int rc = -1;
|
|
|
|
if (device == "")
|
|
|
|
{
|
|
|
|
printf("ALSA: trying to determine audio device\n");
|
|
|
|
/* Open PCM device for playback. */
|
|
|
|
device = "default";
|
|
|
|
rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
device = "plughw:0,0,0";
|
|
|
|
rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
device = "plughw:0,0";
|
|
|
|
rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
|
|
|
|
}
|
2013-12-24 00:56:44 +00:00
|
|
|
|
2019-01-16 18:04:32 +00:00
|
|
|
if (rc >= 0)
|
|
|
|
{
|
|
|
|
// init successfull, write value back to config
|
|
|
|
cfgSaveStr("alsa", "device", device.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
|
|
|
|
}
|
2013-12-24 00:56:44 +00:00
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
2019-01-16 18:04:32 +00:00
|
|
|
fprintf(stderr, "unable to open PCM device %s: %s\n", device.c_str(), snd_strerror(rc));
|
2013-12-24 00:56:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-16 18:04:32 +00:00
|
|
|
printf("ALSA: Successfully initialized \"%s\"\n", device.c_str());
|
|
|
|
|
2013-12-24 00:56:44 +00:00
|
|
|
/* Allocate a hardware parameters object. */
|
|
|
|
snd_pcm_hw_params_alloca(¶ms);
|
|
|
|
|
|
|
|
/* Fill it in with default values. */
|
|
|
|
rc=snd_pcm_hw_params_any(handle, params);
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error:snd_pcm_hw_params_any %s\n", snd_strerror(rc));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the desired hardware parameters. */
|
|
|
|
|
|
|
|
/* Interleaved mode */
|
|
|
|
rc=snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error:snd_pcm_hw_params_set_access %s\n", snd_strerror(rc));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Signed 16-bit little-endian format */
|
|
|
|
rc=snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error:snd_pcm_hw_params_set_format %s\n", snd_strerror(rc));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Two channels (stereo) */
|
|
|
|
rc=snd_pcm_hw_params_set_channels(handle, params, 2);
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error:snd_pcm_hw_params_set_channels %s\n", snd_strerror(rc));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 44100 bits/second sampling rate (CD quality) */
|
|
|
|
val = 44100;
|
|
|
|
rc=snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error:snd_pcm_hw_params_set_rate_near %s\n", snd_strerror(rc));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set period size to settings.aica.BufferSize frames. */
|
2018-05-01 17:25:42 +00:00
|
|
|
period_size = settings.aica.BufferSize;
|
|
|
|
rc=snd_pcm_hw_params_set_period_size_near(handle, params, &period_size, &dir);
|
2013-12-24 00:56:44 +00:00
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error:snd_pcm_hw_params_set_buffer_size_near %s\n", snd_strerror(rc));
|
|
|
|
return;
|
|
|
|
}
|
2018-04-27 10:49:23 +00:00
|
|
|
else
|
2018-05-01 17:25:42 +00:00
|
|
|
printf("ALSA: period size set to %ld\n", period_size);
|
|
|
|
buffer_size = (44100 * 100 /* settings.omx.Audio_Latency */ / 1000 / period_size + 1) * period_size;
|
|
|
|
rc=snd_pcm_hw_params_set_buffer_size_near(handle, params, &buffer_size);
|
2013-12-24 00:56:44 +00:00
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error:snd_pcm_hw_params_set_buffer_size_near %s\n", snd_strerror(rc));
|
|
|
|
return;
|
|
|
|
}
|
2018-04-27 10:49:23 +00:00
|
|
|
else
|
2018-05-01 17:25:42 +00:00
|
|
|
printf("ALSA: buffer size set to %ld\n", buffer_size);
|
2013-12-24 00:56:44 +00:00
|
|
|
|
|
|
|
/* Write the parameters to the driver */
|
|
|
|
rc = snd_pcm_hw_params(handle, params);
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to set hw parameters: %s\n", snd_strerror(rc));
|
|
|
|
return;
|
|
|
|
}
|
2013-12-19 17:10:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 19:54:26 +00:00
|
|
|
static u32 alsa_push(void* frame, u32 samples, bool wait)
|
|
|
|
{
|
2018-04-27 10:49:23 +00:00
|
|
|
if (wait != pcm_blocking) {
|
|
|
|
snd_pcm_nonblock(handle, wait ? 0 : 1);
|
|
|
|
pcm_blocking = wait;
|
|
|
|
}
|
2015-04-14 19:54:26 +00:00
|
|
|
|
|
|
|
int rc = snd_pcm_writei(handle, frame, samples);
|
|
|
|
if (rc == -EPIPE)
|
|
|
|
{
|
|
|
|
/* EPIPE means underrun */
|
|
|
|
fprintf(stderr, "ALSA: underrun occurred\n");
|
|
|
|
snd_pcm_prepare(handle);
|
2018-05-01 17:25:42 +00:00
|
|
|
// Write some silence then our samples
|
2018-06-05 10:18:42 +00:00
|
|
|
const size_t silence_size = period_size * 4;
|
2018-05-01 17:25:42 +00:00
|
|
|
void *silence = alloca(silence_size * 4);
|
|
|
|
memset(silence, 0, silence_size * 4);
|
|
|
|
rc = snd_pcm_writei(handle, silence, silence_size);
|
|
|
|
if (rc < 0)
|
|
|
|
fprintf(stderr, "ALSA: error from writei(silence): %s\n", snd_strerror(rc));
|
|
|
|
else if (rc < silence_size)
|
|
|
|
fprintf(stderr, "ALSA: short write from writei(silence): %d/%ld frames\n", rc, silence_size);
|
|
|
|
rc = snd_pcm_writei(handle, frame, samples);
|
|
|
|
if (rc < 0)
|
|
|
|
fprintf(stderr, "ALSA: error from writei(again): %s\n", snd_strerror(rc));
|
|
|
|
else if (rc < samples)
|
|
|
|
fprintf(stderr, "ALSA: short write from writei(again): %d/%d frames\n", rc, samples);
|
2015-04-14 19:54:26 +00:00
|
|
|
}
|
|
|
|
else if (rc < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ALSA: error from writei: %s\n", snd_strerror(rc));
|
|
|
|
}
|
|
|
|
else if (rc != samples)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ALSA: short write, wrote %d frames of %d\n", rc, samples);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void alsa_term()
|
2013-12-19 17:10:14 +00:00
|
|
|
{
|
|
|
|
snd_pcm_drain(handle);
|
|
|
|
snd_pcm_close(handle);
|
|
|
|
}
|
|
|
|
|
2019-04-24 19:41:38 +00:00
|
|
|
std::vector<std::string> alsa_get_devicelist()
|
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
char **hints;
|
|
|
|
int err = snd_device_name_hint(-1, "pcm", (void***)&hints);
|
|
|
|
|
|
|
|
// Error initializing ALSA
|
|
|
|
if (err != 0)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
|
|
char** n = hints;
|
|
|
|
while (*n != NULL)
|
|
|
|
{
|
|
|
|
// Get the type (NULL/Input/Output)
|
|
|
|
char *type = snd_device_name_get_hint(*n, "IOID");
|
|
|
|
char *name = snd_device_name_get_hint(*n, "NAME");
|
|
|
|
|
|
|
|
if (name != NULL)
|
|
|
|
{
|
|
|
|
// We only want output or special devices (like "default" or "pulse")
|
|
|
|
// TODO Only those with type == NULL?
|
|
|
|
if (type == NULL || strcmp(type, "Output") == 0)
|
|
|
|
{
|
|
|
|
// TODO Check if device works (however we need to hash the resulting list then)
|
|
|
|
/*snd_pcm_t *handle;
|
|
|
|
int rc = snd_pcm_open(&handle, name, SND_PCM_STREAM_PLAYBACK, 0);
|
|
|
|
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
result.push_back(name);
|
|
|
|
snd_pcm_close(handle);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
result.push_back(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type != NULL)
|
|
|
|
free(type);
|
|
|
|
|
|
|
|
if (name != NULL)
|
|
|
|
free(name);
|
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_device_name_free_hint((void**)hints);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static audio_option_t* alsa_audio_options(int* option_count)
|
|
|
|
{
|
|
|
|
*option_count = 1;
|
|
|
|
static audio_option_t result[1];
|
|
|
|
|
|
|
|
result[0].cfg_name = "device";
|
|
|
|
result[0].caption = "Device";
|
|
|
|
result[0].type = list;
|
|
|
|
result[0].list_callback = alsa_get_devicelist;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-04-05 19:05:18 +00:00
|
|
|
static audiobackend_t audiobackend_alsa = {
|
2015-04-14 19:54:26 +00:00
|
|
|
"alsa", // Slug
|
|
|
|
"Advanced Linux Sound Architecture", // Name
|
|
|
|
&alsa_init,
|
|
|
|
&alsa_push,
|
2019-04-24 19:41:38 +00:00
|
|
|
&alsa_term,
|
|
|
|
&alsa_audio_options
|
2015-04-14 19:54:26 +00:00
|
|
|
};
|
2019-04-05 19:05:18 +00:00
|
|
|
|
|
|
|
static bool alsa = RegisterAudioBackend(&audiobackend_alsa);
|
2015-04-15 09:39:12 +00:00
|
|
|
#endif
|