]> Shamusworld >> Repos - apple2/blobdiff - src/sound.cpp
First stab at adding Mockingboard support.
[apple2] / src / sound.cpp
index a68956788fa118e84150efcaf95477809230ba1d..df2de2b4ab59d3c64b80cbc49eb8002ee68734cf 100644 (file)
 
 #include <string.h>                    // For memset, memcpy
 #include <SDL2/SDL.h>
+#include "ay8910.h"
 #include "log.h"
 
 // Useful defines
 
 //#define DEBUG
 
-#define SAMPLE_RATE                    (48000.0)
 #define SAMPLES_PER_FRAME      (SAMPLE_RATE / 60.0)
 #define CYCLES_PER_SAMPLE      (1024000.0 / SAMPLE_RATE)
 // 32K ought to be enough for anybody
@@ -45,13 +45,13 @@ static SDL_AudioSpec desired, obtained;
 static SDL_AudioDeviceID device;
 static bool soundInitialized = false;
 static bool speakerState = false;
-static int16_t soundBuffer[SOUND_BUFFER_SIZE];
+static uint16_t soundBuffer[SOUND_BUFFER_SIZE];
 static uint32_t soundBufferPos;
-static uint64_t lastToggleCycles;
-static SDL_cond * conditional = NULL;
-static SDL_mutex * mutex = NULL;
-static SDL_mutex * mutex2 = NULL;
-static int16_t sample;
+//static uint64_t lastToggleCycles;
+//static SDL_cond * conditional = NULL;
+//static SDL_mutex * mutex = NULL;
+//static SDL_mutex * mutex2 = NULL;
+static uint16_t sample;
 static uint8_t ampPtr = 12;                                            // Start with -2047 - +2047
 static int16_t amplitude[17] = { 0, 1, 2, 3, 7, 15, 31, 63, 127, 255,
        511, 1023, 2047, 4095, 8191, 16383, 32767 };
@@ -67,10 +67,10 @@ static void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
 void SoundInit(void)
 {
        SDL_zero(desired);
-       desired.freq = SAMPLE_RATE;                                     // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
-       desired.format = AUDIO_S16SYS;                          // This uses the native endian (for portability)...
+       desired.freq = SAMPLE_RATE;             // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
+       desired.format = AUDIO_U16SYS;  // This uses the native endian (for portability)...
        desired.channels = 1;
-       desired.samples = 512;                                          // Let's try a 1/2K buffer (can always go lower)
+       desired.samples = 512;                  // Let's try a 1/2K buffer
        desired.callback = SDLSoundCallback;
 
        device = SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0);
@@ -81,11 +81,11 @@ void SoundInit(void)
                return;
        }
 
-       conditional = SDL_CreateCond();
-       mutex = SDL_CreateMutex();
-       mutex2 = SDL_CreateMutex();// Let's try real signalling...
+//     conditional = SDL_CreateCond();
+//     mutex = SDL_CreateMutex();
+//     mutex2 = SDL_CreateMutex();// Let's try real signalling...
        soundBufferPos = 0;
-       lastToggleCycles = 0;
+//     lastToggleCycles = 0;
        sample = desired.silence;       // ? wilwok ? yes
 
        SDL_PauseAudioDevice(device, 0);                        // Start playback!
@@ -103,9 +103,9 @@ void SoundDone(void)
        {
                SDL_PauseAudioDevice(device, 1);
                SDL_CloseAudioDevice(device);
-               SDL_DestroyCond(conditional);
-               SDL_DestroyMutex(mutex);
-               SDL_DestroyMutex(mutex2);
+//             SDL_DestroyCond(conditional);
+//             SDL_DestroyMutex(mutex);
+//             SDL_DestroyMutex(mutex2);
                WriteLog("Sound: Done.\n");
        }
 }
@@ -128,28 +128,31 @@ void SoundResume(void)
 //
 // Sound card callback handler
 //
