TinyALSA support

This commit is contained in:
Charlton Head 2017-06-23 20:44:00 -05:00
parent e7d623f971
commit 4ad5f67663
11 changed files with 2040 additions and 0 deletions

View File

@ -383,6 +383,12 @@ endif
DEFINES += $(ALSA_CFLAGS)
endif
ifeq ($(HAVE_TINYALSA), 1)
OBJ += audio/drivers/tinyalsa.o\
deps/tinyalsa/pcm.o
DEFINES += -DHAVE_TINYALSA
endif
ifeq ($(HAVE_ROAR), 1)
OBJ += audio/drivers/roar.o
LIBS += $(ROAR_LIBS)

View File

@ -55,6 +55,9 @@ static const audio_driver_t *audio_drivers[] = {
&audio_alsathread,
#endif
#endif
#ifdef HAVE_TINYALSA
&audio_tinyalsa,
#endif
#if defined(HAVE_OSS) || defined(HAVE_OSS_BSD)
&audio_oss,
#endif

View File

@ -259,6 +259,7 @@ extern audio_driver_t audio_rsound;
extern audio_driver_t audio_oss;
extern audio_driver_t audio_alsa;
extern audio_driver_t audio_alsathread;
extern audio_driver_t audio_tinyalsa;
extern audio_driver_t audio_roar;
extern audio_driver_t audio_openal;
extern audio_driver_t audio_opensl;

212
audio/drivers/tinyalsa.c Normal file
View File

@ -0,0 +1,212 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
* Copyright (C) 2017 - Charlton Head
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
/* See https://github.com/tinyalsa/tinyalsa */
#include <../../deps/tinyalsa/pcm.h>
#include <string.h>
#include "../audio_driver.h"
typedef struct tinyalsa {
struct pcm *pcm;
size_t buffer_size;
bool nonblock;
bool has_float;
bool can_pause;
bool is_paused;
unsigned int frame_bits;
} tinyalsa_t;
typedef long pcm_sframes_t;
#define BYTES_TO_FRAMES(bytes, frame_bits) ((bytes) * 8 / frame_bits)
static void *
tinyalsa_init(const char *device, unsigned rate,
unsigned latency, unsigned block_frames,
unsigned *new_rate)
{
pcm_sframes_t buffer_size;
struct pcm_config config;
tinyalsa_t *tinyalsa = (tinyalsa_t*)calloc(1, sizeof(tinyalsa_t));
if (!tinyalsa)
return NULL;
config.rate = rate;
config.format = PCM_FORMAT_S16_LE;
config.channels = 2;
config.period_size = 768;
config.period_count = 4;
config.start_threshold = 2048;
config.silence_threshold = 1024 * 2;
config.stop_threshold = 1024 * 2;
tinyalsa->pcm = pcm_open(0, 0, PCM_OUT, &config);
if (tinyalsa->pcm == NULL) {
RARCH_ERR("[TINYALSA]: Failed to allocate memory for pcm.\n");
goto error;
} else if (!pcm_is_ready(tinyalsa->pcm)) {
RARCH_ERR("[TINYALSA]: Cannot open audio device.\n");
goto error;
}
buffer_size = pcm_get_buffer_size(tinyalsa->pcm);
tinyalsa->buffer_size = pcm_frames_to_bytes(tinyalsa->pcm, buffer_size);
tinyalsa->frame_bits = pcm_format_to_bits(config.format) * 2;
tinyalsa->can_pause = true;
tinyalsa->has_float = false;
RARCH_LOG("[TINYALSA] %u \n", (unsigned int)tinyalsa->buffer_size);
return tinyalsa;
error:
RARCH_ERR("[TINYALSA]: Failed to initialize tinyalsa driver.\n");
return NULL;
}
static ssize_t
tinyalsa_write(void *data, const void *buf_, size_t size_)
{
tinyalsa_t *tinyalsa = (tinyalsa_t*)data;
const uint8_t *buf = (const uint8_t*)buf_;
pcm_sframes_t written = 0, frames = 0;
pcm_sframes_t size = BYTES_TO_FRAMES(size_, tinyalsa->frame_bits);
size_t frames_size = tinyalsa->has_float ? sizeof(float) : sizeof(int16_t);
while (size) {
frames = pcm_writei(tinyalsa->pcm, buf, size);
written += frames;
buf += (frames << 1) * frames_size;
size -= frames;
}
return written;
}
static bool
tinyalsa_stop(void *data)
{
tinyalsa_t *tinyalsa = (tinyalsa_t*)data;
if (tinyalsa->can_pause && !tinyalsa->is_paused) {
int ret = pcm_start(tinyalsa->pcm);
if (ret < 0)
return false;
tinyalsa->is_paused = true;
}
return true;
}
static bool
tinyalsa_alive(void *data)
{
tinyalsa_t *tinyalsa = (tinyalsa_t*)data;
if (tinyalsa)
return !tinyalsa->is_paused;
return false;
}
static bool
tinyalsa_start(void *data, bool is_shutdown)
{
tinyalsa_t *tinyalsa = (tinyalsa_t*)data;
if (tinyalsa->can_pause && tinyalsa->is_paused) {
int ret = pcm_stop(tinyalsa->pcm);
if (ret < 0) {
RARCH_ERR("[TINYALSA]: Failed to unpause.\n");
return false;
}
tinyalsa->is_paused = false;
}
return true;
}
static void
tinyalsa_set_nonblock_state(void *data, bool state)
{
tinyalsa_t *tinyalsa = (tinyalsa_t*)data;
tinyalsa->nonblock = state;
}
static bool
tinyalsa_use_float(void *data)
{
tinyalsa_t *tinyalsa = (tinyalsa_t*)data;
return tinyalsa->has_float;
}
static void
tinyalsa_free(void *data)
{
tinyalsa_t *tinyalsa = (tinyalsa_t*)data;
if (tinyalsa) {
if (tinyalsa->pcm) {
pcm_close(tinyalsa->pcm);
tinyalsa->pcm = NULL;
}
free(tinyalsa);
}
}
static size_t
tinyalsa_write_avail(void *data)
{
/*TODO*/
return 0;
}
static size_t
tinyalsa_buffer_size(void *data)
{
tinyalsa_t *tinyalsa = (tinyalsa_t*)data;
return tinyalsa->buffer_size;
}
audio_driver_t audio_tinyalsa = {
tinyalsa_init, /* AUDIO_init */
tinyalsa_write, /* AUDIO_write */
tinyalsa_stop, /* AUDIO_stop */
tinyalsa_start, /* AUDIO_start */
tinyalsa_alive, /* AUDIO_alive */
tinyalsa_set_nonblock_state, /* AUDIO_set_nonblock_sate */
tinyalsa_free, /* AUDIO_free */
tinyalsa_use_float, /* AUDIO_use_float */
"tinyalsa", /* "AUDIO" */
NULL, /* AUDIO_device_list_new */ /*TODO*/
NULL, /* AUDIO_device_list_free */ /*TODO*/
/* tinyalsa_write_avail, AUDIO_write_avail */ /*TODO*/
NULL, /* AUDIO_buffer_size */ /*TODO*/
};

View File

@ -134,6 +134,12 @@ static const bool _alsa_supp = true;
static const bool _alsa_supp = false;
#endif
#ifdef HAVE_TINYALSA
static const bool _tinyalsa_supp = true;
#else
static const bool _tinyalsa_supp = false;
#endif
#ifdef HAVE_COREAUDIO
static const bool _coreaudio_supp = true;
#else

View File

@ -139,6 +139,7 @@ enum audio_driver_enum
AUDIO_OSS,
AUDIO_ALSA,
AUDIO_ALSATHREAD,
AUDIO_TINYALSA,
AUDIO_ROAR,
AUDIO_AL,
AUDIO_SL,
@ -312,6 +313,8 @@ static enum audio_driver_enum AUDIO_DEFAULT_DRIVER = AUDIO_PULSE;
static enum audio_driver_enum AUDIO_DEFAULT_DRIVER = AUDIO_ALSATHREAD;
#elif defined(HAVE_ALSA)
static enum audio_driver_enum AUDIO_DEFAULT_DRIVER = AUDIO_ALSA;
#elif defined(HAVE_TINYALSA)
static enum audio_driver_enum AUDIO_DEFAULT_DRIVER = AUDIO_TINYALSA;
#elif defined(HAVE_OSS)
static enum audio_driver_enum AUDIO_DEFAULT_DRIVER = AUDIO_OSS;
#elif defined(HAVE_JACK)
@ -546,6 +549,8 @@ const char *config_get_default_audio(void)
return "alsa";
case AUDIO_ALSATHREAD:
return "alsathread";
case AUDIO_TINYALSA:
return "tinyalsa";
case AUDIO_ROAR:
return "roar";
case AUDIO_COREAUDIO:

1480
deps/tinyalsa/pcm.c vendored Normal file

File diff suppressed because it is too large Load Diff

318
deps/tinyalsa/pcm.h vendored Normal file
View File

@ -0,0 +1,318 @@
/* pcm.h
**
** Copyright 2011, The Android Open Source Project
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** * Neither the name of The Android Open Source Project nor the names of
** its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
** DAMAGE.
*/
/** @file */
/** @defgroup libtinyalsa-pcm PCM Interface
* @brief All macros, structures and functions that make up the PCM interface.
*/
#ifndef TINYALSA_PCM_H
#define TINYALSA_PCM_H
#include <sys/time.h>
#include <stddef.h>
#if defined(__cplusplus)
extern "C" {
#endif
/** A flag that specifies that the PCM is an output.
* May not be bitwise AND'd with @ref PCM_IN.
* Used in @ref pcm_open.
* @ingroup libtinyalsa-pcm
*/
#define PCM_OUT 0x00000000
/** Specifies that the PCM is an input.
* May not be bitwise AND'd with @ref PCM_OUT.
* Used in @ref pcm_open.
* @ingroup libtinyalsa-pcm
*/
#define PCM_IN 0x10000000
/** Specifies that the PCM will use mmap read and write methods.
* Used in @ref pcm_open.
* @ingroup libtinyalsa-pcm
*/
#define PCM_MMAP 0x00000001
/** Specifies no interrupt requests.
* May only be bitwise AND'd with @ref PCM_MMAP.
* Used in @ref pcm_open.
* @ingroup libtinyalsa-pcm
*/
#define PCM_NOIRQ 0x00000002
/** When set, calls to @ref pcm_write
* for a playback stream will not attempt
* to restart the stream in the case of an
* underflow, but will return -EPIPE instead.
* After the first -EPIPE error, the stream
* is considered to be stopped, and a second
* call to pcm_write will attempt to restart
* the stream.
* Used in @ref pcm_open.
* @ingroup libtinyalsa-pcm
*/
#define PCM_NORESTART 0x00000004
/** Specifies monotonic timestamps.
* Used in @ref pcm_open.
* @ingroup libtinyalsa-pcm
*/
#define PCM_MONOTONIC 0x00000008
/** For inputs, this means the PCM is recording audio samples.
* For outputs, this means the PCM is playing audio samples.
* @ingroup libtinyalsa-pcm
*/
#define PCM_STATE_RUNNING 0x03
/** For inputs, this means an overrun occured.
* For outputs, this means an underrun occured.
*/
#define PCM_STATE_XRUN 0x04
/** For outputs, this means audio samples are played.
* A PCM is in a draining state when it is coming to a stop.
*/
#define PCM_STATE_DRAINING 0x05
/** Means a PCM is suspended.
* @ingroup libtinyalsa-pcm
*/
#define PCM_STATE_SUSPENDED 0x07
/** Means a PCM has been disconnected.
* @ingroup libtinyalsa-pcm
*/
#define PCM_STATE_DISCONNECTED 0x08
/** Audio sample format of a PCM.
* The first letter specifiers whether the sample is signed or unsigned.
* The letter 'S' means signed. The letter 'U' means unsigned.
* The following number is the amount of bits that the sample occupies in memory.
* Following the underscore, specifiers whether the sample is big endian or little endian.
* The letters 'LE' mean little endian.
* The letters 'BE' mean big endian.
* This enumeration is used in the @ref pcm_config structure.
* @ingroup libtinyalsa-pcm
*/
enum pcm_format {
/** Signed, 8-bit */
PCM_FORMAT_S8 = 1,
/** Signed 16-bit, little endian */
PCM_FORMAT_S16_LE = 0,
/** Signed, 16-bit, big endian */
PCM_FORMAT_S16_BE = 2,
/** Signed, 24-bit (32-bit in memory), little endian */
PCM_FORMAT_S24_LE,
/** Signed, 24-bit (32-bit in memory), big endian */
PCM_FORMAT_S24_BE,
/** Signed, 24-bit, little endian */
PCM_FORMAT_S24_3LE,
/** Signed, 24-bit, big endian */
PCM_FORMAT_S24_3BE,
/** Signed, 32-bit, little endian */
PCM_FORMAT_S32_LE,
/** Signed, 32-bit, big endian */
PCM_FORMAT_S32_BE,
/** Max of the enumeration list, not an actual format. */
PCM_FORMAT_MAX
};
/** A bit mask of 256 bits (32 bytes) that describes some hardware parameters of a PCM */
struct pcm_mask {
/** bits of the bit mask */
unsigned int bits[32 / sizeof(unsigned int)];
};
/** Encapsulates the hardware and software parameters of a PCM.
* @ingroup libtinyalsa-pcm
*/
struct pcm_config {
/** The number of channels in a frame */
unsigned int channels;
/** The number of frames per second */
unsigned int rate;
/** The number of frames in a period */
unsigned int period_size;
/** The number of periods in a PCM */
unsigned int period_count;
/** The sample format of a PCM */
enum pcm_format format;
/* Values to use for the ALSA start, stop and silence thresholds. Setting
* any one of these values to 0 will cause the default tinyalsa values to be
* used instead. Tinyalsa defaults are as follows.
*
* start_threshold : period_count * period_size
* stop_threshold : period_count * period_size
* silence_threshold : 0
*/
/** The minimum number of frames required to start the PCM */
unsigned int start_threshold;
/** The minimum number of frames required to stop the PCM */
unsigned int stop_threshold;
/** The minimum number of frames to silence the PCM */
unsigned int silence_threshold;
};
/** Enumeration of a PCM's hardware parameters.
* Each of these parameters is either a mask or an interval.
* @ingroup libtinyalsa-pcm
*/
enum pcm_param
{
/** A mask that represents the type of read or write method available (e.g. interleaved, mmap). */
PCM_PARAM_ACCESS,
/** A mask that represents the @ref pcm_format available (e.g. @ref PCM_FORMAT_S32_LE) */
PCM_PARAM_FORMAT,
/** A mask that represents the subformat available */
PCM_PARAM_SUBFORMAT,
/** An interval representing the range of sample bits available (e.g. 8 to 32) */
PCM_PARAM_SAMPLE_BITS,
/** An interval representing the range of frame bits available (e.g. 8 to 64) */
PCM_PARAM_FRAME_BITS,
/** An interval representing the range of channels available (e.g. 1 to 2) */
PCM_PARAM_CHANNELS,
/** An interval representing the range of rates available (e.g. 44100 to 192000) */
PCM_PARAM_RATE,
PCM_PARAM_PERIOD_TIME,
/** The number of frames in a period */
PCM_PARAM_PERIOD_SIZE,
/** The number of bytes in a period */
PCM_PARAM_PERIOD_BYTES,
/** The number of periods for a PCM */
PCM_PARAM_PERIODS,
PCM_PARAM_BUFFER_TIME,
PCM_PARAM_BUFFER_SIZE,
PCM_PARAM_BUFFER_BYTES,
PCM_PARAM_TICK_TIME,
}; /* enum pcm_param */
struct pcm_params;
struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
unsigned int flags);
void pcm_params_free(struct pcm_params *pcm_params);
const struct pcm_mask *pcm_params_get_mask(const struct pcm_params *pcm_params, enum pcm_param param);
unsigned int pcm_params_get_min(const struct pcm_params *pcm_params, enum pcm_param param);
unsigned int pcm_params_get_max(const struct pcm_params *pcm_params, enum pcm_param param);
struct pcm;
struct pcm *pcm_open(unsigned int card,
unsigned int device,
unsigned int flags,
const struct pcm_config *config);
struct pcm *pcm_open_by_name(const char *name,
unsigned int flags,
const struct pcm_config *config);
int pcm_close(struct pcm *pcm);
int pcm_is_ready(const struct pcm *pcm);
unsigned int pcm_get_channels(const struct pcm *pcm);
const struct pcm_config * pcm_get_config(const struct pcm *pcm);
unsigned int pcm_get_rate(const struct pcm *pcm);
enum pcm_format pcm_get_format(const struct pcm *pcm);
int pcm_get_file_descriptor(const struct pcm *pcm);
const char *pcm_get_error(const struct pcm *pcm);
int pcm_set_config(struct pcm *pcm, const struct pcm_config *config);
unsigned int pcm_format_to_bits(enum pcm_format format);
unsigned int pcm_get_buffer_size(const struct pcm *pcm);
unsigned int pcm_frames_to_bytes(const struct pcm *pcm, unsigned int frames);
unsigned int pcm_bytes_to_frames(const struct pcm *pcm, unsigned int bytes);
int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail, struct timespec *tstamp);
unsigned int pcm_get_subdevice(const struct pcm *pcm);
int pcm_writei(struct pcm *pcm, const void *data, unsigned int frame_count);
int pcm_readi(struct pcm *pcm, void *data, unsigned int frame_count);
#ifdef __GNUC__
int pcm_write(struct pcm *pcm, const void *data, unsigned int count) __attribute((deprecated));
int pcm_read(struct pcm *pcm, void *data, unsigned int count) __attribute((deprecated));
#else
int pcm_write(struct pcm *pcm, const void *data, unsigned int count);
int pcm_read(struct pcm *pcm, void *data, unsigned int count);
#endif
int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count);
int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count);
int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset, unsigned int *frames);
int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames);
int pcm_link(struct pcm *pcm1, struct pcm *pcm2);
int pcm_unlink(struct pcm *pcm);
int pcm_prepare(struct pcm *pcm);
int pcm_start(struct pcm *pcm);
int pcm_stop(struct pcm *pcm);
int pcm_wait(struct pcm *pcm, int timeout);
long pcm_get_delay(struct pcm *pcm);
#if defined(__cplusplus)
} /* extern "C" */
#endif
#endif

View File

@ -650,6 +650,10 @@ AUDIO
#endif
#endif
#ifdef HAVE_TINYALSA
#include "../audio/drivers/tinyalsa.c"
#endif
#ifdef HAVE_AL
#include "../audio/drivers/openal.c"
#endif

View File

@ -227,6 +227,10 @@ check_header OSS sys/soundcard.h
check_header OSS_BSD soundcard.h
check_lib OSS_LIB -lossaudio
if [ "$OS" = 'Linux' ]; then
HAVE_TINYALSA=yes
fi
if [ "$OS" = 'Darwin' ]; then
check_lib AL "-framework OpenAL" alcOpenDevice
HAVE_SDL=no

View File

@ -58,6 +58,7 @@ HAVE_ZLIB=auto # zlib support (ZIP extract, PNG decoding/encoding)
HAVE_FBO=auto # render-to-texture (FBO) support
HAVE_ALSA=auto # ALSA support
C89_ALSA=no
HAVE_TINYALSA=auto # TinyALSA support
HAVE_OSS=auto # OSS support
HAVE_RSOUND=auto # RSound support
HAVE_ROAR=auto # RoarAudio support