2017-02-26 16:00:41 +00:00
|
|
|
#include "sdl-speaker.h"
|
|
|
|
#include <pthread.h>
|
2018-01-03 01:28:47 +00:00
|
|
|
#include <unistd.h>
|
2020-07-13 23:01:41 +00:00
|
|
|
#include <fcntl.h> // for open()
|
2017-02-26 16:00:41 +00:00
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include <SDL.h>
|
|
|
|
#include <SDL_thread.h>
|
|
|
|
};
|
|
|
|
|
2020-07-13 23:01:41 +00:00
|
|
|
// What values do we use for logical speaker-high and speaker-low?
|
2020-12-29 03:57:11 +00:00
|
|
|
#define HIGHVAL ((0x4FFF) >> (15-g_volume))
|
|
|
|
#define LOWVAL (-((0x4FFF) >> (15-g_volume)))
|
2020-07-13 23:01:41 +00:00
|
|
|
|
2017-02-27 01:34:38 +00:00
|
|
|
#include "globals.h"
|
|
|
|
|
2020-07-15 14:19:41 +00:00
|
|
|
#define SDLSIZE (2048)
|
2020-07-13 23:01:41 +00:00
|
|
|
// But we want to keep more than just that, so we can fill it full every time
|
|
|
|
#define CACHEMULTIPLIER 2
|
2019-02-20 22:49:51 +00:00
|
|
|
|
2020-07-15 14:19:41 +00:00
|
|
|
#define WATERLEVEL SDLSIZE
|
|
|
|
|
2020-08-02 14:02:35 +00:00
|
|
|
#define AUDIO_SAMPLE_RATE_EXACT 44100
|
|
|
|
|
2017-02-26 16:00:41 +00:00
|
|
|
// FIXME: Globals; ick.
|
2018-01-03 01:28:47 +00:00
|
|
|
static volatile uint32_t bufIdx = 0;
|
2020-07-15 00:43:41 +00:00
|
|
|
static volatile short soundBuf[CACHEMULTIPLIER*SDLSIZE];
|
2017-12-31 22:21:34 +00:00
|
|
|
static pthread_mutex_t togmutex = PTHREAD_MUTEX_INITIALIZER;
|
2020-08-02 14:02:35 +00:00
|
|
|
static volatile uint64_t skippedSamples = 0;
|
2020-07-15 00:43:41 +00:00
|
|
|
#define SAMPLEBYTES sizeof(short)
|
2017-02-26 16:00:41 +00:00
|
|
|
|
2020-07-13 23:01:41 +00:00
|
|
|
volatile uint8_t audioRunning = 0;
|
2020-08-02 13:06:15 +00:00
|
|
|
volatile int64_t lastFilledTime = 0;
|
2020-07-13 23:01:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Debugging by writing a wav file with the sound output...
|
|
|
|
//#define DEBUG_OUT_WAV
|
|
|
|
#ifdef DEBUG_OUT_WAV
|
|
|
|
int outputFD = -1;
|
|
|
|
#endif
|
|
|
|
|
2017-02-26 16:00:41 +00:00
|
|
|
static void audioCallback(void *unused, Uint8 *stream, int len)
|
|
|
|
{
|
2020-07-13 23:01:41 +00:00
|
|
|
if (audioRunning==0)
|
|
|
|
audioRunning=1;
|
2019-02-20 22:49:51 +00:00
|
|
|
pthread_mutex_lock(&togmutex);
|
2018-02-07 15:20:26 +00:00
|
|
|
if (g_biosInterrupt) {
|
|
|
|
// While the BIOS is running, we don't put samples in the audio
|
|
|
|
// queue.
|
2020-07-13 23:01:41 +00:00
|
|
|
audioRunning = 0;
|
2020-07-15 00:43:41 +00:00
|
|
|
memset(stream, 0, SDLSIZE*SAMPLEBYTES);
|
2019-02-20 22:49:51 +00:00
|
|
|
pthread_mutex_unlock(&togmutex);
|
2018-02-07 15:20:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 14:19:41 +00:00
|
|
|
if (audioRunning==1 && bufIdx >= WATERLEVEL) {
|
2020-07-13 23:01:41 +00:00
|
|
|
// Fully up and running now; we got a full cache
|
|
|
|
audioRunning = 2;
|
|
|
|
} else if (audioRunning==1) {
|
|
|
|
// waiting for first fill; return an empty buffer.
|
2020-07-15 00:43:41 +00:00
|
|
|
memset(stream, 0, SDLSIZE*SAMPLEBYTES);
|
2020-12-28 19:23:47 +00:00
|
|
|
pthread_mutex_unlock(&togmutex);
|
2020-07-13 23:01:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:43:41 +00:00
|
|
|
static short lastKnownSample = 0; // saved for when the apple is quiescent
|
2019-02-20 22:49:51 +00:00
|
|
|
|
2020-08-02 14:02:35 +00:00
|
|
|
if (bufIdx >= SDLSIZE) { // technically 'len/SAMPLEBYTES' but it should always be constant I think?
|
2020-07-15 00:43:41 +00:00
|
|
|
memcpy(stream, (void *)soundBuf, SDLSIZE*SAMPLEBYTES);
|
|
|
|
lastKnownSample = stream[SDLSIZE-1];
|
2018-01-03 01:28:47 +00:00
|
|
|
|
2020-07-15 00:43:41 +00:00
|
|
|
if (bufIdx > SDLSIZE) {
|
2017-02-26 16:00:41 +00:00
|
|
|
// move the remaining data down
|
2020-07-15 00:43:41 +00:00
|
|
|
memcpy((void *)soundBuf, (void *)&soundBuf[SDLSIZE], (bufIdx - SDLSIZE + 1)*SAMPLEBYTES);
|
|
|
|
bufIdx -= SDLSIZE;
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-20 22:49:51 +00:00
|
|
|
if (bufIdx) {
|
|
|
|
// partial buffer exists
|
2020-07-15 00:43:41 +00:00
|
|
|
memcpy(stream, (void *)soundBuf, bufIdx*SAMPLEBYTES);
|
2020-07-14 00:28:33 +00:00
|
|
|
// and it's a partial underrun. Track the number of samples we skipped
|
|
|
|
// so we can keep the audio buffer in sync.
|
2020-07-15 00:43:41 +00:00
|
|
|
skippedSamples += SDLSIZE-bufIdx;
|
|
|
|
for (long i=0; i<SDLSIZE-bufIdx; i++) {
|
|
|
|
stream[bufIdx+i] = lastKnownSample;
|
|
|
|
}
|
2019-02-20 22:49:51 +00:00
|
|
|
bufIdx = 0;
|
|
|
|
} else {
|
2020-07-13 23:01:41 +00:00
|
|
|
// No big deal - buffer underrun might just mean nothing
|
|
|
|
// is trying to play audio right now.
|
2020-07-15 00:43:41 +00:00
|
|
|
skippedSamples += SDLSIZE;
|
2020-07-13 23:01:41 +00:00
|
|
|
|
2020-07-15 00:43:41 +00:00
|
|
|
memset(stream, 0, SDLSIZE*SAMPLEBYTES);
|
|
|
|
// memset(stream, lastKnownSample, SDLSIZE);
|
2020-07-13 23:01:41 +00:00
|
|
|
// Trend toward DC voltage = 0v
|
|
|
|
// if (lastKnownSample < 0x7F) lastKnownSample++;
|
|
|
|
// if (lastKnownSample >= 0x80) lastKnownSample--;
|
2018-01-10 13:05:14 +00:00
|
|
|
}
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
2020-07-13 23:01:41 +00:00
|
|
|
#ifdef DEBUG_OUT_WAV
|
|
|
|
if (outputFD == -1) {
|
|
|
|
outputFD = open("/tmp/out.wav", O_RDWR | O_CREAT | O_TRUNC, 0600);
|
|
|
|
unsigned char buf[44] = { 'R', 'I', 'F', 'F',
|
|
|
|
0xff,0xff,0xff,0, // size == 0 for now
|
|
|
|
'W', 'A', 'V', 'E',
|
|
|
|
'f', 'm', 't', ' ',
|
|
|
|
16,0,0,0, // no extensions
|
|
|
|
1,0, // PCM
|
|
|
|
1,0, // 1 channel
|
|
|
|
0x44, 0xAC, 0, 0, // 44100 Hz
|
|
|
|
0x44, 0xAC, 0, 0, // (sample rate * bits * channels)/8
|
|
|
|
1,0, // sample size (1 byte here b/c 1 channel @ 8bit)
|
|
|
|
8,0, // bits per sample
|
|
|
|
'd', 'a', 't', 'a',
|
|
|
|
0xff, 0xff, 0xff, 0, // size of data chunk
|
|
|
|
};
|
|
|
|
write(outputFD, buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:43:41 +00:00
|
|
|
write(outputFD, (void *)(stream), SDLSIZE*SAMPLEBYTES);
|
2020-07-13 23:01:41 +00:00
|
|
|
#endif
|
|
|
|
|
2019-02-20 22:49:51 +00:00
|
|
|
pthread_mutex_unlock(&togmutex);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 01:28:47 +00:00
|
|
|
SDLSpeaker::SDLSpeaker()
|
|
|
|
{
|
|
|
|
toggleState = false;
|
2018-02-07 15:20:26 +00:00
|
|
|
mixerValue = 0x80;
|
2018-01-03 01:28:47 +00:00
|
|
|
|
|
|
|
pthread_mutex_init(&togmutex, NULL);
|
2019-02-20 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SDLSpeaker::~SDLSpeaker()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDLSpeaker::begin()
|
|
|
|
{
|
2017-02-26 16:00:41 +00:00
|
|
|
SDL_AudioSpec audioDevice;
|
|
|
|
SDL_AudioSpec audioActual;
|
|
|
|
SDL_memset(&audioDevice, 0, sizeof(audioDevice));
|
2020-08-02 14:02:35 +00:00
|
|
|
audioDevice.freq = AUDIO_SAMPLE_RATE_EXACT; // count of 16-bit samples
|
2020-07-15 00:43:41 +00:00
|
|
|
audioDevice.format = AUDIO_S16;
|
2017-02-26 16:00:41 +00:00
|
|
|
audioDevice.channels = 1;
|
2020-07-15 00:43:41 +00:00
|
|
|
audioDevice.samples = SDLSIZE; // SDLSIZE 16-bit samples @ 44100Hz: 4096 is about 1/10th second out of sync
|
2017-02-26 16:00:41 +00:00
|
|
|
audioDevice.callback = audioCallback;
|
2018-02-07 15:20:26 +00:00
|
|
|
audioDevice.userdata = NULL;
|
2017-02-26 16:00:41 +00:00
|
|
|
|
2020-07-15 00:43:41 +00:00
|
|
|
memset((void *)&soundBuf[0], 0, CACHEMULTIPLIER*SDLSIZE*SAMPLEBYTES);
|
2020-07-13 23:01:41 +00:00
|
|
|
bufIdx = 0;
|
2020-07-14 01:10:23 +00:00
|
|
|
skippedSamples = 0;
|
|
|
|
audioRunning = 0;
|
2019-02-20 22:49:51 +00:00
|
|
|
|
2017-02-26 16:00:41 +00:00
|
|
|
SDL_OpenAudio(&audioDevice, &audioActual); // FIXME retval
|
|
|
|
printf("Actual: freq %d channels %d samples %d\n",
|
|
|
|
audioActual.freq, audioActual.channels, audioActual.samples);
|
2020-07-13 23:01:41 +00:00
|
|
|
// FIXME: if any of those don't match the orginal we're gonna be unhappy
|
2017-02-26 16:00:41 +00:00
|
|
|
SDL_PauseAudio(0);
|
|
|
|
}
|
|
|
|
|
2020-08-02 13:06:15 +00:00
|
|
|
void SDLSpeaker::toggle(int64_t c)
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
2017-12-31 22:21:34 +00:00
|
|
|
pthread_mutex_lock(&togmutex);
|
|
|
|
|
2020-08-02 14:02:35 +00:00
|
|
|
int64_t expectedCycleNumber = (float)c * (float)AUDIO_SAMPLE_RATE_EXACT / (float)g_speed;
|
2020-07-13 23:01:41 +00:00
|
|
|
if (lastFilledTime == 0) {
|
|
|
|
lastFilledTime = expectedCycleNumber;
|
|
|
|
}
|
2020-07-14 00:28:33 +00:00
|
|
|
// This subtracts skippedSamples because those were filled automatically
|
|
|
|
// by the audioCallback when we had no data.
|
2020-08-02 14:02:35 +00:00
|
|
|
int64_t audioBufferSamples = expectedCycleNumber - lastFilledTime - skippedSamples;
|
2020-07-14 00:28:33 +00:00
|
|
|
// If audioBufferSamples < 0, then we need to keep some
|
|
|
|
// skippedSamples for later; otherwise we can keep moving forward.
|
|
|
|
if (audioBufferSamples < 0) {
|
|
|
|
skippedSamples = -audioBufferSamples;
|
|
|
|
audioBufferSamples = 0;
|
|
|
|
} else {
|
|
|
|
// Otherwise we consumed them and can forget about it.
|
|
|
|
skippedSamples = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t newIdx = bufIdx + audioBufferSamples;
|
|
|
|
|
2020-07-13 23:01:41 +00:00
|
|
|
if (audioBufferSamples == 0) {
|
|
|
|
// If the toggle wouldn't result in at least 1 buffer sample change,
|
|
|
|
// then we'll blatantly skip it here. If this turns out to be
|
|
|
|
// a problem, we could try setting audioBufferSamples++ and then
|
|
|
|
// twiddle the lastFilledTime so it looks like it's more in the
|
|
|
|
// future, but I suspect that would mean missing more future events,
|
|
|
|
// just like we would have missed this one.
|
|
|
|
//
|
|
|
|
// But I think this is probably okay - because something that's
|
|
|
|
// toggling the speaker fast enough that our 44k audio can't keep
|
|
|
|
// up with the individual changes is likely to toggle again in a
|
|
|
|
// moment without significant distortion?
|
|
|
|
pthread_mutex_unlock(&togmutex);
|
|
|
|
return;
|
|
|
|
}
|
2019-02-20 22:49:51 +00:00
|
|
|
|
2020-07-15 00:43:41 +00:00
|
|
|
if (newIdx >= sizeof(soundBuf)/SAMPLEBYTES) {
|
|
|
|
printf("ERROR: buffer overrun: size %lu idx %d\n", sizeof(soundBuf)/SAMPLEBYTES, newIdx);
|
|
|
|
newIdx = (sizeof(soundBuf)/SAMPLEBYTES)-1;
|
2019-02-20 22:49:51 +00:00
|
|
|
}
|
2020-07-13 23:01:41 +00:00
|
|
|
lastFilledTime = expectedCycleNumber;
|
2019-02-20 22:49:51 +00:00
|
|
|
|
|
|
|
// Flip the toggle state
|
|
|
|
toggleState = !toggleState;
|
|
|
|
|
2020-07-13 23:01:41 +00:00
|
|
|
// Fill from bufIdx .. newIdx and set bufIdx to newIdx when done.
|
2019-02-20 22:49:51 +00:00
|
|
|
if (newIdx > bufIdx) {
|
|
|
|
long count = (long)newIdx - bufIdx;
|
2020-07-15 00:43:41 +00:00
|
|
|
for (long i=0; i<count; i++) {
|
|
|
|
soundBuf[bufIdx+i] = toggleState ? HIGHVAL : LOWVAL;
|
|
|
|
}
|
2019-02-20 22:49:51 +00:00
|
|
|
bufIdx = newIdx;
|
2017-12-31 22:21:34 +00:00
|
|
|
}
|
2019-02-20 22:49:51 +00:00
|
|
|
|
2017-12-31 22:21:34 +00:00
|
|
|
pthread_mutex_unlock(&togmutex);
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 13:06:15 +00:00
|
|
|
void SDLSpeaker::maintainSpeaker(int64_t c, uint64_t microseconds)
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDLSpeaker::beginMixing()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDLSpeaker::mixOutput(uint8_t v)
|
|
|
|
{
|
|
|
|
}
|