+static uint32_t sndFrmCnt = 0;
+static uint32_t lastStarve = 0;
 static void SDLSoundCallback(void * /*userdata*/, Uint8 * buffer8, int length8)
 {
+sndFrmCnt++;
 //WriteLog("SDLSoundCallback(): begin (soundBufferPos=%i)\n", soundBufferPos);
-       // The sound buffer should only starve when starting which will cause it to
-       // lag behind the emulation at most by around 1 frame...
-       // (Actually, this should never happen since we fill the buffer beforehand.)
-       // (But, then again, if the sound hasn't been toggled for a while, then this
-       //  makes perfect sense as the buffer won't have been filled at all!)
-       // (Should NOT starve now, now that we properly handle frame edges...)
 
        // Let's try using a mutex for shared resource consumption...
 //Actually, I think Lock/UnlockAudio() does this already...
 //WriteLog("SDLSoundCallback: soundBufferPos = %i\n", soundBufferPos);
-       SDL_mutexP(mutex2);
+//     SDL_mutexP(mutex2);
 
        // Recast this as a 16-bit type...
-       int16_t * buffer = (int16_t *)buffer8;
+       uint16_t * buffer = (uint16_t *)buffer8;
        uint32_t length = (uint32_t)length8 / 2;
 
 //WriteLog("SDLSoundCallback(): filling buffer...\n");
        if (soundBufferPos < length)
        {
+WriteLog("*** Sound buffer starved (%d short) *** [%d delta %d]\n", length - soundBufferPos, sndFrmCnt, sndFrmCnt - lastStarve);
+lastStarve = sndFrmCnt;
+#if 1
+               for(uint32_t i=0; i<length; i++)
+                       buffer[i] = desired.silence;
+#else
                // The sound buffer is starved...
                for(uint32_t i=0; i<soundBufferPos; i++)
                        buffer[i] = soundBuffer[i];
@@ -160,6 +163,7 @@ static void SDLSoundCallback(void * /*userdata*/, Uint8 * buffer8, int length8)
 
                // Reset soundBufferPos to start of buffer...
                soundBufferPos = 0;
+#endif
        }
        else
        {
@@ -176,9 +180,9 @@ static void SDLSoundCallback(void * /*userdata*/, Uint8 * buffer8, int length8)
 
        // Free the mutex...
 //WriteLog("SDLSoundCallback(): SDL_mutexV(mutex2)\n");
-       SDL_mutexV(mutex2);
+//     SDL_mutexV(mutex2);
        // Wake up any threads waiting for the buffer to drain...
-       SDL_CondSignal(conditional);
+//     SDL_CondSignal(conditional);
 //WriteLog("SDLSoundCallback(): end\n");
 }
 
@@ -186,23 +190,43 @@ static void SDLSoundCallback(void * /*userdata*/, Uint8 * buffer8, int length8)
 // This is called by the main CPU thread every ~21.666 cycles.
 void WriteSampleToBuffer(void)
 {
+#ifdef USE_NEW_AY8910
+       uint16_t s1 = AYGetSample(0);
+       uint16_t s2 = AYGetSample(1);
+       uint16_t adjustedMockingboard = s1 + s2;
+#else
+       int16_t s1, s2, s3, s4, s5, s6;
+       int16_t * bufPtrs[6] = { &s1, &s2, &s3, &s4, &s5, &s6 };
+       AY8910Update(0, bufPtrs, 1);
+       AY8910Update(1, &bufPtrs[3], 1);
+       int16_t adjustedMockingboard = (s1 / 8) + (s2 / 8) + (s3 / 8)
+               + (s4 / 8) + (s5 / 8) + (s6 / 8);
+#endif
+//need to do this *before* mixing, as by this time, it's too late and the sample is probably already oversaturated
+//     adjustedMockingboard /= 8;
+
 //WriteLog("WriteSampleToBuffer(): SDL_mutexP(mutex2)\n");
-       SDL_mutexP(mutex2);
+//     SDL_mutexP(mutex2);
 
        // This should almost never happen, but, if it does...
        while (soundBufferPos >= (SOUND_BUFFER_SIZE - 1))
        {
 //WriteLog("WriteSampleToBuffer(): Waiting for sound thread. soundBufferPos=%i, SOUNDBUFFERSIZE-1=%i\n", soundBufferPos, SOUND_BUFFER_SIZE-1);
-               SDL_mutexV(mutex2);     // Release it so sound thread can get it,
-               SDL_mutexP(mutex);      // Must lock the mutex for the cond to work properly...
-               SDL_CondWait(conditional, mutex);       // Sleep/wait for the sound thread
-               SDL_mutexV(mutex);      // Must unlock the mutex for the cond to work properly...
-               SDL_mutexP(mutex2);     // Re-lock it until we're done with it...
+//             SDL_mutexV(mutex2);     // Release it so sound thread can get it,
+//             SDL_mutexP(mutex);      // Must lock the mutex for the cond to work properly...
+//             SDL_CondWait(conditional, mutex);       // Sleep/wait for the sound thread
+//             SDL_mutexV(mutex);      // Must unlock the mutex for the cond to work properly...
+//             SDL_mutexP(mutex2);     // Re-lock it until we're done with it...
+               SDL_Delay(1);
        }
 
-       soundBuffer[soundBufferPos++] = sample;
+       SDL_LockAudioDevice(device);
+       soundBuffer[soundBufferPos++] = sample + adjustedMockingboard;
+       SDL_UnlockAudioDevice(device);
+
+//     soundBuffer[soundBufferPos++] = sample;
 //WriteLog("WriteSampleToBuffer(): SDL_mutexV(mutex2)\n");
-       SDL_mutexV(mutex2);
+//     SDL_mutexV(mutex2);
 }
 
 
@@ -212,7 +236,7 @@ void ToggleSpeaker(void)
                return;
 
        speakerState = !speakerState;
-       sample = (speakerState ? amplitude[ampPtr] : -amplitude[ampPtr]);
+       sample = (speakerState ? amplitude[ampPtr] : 0);//-amplitude[ampPtr]);
 }