Simplified the SDL audio driver by using a generic ring buffer (from Gambatte). Changed the A/V max delay and sound update framerate to saner values. Emulation smoothness and sound quality have improved on my system. Please report any issue.

This commit is contained in:
bgk 2008-12-30 14:34:33 +00:00
parent 9383abc719
commit 973c7123c9
10 changed files with 248 additions and 154 deletions

View File

@ -348,8 +348,11 @@ static void end_frame( blip_time_t time )
void flush_samples(Multi_Buffer * buffer)
{
// get the size in bytes of the sound driver buffer
int soundBufferLen = soundDriver->getBufferLength();
// We want to write the data frame by frame to support legacy audio drivers
// that don't use the length parameter of the write method.
// TODO: Update the Win32 audio drivers (DS, OAL, XA2), and flush all the
// samples at once to help reducing the audio delay on all platforms.
int soundBufferLen = ( soundSampleRate / 60 ) * 4;
// soundBufferLen should have a whole number of sample pairs
assert( soundBufferLen % (2 * sizeof *soundFinalWave) == 0 );

40
src/common/Array.h Normal file
View File

@ -0,0 +1,40 @@
/***************************************************************************
* Copyright (C) 2008 by Sindre Aam<EFBFBD>s *
* aamas@stud.ntnu.no *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License version 2 as *
* published by the Free Software Foundation. *
* *
* This program 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 version 2 for more details. *
* *
* You should have received a copy of the GNU General Public License *
* version 2 along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef ARRAY_H
#define ARRAY_H
#include <cstddef>
template<typename T>
class Array {
T *a;
std::size_t sz;
Array(const Array &ar);
public:
Array(const std::size_t size = 0) : a(size ? new T[size] : 0), sz(size) {}
~Array() { delete []a; }
void reset(const std::size_t size) { delete []a; a = size ? new T[size] : 0; sz = size; }
std::size_t size() const { return sz; }
operator T*() { return a; }
operator const T*() const { return a; }
};
#endif

112
src/common/RingBuffer.h Normal file
View File

@ -0,0 +1,112 @@
/***************************************************************************
* Copyright (C) 2008 by Sindre Aamås *
* aamas@stud.ntnu.no *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License version 2 as *
* published by the Free Software Foundation. *
* *
* This program 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 version 2 for more details. *
* *
* You should have received a copy of the GNU General Public License *
* version 2 along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef RINGBUFFER_H
#define RINGBUFFER_H
#include "Array.h"
#include <cstddef>
#include <algorithm>
#include <cstring>
template<typename T>
class RingBuffer {
Array<T> buf;
std::size_t sz;
std::size_t rpos;
std::size_t wpos;
public:
RingBuffer(const std::size_t sz_in = 0) : sz(0), rpos(0), wpos(0) { reset(sz_in); }
std::size_t avail() const {
return (wpos < rpos ? 0 : sz) + rpos - wpos - 1;
}
void clear() {
wpos = rpos = 0;
}
void fill(T value);
void read(T *out, std::size_t num);
void reset(std::size_t sz_in);
std::size_t size() const {
return sz - 1;
}
std::size_t used() const {
return (wpos < rpos ? sz : 0) + wpos - rpos;
}
void write(const T *in, std::size_t num);
};
template<typename T>
void RingBuffer<T>::fill(const T value) {
std::fill(buf + 0, buf + sz, value);
rpos = 0;
wpos = sz - 1;
}
template<typename T>
void RingBuffer<T>::read(T *out, std::size_t num) {
if (rpos + num > sz) {
const std::size_t n = sz - rpos;
std::memcpy(out, buf + rpos, n * sizeof(T));
rpos = 0;
num -= n;
out += n;
}
std::memcpy(out, buf + rpos, num * sizeof(T));
if ((rpos += num) == sz)
rpos = 0;
}
template<typename T>
void RingBuffer<T>::reset(const std::size_t sz_in) {
sz = sz_in + 1;
rpos = wpos = 0;
buf.reset(sz_in ? sz : 0);
}
template<typename T>
void RingBuffer<T>::write(const T *in, std::size_t num) {
if (wpos + num > sz) {
const std::size_t n = sz - wpos;
std::memcpy(buf + wpos, in, n * sizeof(T));
wpos = 0;
num -= n;
in += n;
}
std::memcpy(buf + wpos, in, num * sizeof(T));
if ((wpos += num) == sz)
wpos = 0;
}
#endif

View File

@ -55,12 +55,7 @@ public:
/**
* Write length bytes of data from the finalWave buffer to the driver output buffer.
*/
virtual void write(const u16 * finalWave, int length) = 0;
/**
* Return the size in bytes of the core sound buffer.
*/
virtual int getBufferLength() = 0;
virtual void write(u16 * finalWave, int length) = 0;
virtual void setThrottle(unsigned short throttle) { };
};

