Start creating state machien for audio mixer streams

This commit is contained in:
twinaphex 2017-05-18 06:16:16 +02:00
parent 312a2b5c80
commit 5343648dc9
4 changed files with 154 additions and 23 deletions

View File

@ -23,7 +23,6 @@
#include <audio/conversion/s16_to_float.h>
#include <audio/audio_resampler.h>
#include <audio/dsp_filter.h>
#include <audio/audio_mixer.h>
#include <file/file_path.h>
#include <lists/dir_list.h>
@ -115,6 +114,20 @@ static const audio_driver_t *audio_drivers[] = {
NULL,
};
struct audio_mixer_stream
{
audio_mixer_sound_t *handle;
audio_mixer_voice_t *voice;
audio_mixer_stop_cb_t stop_cb;
enum audio_mixer_state state;
float volume;
};
#define MAX_STREAMS 8
static unsigned audio_mixer_current_max_idx = 0;
static struct audio_mixer_stream audio_mixer_streams[MAX_STREAMS] = {0};
static size_t audio_driver_chunk_size = 0;
static size_t audio_driver_chunk_nonblock_size = 0;
static size_t audio_driver_chunk_block_size = 0;
@ -878,10 +891,104 @@ bool audio_driver_get_devices_list(void **data)
return true;
}
bool audio_driver_mixer_add_stream(audio_mixer_stream_params_t *params)
{
audio_mixer_voice_t *voice = NULL;
audio_mixer_sound_t *handle = NULL;
audio_mixer_stop_cb_t stop_cb = NULL;
bool looped = false;
if (audio_mixer_current_max_idx >= MAX_STREAMS)
return false;
switch (params->type)
{
case AUDIO_MIXER_TYPE_WAV:
handle = audio_mixer_load_wav(params->buf, params->bufsize);
break;
case AUDIO_MIXER_TYPE_OGG:
handle = audio_mixer_load_ogg(params->buf, params->bufsize);
break;
case AUDIO_MIXER_TYPE_NONE:
return false;
}
if (!handle)
return false;
switch (params->state)
{
case AUDIO_STREAM_STATE_NONE:
return false;
case AUDIO_STREAM_STATE_STOPPED:
break;
case AUDIO_STREAM_STATE_PLAYING:
voice = audio_mixer_play(handle, looped, params->volume, stop_cb);
audio_set_bool(AUDIO_ACTION_MIXER, true);
break;
case AUDIO_STREAM_STATE_PLAYING_LOOPED:
looped = true;
voice = audio_mixer_play(handle, looped, params->volume, stop_cb);
audio_set_bool(AUDIO_ACTION_MIXER, true);
break;
}
audio_mixer_streams[audio_mixer_current_max_idx].handle = handle;
audio_mixer_streams[audio_mixer_current_max_idx].voice = voice;
audio_mixer_streams[audio_mixer_current_max_idx].state = params->state;
audio_mixer_streams[audio_mixer_current_max_idx].volume = params->volume;
audio_mixer_streams[audio_mixer_current_max_idx].stop_cb = stop_cb;
audio_mixer_current_max_idx++;
return true;
}
static void audio_driver_mixer_deinit(void)
{
unsigned i;
audio_set_bool(AUDIO_ACTION_MIXER, false);
for (i = 0; i < MAX_STREAMS; i++)
{
bool deassign_slot = false;
audio_mixer_sound_t *handle = audio_mixer_streams[i].handle;
audio_mixer_voice_t *voice = audio_mixer_streams[i].voice;
switch (audio_mixer_streams[i].state)
{
case AUDIO_STREAM_STATE_PLAYING:
case AUDIO_STREAM_STATE_PLAYING_LOOPED:
audio_mixer_stop(voice);
#if 0
/* TODO - crashes at this part */
if (handle)
audio_mixer_destroy(handle);
#endif
break;
case AUDIO_STREAM_STATE_STOPPED:
if (handle)
audio_mixer_destroy(handle);
break;
case AUDIO_STREAM_STATE_NONE:
break;
}
audio_mixer_streams[i].state = AUDIO_STREAM_STATE_NONE;
audio_mixer_streams[i].volume = 0.0f;
audio_mixer_streams[i].stop_cb = NULL;
audio_mixer_streams[i].handle = NULL;
audio_mixer_streams[i].voice = NULL;
}
audio_mixer_current_max_idx = 0;
audio_mixer_done();
}
bool audio_driver_deinit(void)
{
audio_mixer_done();
audio_driver_mixer_deinit();
audio_driver_free_devices_list();
if (!audio_driver_deinit_internal())
return false;

View File

@ -22,6 +22,7 @@
#include <sys/types.h>
#include <boolean.h>
#include <audio/audio_mixer.h>
#include <retro_common_api.h>
RETRO_BEGIN_DECLS
@ -123,6 +124,24 @@ typedef struct audio_driver
size_t (*buffer_size)(void *data);
} audio_driver_t;
enum audio_mixer_state
{
AUDIO_STREAM_STATE_NONE = 0,
AUDIO_STREAM_STATE_STOPPED,
AUDIO_STREAM_STATE_PLAYING,
AUDIO_STREAM_STATE_PLAYING_LOOPED
};
typedef struct audio_mixer_stream_params
{
float volume;
enum audio_mixer_type type;
enum audio_mixer_state state;
void *buf;
size_t bufsize;
audio_mixer_stop_cb_t cb;
} audio_mixer_stream_params_t;
void audio_driver_destroy_data(void);
void audio_driver_set_own_driver(void);
@ -231,6 +250,8 @@ bool audio_driver_deinit(void);
bool audio_driver_init(void);
bool audio_driver_mixer_add_stream(audio_mixer_stream_params_t *params);
extern audio_driver_t audio_rsound;
extern audio_driver_t audio_oss;
extern audio_driver_t audio_alsa;

