2011-08-01 19:12:44 +00:00
|
|
|
#include "Supermodel.h"
|
|
|
|
|
|
|
|
#ifdef SUPERMODEL_OSX
|
2011-07-20 21:14:00 +00:00
|
|
|
#include <SDL/SDL.h>
|
2011-08-01 19:12:44 +00:00
|
|
|
#include <SDL/SDL_audio.h>
|
|
|
|
#else
|
2011-07-20 21:14:00 +00:00
|
|
|
#include <SDL.h>
|
2011-08-01 19:12:44 +00:00
|
|
|
#include <SDL_audio.h>
|
2011-07-20 21:14:00 +00:00
|
|
|
#endif
|
|
|
|
|
2011-08-01 19:12:44 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
// Model3 audio output is 44.1KHz 2-channel sound and frame rate is 60fps
|
|
|
|
#define SAMPLE_RATE 44100
|
|
|
|
#define NUM_CHANNELS 2
|
|
|
|
#define SUPERMODEL_FPS 60
|
|
|
|
|
|
|
|
#define BYTES_PER_SAMPLE (NUM_CHANNELS * sizeof(INT16))
|
|
|
|
#define SAMPLES_PER_FRAME (SAMPLE_RATE / SUPERMODEL_FPS)
|
2011-07-20 21:14:00 +00:00
|
|
|
#define BYTES_PER_FRAME (SAMPLES_PER_FRAME * BYTES_PER_SAMPLE)
|
|
|
|
|
2011-08-01 19:12:44 +00:00
|
|
|
#define MAX_LATENCY 100
|
2011-07-20 21:14:00 +00:00
|
|
|
|
|
|
|
static bool enabled = true; // True if sound output is enabled
|
|
|
|
static unsigned latency = 20; // Audio latency to use (ie size of audio buffer) as percentage of max buffer size
|
2011-08-07 23:09:18 +00:00
|
|
|
static bool underRunLoop = true; // True if should loop back to beginning of buffer on under-run, otherwise sound is just skipped
|
2011-07-20 21:14:00 +00:00
|
|
|
|
|
|
|
static unsigned playSamples = 512; // Size (in samples) of callback play buffer
|
|
|
|
|
|
|
|
static UINT32 audioBufferSize = 0; // Size (in bytes) of audio buffer
|
|
|
|
static INT8 *audioBuffer = NULL; // Audio buffer
|
|
|
|
|
|
|
|
static UINT32 writePos = 0; // Current position at which writing into buffer
|
|
|
|
static UINT32 playPos = 0; // Current position at which playing data in buffer via callback
|
|
|
|
|
|
|
|
static bool writeWrapped = false; // True if write position has wrapped around at end of buffer but play position has not done so yet
|
|
|
|
|
|
|
|
static unsigned underRuns = 0; // Number of buffer under-runs that have occured
|
|
|
|
static unsigned overRuns = 0; // Number of buffer over-runs that have occured
|
|
|
|
|
|
|
|
static void PlayCallback(void *data, Uint8 *stream, int len)
|
|
|
|
{
|
|
|
|
//printf("PlayCallback(%d)\n", len);
|
|
|
|
|
2011-08-07 23:09:18 +00:00
|
|
|
// Get current write position and adjust it if write has wrapped but play position has not
|
2011-07-20 21:14:00 +00:00
|
|
|
UINT32 adjWritePos = writePos;
|
|
|
|
if (writeWrapped)
|
|
|
|
adjWritePos += audioBufferSize;
|
|
|
|
|
|
|
|
// Check if play position overlaps write position (ie buffer under-run)
|
2011-08-07 23:09:18 +00:00
|
|
|
if (playPos + len > adjWritePos)
|
2011-07-20 21:14:00 +00:00
|
|
|
{
|
|
|
|
//printf("Audio buffer under-run in PlayCallback\n");
|
|
|
|
underRuns++;
|
2011-08-07 23:09:18 +00:00
|
|
|
|
|
|
|
// See what action to take on under-run
|
|
|
|
if (underRunLoop)
|
|
|
|
{
|
|
|
|
// If loop, then move play position back to beginning of data in buffer
|
|
|
|
playPos = adjWritePos + BYTES_PER_FRAME;
|
|
|
|
|
|
|
|
// Check if play position has moved past end of buffer
|
|
|
|
if (playPos >= audioBufferSize)
|
2011-08-31 22:30:45 +00:00
|
|
|
// If so, wrap it around to beginning again (but keep write wrapped flag as before)
|
2011-08-07 23:09:18 +00:00
|
|
|
playPos -= audioBufferSize;
|
|
|
|
else
|
|
|
|
// Otherwise, set write wrapped flag as will now appear as if write has wrapped but play position has not
|
|
|
|
writeWrapped = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Otherwise, just copy silence to audio output stream and exit
|
|
|
|
memset(stream, 0, len);
|
|
|
|
return;
|
|
|
|
}
|
2011-07-20 21:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INT8* src1;
|
|
|
|
INT8* src2;
|
|
|
|
UINT32 len1;
|
|
|
|
UINT32 len2;
|
|
|
|
|
|
|
|
// Check if play region extends past end of buffer
|
|
|
|
if (playPos + len > audioBufferSize)
|
|
|
|
{
|
|
|
|
// If so, split play region into two
|
|
|
|
src1 = audioBuffer + playPos;
|
|
|
|
src2 = audioBuffer;
|
|
|
|
len1 = audioBufferSize - playPos;
|
|
|
|
len2 = len - len1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Otherwise, just copy whole region
|
|
|
|
src1 = audioBuffer + playPos;
|
|
|
|
src2 = 0;
|
|
|
|
len1 = len;
|
|
|
|
len2 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if audio is enabled
|
|
|
|
if (enabled)
|
|
|
|
{
|
2011-08-07 23:09:18 +00:00
|
|
|
// If so, copy play region into audio output stream
|
2011-07-20 21:14:00 +00:00
|
|
|
memcpy(stream, src1, len1);
|
|
|
|
|
2011-08-07 23:09:18 +00:00
|
|
|
// Also, if not looping on under-runs then blank region out
|
|
|
|
if (!underRunLoop)
|
|
|
|
memset(src1, 0, len1);
|
|
|
|
|
2011-07-20 21:14:00 +00:00
|
|
|
if (len2)
|
|
|
|
{
|
2011-08-07 23:09:18 +00:00
|
|
|
// If region was split into two, copy second half into audio output stream as well
|
2011-07-20 21:14:00 +00:00
|
|
|
memcpy(stream + len1, src2, len2);
|
2011-08-07 23:09:18 +00:00
|
|
|
|
|
|
|
// Also, if not looping on under-runs then blank region out
|
|
|
|
if (!underRunLoop)
|
|
|
|
memset(src2, 0, len2);
|
2011-07-20 21:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// Otherwise, just copy silence to audio output stream
|
|
|
|
memset(stream, 0, len);
|
|
|
|
|
|
|
|
// Move play position forward for next time
|
|
|
|
playPos += len;
|
|
|
|
|
|
|
|
// Check if play position has moved past end of buffer
|
|
|
|
if (playPos >= audioBufferSize)
|
|
|
|
{
|
2011-08-07 23:09:18 +00:00
|
|
|
// If so, wrap it around to beginning again and reset write wrapped flag
|
2011-07-20 21:14:00 +00:00
|
|
|
playPos -= audioBufferSize;
|
|
|
|
writeWrapped = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MixChannels(unsigned numSamples, INT16 *leftBuffer, INT16 *rightBuffer, void *dest)
|
|
|
|
{
|
|
|
|
INT16 *p = (INT16*)dest;
|
2011-09-01 06:43:01 +00:00
|
|
|
|
2011-07-20 21:14:00 +00:00
|
|
|
#if (NUM_CHANNELS == 1)
|
2011-09-01 06:43:01 +00:00
|
|
|
for (unsigned i = 0; i < numSamples; i++)
|
|
|
|
*p++ = leftBuffer[i] + rightBuffer[i]; // TODO: these should probably be clipped!
|
2011-07-20 21:14:00 +00:00
|
|
|
#else
|
2011-09-01 06:43:01 +00:00
|
|
|
if (g_Config.flipStereo) // swap left and right channels
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < numSamples; i++)
|
|
|
|
{
|
|
|
|
*p++ = rightBuffer[i];
|
|
|
|
*p++ = leftBuffer[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // stereo as God intended!
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < numSamples; i++)
|
|
|
|
{
|
|
|
|
*p++ = leftBuffer[i];
|
|
|
|
*p++ = rightBuffer[i];
|
|
|
|
}
|
2011-07-20 21:14:00 +00:00
|
|
|
}
|
2011-09-01 06:43:01 +00:00
|
|
|
#endif // NUM_CHANNELS
|
|
|
|
}
|
|
|
|
|
|
|
|
static void LogAudioInfo(SDL_AudioSpec *fmt)
|
|
|
|
{
|
|
|
|
InfoLog("Audio device information:");
|
|
|
|
InfoLog(" Frequency: %d", fmt->freq);
|
|
|
|
InfoLog(" Channels: %d", fmt->channels);
|
|
|
|
InfoLog("Sample Format: %d", fmt->format);
|
|
|
|
InfoLog("");
|
2011-07-20 21:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL OpenAudio()
|
|
|
|
{
|
|
|
|
// Initialize SDL audio sub-system
|
|
|
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO) != 0)
|
2011-08-07 23:09:18 +00:00
|
|
|
return ErrorLog("Unable to initialize SDL audio sub-system: %s\n", SDL_GetError());
|
2011-07-20 21:14:00 +00:00
|
|
|
|
|
|
|
// Set up audio specification
|
|
|
|
SDL_AudioSpec fmt;
|
|
|
|
memset(&fmt, 0, sizeof(SDL_AudioSpec));
|
|
|
|
fmt.freq = SAMPLE_RATE;
|
|
|
|
fmt.channels = NUM_CHANNELS;
|
|
|
|
fmt.format = AUDIO_S16SYS;
|
|
|
|
fmt.samples = playSamples;
|
|
|
|
fmt.callback = PlayCallback;
|
|
|
|
|
|
|
|
// Try opening SDL audio output with that specification
|
|
|
|
SDL_AudioSpec obtained;
|
|
|
|
if (SDL_OpenAudio(&fmt, &obtained) < 0)
|
2011-08-07 23:09:18 +00:00
|
|
|
return ErrorLog("Unable to open 44.1KHz 2-channel audio with SDL: %s\n", SDL_GetError());
|
2011-09-01 06:43:01 +00:00
|
|
|
LogAudioInfo(&obtained);
|
|
|
|
|
|
|
|
// Check if obtained format is what we really requested
|
|
|
|
if ((obtained.freq!=fmt.freq) || (obtained.channels!=fmt.channels) || (obtained.format!=fmt.format))
|
|
|
|
ErrorLog("Incompatible audio settings (44.1KHz, 16-bit required). Check drivers!\n");
|
|
|
|
|
2011-07-20 21:14:00 +00:00
|
|
|
// Check what buffer sample size was actually obtained, and use that
|
|
|
|
playSamples = obtained.samples;
|
|
|
|
|
|
|
|
// Create audio buffer
|
|
|
|
audioBufferSize = SAMPLE_RATE * BYTES_PER_SAMPLE * latency / MAX_LATENCY;
|
|
|
|
int roundBuffer = 2 * playSamples;
|
|
|
|
audioBufferSize = max<int>(roundBuffer, (audioBufferSize / roundBuffer) * roundBuffer);
|
2011-08-07 23:09:18 +00:00
|
|
|
audioBuffer = new(std::nothrow) INT8[audioBufferSize];
|
|
|
|
if (audioBuffer == NULL)
|
|
|
|
{
|
|
|
|
float audioBufMB = (float)audioBufferSize / (float)0x100000;
|
|
|
|
return ErrorLog("Insufficient memory for audio latency buffer (need %1.1f MB).", audioBufMB);
|
|
|
|
}
|
2011-07-20 21:14:00 +00:00
|
|
|
memset(audioBuffer, 0, sizeof(INT8) * audioBufferSize);
|
|
|
|
|
|
|
|
// Set initial play position to be beginning of buffer and initial write position to be half-way into buffer
|
|
|
|
playPos = 0;
|
|
|
|
writePos = (BYTES_PER_FRAME + audioBufferSize) / 2;
|
|
|
|
writeWrapped = false;
|
|
|
|
|
|
|
|
// Reset counters
|
|
|
|
underRuns = 0;
|
|
|
|
overRuns = 0;
|
|
|
|
|
|
|
|
// Start audio playing
|
|
|
|
SDL_PauseAudio(0);
|
|
|
|
return OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputAudio(unsigned numSamples, INT16 *leftBuffer, INT16 *rightBuffer)
|
|
|
|
{
|
|
|
|
//printf("OutputAudio(%u)\n", numSamples);
|
|
|
|
|
2011-08-01 19:12:44 +00:00
|
|
|
UINT32 bytesRemaining;
|
|
|
|
UINT32 bytesToCopy;
|
|
|
|
INT16 *src;
|
|
|
|
|
2011-07-20 21:14:00 +00:00
|
|
|
// Number of samples should never be more than max number of samples per frame
|
|
|
|
if (numSamples > SAMPLES_PER_FRAME)
|
|
|
|
numSamples = SAMPLES_PER_FRAME;
|
|
|
|
|
|
|
|
// Mix together left and right channels into single chunk of data
|
|
|
|
INT16 mixBuffer[NUM_CHANNELS * SAMPLES_PER_FRAME];
|
|
|
|
MixChannels(numSamples, leftBuffer, rightBuffer, mixBuffer);
|
|
|
|
|
|
|
|
// Lock SDL audio callback so that it doesn't interfere with following code
|
|
|
|
SDL_LockAudio();
|
|
|
|
|
|
|
|
// Calculate number of bytes for current sound chunk
|
|
|
|
UINT32 numBytes = numSamples * BYTES_PER_SAMPLE;
|
|
|
|
|
|
|
|
// Get end of current play region (writing must occur past this point)
|
|
|
|
UINT32 playEndPos = playPos + BYTES_PER_FRAME;
|
|
|
|
|
|
|
|
// Undo any wrap-around of the write position that may have occured to create following ordering: playPos < playEndPos < writePos
|
2011-08-07 23:09:18 +00:00
|
|
|
if (playEndPos > writePos && writeWrapped)
|
2011-07-20 21:14:00 +00:00
|
|
|
writePos += audioBufferSize;
|
|
|
|
|
2011-08-07 23:09:18 +00:00
|
|
|
// Check if play region has caught up with write position and now overlaps it (ie buffer under-run)
|
|
|
|
if (playEndPos > writePos)
|
2011-07-20 21:14:00 +00:00
|
|
|
{
|
|
|
|
//printf("Audio buffer under-run in OutputAudio\n");
|
|
|
|
underRuns++;
|
2011-08-07 23:09:18 +00:00
|
|
|
|
|
|
|
// See what action to take on under-run
|
|
|
|
if (underRunLoop)
|
|
|
|
{
|
|
|
|
// If loop, then move play position back to beginning of data in buffer
|
|
|
|
playPos = writePos + numBytes + BYTES_PER_FRAME;
|
|
|
|
|
|
|
|
// Check if play position has moved past end of buffer
|
|
|
|
if (playPos >= audioBufferSize)
|
2011-08-31 22:30:45 +00:00
|
|
|
// If so, wrap it around to beginning again (but keep write wrapped flag as before)
|
2011-08-07 23:09:18 +00:00
|
|
|
playPos -= audioBufferSize;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Otherwise, set write wrapped flag as will now appear as if write has wrapped but play position has not
|
|
|
|
writeWrapped = true;
|
|
|
|
writePos += audioBufferSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Otherwise, bump write position forward in chunks until it is past end of play region
|
|
|
|
do
|
|
|
|
{
|
|
|
|
writePos += numBytes;
|
|
|
|
}
|
|
|
|
while (playEndPos > writePos);
|
|
|
|
}
|
2011-07-20 21:14:00 +00:00
|
|
|
}
|
|
|
|
|
2011-08-07 23:09:18 +00:00
|
|
|
// Check if write position has caught up with play region and now overlaps it (ie buffer over-run)
|
2011-07-20 21:14:00 +00:00
|
|
|
if (writePos + numBytes > playPos + audioBufferSize)
|
|
|
|
{
|
|
|
|
//printf("Audio buffer over-run in OutputAudio\n");
|
|
|
|
overRuns++;
|
2011-08-07 23:09:18 +00:00
|
|
|
|
|
|
|
// If so, then discard current chunk of data
|
2011-07-20 21:14:00 +00:00
|
|
|
goto Finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if write position has moved past end of buffer
|
|
|
|
if (writePos >= audioBufferSize)
|
|
|
|
{
|
2011-08-07 23:09:18 +00:00
|
|
|
// If so, wrap it around to beginning again and set write wrapped flag
|
2011-07-20 21:14:00 +00:00
|
|
|
writePos -= audioBufferSize;
|
|
|
|
writeWrapped = true;
|
|
|
|
}
|
|
|
|
|
2011-08-01 19:12:44 +00:00
|
|
|
src = mixBuffer;
|
2011-07-20 21:14:00 +00:00
|
|
|
INT8 *dst1;
|
|
|
|
INT8 *dst2;
|
|
|
|
UINT32 len1;
|
|
|
|
UINT32 len2;
|
|
|
|
|
|
|
|
// Check if write region extends past end of buffer
|
|
|
|
if (writePos + numBytes > audioBufferSize)
|
|
|
|
{
|
|
|
|
// If so, split write region into two
|
|
|
|
dst1 = audioBuffer + writePos;
|
|
|
|
dst2 = audioBuffer;
|
|
|
|
len1 = audioBufferSize - writePos;
|
|
|
|
len2 = numBytes - len1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Otherwise, just copy whole region
|
|
|
|
dst1 = audioBuffer + writePos;
|
|
|
|
dst2 = 0;
|
|
|
|
len1 = numBytes;
|
|
|
|
len2 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy chunk to write position in buffer
|
2011-08-01 19:12:44 +00:00
|
|
|
bytesRemaining = numBytes;
|
|
|
|
bytesToCopy = (bytesRemaining > len1 ? len1 : bytesRemaining);
|
2011-07-20 21:14:00 +00:00
|
|
|
memcpy(dst1, src, bytesToCopy);
|
|
|
|
|
|
|
|
// Adjust for number of bytes copied
|
|
|
|
bytesRemaining -= bytesToCopy;
|
|
|
|
src = (INT16*)((UINT8*)src + bytesToCopy);
|
|
|
|
|
|
|
|
if (bytesRemaining)
|
|
|
|
{
|
|
|
|
// If write region was split into two, copy second half of chunk into buffer as well
|
|
|
|
bytesToCopy = (bytesRemaining > len2 ? len2 : bytesRemaining);
|
|
|
|
memcpy(dst2, src, bytesToCopy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move write position forward for next time
|
|
|
|
writePos += numBytes;
|
|
|
|
|
|
|
|
// Check if write position has moved past end of buffer
|
|
|
|
if (writePos >= audioBufferSize)
|
|
|
|
{
|
2011-08-07 23:09:18 +00:00
|
|
|
// If so, wrap it around to beginning again and set write wrapped flag
|
2011-07-20 21:14:00 +00:00
|
|
|
writePos -= audioBufferSize;
|
|
|
|
writeWrapped = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Finish:
|
|
|
|
// Unlock SDL audio callback
|
|
|
|
SDL_UnlockAudio();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseAudio()
|
|
|
|
{
|
|
|
|
// Close SDL audio output
|
|
|
|
SDL_CloseAudio();
|
|
|
|
|
|
|
|
// Delete audio buffer
|
|
|
|
if (audioBuffer != NULL)
|
|
|
|
{
|
|
|
|
delete[] audioBuffer;
|
|
|
|
audioBuffer = NULL;
|
|
|
|
}
|
|
|
|
}
|