View File

@ -6,153 +6,120 @@
extern int emulating;
u8 SoundSDL::_buffer[_bufferTotalLen];
int SoundSDL::_readPosition;
int SoundSDL::_writePosition;
SDL_cond * SoundSDL::_cond;
SDL_mutex * SoundSDL::_mutex;
inline int SoundSDL::getBufferFree()
SoundSDL::SoundSDL():
_rbuf(0)
{
int ret = _readPosition - _writePosition - _bufferAlign;
if (ret < 0)
ret += _bufferTotalLen;
return ret;
}
inline int SoundSDL::getBufferUsed()
void SoundSDL::soundCallback(void *data, u8 *stream, int len)
{
int ret = _writePosition - _readPosition;
if (ret < 0)
ret += _bufferTotalLen;
return ret;
reinterpret_cast<SoundSDL*>(data)->read(reinterpret_cast<u16 *>(stream), len);
}
void SoundSDL::soundCallback(void *,u8 *stream,int len)
void SoundSDL::read(u16 * stream, int length)
{
if (len <= 0 || !emulating)
return;
if (length <= 0 || !emulating)
return;
SDL_mutexP(_mutex);
const int nAvail = getBufferUsed();
if (len > nAvail)
len = nAvail;
const int nAvail2 = _bufferTotalLen - _readPosition;
if (len >= nAvail2) {
memcpy(stream, &_buffer[_readPosition], nAvail2);
_readPosition = 0;
stream += nAvail2;
len -= nAvail2;
}
if (len > 0) {
memcpy(stream, &_buffer[_readPosition], len);
_readPosition = (_readPosition + len) % _bufferTotalLen;
stream += len;
}
SDL_CondSignal(_cond);
SDL_mutexV(_mutex);
SDL_mutexP(_mutex);
_rbuf.read(stream, std::min(static_cast<std::size_t>(length) / 2, _rbuf.used()));
SDL_CondSignal(_cond);
SDL_mutexV(_mutex);
}
void SoundSDL::write(const u16 * finalWave, int length)
void SoundSDL::write(u16 * finalWave, int length)
{
if (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING)
{
SDL_PauseAudio(0);
}
if (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING)
SDL_PauseAudio(0);
int remain = length;
const u8 *wave = reinterpret_cast<const u8 *>(finalWave);
SDL_mutexP(_mutex);
SDL_mutexP(_mutex);
unsigned int samples = length / 4;
int n;
while (remain >= (n = getBufferFree())) {
const int nAvail = (_bufferTotalLen - _writePosition) < n ? (_bufferTotalLen - _writePosition) : n;
memcpy(&_buffer[_writePosition], wave, nAvail);
_writePosition = (_writePosition + nAvail) % _bufferTotalLen;
wave += nAvail;
remain -= nAvail;
std::size_t avail;
while ((avail = _rbuf.avail() / 2) < samples)
{
_rbuf.write(finalWave, avail * 2);
if (!emulating || speedup || systemThrottle) {
SDL_mutexV(_mutex);
return;
}
SDL_CondWait(_cond, _mutex);
}
finalWave += avail * 2;
samples -= avail;
const int nAvail = _bufferTotalLen - _writePosition;
if (remain >= nAvail) {
memcpy(&_buffer[_writePosition], wave, nAvail);
_writePosition = 0;
wave += nAvail;
remain -= nAvail;
}
if (remain > 0) {
memcpy(&_buffer[_writePosition], wave, remain);
_writePosition = (_writePosition + remain) % _bufferTotalLen;
}
SDL_mutexV(_mutex);
// If emulating and not in speed up mode, synchronize to audio
// by waiting till there is enough room in the buffer
if (emulating && !speedup && !systemThrottle)
{
SDL_CondWait(_cond, _mutex);
}
else
{
// Drop the remaining of the audio data
SDL_mutexV(_mutex);
return;
}
}
_rbuf.write(finalWave, samples * 2);
SDL_mutexV(_mutex);
}
bool SoundSDL::init(long sampleRate)
{
SDL_AudioSpec audio;
SDL_AudioSpec audio;
audio.freq = sampleRate;
audio.format = AUDIO_S16SYS;
audio.channels = 2;
audio.samples = sampleRate / 60;
audio.callback = soundCallback;
audio.userdata = this;
_bufferLen = sampleRate * 4 / 60;
if(SDL_OpenAudio(&audio, NULL))
{
fprintf(stderr,"Failed to open audio: %s\n", SDL_GetError());
return false;
}
audio.freq = sampleRate;
audio.format = AUDIO_S16SYS;
audio.channels = 2;
audio.samples = 1024;
audio.callback = soundCallback;
audio.userdata = NULL;
if(SDL_OpenAudio(&audio, NULL)) {
fprintf(stderr,"Failed to open audio: %s\n", SDL_GetError());
return false;
}
_rbuf.reset(_delay * sampleRate * 2);
_cond = SDL_CreateCond();
_mutex = SDL_CreateMutex();
_readPosition = _writePosition = 0;
return true;
_cond = SDL_CreateCond();
_mutex = SDL_CreateMutex();
return true;
}
SoundSDL::~SoundSDL()
{
SDL_mutexP(_mutex);
int iSave = emulating;
emulating = 0;
SDL_CondSignal(_cond);
SDL_mutexV(_mutex);
SDL_mutexP(_mutex);
int iSave = emulating;
emulating = 0;
SDL_CondSignal(_cond);
SDL_mutexV(_mutex);
SDL_DestroyCond(_cond);
_cond = NULL;
SDL_DestroyCond(_cond);
_cond = NULL;
SDL_DestroyMutex(_mutex);
_mutex = NULL;
SDL_DestroyMutex(_mutex);
_mutex = NULL;
SDL_CloseAudio();
SDL_CloseAudio();
emulating = iSave;
emulating = iSave;
}
void SoundSDL::pause()
{
SDL_PauseAudio(1);
SDL_PauseAudio(1);
}
void SoundSDL::resume()
{
SDL_PauseAudio(0);
SDL_PauseAudio(0);
}
void SoundSDL::reset()
{
}
int SoundSDL::getBufferLength()
{
return _bufferLen;
}

