2012-06-09 14:12:05 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// $Id$
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2017-02-05 11:33:03 +03:00
|
|
|
// Copyright (C) 2012-2017 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"
|
|
|
|
|
2015-02-24 00:32:34 +03:00
|
|
|
#if BX_HAVE_SOUND_SDL && 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>
|
|
|
|
|
2015-01-31 01:52:56 +03:00
|
|
|
// SDL audio callback
|
2015-01-05 00:12:41 +03:00
|
|
|
|
|
|
|
void sdl_callback(void *thisptr, Bit8u *stream, int len)
|
|
|
|
{
|
|
|
|
memset(stream, 0, len);
|
2015-02-13 14:30:46 +03:00
|
|
|
((bx_soundlow_waveout_sdl_c*)thisptr)->mixer_common(stream, len);
|
2012-06-09 14:12:05 +04:00
|
|
|
}
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
// bx_soundlow_waveout_sdl_c class implemenzation
|
2015-01-25 23:03:11 +03:00
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
bx_soundlow_waveout_sdl_c::bx_soundlow_waveout_sdl_c()
|
|
|
|
:bx_soundlow_waveout_c()
|
2015-01-25 23:03:11 +03:00
|
|
|
{
|
2017-02-08 21:48:49 +03:00
|
|
|
WaveOutOpen = 0;
|
2015-01-25 23:03:11 +03:00
|
|
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO)) {
|
|
|
|
BX_PANIC(("Initialization of sound lowlevel module 'sdl' failed"));
|
|
|
|
} else {
|
|
|
|
BX_INFO(("Sound lowlevel module 'sdl' initialized"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
bx_soundlow_waveout_sdl_c::~bx_soundlow_waveout_sdl_c()
|
2015-01-25 23:03:11 +03:00
|
|
|
{
|
2015-03-29 18:53:56 +03:00
|
|
|
if (pcm_callback_id >= 0) {
|
|
|
|
unregister_wave_callback(pcm_callback_id);
|
|
|
|
pcm_callback_id = -1;
|
|
|
|
}
|
2017-02-08 21:48:49 +03:00
|
|
|
WaveOutOpen = 0;
|
2015-02-18 19:34:53 +03:00
|
|
|
mixer_control = 0;
|
2015-02-13 14:30:46 +03:00
|
|
|
SDL_CloseAudio();
|
2015-01-25 23:03:11 +03:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
|
|
|
}
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
int bx_soundlow_waveout_sdl_c::openwaveoutput(const char *wavedev)
|
2015-01-25 23:03:11 +03:00
|
|
|
{
|
2015-02-13 14:30:46 +03:00
|
|
|
set_pcm_params(&real_pcm_param);
|
2017-02-05 11:33:03 +03:00
|
|
|
start_resampler_thread();
|
2015-01-25 23:03:11 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
int bx_soundlow_waveout_sdl_c::set_pcm_params(bx_pcm_param_t *param)
|
2012-06-09 14:12:05 +04:00
|
|
|
{
|
2015-02-13 14:30:46 +03:00
|
|
|
int signeddata = param->format & 1;
|
2012-06-09 14:12:05 +04:00
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
BX_DEBUG(("set_pcm_params(): %u, %u, %u, %02x", param->samplerate, param->bits,
|
|
|
|
param->channels, param->format));
|
|
|
|
fmt.freq = param->samplerate;
|
2012-06-09 14:12:05 +04:00
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
if (param->bits == 16) {
|
2012-06-09 14:12:05 +04:00
|
|
|
if (signeddata == 1)
|
|
|
|
fmt.format = AUDIO_S16;
|
|
|
|
else
|
|
|
|
fmt.format = AUDIO_U16;
|
2015-02-13 14:30:46 +03:00
|
|
|
} else if (param->bits == 8) {
|
2012-06-09 14:12:05 +04:00
|
|
|
if (signeddata == 1)
|
|
|
|
fmt.format = AUDIO_S8;
|
|
|
|
else
|
|
|
|
fmt.format = AUDIO_U8;
|
|
|
|
} else
|
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
fmt.channels = param->channels;
|
2015-01-27 00:10:51 +03:00
|
|
|
fmt.samples = fmt.freq / 10;
|
2012-06-09 14:12:05 +04:00
|
|
|
fmt.callback = sdl_callback;
|
2015-01-05 00:12:41 +03:00
|
|
|
fmt.userdata = this;
|
2017-02-08 21:48:49 +03:00
|
|
|
if (WaveOutOpen) {
|
2015-01-05 00:12:41 +03:00
|
|
|
SDL_CloseAudio();
|
2015-01-11 23:13:50 +03:00
|
|
|
} else {
|
|
|
|
pcm_callback_id = register_wave_callback(this, pcm_callback);
|
2015-01-05 00:12:41 +03:00
|
|
|
}
|
2012-06-09 14:12:05 +04:00
|
|
|
if (SDL_OpenAudio(&fmt, NULL) < 0) {
|
|
|
|
BX_PANIC(("SDL_OpenAudio() failed"));
|
2017-02-08 21:48:49 +03:00
|
|
|
WaveOutOpen = 0;
|
2012-06-09 14:12:05 +04:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2015-01-05 00:12:41 +03:00
|
|
|
} else {
|
2017-02-05 11:33:03 +03:00
|
|
|
if (fmt.freq != param->samplerate) {
|
|
|
|
param->samplerate = fmt.freq;
|
|
|
|
BX_INFO(("changed sample rate to %d", fmt.freq));
|
|
|
|
}
|
2017-02-08 21:48:49 +03:00
|
|
|
WaveOutOpen = 1;
|
2015-02-18 19:34:53 +03:00
|
|
|
mixer_control = 1;
|
2012-06-09 14:12:05 +04:00
|
|
|
}
|
2015-01-05 00:12:41 +03:00
|
|
|
SDL_PauseAudio(0);
|
2012-06-09 14:12:05 +04:00
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
2017-02-05 11:33:03 +03:00
|
|
|
void bx_soundlow_waveout_sdl_c::resampler(audio_buffer_t *inbuffer, audio_buffer_t *outbuffer)
|
2012-06-09 14:12:05 +04:00
|
|
|
{
|
2017-02-05 11:33:03 +03:00
|
|
|
Bit32u fcount;
|
|
|
|
float *fbuffer = NULL;
|
|
|
|
|
|
|
|
UNUSED(outbuffer);
|
|
|
|
fcount = resampler_common(inbuffer, &fbuffer);
|
2015-01-06 01:07:03 +03:00
|
|
|
SDL_LockAudio();
|
2017-02-08 21:48:49 +03:00
|
|
|
if (WaveOutOpen) {
|
2017-02-05 11:33:03 +03:00
|
|
|
audio_buffer_t *newbuffer = audio_buffers[1]->new_buffer(fcount << 1);
|
|
|
|
convert_float_to_s16le(fbuffer, fcount, newbuffer->data);
|
2012-06-09 14:12:05 +04:00
|
|
|
}
|
2015-01-06 01:07:03 +03:00
|
|
|
SDL_UnlockAudio();
|
2017-02-05 11:33:03 +03:00
|
|
|
if (fbuffer != NULL) {
|
|
|
|
delete [] fbuffer;
|
|
|
|
}
|
2012-06-09 14:12:05 +04:00
|
|
|
}
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
bx_bool bx_soundlow_waveout_sdl_c::mixer_common(Bit8u *buffer, int len)
|
2015-01-25 23:03:11 +03:00
|
|
|
{
|
|
|
|
Bit32u len2 = 0;
|
|
|
|
|
|
|
|
Bit8u *tmpbuffer = (Bit8u*)malloc(len);
|
|
|
|
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) {
|
2015-02-01 23:50:20 +03:00
|
|
|
SDL_MixAudio(buffer, tmpbuffer, len2, SDL_MIX_MAXVOLUME);
|
2015-01-25 23:03:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(tmpbuffer);
|
2015-02-01 23:50:20 +03:00
|
|
|
return 1;
|
2015-01-25 23:03:11 +03:00
|
|
|
}
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
void bx_soundlow_waveout_sdl_c::unregister_wave_callback(int callback_id)
|
2015-01-08 22:12:01 +03:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-08 21:48:49 +03:00
|
|
|
// bx_soundlow_wavein_sdl2_c class implemenzation
|
|
|
|
|
|
|
|
#if BX_HAVE_SDL2_AUDIO_CAPTURE
|
|
|
|
bx_soundlow_wavein_sdl2_c::bx_soundlow_wavein_sdl2_c()
|
|
|
|
:bx_soundlow_wavein_c()
|
|
|
|
{
|
|
|
|
WaveInOpen = 0;
|
|
|
|
devID = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bx_soundlow_wavein_sdl2_c::~bx_soundlow_wavein_sdl2_c()
|
|
|
|
{
|
|
|
|
if (WaveInOpen) {
|
|
|
|
SDL_CloseAudioDevice(devID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_soundlow_wavein_sdl2_c::openwaveinput(const char *wavedev, sound_record_handler_t rh)
|
|
|
|
{
|
|
|
|
UNUSED(wavedev);
|
|
|
|
record_handler = rh;
|
|
|
|
if (rh != NULL) {
|
|
|
|
record_timer_index = bx_pc_system.register_timer(this, record_timer_handler, 1, 1, 0, "wavein");
|
|
|
|
// record timer: inactive, continuous, frequency variable
|
|
|
|
}
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_soundlow_wavein_sdl2_c::startwaverecord(bx_pcm_param_t *param)
|
|
|
|
{
|
|
|
|
int signeddata = param->format & 1;
|
|
|
|
Bit64u timer_val;
|
|
|
|
Bit8u shift = 0;
|
|
|
|
|
|
|
|
if (record_timer_index != BX_NULL_TIMER_HANDLE) {
|
|
|
|
if (param->bits == 16) shift++;
|
|
|
|
if (param->channels == 2) shift++;
|
|
|
|
record_packet_size = (param->samplerate / 10) << shift; // 0.1 sec
|
|
|
|
if (record_packet_size > BX_SOUNDLOW_WAVEPACKETSIZE) {
|
|
|
|
record_packet_size = BX_SOUNDLOW_WAVEPACKETSIZE;
|
|
|
|
}
|
|
|
|
timer_val = (Bit64u)record_packet_size * 1000000 / (param->samplerate << shift);
|
|
|
|
bx_pc_system.activate_timer(record_timer_index, (Bit32u)timer_val, 1);
|
|
|
|
}
|
|
|
|
fmt.freq = param->samplerate;
|
|
|
|
|
|
|
|
if (param->bits == 16) {
|
|
|
|
if (signeddata == 1)
|
|
|
|
fmt.format = AUDIO_S16;
|
|
|
|
else
|
|
|
|
fmt.format = AUDIO_U16;
|
|
|
|
} else if (param->bits == 8) {
|
|
|
|
if (signeddata == 1)
|
|
|
|
fmt.format = AUDIO_S8;
|
|
|
|
else
|
|
|
|
fmt.format = AUDIO_U8;
|
|
|
|
} else
|
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
|
|
|
|
fmt.channels = param->channels;
|
|
|
|
fmt.samples = fmt.freq / 10;
|
|
|
|
fmt.callback = NULL;
|
|
|
|
fmt.userdata = NULL;
|
|
|
|
if (WaveInOpen) {
|
|
|
|
SDL_CloseAudioDevice(devID);
|
|
|
|
}
|
|
|
|
devID = SDL_OpenAudioDevice(NULL, 1, &fmt, NULL, SDL_AUDIO_ALLOW_ANY_CHANGE);
|
|
|
|
if (devID <= 0) {
|
|
|
|
BX_PANIC(("SDL_OpenAudioDevive() failed"));
|
|
|
|
WaveInOpen = 0;
|
|
|
|
return BX_SOUNDLOW_ERR;
|
|
|
|
} else {
|
|
|
|
if (fmt.freq != param->samplerate) {
|
|
|
|
param->samplerate = fmt.freq;
|
|
|
|
BX_INFO(("changed sample rate to %d", fmt.freq));
|
|
|
|
}
|
|
|
|
WaveInOpen = 1;
|
|
|
|
}
|
|
|
|
SDL_PauseAudioDevice(devID, 0);
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_soundlow_wavein_sdl2_c::getwavepacket(int length, Bit8u data[])
|
|
|
|
{
|
|
|
|
SDL_DequeueAudio(devID, data, length);
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bx_soundlow_wavein_sdl2_c::stopwaverecord()
|
|
|
|
{
|
|
|
|
SDL_PauseAudioDevice(devID, 1);
|
|
|
|
if (record_timer_index != BX_NULL_TIMER_HANDLE) {
|
|
|
|
bx_pc_system.deactivate_timer(record_timer_index);
|
|
|
|
}
|
|
|
|
return BX_SOUNDLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_soundlow_wavein_sdl2_c::record_timer_handler(void *this_ptr)
|
|
|
|
{
|
|
|
|
bx_soundlow_wavein_sdl2_c *class_ptr = (bx_soundlow_wavein_sdl2_c *) this_ptr;
|
|
|
|
|
|
|
|
class_ptr->record_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_soundlow_wavein_sdl2_c::record_timer(void)
|
|
|
|
{
|
|
|
|
record_handler(this, record_packet_size);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-13 14:30:46 +03:00
|
|
|
// bx_sound_sdl_c class implemenzation
|
|
|
|
|
|
|
|
bx_sound_sdl_c::bx_sound_sdl_c()
|
|
|
|
:bx_sound_lowlevel_c()
|
|
|
|
{
|
|
|
|
// nothing here yet
|
|
|
|
}
|
|
|
|
|
|
|
|
bx_soundlow_waveout_c* bx_sound_sdl_c::get_waveout()
|
|
|
|
{
|
|
|
|
if (waveout == NULL) {
|
|
|
|
waveout = new bx_soundlow_waveout_sdl_c();
|
|
|
|
}
|
|
|
|
return waveout;
|
|
|
|
}
|
|
|
|
|
2017-02-08 21:48:49 +03:00
|
|
|
#if BX_HAVE_SDL2_AUDIO_CAPTURE
|
|
|
|
bx_soundlow_wavein_c* bx_sound_sdl_c::get_wavein()
|
|
|
|
{
|
|
|
|
if (wavein == NULL) {
|
|
|
|
wavein = new bx_soundlow_wavein_sdl2_c();
|
|
|
|
}
|
|
|
|
return wavein;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-24 00:32:34 +03:00
|
|
|
#endif // BX_HAVE_SOUND_SDL
|