2012-06-09 14:12:05 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// $Id$
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2015-01-05 00:12:41 +03:00
|
|
|
// Copyright (C) 2012-2015 The Bochs Project
|
2012-06-09 14:12:05 +04:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library 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
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
// Lowlevel sound output support for SDL written by Volker Ruppert
|
|
|
|
|
|
|
|
#include "iodev.h"
|
2014-11-24 21:25:14 +03:00
|
|
|
#include "soundlow.h"
|
2012-06-09 14:12:05 +04:00
|
|
|
#include "soundsdl.h"
|
|
|
|
|
2014-06-23 23:37:58 +04:00
|
|
|
#if (BX_WITH_SDL || BX_WITH_SDL2) && BX_SUPPORT_SOUNDLOW
|
2012-06-09 14:12:05 +04:00
|
|
|
|
2013-06-24 23:19:12 +04:00
|
|
|
#define LOG_THIS
|
2012-06-09 14:12:05 +04:00
|
|
|
|
|
|
|
#include <SDL.h>
|
|
|
|
|
|
|
|
#define BX_SOUND_SDL_BUFSIZE BX_SOUNDLOW_WAVEPACKETSIZE * 8
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
int iptr;
|
|
|
|
int optr;
|
|
|
|
Bit8u data[BX_SOUND_SDL_BUFSIZE];
|
|
|
|
} audio_buffer;
|
|
|
|
|
|
|
|
|
2013-06-24 23:19:12 +04:00
|
|
|
bx_sound_sdl_c::bx_sound_sdl_c()
|
|
|
|
:bx_sound_lowlevel_c()
|
2012-06-09 14:12:05 +04:00
|
|
|
{
|
|
|
|
WaveOpen = 0;
|
2015-01-08 22:12:01 +03:00
|
|
|
cb_count = 0;
|
2012-06-09 14:12:05 +04:00
|
|
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO)) {
|
2013-07-25 22:47:11 +04:00
|
|
|
BX_PANIC(("Initialization of sound lowlevel module 'sdl' failed"));
|
2012-06-09 14:12:05 +04:00
|
|
|
} else {
|
2013-07-25 22:47:11 +04:00
|
|
|
BX_INFO(("Sound lowlevel module 'sdl' initialized"));
|
2012-06-09 14:12:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bx_sound_sdl_c::~bx_sound_sdl_c()
|
|
|
|
{
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int bx_sound_sdl_c::openwaveoutput(const char *wavedev)
|
|
|
|
{
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-05 00:12:41 +03:00
|
|
|
Bit32u bx_sound_sdl_c::get_wave_data(Bit8u *stream, int len)
|
2012-06-09 14:12:05 +04:00
|
|
|
{
|
2015-01-08 22:12:01 +03:00
|
|
|
Bit32u len2 = 0;
|
|
|
|
|
2015-01-05 00:12:41 +03:00
|
|
|
Bit8u *tmpbuffer = (Bit8u*)malloc(len);
|
2015-01-08 22:12:01 +03:00
|
|
|
for (int i = 0; i < cb_count; i++) {
|
|
|
|
if (get_wave[i].cb != NULL) {
|
|
|
|
memset(tmpbuffer, 0, len);
|
|
|
|
len2 = get_wave[i].cb(get_wave[i].device, fmt.freq, tmpbuffer, len);
|
|
|
|
if (len2 > 0) {
|
|
|
|
SDL_MixAudio(stream, tmpbuffer, len2, SDL_MIX_MAXVOLUME);
|
|
|
|
// FIXME: mix wave data
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-01-05 00:12:41 +03:00
|
|
|
}
|
|
|
|
free(tmpbuffer);
|
|
|
|
return len2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sdl_callback(void *thisptr, Bit8u *stream, int len)
|
|
|
|
{
|
|
|
|
memset(stream, 0, len);
|
|
|
|
// FIXME: mix wave data
|
|
|
|
if (((bx_sound_sdl_c*)thisptr)->get_wave_data(stream, len) > 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-09 14:12:05 +04:00
|
|
|
int amount = audio_buffer.iptr - audio_buffer.optr;
|
|
|
|
if (amount < 0) {
|
|
|
|
amount += BX_SOUND_SDL_BUFSIZE;
|
|
|
|
}
|
|
|
|
if (amount > len) {
|
|
|
|
amount = len;
|
|
|
|
}
|
|
|
|
int tmplen = BX_SOUND_SDL_BUFSIZE - audio_buffer.optr;
|
|
|
|
if (amount <= tmplen) {
|
|
|
|
SDL_MixAudio(stream, &audio_buffer.data[audio_buffer.optr], amount, SDL_MIX_MAXVOLUME);
|
|
|
|
audio_buffer.optr += amount;
|
|
|
|
} else {
|
|
|
|
SDL_MixAudio(stream, &audio_buffer.data[audio_buffer.optr], tmplen, SDL_MIX_MAXVOLUME);
|
|
|
|
amount -= tmplen;
|
|
|
|
SDL_MixAudio(stream, &audio_buffer.data[0], amount, SDL_MIX_MAXVOLUME);
|
|
|
|
audio_buffer.optr = amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_sdl_c::startwaveplayback(int frequency, int bits, bx_bool stereo, int format)
|
|
|
|
{
|
|
|
|
int signeddata = format & 1;
|
|
|
|
|
|
|
|
BX_DEBUG(("startwaveplayback(%d, %d, %d, %x)", frequency, bits, stereo, format));
|
|
|
|
fmt.freq = frequency;
|
|
|
|
|
|
|
|
if (bits == 16) {
|
|
|
|
if (signeddata == 1)
|
|
|
|
fmt.format = AUDIO_S16;
|
|
|
|
else
|
|
|
|
fmt.format = AUDIO_U16;
|
|
|
|
} else if (bits == 8) {
|
|
|
|
if (signeddata == 1)
|
|
|
|
fmt.format = AUDIO_S8;
|
|
|
|
else
|
|
|
|
fmt.format = AUDIO_U8;
|
|
|
|
} else
|
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
|
|
|
|
fmt.channels = stereo + 1;
|
|
|
|
fmt.samples = frequency / 10;
|
|
|
|
fmt.callback = sdl_callback;
|
2015-01-05 00:12:41 +03:00
|
|
|
fmt.userdata = this;
|
|
|
|
if (WaveOpen) {
|
|
|
|
SDL_CloseAudio();
|
|
|
|
}
|
2012-06-09 14:12:05 +04:00
|
|
|
if (SDL_OpenAudio(&fmt, NULL) < 0) {
|
|
|
|
BX_PANIC(("SDL_OpenAudio() failed"));
|
2015-01-05 00:12:41 +03:00
|
|
|
WaveOpen = 0;
|
2012-06-09 14:12:05 +04:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2015-01-05 00:12:41 +03:00
|
|
|
} else {
|
|
|
|
WaveOpen = 1;
|
2012-06-09 14:12:05 +04:00
|
|
|
}
|
|
|
|
audio_buffer.iptr = 0;
|
|
|
|
audio_buffer.optr = 0;
|
2015-01-05 00:12:41 +03:00
|
|
|
SDL_PauseAudio(0);
|
2012-06-09 14:12:05 +04:00
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_sdl_c::waveready()
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (WaveOpen) {
|
|
|
|
size = audio_buffer.optr - audio_buffer.iptr;
|
|
|
|
if (size <= 0) {
|
|
|
|
size += BX_SOUND_SDL_BUFSIZE;
|
|
|
|
}
|
|
|
|
if (size < BX_SOUNDLOW_WAVEPACKETSIZE) {
|
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
} else {
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_sdl_c::sendwavepacket(int length, Bit8u data[])
|
|
|
|
{
|
|
|
|
int ret = BX_SOUNDLOW_OK;
|
|
|
|
int tmpsize;
|
|
|
|
|
2015-01-06 01:07:03 +03:00
|
|
|
SDL_LockAudio();
|
2012-06-09 14:12:05 +04:00
|
|
|
if (waveready() == BX_SOUNDLOW_OK) {
|
|
|
|
if ((audio_buffer.iptr + length) > BX_SOUND_SDL_BUFSIZE) {
|
|
|
|
tmpsize = BX_SOUND_SDL_BUFSIZE - audio_buffer.iptr;
|
|
|
|
memcpy(audio_buffer.data+audio_buffer.iptr, data, tmpsize);
|
|
|
|
memcpy(audio_buffer.data, data+tmpsize, length-tmpsize);
|
|
|
|
audio_buffer.iptr = length-tmpsize;
|
|
|
|
} else {
|
|
|
|
memcpy(audio_buffer.data+audio_buffer.iptr, data, length);
|
|
|
|
audio_buffer.iptr += length;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BX_ERROR(("SDL: audio buffer overflow"));
|
|
|
|
ret = BX_SOUNDLOW_ERR;
|
|
|
|
}
|
2015-01-06 01:07:03 +03:00
|
|
|
SDL_UnlockAudio();
|
2012-06-09 14:12:05 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_sdl_c::stopwaveplayback()
|
|
|
|
{
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_sdl_c::closewaveoutput()
|
|
|
|
{
|
2015-01-08 22:12:01 +03:00
|
|
|
WaveOpen = 0;
|
|
|
|
SDL_CloseAudio();
|
2012-06-09 14:12:05 +04:00
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-08 22:12:01 +03:00
|
|
|
int bx_sound_sdl_c::register_wave_callback(void *arg, get_wave_cb_t wd_cb)
|
2015-01-05 00:12:41 +03:00
|
|
|
{
|
2015-01-08 22:12:01 +03:00
|
|
|
if (cb_count < BX_MAX_WAVE_CALLBACKS) {
|
|
|
|
get_wave[cb_count].device = arg;
|
|
|
|
get_wave[cb_count].cb = wd_cb;
|
2015-01-10 22:36:50 +03:00
|
|
|
return cb_count++;
|
2015-01-08 22:12:01 +03:00
|
|
|
}
|
2015-01-10 22:36:50 +03:00
|
|
|
return -1;
|
2015-01-08 22:12:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void bx_sound_sdl_c::unregister_wave_callback(int callback_id)
|
|
|
|
{
|
|
|
|
SDL_LockAudio();
|
|
|
|
if ((callback_id >= 0) && (callback_id < BX_MAX_WAVE_CALLBACKS)) {
|
2015-01-10 22:36:50 +03:00
|
|
|
get_wave[callback_id].device = NULL;
|
2015-01-08 22:12:01 +03:00
|
|
|
get_wave[callback_id].cb = NULL;
|
|
|
|
}
|
|
|
|
SDL_UnlockAudio();
|
2015-01-05 00:12:41 +03:00
|
|
|
}
|
|
|
|
|
2014-06-23 23:37:58 +04:00
|
|
|
#endif // BX_WITH_SDL || BX_WITH_SDL2
|