View File

@ -19,35 +19,31 @@
#define __VBA_SOUND_SDL_H__
#include "SoundDriver.h"
#include "RingBuffer.h"
class SoundSDL: public SoundDriver
{
public:
SoundSDL();
virtual ~SoundSDL();
virtual bool init(long sampleRate);
virtual void pause();
virtual void reset();
virtual void resume();
virtual void write(const u16 * finalWave, int length);
virtual int getBufferLength();
virtual void write(u16 * finalWave, int length);
private:
static const int _sampleCount = 4096;
static const int _bufferAlign = 4;
static const int _bufferCapacity = _sampleCount * 2;
static const int _bufferTotalLen = _bufferCapacity + _bufferAlign;
RingBuffer<u16> _rbuf;
static u8 _buffer[_bufferTotalLen];
static int _readPosition;
static int _writePosition;
static SDL_cond * _cond;
static SDL_mutex * _mutex;
int _bufferLen;
SDL_cond * _cond;
SDL_mutex * _mutex;
static int getBufferFree();
static int getBufferUsed();
static void soundCallback(void *, u8 *stream, int len);
// Hold up to 100 ms of data in the ring buffer
static const float _delay = 0.1f;
static void soundCallback(void *data, u8 *stream, int length);
virtual void read(u16 * stream, int length);
};
#endif // __VBA_SOUND_SDL_H__

