2017-05-14 01:24:59 +00: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"
|
2017-05-14 21:12:10 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2017-05-17 12:04:33 +00:00
|
|
|
#include <file/nbio.h>
|
2017-05-14 21:12:10 +00:00
|
|
|
#include <audio/audio_mixer.h>
|
|
|
|
#include <compat/strl.h>
|
2017-09-29 20:16:17 +00:00
|
|
|
#include <string/stdstring.h>
|
2017-05-14 21:12:10 +00:00
|
|
|
#include <retro_miscellaneous.h>
|
|
|
|
|
2017-05-15 09:07:12 +00:00
|
|
|
#include "../audio/audio_driver.h"
|
|
|
|
|
2017-05-14 21:12:10 +00:00
|
|
|
#include "../file_path_special.h"
|
|
|
|
#include "../verbosity.h"
|
|
|
|
|
|
|
|
#include "tasks_internal.h"
|
|
|
|
|
2017-05-15 09:25:45 +00:00
|
|
|
struct audio_mixer_handle
|
2017-05-14 21:12:10 +00:00
|
|
|
{
|
2017-05-17 23:19:47 +00:00
|
|
|
nbio_buf_t *buffer;
|
|
|
|
bool copy_data_over;
|
|
|
|
bool is_finished;
|
2017-05-15 09:07:12 +00:00
|
|
|
enum audio_mixer_type type;
|
|
|
|
char path[4095];
|
2017-05-14 21:12:10 +00:00
|
|
|
};
|
|
|
|
|
2017-05-17 12:04:33 +00:00
|
|
|
static void task_audio_mixer_load_free(retro_task_t *task)
|
2017-05-14 21:12:10 +00:00
|
|
|
{
|
2017-05-18 03:12:41 +00:00
|
|
|
nbio_handle_t *nbio = (nbio_handle_t*)task->state;
|
2017-05-17 23:19:47 +00:00
|
|
|
struct audio_mixer_handle *image = (struct audio_mixer_handle*)nbio->data;
|
|
|
|
|
|
|
|
if (image)
|
|
|
|
{
|
|
|
|
if (image->buffer)
|
|
|
|
free(image->buffer);
|
|
|
|
}
|
2017-05-14 21:12:10 +00:00
|
|
|
|
2017-09-30 18:45:06 +00:00
|
|
|
if (!string_is_empty(nbio->path))
|
2017-09-29 20:16:17 +00:00
|
|
|
free(nbio->path);
|
2017-05-18 03:12:41 +00:00
|
|
|
if (nbio->data)
|
|
|
|
free(nbio->data);
|
|
|
|
nbio_free(nbio->handle);
|
|
|
|
free(nbio);
|
2017-05-17 12:04:33 +00:00
|
|
|
}
|
2017-05-14 21:12:10 +00:00
|
|
|
|
2017-05-17 12:04:33 +00:00
|
|
|
static int cb_nbio_audio_mixer_load(void *data, size_t len)
|
|
|
|
{
|
|
|
|
nbio_handle_t *nbio = (nbio_handle_t*)data;
|
2017-05-18 03:12:41 +00:00
|
|
|
struct audio_mixer_handle *image= (struct audio_mixer_handle*)nbio->data;
|
2017-05-17 12:13:02 +00:00
|
|
|
void *ptr = nbio_get_ptr(nbio->handle, &len);
|
2017-05-18 03:12:41 +00:00
|
|
|
nbio_buf_t *buffer = (nbio_buf_t*)calloc(1, sizeof(*image->buffer));
|
2017-05-14 21:12:10 +00:00
|
|
|
|
2017-05-18 03:12:41 +00:00
|
|
|
if (!buffer)
|
2017-05-17 23:19:47 +00:00
|
|
|
return -1;
|
|
|
|
|
2017-05-18 03:12:41 +00:00
|
|
|
image->buffer = buffer;
|
2017-05-17 23:19:47 +00:00
|
|
|
image->buffer->buf = ptr;
|
|
|
|
image->buffer->bufsize = len;
|
|
|
|
image->copy_data_over = true;
|
|
|
|
nbio->is_finished = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void task_audio_mixer_handle_upload_ogg(void *task_data,
|
|
|
|
void *user_data, const char *err)
|
|
|
|
{
|
2017-05-18 04:16:16 +00:00
|
|
|
audio_mixer_stream_params_t params;
|
2017-05-17 23:19:47 +00:00
|
|
|
nbio_buf_t *img = (nbio_buf_t*)task_data;
|
|
|
|
|
|
|
|
if (!img)
|
|
|
|
return;
|
2017-05-14 21:12:10 +00:00
|
|
|
|
2017-05-18 04:16:16 +00:00
|
|
|
params.volume = 1.0f;
|
|
|
|
params.type = AUDIO_MIXER_TYPE_OGG;
|
2017-05-18 05:27:41 +00:00
|
|
|
params.state = AUDIO_STREAM_STATE_PLAYING;
|
2017-05-18 04:16:16 +00:00
|
|
|
params.buf = img->buf;
|
|
|
|
params.bufsize = img->bufsize;
|
|
|
|
params.cb = NULL;
|
2017-05-17 12:13:02 +00:00
|
|
|
|
2017-05-18 04:16:16 +00:00
|
|
|
audio_driver_mixer_add_stream(¶ms);
|
2017-05-17 12:04:33 +00:00
|
|
|
|
2017-05-17 23:19:47 +00:00
|
|
|
free(img);
|
|
|
|
free(user_data);
|
|
|
|
}
|
|
|
|
|
2017-08-04 13:05:29 +00:00
|
|
|
static void task_audio_mixer_handle_upload_mod(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;
|
|
|
|
|
|
|
|
params.volume = 1.0f;
|
|
|
|
params.type = AUDIO_MIXER_TYPE_MOD;
|
|
|
|
params.state = AUDIO_STREAM_STATE_PLAYING;
|
|
|
|
params.buf = img->buf;
|
|
|
|
params.bufsize = img->bufsize;
|
|
|
|
params.cb = NULL;
|
|
|
|
|
|
|
|
audio_driver_mixer_add_stream(¶ms);
|
|
|
|
|
|
|
|
free(img);
|
|
|
|
free(user_data);
|
|
|
|
}
|
|
|
|
|
2017-05-17 23:19:47 +00:00
|
|
|
static void task_audio_mixer_handle_upload_wav(void *task_data,
|
|
|
|
void *user_data, const char *err)
|
|
|
|
{
|
2017-05-18 04:16:16 +00:00
|
|
|
audio_mixer_stream_params_t params;
|
2017-05-17 23:19:47 +00:00
|
|
|
nbio_buf_t *img = (nbio_buf_t*)task_data;
|
2017-05-14 21:12:10 +00:00
|
|
|
|
2017-05-17 23:19:47 +00:00
|
|
|
if (!img)
|
|
|
|
return;
|
2017-05-14 21:12:10 +00:00
|
|
|
|
2017-05-18 04:16:16 +00:00
|
|
|
params.volume = 1.0f;
|
|
|
|
params.type = AUDIO_MIXER_TYPE_WAV;
|
2017-05-18 05:27:41 +00:00
|
|
|
params.state = AUDIO_STREAM_STATE_PLAYING;
|
2017-05-18 04:16:16 +00:00
|
|
|
params.buf = img->buf;
|
|
|
|
params.bufsize = img->bufsize;
|
|
|
|
params.cb = NULL;
|
|
|
|
|
|
|
|
audio_driver_mixer_add_stream(¶ms);
|
2017-05-17 23:19:47 +00:00
|
|
|
|
|
|
|
free(img);
|
|
|
|
free(user_data);
|
2017-05-17 12:04:33 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 23:19:47 +00:00
|
|
|
bool task_audio_mixer_load_handler(retro_task_t *task)
|
2017-05-17 12:04:33 +00:00
|
|
|
{
|
2017-05-17 23:19:47 +00:00
|
|
|
nbio_handle_t *nbio = (nbio_handle_t*)task->state;
|
|
|
|
struct audio_mixer_handle *image = (struct audio_mixer_handle*)nbio->data;
|
|
|
|
|
|
|
|
if (
|
|
|
|
nbio->is_finished
|
|
|
|
&& (image && !image->is_finished)
|
|
|
|
&& (image->copy_data_over)
|
|
|
|
&& (!task_get_cancelled(task)))
|
|
|
|
{
|
|
|
|
nbio_buf_t *img = (nbio_buf_t*)calloc(1, sizeof(*img));
|
|
|
|
|
|
|
|
if (img)
|
|
|
|
{
|
|
|
|
img->buf = image->buffer->buf;
|
|
|
|
img->bufsize = image->buffer->bufsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
task_set_data(task, img);
|
|
|
|
|
|
|
|
image->copy_data_over = false;
|
|
|
|
image->is_finished = true;
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2017-05-14 21:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool task_push_audio_mixer_load(const char *fullpath, retro_task_callback_t cb, void *user_data)
|
|
|
|
{
|
2017-05-17 12:04:33 +00:00
|
|
|
nbio_handle_t *nbio = NULL;
|
2017-05-15 09:25:45 +00:00
|
|
|
struct audio_mixer_handle *image = NULL;
|
2017-05-17 12:04:33 +00:00
|
|
|
retro_task_t *t = (retro_task_t*)calloc(1, sizeof(*t));
|
2017-05-14 21:12:10 +00:00
|
|
|
|
|
|
|
if (!t)
|
2017-05-18 06:04:24 +00:00
|
|
|
goto error;
|
2017-05-14 21:12:10 +00:00
|
|
|
|
2017-05-17 12:04:33 +00:00
|
|
|
nbio = (nbio_handle_t*)calloc(1, sizeof(*nbio));
|
|
|
|
|
|
|
|
if (!nbio)
|
|
|
|
goto error;
|
|
|
|
|
2017-09-29 20:16:17 +00:00
|
|
|
nbio->path = strdup(fullpath);
|
2017-05-17 12:04:33 +00:00
|
|
|
|
2017-05-15 09:25:45 +00:00
|
|
|
image = (struct audio_mixer_handle*)calloc(1, sizeof(*image));
|
2017-05-14 21:12:10 +00:00
|
|
|
if (!image)
|
|
|
|
goto error;
|
|
|
|
|
2017-05-17 23:19:47 +00:00
|
|
|
image->is_finished = false;
|
|
|
|
|
2017-05-15 09:07:12 +00:00
|
|
|
strlcpy(image->path, fullpath, sizeof(image->path));
|
|
|
|
|
2017-05-17 12:04:33 +00:00
|
|
|
nbio->type = NBIO_TYPE_NONE;
|
2017-05-15 09:07:12 +00:00
|
|
|
image->type = AUDIO_MIXER_TYPE_NONE;
|
2017-05-14 21:12:10 +00:00
|
|
|
|
|
|
|
if (strstr(fullpath, file_path_str(FILE_PATH_WAV_EXTENSION)))
|
|
|
|
{
|
2017-05-15 09:07:12 +00:00
|
|
|
image->type = AUDIO_MIXER_TYPE_WAV;
|
2017-05-17 23:19:47 +00:00
|
|
|
nbio->type = NBIO_TYPE_WAV;
|
|
|
|
t->callback = task_audio_mixer_handle_upload_wav;
|
2017-05-14 21:12:10 +00:00
|
|
|
}
|
|
|
|
else if (strstr(fullpath, file_path_str(FILE_PATH_OGG_EXTENSION)))
|
|
|
|
{
|
2017-05-15 09:07:12 +00:00
|
|
|
image->type = AUDIO_MIXER_TYPE_OGG;
|
2017-05-17 23:19:47 +00:00
|
|
|
nbio->type = NBIO_TYPE_OGG;
|
|
|
|
t->callback = task_audio_mixer_handle_upload_ogg;
|
2017-05-14 21:12:10 +00:00
|
|
|
}
|
2017-08-04 13:05:29 +00:00
|
|
|
else if ( strstr(fullpath, file_path_str(FILE_PATH_MOD_EXTENSION)) ||
|
|
|
|
strstr(fullpath, file_path_str(FILE_PATH_S3M_EXTENSION)) ||
|
|
|
|
strstr(fullpath, file_path_str(FILE_PATH_XM_EXTENSION)))
|
|
|
|
{
|
|
|
|
image->type = AUDIO_MIXER_TYPE_MOD;
|
|
|
|
nbio->type = NBIO_TYPE_MOD;
|
|
|
|
t->callback = task_audio_mixer_handle_upload_mod;
|
|
|
|
}
|
2017-05-14 21:12:10 +00:00
|
|
|
|
2017-05-17 12:04:33 +00: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->user_data = user_data;
|
2017-05-14 21:12:10 +00:00
|
|
|
|
|
|
|
task_queue_push(t);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
2017-05-18 03:12:41 +00:00
|
|
|
if (nbio)
|
|
|
|
{
|
2017-09-30 18:45:06 +00:00
|
|
|
if (!string_is_empty(nbio->path))
|
2017-09-29 20:16:17 +00:00
|
|
|
free(nbio->path);
|
2017-05-18 03:12:41 +00:00
|
|
|
if (nbio->data)
|
|
|
|
free(nbio->data);
|
|
|
|
nbio_free(nbio->handle);
|
|
|
|
free(nbio);
|
|
|
|
}
|
2017-05-18 06:04:24 +00:00
|
|
|
if (t)
|
|
|
|
free(t);
|
2017-05-14 21:12:10 +00:00
|
|
|
|
|
|
|
RARCH_ERR("[audio mixer load] Failed to open '%s': %s.\n",
|
|
|
|
fullpath, strerror(errno));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|