View File

@ -302,17 +302,22 @@ audio_mixer_sound_t* audio_mixer_load_ogg(void *buffer, int32_t size)
void audio_mixer_destroy(audio_mixer_sound_t* sound)
{
void *handle = NULL;
if (!sound)
return;
switch (sound->type)
{
case AUDIO_MIXER_TYPE_WAV:
memalign_free((void*)sound->types.wav.pcm);
handle = (void*)sound->types.wav.pcm;
if (handle)
memalign_free(handle);
break;
case AUDIO_MIXER_TYPE_OGG:
#ifdef HAVE_STB_VORBIS
memalign_free((void*)sound->types.ogg.data);
handle = (void*)sound->types.ogg.data;
if (handle)
memalign_free(handle);
#endif
break;
case AUDIO_MIXER_TYPE_NONE:

View File

@ -41,15 +41,6 @@ struct audio_mixer_handle
char path[4095];
};
static void audio_mixer_stopped(audio_mixer_sound_t *sound, unsigned reason)
{
if (reason != AUDIO_MIXER_SOUND_REPEATED)
{
audio_mixer_destroy(sound);
audio_set_bool(AUDIO_ACTION_MIXER, false);
}
}
static void task_audio_mixer_load_free(retro_task_t *task)
{
nbio_handle_t *nbio = (nbio_handle_t*)task->state;
@ -89,17 +80,20 @@ static int cb_nbio_audio_mixer_load(void *data, size_t len)
static void task_audio_mixer_handle_upload_ogg(void *task_data,
void *user_data, const char *err)
{
audio_mixer_stream_params_t params;
nbio_buf_t *img = (nbio_buf_t*)task_data;
if (!img)
return;
{
audio_mixer_sound_t *handle = audio_mixer_load_ogg(img->buf, img->bufsize);
audio_mixer_play(handle, true, 1.0f, audio_mixer_stopped);
params.volume = 1.0f;
params.type = AUDIO_MIXER_TYPE_OGG;
params.state = AUDIO_STREAM_STATE_PLAYING_LOOPED;
params.buf = img->buf;
params.bufsize = img->bufsize;
params.cb = NULL;
audio_set_bool(AUDIO_ACTION_MIXER, true);
}
audio_driver_mixer_add_stream(&params);
free(img);
free(user_data);
@ -108,16 +102,20 @@ static void task_audio_mixer_handle_upload_ogg(void *task_data,
static void task_audio_mixer_handle_upload_wav(void *task_data,
void *user_data, const char *err)
{
audio_mixer_stream_params_t params;
nbio_buf_t *img = (nbio_buf_t*)task_data;
if (!img)
return;
{
audio_mixer_sound_t *handle = audio_mixer_load_wav(img->buf, img->bufsize);
audio_mixer_play(handle, true, 1.0f, audio_mixer_stopped);
audio_set_bool(AUDIO_ACTION_MIXER, true);
}
params.volume = 1.0f;
params.type = AUDIO_MIXER_TYPE_WAV;
params.state = AUDIO_STREAM_STATE_PLAYING_LOOPED;
params.buf = img->buf;
params.bufsize = img->bufsize;
params.cb = NULL;
audio_driver_mixer_add_stream(&params);
free(img);
free(user_data);