2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_driver_alsa.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2018-01-02 10:27:24 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "audio_driver_alsa.h"
|
|
|
|
|
|
|
|
#ifdef ALSA_ENABLED
|
|
|
|
|
|
|
|
#include "globals.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include <errno.h>
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Error AudioDriverALSA::init() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
active = false;
|
|
|
|
thread_exited = false;
|
|
|
|
exit_thread = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
pcm_open = false;
|
|
|
|
samples_in = NULL;
|
|
|
|
samples_out = NULL;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
mix_rate = GLOBAL_DEF("audio/mix_rate", 44100);
|
2014-02-10 01:10:30 +00:00
|
|
|
output_format = OUTPUT_STEREO;
|
|
|
|
channels = 2;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int status;
|
2014-02-10 01:10:30 +00:00
|
|
|
snd_pcm_hw_params_t *hwparams;
|
|
|
|
snd_pcm_sw_params_t *swparams;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#define CHECK_FAIL(m_cond) \
|
|
|
|
if (m_cond) { \
|
|
|
|
fprintf(stderr, "ALSA ERR: %s\n", snd_strerror(status)); \
|
|
|
|
snd_pcm_close(pcm_handle); \
|
|
|
|
ERR_FAIL_COND_V(m_cond, ERR_CANT_OPEN); \
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//todo, add
|
|
|
|
//6 chans - "plug:surround51"
|
|
|
|
//4 chans - "plug:surround40";
|
|
|
|
|
|
|
|
status = snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(status < 0, ERR_CANT_OPEN);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
snd_pcm_hw_params_alloca(&hwparams);
|
|
|
|
|
2017-01-03 18:00:31 +00:00
|
|
|
status = snd_pcm_hw_params_any(pcm_handle, hwparams);
|
2017-03-18 23:36:26 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
status = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
|
2017-03-18 23:36:26 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//not interested in anything else
|
|
|
|
status = snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE);
|
2017-03-18 23:36:26 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//todo: support 4 and 6
|
|
|
|
status = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, 2);
|
2017-03-18 23:36:26 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
status = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &mix_rate, NULL);
|
2017-03-18 23:36:26 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int latency = GLOBAL_DEF("audio/output_latency", 25);
|
2017-08-18 15:10:21 +00:00
|
|
|
buffer_size = closest_power_of_2(latency * mix_rate / 1000);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-03 18:00:31 +00:00
|
|
|
// set buffer size from project settings
|
|
|
|
status = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size);
|
2017-03-18 23:36:26 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
// make period size 1/8
|
2017-01-03 18:00:31 +00:00
|
|
|
period_size = buffer_size >> 3;
|
|
|
|
status = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &period_size, NULL);
|
2017-03-18 23:36:26 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
unsigned int periods = 2;
|
2014-02-10 01:10:30 +00:00
|
|
|
status = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &periods, NULL);
|
2017-03-18 23:36:26 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
status = snd_pcm_hw_params(pcm_handle, hwparams);
|
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//snd_pcm_hw_params_free(&hwparams);
|
|
|
|
|
|
|
|
snd_pcm_sw_params_alloca(&swparams);
|
2017-01-03 18:00:31 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
status = snd_pcm_sw_params_current(pcm_handle, swparams);
|
2017-03-18 23:36:26 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-03 18:00:31 +00:00
|
|
|
status = snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, period_size);
|
2017-03-18 23:36:26 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
status = snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 1);
|
2017-03-18 23:36:26 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
status = snd_pcm_sw_params(pcm_handle, swparams);
|
2017-03-18 23:36:26 +00:00
|
|
|
CHECK_FAIL(status < 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
samples_in = memnew_arr(int32_t, period_size * channels);
|
|
|
|
samples_out = memnew_arr(int16_t, period_size * channels);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
snd_pcm_nonblock(pcm_handle, 0);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
mutex = Mutex::create();
|
2014-02-10 01:10:30 +00:00
|
|
|
thread = Thread::create(AudioDriverALSA::thread_func, this);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioDriverALSA::thread_func(void *p_udata) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
AudioDriverALSA *ad = (AudioDriverALSA *)p_udata;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
while (!ad->exit_thread) {
|
|
|
|
if (!ad->active) {
|
2017-03-18 23:36:26 +00:00
|
|
|
for (unsigned int i = 0; i < ad->period_size * ad->channels; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
ad->samples_out[i] = 0;
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
ad->lock();
|
|
|
|
|
2017-01-03 18:00:31 +00:00
|
|
|
ad->audio_server_process(ad->period_size, ad->samples_in);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ad->unlock();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (unsigned int i = 0; i < ad->period_size * ad->channels; i++) {
|
|
|
|
ad->samples_out[i] = ad->samples_in[i] >> 16;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-03 18:00:31 +00:00
|
|
|
int todo = ad->period_size;
|
2014-02-10 01:10:30 +00:00
|
|
|
int total = 0;
|
|
|
|
|
|
|
|
while (todo) {
|
|
|
|
if (ad->exit_thread)
|
|
|
|
break;
|
2017-03-18 23:36:26 +00:00
|
|
|
uint8_t *src = (uint8_t *)ad->samples_out;
|
|
|
|
int wrote = snd_pcm_writei(ad->pcm_handle, (void *)(src + (total * ad->channels)), todo);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (wrote < 0) {
|
|
|
|
if (ad->exit_thread)
|
|
|
|
break;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (wrote == -EAGAIN) {
|
2017-01-03 18:00:31 +00:00
|
|
|
//can't write yet (though this is blocking..)
|
2017-03-18 23:36:26 +00:00
|
|
|
usleep(1000);
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
wrote = snd_pcm_recover(ad->pcm_handle, wrote, 0);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (wrote < 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
//absolute fail
|
|
|
|
fprintf(stderr, "ALSA failed and can't recover: %s\n", snd_strerror(wrote));
|
2017-03-18 23:36:26 +00:00
|
|
|
ad->active = false;
|
|
|
|
ad->exit_thread = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
};
|
2017-01-03 18:00:31 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
total += wrote;
|
|
|
|
todo -= wrote;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ad->thread_exited = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void AudioDriverALSA::start() {
|
|
|
|
|
|
|
|
active = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
int AudioDriverALSA::get_mix_rate() const {
|
|
|
|
|
|
|
|
return mix_rate;
|
|
|
|
};
|
|
|
|
|
|
|
|
AudioDriverSW::OutputFormat AudioDriverALSA::get_output_format() const {
|
|
|
|
|
|
|
|
return output_format;
|
|
|
|
};
|
|
|
|
void AudioDriverALSA::lock() {
|
|
|
|
|
|
|
|
if (!thread || !mutex)
|
|
|
|
return;
|
|
|
|
mutex->lock();
|
|
|
|
};
|
|
|
|
void AudioDriverALSA::unlock() {
|
|
|
|
|
|
|
|
if (!thread || !mutex)
|
|
|
|
return;
|
|
|
|
mutex->unlock();
|
|
|
|
};
|
|
|
|
|
|
|
|
void AudioDriverALSA::finish() {
|
|
|
|
|
|
|
|
if (!thread)
|
|
|
|
return;
|
|
|
|
|
|
|
|
exit_thread = true;
|
|
|
|
Thread::wait_to_finish(thread);
|
|
|
|
|
|
|
|
if (pcm_open)
|
|
|
|
snd_pcm_close(pcm_handle);
|
|
|
|
|
|
|
|
if (samples_in) {
|
|
|
|
memdelete_arr(samples_in);
|
|
|
|
memdelete_arr(samples_out);
|
|
|
|
};
|
|
|
|
|
|
|
|
memdelete(thread);
|
|
|
|
if (mutex)
|
|
|
|
memdelete(mutex);
|
|
|
|
thread = NULL;
|
|
|
|
};
|
|
|
|
|
|
|
|
AudioDriverALSA::AudioDriverALSA() {
|
|
|
|
|
|
|
|
mutex = NULL;
|
2017-03-18 23:36:26 +00:00
|
|
|
thread = NULL;
|
|
|
|
pcm_handle = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
AudioDriverALSA::~AudioDriverALSA(){
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|