RetroArch/tasks/task_audio_mixer.c

145 lines
3.8 KiB
C
Raw Normal View History

2017-05-14 03:24:59 +02:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* 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/>.
*/
#include "tasks_internal.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
2017-05-17 14:04:33 +02:00
#include <file/nbio.h>
#include <audio/audio_mixer.h>
2017-05-15 11:07:12 +02:00
#include <streams/file_stream.h>
#include <compat/strl.h>
#include <retro_miscellaneous.h>
2017-05-15 11:07:12 +02:00
#include "../audio/audio_driver.h"
#include "../file_path_special.h"
#include "../verbosity.h"
#include "tasks_internal.h"
2017-05-15 11:25:45 +02:00
struct audio_mixer_handle
{
2017-05-15 11:07:12 +02:00
enum audio_mixer_type type;
char path[4095];
};
2017-05-15 11:07:12 +02:00
static void audio_mixer_stopped(audio_mixer_sound_t *sound, unsigned reason)
{
2017-05-16 00:02:26 +02:00
if (reason != AUDIO_MIXER_SOUND_REPEATED)
{
audio_mixer_destroy(sound);
audio_set_bool(AUDIO_ACTION_MIXER, false);
}
}
2017-05-17 14:04:33 +02:00
static void task_audio_mixer_load_free(retro_task_t *task)
{
2017-05-17 14:04:33 +02:00
nbio_handle_t *nbio = task ? (nbio_handle_t*)task->state : NULL;
2017-05-17 14:04:33 +02:00
if (nbio)
{
2017-05-17 14:04:33 +02:00
free(nbio);
}
2017-05-17 14:04:33 +02:00
}
2017-05-17 14:04:33 +02:00
static int cb_nbio_audio_mixer_load(void *data, size_t len)
{
void *ptr = NULL;
audio_mixer_sound_t *handle = NULL;
nbio_handle_t *nbio = (nbio_handle_t*)data;
struct audio_mixer_handle *image = nbio ?
(struct audio_mixer_handle*)nbio->data : NULL;
2017-05-17 14:04:33 +02:00
ptr = nbio_get_ptr(nbio->handle, &len);
image->handle = audio_mixer_load_ogg(ptr, len);
2017-05-17 14:04:33 +02:00
audio_mixer_play(handle, true, 1.0f, audio_mixer_stopped);
nbio->is_finished = true;
2017-05-17 14:04:33 +02:00
return 0;
}
void task_audio_mixer_handle_upload(void *task_data,
void *user_data, const char *err)
{
audio_set_bool(AUDIO_ACTION_MIXER, true);
}
bool task_push_audio_mixer_load(const char *fullpath, retro_task_callback_t cb, void *user_data)
{
2017-05-17 14:04:33 +02:00
nbio_handle_t *nbio = NULL;
2017-05-15 11:25:45 +02:00
struct audio_mixer_handle *image = NULL;
2017-05-17 14:04:33 +02:00
retro_task_t *t = (retro_task_t*)calloc(1, sizeof(*t));
if (!t)
goto error_msg;
2017-05-17 14:04:33 +02:00
nbio = (nbio_handle_t*)calloc(1, sizeof(*nbio));
if (!nbio)
goto error;
strlcpy(nbio->path, fullpath, sizeof(nbio->path));
2017-05-15 11:25:45 +02:00
image = (struct audio_mixer_handle*)calloc(1, sizeof(*image));
if (!image)
goto error;
2017-05-15 11:07:12 +02:00
strlcpy(image->path, fullpath, sizeof(image->path));
2017-05-17 14:04:33 +02:00
nbio->type = NBIO_TYPE_NONE;
2017-05-15 11:07:12 +02:00
image->type = AUDIO_MIXER_TYPE_NONE;
if (strstr(fullpath, file_path_str(FILE_PATH_WAV_EXTENSION)))
{
2017-05-15 11:07:12 +02:00
image->type = AUDIO_MIXER_TYPE_WAV;
}
else if (strstr(fullpath, file_path_str(FILE_PATH_OGG_EXTENSION)))
{
2017-05-15 11:07:12 +02:00
image->type = AUDIO_MIXER_TYPE_OGG;
}
2017-05-17 14:04:33 +02:00
nbio->data = (struct audio_mixer_handle*)image;
nbio->is_finished = false;
nbio->cb = &cb_nbio_audio_mixer_load;
nbio->status = NBIO_STATUS_INIT;
t->state = nbio;
t->handler = task_file_load_handler;
t->cleanup = task_audio_mixer_load_free;
t->callback = task_audio_mixer_handle_upload;
t->user_data = user_data;
task_queue_push(t);
return true;
error:
free(t);
error_msg:
RARCH_ERR("[audio mixer load] Failed to open '%s': %s.\n",
fullpath, strerror(errno));
return false;
}