2011-01-17 19:54:58 +00:00
|
|
|
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
|
2011-01-23 19:29:28 +00:00
|
|
|
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
|
2010-12-24 00:33:40 +00:00
|
|
|
*
|
|
|
|
* Some code herein may be based on code found in BSNES.
|
|
|
|
*
|
|
|
|
* SSNES 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.
|
|
|
|
*
|
|
|
|
* SSNES 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 SSNES.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-12-24 00:26:36 +00:00
|
|
|
#include "driver.h"
|
|
|
|
#include "general.h"
|
|
|
|
#include <stdio.h>
|
2010-12-29 18:43:17 +00:00
|
|
|
#include <string.h>
|
2010-12-30 00:38:20 +00:00
|
|
|
#include "hqflt/filters.h"
|
2011-01-14 14:34:38 +00:00
|
|
|
#include <assert.h>
|
2011-01-07 16:59:53 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-12-30 02:02:30 +00:00
|
|
|
#include "config.h"
|
2011-01-07 16:59:53 +00:00
|
|
|
#endif
|
2010-12-24 00:26:36 +00:00
|
|
|
|
2010-12-29 19:50:50 +00:00
|
|
|
static const audio_driver_t *audio_drivers[] = {
|
2010-12-29 19:05:57 +00:00
|
|
|
#ifdef HAVE_ALSA
|
|
|
|
&audio_alsa,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_OSS
|
|
|
|
&audio_oss,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_RSOUND
|
|
|
|
&audio_rsound,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_AL
|
|
|
|
&audio_openal,
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ROAR
|
|
|
|
&audio_roar,
|
|
|
|
#endif
|
2011-01-01 02:53:30 +00:00
|
|
|
#ifdef HAVE_JACK
|
|
|
|
&audio_jack,
|
|
|
|
#endif
|
2011-01-07 14:50:16 +00:00
|
|
|
#ifdef HAVE_SDL
|
|
|
|
&audio_sdl,
|
|
|
|
#endif
|
2010-12-29 19:05:57 +00:00
|
|
|
};
|
|
|
|
|
2010-12-29 19:50:50 +00:00
|
|
|
static const video_driver_t *video_drivers[] = {
|
2011-01-06 17:38:02 +00:00
|
|
|
#ifdef HAVE_SDL
|
2010-12-29 19:05:57 +00:00
|
|
|
&video_gl,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2011-01-06 17:34:11 +00:00
|
|
|
static const input_driver_t *input_drivers[] = {
|
|
|
|
#ifdef HAVE_SDL
|
|
|
|
&input_sdl,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2010-12-29 19:50:50 +00:00
|
|
|
static void find_audio_driver(void)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < sizeof(audio_drivers) / sizeof(audio_driver_t*); i++)
|
|
|
|
{
|
|
|
|
if (strcasecmp(g_settings.audio.driver, audio_drivers[i]->ident) == 0)
|
|
|
|
{
|
|
|
|
driver.audio = audio_drivers[i];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SSNES_ERR("Couldn't find any audio driver named \"%s\"\n", g_settings.audio.driver);
|
2010-12-29 19:56:56 +00:00
|
|
|
fprintf(stderr, "Available audio drivers are:\n");
|
|
|
|
for (int i = 0; i < sizeof(audio_drivers) / sizeof(audio_driver_t*); i++)
|
|
|
|
fprintf(stderr, "\t%s\n", audio_drivers[i]->ident);
|
|
|
|
|
2010-12-29 19:50:50 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void find_video_driver(void)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < sizeof(video_drivers) / sizeof(video_driver_t*); i++)
|
|
|
|
{
|
|
|
|
if (strcasecmp(g_settings.video.driver, video_drivers[i]->ident) == 0)
|
|
|
|
{
|
|
|
|
driver.video = video_drivers[i];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SSNES_ERR("Couldn't find any video driver named \"%s\"\n", g_settings.video.driver);
|
2010-12-29 19:56:56 +00:00
|
|
|
fprintf(stderr, "Available video drivers are:\n");
|
|
|
|
for (int i = 0; i < sizeof(video_drivers) / sizeof(video_driver_t*); i++)
|
|
|
|
fprintf(stderr, "\t%s\n", video_drivers[i]->ident);
|
|
|
|
|
2010-12-29 19:50:50 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2010-12-29 19:05:57 +00:00
|
|
|
|
2011-01-06 17:34:11 +00:00
|
|
|
static void find_input_driver(void)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < sizeof(input_drivers) / sizeof(input_driver_t*); i++)
|
|
|
|
{
|
|
|
|
if (strcasecmp(g_settings.input.driver, input_drivers[i]->ident) == 0)
|
|
|
|
{
|
|
|
|
driver.input = input_drivers[i];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SSNES_ERR("Couldn't find any input driver named \"%s\"\n", g_settings.input.driver);
|
2011-01-06 19:01:32 +00:00
|
|
|
fprintf(stderr, "Available input drivers are:\n");
|
2011-01-06 17:34:11 +00:00
|
|
|
for (int i = 0; i < sizeof(input_drivers) / sizeof(input_driver_t*); i++)
|
|
|
|
fprintf(stderr, "\t%s\n", video_drivers[i]->ident);
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-12-24 00:26:36 +00:00
|
|
|
void init_drivers(void)
|
|
|
|
{
|
|
|
|
init_video_input();
|
|
|
|
init_audio();
|
|
|
|
}
|
|
|
|
|
|
|
|
void uninit_drivers(void)
|
|
|
|
{
|
|
|
|
uninit_video_input();
|
|
|
|
uninit_audio();
|
|
|
|
}
|
|
|
|
|
2011-01-14 14:34:38 +00:00
|
|
|
#define AUDIO_CHUNK_SIZE_BLOCKING 64
|
|
|
|
#define AUDIO_CHUNK_SIZE_NONBLOCKING 2048 // So we don't get complete line-noise when fast-forwarding audio.
|
|
|
|
#define AUDIO_MAX_RATIO 16
|
2010-12-24 00:26:36 +00:00
|
|
|
void init_audio(void)
|
|
|
|
{
|
2010-12-29 18:43:17 +00:00
|
|
|
if (!g_settings.audio.enable)
|
2010-12-24 00:26:36 +00:00
|
|
|
{
|
2010-12-29 18:18:37 +00:00
|
|
|
g_extern.audio_active = false;
|
2010-12-24 00:26:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-29 19:50:50 +00:00
|
|
|
find_audio_driver();
|
|
|
|
|
2011-01-14 14:34:38 +00:00
|
|
|
g_extern.audio_data.block_chunk_size = AUDIO_CHUNK_SIZE_BLOCKING;
|
|
|
|
g_extern.audio_data.nonblock_chunk_size = AUDIO_CHUNK_SIZE_NONBLOCKING;
|
|
|
|
|
2010-12-29 18:43:17 +00:00
|
|
|
driver.audio_data = driver.audio->init(strlen(g_settings.audio.device) ? g_settings.audio.device : NULL, g_settings.audio.out_rate, g_settings.audio.latency);
|
2010-12-24 00:26:36 +00:00
|
|
|
if ( driver.audio_data == NULL )
|
2010-12-29 18:18:37 +00:00
|
|
|
g_extern.audio_active = false;
|
2010-12-24 00:26:36 +00:00
|
|
|
|
2011-01-15 19:37:42 +00:00
|
|
|
if (g_extern.audio_active && driver.audio->use_float && driver.audio->use_float(driver.audio_data))
|
|
|
|
g_extern.audio_data.use_float = true;
|
2011-01-14 14:34:38 +00:00
|
|
|
|
2010-12-29 18:43:17 +00:00
|
|
|
if (!g_settings.audio.sync && g_extern.audio_active)
|
2011-01-14 14:34:38 +00:00
|
|
|
{
|
2010-12-24 00:26:36 +00:00
|
|
|
driver.audio->set_nonblock_state(driver.audio_data, true);
|
2011-01-14 14:34:38 +00:00
|
|
|
g_extern.audio_data.chunk_size = g_extern.audio_data.nonblock_chunk_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_extern.audio_data.chunk_size = g_extern.audio_data.block_chunk_size;
|
2010-12-24 00:26:36 +00:00
|
|
|
|
|
|
|
int err;
|
2010-12-29 18:43:17 +00:00
|
|
|
g_extern.source = src_new(g_settings.audio.src_quality, 2, &err);
|
2010-12-29 18:18:37 +00:00
|
|
|
if (!g_extern.source)
|
|
|
|
g_extern.audio_active = false;
|
2011-01-14 14:34:38 +00:00
|
|
|
|
|
|
|
size_t max_bufsamples = g_extern.audio_data.block_chunk_size > g_extern.audio_data.nonblock_chunk_size ?
|
|
|
|
g_extern.audio_data.block_chunk_size : g_extern.audio_data.nonblock_chunk_size;
|
|
|
|
|
|
|
|
assert(g_settings.audio.out_rate < g_settings.audio.in_rate * AUDIO_MAX_RATIO);
|
|
|
|
assert((g_extern.audio_data.data = malloc(max_bufsamples * sizeof(float))));
|
|
|
|
g_extern.audio_data.data_ptr = 0;
|
|
|
|
assert((g_extern.audio_data.outsamples = malloc(max_bufsamples * sizeof(float) * AUDIO_MAX_RATIO)));
|
|
|
|
assert((g_extern.audio_data.conv_outsamples = malloc(max_bufsamples * sizeof(int16_t) * AUDIO_MAX_RATIO)));
|
2010-12-24 00:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void uninit_audio(void)
|
|
|
|
{
|
2010-12-29 18:43:17 +00:00
|
|
|
if (!g_settings.audio.enable)
|
2010-12-24 00:26:36 +00:00
|
|
|
{
|
2010-12-29 18:18:37 +00:00
|
|
|
g_extern.audio_active = false;
|
2010-12-24 00:26:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( driver.audio_data && driver.audio )
|
|
|
|
driver.audio->free(driver.audio_data);
|
|
|
|
|
2010-12-29 18:18:37 +00:00
|
|
|
if ( g_extern.source )
|
|
|
|
src_delete(g_extern.source);
|
2011-01-14 14:34:38 +00:00
|
|
|
|
|
|
|
free(g_extern.audio_data.data); g_extern.audio_data.data = NULL;
|
|
|
|
free(g_extern.audio_data.outsamples); g_extern.audio_data.outsamples = NULL;
|
|
|
|
free(g_extern.audio_data.conv_outsamples); g_extern.audio_data.conv_outsamples = NULL;
|
2010-12-24 00:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_video_input(void)
|
|
|
|
{
|
2010-12-30 00:33:40 +00:00
|
|
|
int scale = 2;
|
2010-12-24 00:26:36 +00:00
|
|
|
|
2010-12-29 19:50:50 +00:00
|
|
|
find_video_driver();
|
2011-01-06 17:34:11 +00:00
|
|
|
find_input_driver();
|
2010-12-29 19:50:50 +00:00
|
|
|
|
2010-12-24 00:26:36 +00:00
|
|
|
// We multiply scales with 2 to allow for hi-res games.
|
2010-12-30 00:33:40 +00:00
|
|
|
#if HAVE_FILTER
|
|
|
|
switch (g_settings.video.filter)
|
|
|
|
{
|
|
|
|
case FILTER_HQ2X:
|
|
|
|
scale = 4;
|
|
|
|
break;
|
|
|
|
case FILTER_HQ4X:
|
|
|
|
case FILTER_NTSC:
|
|
|
|
scale = 8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-12-29 18:43:17 +00:00
|
|
|
#endif
|
2010-12-24 00:26:36 +00:00
|
|
|
|
|
|
|
video_info_t video = {
|
2010-12-29 18:43:17 +00:00
|
|
|
.width = (g_settings.video.fullscreen) ? g_settings.video.fullscreen_x : (296 * g_settings.video.xscale),
|
|
|
|
.height = (g_settings.video.fullscreen) ? g_settings.video.fullscreen_y : (224 * g_settings.video.yscale),
|
|
|
|
.fullscreen = g_settings.video.fullscreen,
|
|
|
|
.vsync = g_settings.video.vsync,
|
|
|
|
.force_aspect = g_settings.video.force_aspect,
|
|
|
|
.smooth = g_settings.video.smooth,
|
2010-12-24 00:26:36 +00:00
|
|
|
.input_scale = scale,
|
|
|
|
};
|
|
|
|
|
|
|
|
const input_driver_t *tmp = driver.input;
|
2011-01-06 19:01:32 +00:00
|
|
|
driver.video_data = driver.video->init(&video, &driver.input, &driver.input_data);
|
2010-12-24 00:26:36 +00:00
|
|
|
|
|
|
|
if ( driver.video_data == NULL )
|
|
|
|
{
|
|
|
|
SSNES_ERR("Cannot open video driver... Exiting ...\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-01-06 19:01:32 +00:00
|
|
|
// Video driver didn't provide an input driver so we use configured one.
|
|
|
|
if (driver.input == NULL)
|
2010-12-24 00:26:36 +00:00
|
|
|
{
|
|
|
|
driver.input = tmp;
|
|
|
|
if (driver.input != NULL)
|
|
|
|
{
|
|
|
|
driver.input_data = driver.input->init();
|
|
|
|
if ( driver.input_data == NULL )
|
2011-01-06 17:34:11 +00:00
|
|
|
{
|
|
|
|
SSNES_ERR("Cannot init input driver. Exiting ...\n");
|
2010-12-24 00:26:36 +00:00
|
|
|
exit(1);
|
2011-01-06 17:34:11 +00:00
|
|
|
}
|
2010-12-24 00:26:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SSNES_ERR("Cannot find input driver. Exiting ...\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void uninit_video_input(void)
|
|
|
|
{
|
|
|
|
if ( driver.video_data && driver.video )
|
|
|
|
driver.video->free(driver.video_data);
|
|
|
|
|
|
|
|
if ( driver.input_data != driver.video_data && driver.input )
|
|
|
|
driver.input->free(driver.input_data);
|
|
|
|
}
|
|
|
|
|
2010-12-29 19:05:57 +00:00
|
|
|
driver_t driver;
|
|
|
|
|
|
|
|
#if 0
|
2010-12-24 00:26:36 +00:00
|
|
|
driver_t driver = {
|
|
|
|
#if VIDEO_DRIVER == VIDEO_GL
|
|
|
|
.video = &video_gl,
|
|
|
|
#else
|
|
|
|
#error "Define a valid video driver in config.h"
|
|
|
|
#endif
|
|
|
|
|
2010-12-29 18:43:17 +00:00
|
|
|
#if AUDIO_DRIVER == AUDIO_ALSA
|
|
|
|
.audio = &audio_alsa,
|
|
|
|
#elif AUDIO_DRIVER == AUDIO_RSOUND
|
2010-12-24 00:26:36 +00:00
|
|
|
.audio = &audio_rsound,
|
|
|
|
#elif AUDIO_DRIVER == AUDIO_OSS
|
|
|
|
.audio = &audio_oss,
|
|
|
|
#elif AUDIO_DRIVER == AUDIO_ROAR
|
|
|
|
.audio = &audio_roar,
|
|
|
|
#elif AUDIO_DRIVER == AUDIO_AL
|
|
|
|
.audio = &audio_openal,
|
|
|
|
#else
|
|
|
|
#error "Define a valid audio driver in config.h"
|
|
|
|
#endif
|
|
|
|
};
|
2010-12-29 19:05:57 +00:00
|
|
|
#endif
|
2010-12-24 00:26:36 +00:00
|
|
|
|