View File

@ -40,7 +40,7 @@ public:
// a new time frame at the end of the current frame.
void end_frame( blip_time_t time );
// Reads at most 'max_samples' out of buffer into 'dest', removing them from from
// Reads at most 'max_samples' out of buffer into 'dest', removing them from
// the buffer. Returns number of samples actually read and removed. If stereo is
// true, increments 'dest' one extra time after writing each sample, to allow
// easy interleving of two channels into a stereo output buffer.

View File

@ -38,8 +38,7 @@ public:
void pause(); // pause the secondary sound buffer
void reset(); // stop and reset the secondary sound buffer
void resume(); // resume the secondary sound buffer
void write(const u16 * finalWave, int length); // write the emulated sound to the secondary sound buffer
virtual int getBufferLength();
void write(u16 * finalWave, int length); // write the emulated sound to the secondary sound buffer
};
@ -224,7 +223,7 @@ void DirectSound::resume()
}
void DirectSound::write(const u16 * finalWave, int length)
void DirectSound::write(u16 * finalWave, int length)
{
if(!pDirectSound) return;
@ -307,11 +306,6 @@ void DirectSound::write(const u16 * finalWave, int length)
}
}
int DirectSound::getBufferLength()
{
return soundBufferLen;
}
SoundDriver *newDirectSound()
{
return new DirectSound();

View File

@ -41,8 +41,7 @@ public:
void pause(); // pause the secondary sound buffer
void reset(); // stop and reset the secondary sound buffer
void resume(); // play/resume the secondary sound buffer
void write(const u16 * finalWave, int length); // write the emulated sound to a sound buffer
virtual int getBufferLength();
void write(u16 * finalWave, int length); // write the emulated sound to a sound buffer
private:
OPENALFNTABLE ALFunction;
@ -242,7 +241,7 @@ void OpenAL::reset()
}
void OpenAL::write(const u16 * finalWave, int length)
void OpenAL::write(u16 * finalWave, int length)
{
if( !initialized ) return;
winlog( "OpenAL::write\n" );
@ -321,11 +320,6 @@ void OpenAL::write(const u16 * finalWave, int length)
}
}
int OpenAL::getBufferLength()
{
return soundBufferLen;
}
SoundDriver *newOpenAL()
{
winlog( "newOpenAL\n" );

View File

@ -59,7 +59,7 @@ public:
bool init(long sampleRate);
// Sound Data Feed
void write(const u16 * finalWave, int length);
void write(u16 * finalWave, int length);
// Play Control
void pause();
@ -69,8 +69,6 @@ public:
// Configuration Changes
void setThrottle( unsigned short throttle );
virtual int getBufferLength();
private:
bool failed;
bool initialized;
@ -280,7 +278,7 @@ bool XAudio2_Output::init(long sampleRate)
}
void XAudio2_Output::write(const u16 * finalWave, int length)
void XAudio2_Output::write(u16 * finalWave, int length)
{
if( !initialized || failed ) return;
@ -373,11 +371,6 @@ void XAudio2_Output::setThrottle( unsigned short throttle )
ASSERT( hr == S_OK );
}
int XAudio2_Output::getBufferLength()
{
return soundBufferLen;
}
SoundDriver *newXAudio2_Output()
{
return new XAudio2_Output();