mirror of
https://github.com/libretro/RetroArch
synced 2025-02-06 00:39:53 +00:00
Try to write initial implementation of audio mixer task
This commit is contained in:
parent
2ee3f12c9d
commit
dd4146e302
@ -242,7 +242,7 @@ void audio_mixer_done(void)
|
||||
s_voices[i].type = AUDIO_MIXER_TYPE_NONE;
|
||||
}
|
||||
|
||||
audio_mixer_sound_t* audio_mixer_load_wav(const char* path, void *buffer, int32_t size)
|
||||
audio_mixer_sound_t* audio_mixer_load_wav(void *buffer, int32_t size)
|
||||
{
|
||||
/* WAV data */
|
||||
rwav_t wav;
|
||||
@ -289,7 +289,7 @@ audio_mixer_sound_t* audio_mixer_load_wav(const char* path, void *buffer, int32_
|
||||
return sound;
|
||||
}
|
||||
|
||||
audio_mixer_sound_t* audio_mixer_load_ogg(const char* path, void *buffer, int32_t size)
|
||||
audio_mixer_sound_t* audio_mixer_load_ogg(void *buffer, int32_t size)
|
||||
{
|
||||
#ifdef HAVE_STB_VORBIS
|
||||
audio_mixer_sound_t* sound = (audio_mixer_sound_t*)calloc(1, sizeof(*sound));
|
||||
|
@ -46,8 +46,8 @@ void audio_mixer_init(unsigned rate);
|
||||
|
||||
void audio_mixer_done(void);
|
||||
|
||||
audio_mixer_sound_t* audio_mixer_load_wav(const char* path, void *buffer, int32_t size);
|
||||
audio_mixer_sound_t* audio_mixer_load_ogg(const char* path, void *buffer, int32_t size);
|
||||
audio_mixer_sound_t* audio_mixer_load_wav(void *buffer, int32_t size);
|
||||
audio_mixer_sound_t* audio_mixer_load_ogg(void *buffer, int32_t size);
|
||||
|
||||
void audio_mixer_destroy(audio_mixer_sound_t* sound);
|
||||
|
||||
|
@ -14,3 +14,176 @@
|
||||
*/
|
||||
|
||||
#include "tasks_internal.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <file/nbio.h>
|
||||
#include <audio/audio_mixer.h>
|
||||
#include <compat/strl.h>
|
||||
#include <retro_miscellaneous.h>
|
||||
|
||||
#include "../file_path_special.h"
|
||||
#include "../verbosity.h"
|
||||
|
||||
#include "tasks_internal.h"
|
||||
|
||||
struct nbio_audio_mixer_handle
|
||||
{
|
||||
enum nbio_type type;
|
||||
bool is_finished;
|
||||
void *handle;
|
||||
};
|
||||
|
||||
static void task_audio_mixer_cleanup(nbio_handle_t *nbio)
|
||||
{
|
||||
struct nbio_audio_mixer_handle *image = (struct nbio_audio_mixer_handle*)nbio->data;
|
||||
|
||||
if (nbio->data)
|
||||
free(nbio->data);
|
||||
nbio_free(nbio->handle);
|
||||
nbio->data = NULL;
|
||||
nbio->handle = NULL;
|
||||
}
|
||||
|
||||
static void task_audio_mixer_load_free(retro_task_t *task)
|
||||
{
|
||||
nbio_handle_t *nbio = task ? (nbio_handle_t*)task->state : NULL;
|
||||
|
||||
if (nbio)
|
||||
{
|
||||
task_audio_mixer_cleanup(nbio);
|
||||
free(nbio);
|
||||
}
|
||||
}
|
||||
|
||||
static int cb_nbio_audio_wav_loaded(void *data, size_t len)
|
||||
{
|
||||
void *ptr = NULL;
|
||||
nbio_handle_t *nbio = (nbio_handle_t*)data;
|
||||
struct nbio_audio_mixer_handle *image = nbio ?
|
||||
(struct nbio_audio_mixer_handle*)nbio->data : NULL;
|
||||
audio_mixer_sound_t *audmix = NULL;
|
||||
|
||||
ptr = nbio_get_ptr(nbio->handle, &len);
|
||||
audmix = audio_mixer_load_wav(ptr, len);
|
||||
|
||||
if (!audmix ||
|
||||
!audio_mixer_play(audmix, true, 0.0f, NULL)
|
||||
)
|
||||
{
|
||||
task_audio_mixer_cleanup(nbio);
|
||||
return -1;
|
||||
}
|
||||
|
||||
image->is_finished = true;
|
||||
nbio->is_finished = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cb_nbio_audio_ogg_loaded(void *data, size_t len)
|
||||
{
|
||||
void *ptr = NULL;
|
||||
nbio_handle_t *nbio = (nbio_handle_t*)data;
|
||||
struct nbio_audio_mixer_handle
|
||||
*image = nbio ?
|
||||
(struct nbio_audio_mixer_handle*)nbio->data : NULL;
|
||||
audio_mixer_sound_t *audmix = NULL;
|
||||
|
||||
ptr = nbio_get_ptr(nbio->handle, &len);
|
||||
audmix = audio_mixer_load_ogg(ptr, len);
|
||||
|
||||
if (!audmix ||
|
||||
!audio_mixer_play(audmix, true, 0.0f, NULL)
|
||||
)
|
||||
{
|
||||
task_audio_mixer_cleanup(nbio);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
image->is_finished = true;
|
||||
nbio->is_finished = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool task_audio_mixer_load_handler(retro_task_t *task)
|
||||
{
|
||||
unsigned i;
|
||||
nbio_handle_t *nbio = (nbio_handle_t*)task->state;
|
||||
struct nbio_audio_mixer_handle *image = (struct nbio_audio_mixer_handle*)nbio->data;
|
||||
|
||||
if ( nbio->is_finished
|
||||
&& (image && image->is_finished )
|
||||
&& (!task_get_cancelled(task)))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool task_push_audio_mixer_load(const char *fullpath, retro_task_callback_t cb, void *user_data)
|
||||
{
|
||||
nbio_handle_t *nbio = NULL;
|
||||
struct nbio_audio_mixer_handle *image = NULL;
|
||||
retro_task_t *t = (retro_task_t*)calloc(1, sizeof(*t));
|
||||
|
||||
if (!t)
|
||||
goto error_msg;
|
||||
|
||||
nbio = (nbio_handle_t*)calloc(1, sizeof(*nbio));
|
||||
|
||||
if (!nbio)
|
||||
goto error;
|
||||
|
||||
strlcpy(nbio->path, fullpath, sizeof(nbio->path));
|
||||
|
||||
image = (struct nbio_audio_mixer_handle*)calloc(1, sizeof(*image));
|
||||
if (!image)
|
||||
goto error;
|
||||
|
||||
nbio->type = NBIO_TYPE_NONE;
|
||||
image->type = NBIO_TYPE_NONE;
|
||||
|
||||
if (strstr(fullpath, file_path_str(FILE_PATH_WAV_EXTENSION)))
|
||||
{
|
||||
nbio->type = NBIO_TYPE_WAV;
|
||||
image->type = NBIO_TYPE_WAV;
|
||||
nbio->cb = &cb_nbio_audio_wav_loaded;
|
||||
}
|
||||
else if (strstr(fullpath, file_path_str(FILE_PATH_OGG_EXTENSION)))
|
||||
{
|
||||
nbio->type = NBIO_TYPE_OGG;
|
||||
image->type = NBIO_TYPE_OGG;
|
||||
nbio->cb = &cb_nbio_audio_ogg_loaded;
|
||||
}
|
||||
|
||||
nbio->data = (struct nbio_audio_mixer_handle*)image;
|
||||
nbio->is_finished = false;
|
||||
nbio->status = NBIO_STATUS_INIT;
|
||||
|
||||
t->state = nbio;
|
||||
t->handler = task_file_load_handler;
|
||||
t->cleanup = task_audio_mixer_load_free;
|
||||
t->callback = cb;
|
||||
t->user_data = user_data;
|
||||
|
||||
task_queue_push(t);
|
||||
|
||||
return true;
|
||||
|
||||
error:
|
||||
task_audio_mixer_load_free(t);
|
||||
free(t);
|
||||
if (nbio)
|
||||
free(nbio);
|
||||
|
||||
error_msg:
|
||||
RARCH_ERR("[audio mixer load] Failed to open '%s': %s.\n",
|
||||
fullpath, strerror(errno));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -111,6 +111,11 @@ void task_file_load_handler(retro_task_t *task)
|
||||
if (!task_image_load_handler(task))
|
||||
task_set_finished(task, true);
|
||||
break;
|
||||
case NBIO_TYPE_OGG:
|
||||
case NBIO_TYPE_WAV:
|
||||
if (!task_audio_mixer_load_handler(task))
|
||||
task_set_finished(task, true);
|
||||
break;
|
||||
case NBIO_TYPE_NONE:
|
||||
default:
|
||||
if (nbio->is_finished)
|
||||
|
Loading…
x
Reference in New Issue
Block a user