]> Shamusworld >> Repos - thunder/blobdiff - src/sound.cpp
Emulator working with YM2151 and V63701 emus in place.
[thunder] / src / sound.cpp
index 930de351e081505acd3f19a3513c2e70590c8bd3..744eafaca7df547f422fbb06b38e8dace7763b5a 100644 (file)
@@ -14,6 +14,7 @@
 #include "sound.h"
 #include <SDL2/SDL.h>
 #include "resource.h"
+#include "ym2151.h"
 
 // Function prototypes
 void SoundFunc(void *, Uint8 *, int);
@@ -22,7 +23,7 @@ void SoundFunc(void *, Uint8 *, int);
 extern uint8_t voice_rom[];                    // PCM data pointer
 
 static bool soundInitialized = false;
-const float sampleBase = 22050.0/6000.0;  // Btwn 5512.5 and 6000
+const float sampleBase = 44010.0/6000.0;  // Btwn 5512.5 and 6000
 bool snd_go = false;
 bool chan1_go = false, chan2_go = false, chan3_go = false;
 bool chan4_go = false, chan5_go = false, chan6_go = false;
@@ -47,9 +48,17 @@ uint32_t snd_lens[3]   = { sunknownlen, scyalen, scameralen };
 
 void InitSound(void)
 {
+       // params 1, 4 & 5 are useless
+//     if (YMInit(1, 3579580, 22050, 16, 512))//, (SAMPLE **)sbp))
+       if (YMInit(1, 3579580, 44100, 16, 512))
+       {
+               printf("SOUND: Could not init YM2151 emulator!\n");
+               return;// -1;
+       }
+
        SDL_AudioSpec desired, obtained;
-       desired.freq = 22050;
-       desired.format = AUDIO_U8;
+       desired.freq = 44100;
+       desired.format = AUDIO_S16;
        desired.channels = 1;
        desired.samples = 1024;
        desired.callback = SoundFunc;
@@ -58,7 +67,7 @@ void InitSound(void)
        if (SDL_OpenAudio(&desired, &obtained) < 0)
        {
                soundInitialized = false;
-               printf("Couldn't open audio: %s\n", SDL_GetError());
+               printf("SOUND: Couldn't open audio: %s\n", SDL_GetError());
                return;
        }
 
@@ -145,6 +154,7 @@ void SpawnSound(int type, int snd, int channel/* = 0*/)
                                chan4_go = false;                                               // No sound loaded, so don't do it!
                }
        }
+#if 0
        else if (type == FMSOUND)
        {
                spos5 = 0;
@@ -155,6 +165,7 @@ void SpawnSound(int type, int snd, int channel/* = 0*/)
                if (spos5 == end_pos5)
                        chan5_go = false;                                                       // No sound loaded, so don't do it!
        }
+#endif
        else if (type == USERSOUND)
        {
                spos6 = 0;
@@ -167,6 +178,13 @@ void SpawnSound(int type, int snd, int channel/* = 0*/)
 
 void SoundFunc(void * userdata, Uint8 * buff, int num)
 {
+       static bool psg1go = false, psg2go = false;
+       // Length / 2 is because it's set up for 16-bit samples
+//     YM2151UpdateOne(buffer, length / 2);
+       // Have to go into ym2151.cpp and change this manually back to 8 bit
+       YM2151UpdateOne(buff, num / 2);
+//     return;
+
        // 0-22 different sounds...
        uint16_t cnt = 0, sample;
        uint8_t start_samp1, end_samp1, start_samp2, end_samp2;
@@ -174,123 +192,127 @@ void SoundFunc(void * userdata, Uint8 * buff, int num)
                samp4 = 128, samp5 = 128, samp6 = 128;                  // Zero samples...
 
        // Kill sound...
-       memset(buff, 128, num);
+//     memset(buff, 128, num);
+
+       if (!(chan1_go || chan2_go || chan3_go || chan4_go /*|| chan5_go*/ || chan6_go))
+               return;
 
-       if (chan1_go || chan2_go || chan3_go || chan4_go || chan5_go || chan6_go)
+       while (cnt != (num / 2))
        {
-               while (cnt != num)
+               if (chan1_go)
                {
-                       if (chan1_go)
+                       if (sample1 < 1)
                        {
-                               if (sample1 < 1)
+                               samp1 = voice_rom[spos1++];
+
+                               // Kill channel 1 if done...
+                               if (samp1 == 0xFF)
                                {
-                                       samp1 = voice_rom[spos1++];
-
-                                       // Kill channel 1 if done...
-                                       if (samp1 == 0xFF)
-                                       {
-                                               chan1_go = false;
-                                               samp1 = 128;
-                                       }
-                                       // RLE compression...
-                                       else if (samp1 == 0x00)
-                                       {
-                                               // # of repeats
-                                               sample1 += (float)voice_rom[spos1++] * sampleBase;
-                                               // Get last good sample
-                                               samp1   = prevSamp1;
-                                       }
-                                       else
-                                               // Keep fractional part intact
-                                               sample1 += sampleBase;
+                                       chan1_go = false;
+                                       samp1 = 128;
                                }
-
-                               prevSamp1 = samp1;              // Save last sample value
-                               sample1 -= 1.0;                 // Decrement repeat counter
+                               // RLE compression...
+                               else if (samp1 == 0x00)
+                               {
+                                       // # of repeats
+                                       sample1 += (float)voice_rom[spos1++] * sampleBase;
+                                       // Get last good sample
+                                       samp1   = prevSamp1;
+                               }
+                               else
+                                       // Keep fractional part intact
+                                       sample1 += sampleBase;
                        }
 
+                       prevSamp1 = samp1;              // Save last sample value
+                       sample1 -= 1.0;                 // Decrement repeat counter
+               }
+
 // Stretching 5KHz samples to 22KHz:
 //   numRepeats = 4;
 //            6KHz -> 22KHz:  22/6 repeats...
-                       if (chan2_go)
+               if (chan2_go)
+               {
+                       if (sample2 < 1)
                        {
-                               if (sample2 < 1)
+                               samp2 = voice_rom[spos2++];
+
+                               if (samp2 == 0xFF)
                                {
-                                       samp2 = voice_rom[spos2++];
-
-                                       if (samp2 == 0xFF)
-                                       {
-                                               chan2_go = false;
-                                               samp2 = 128;                                    // Kill channel 2 if done...
-                                       }
-                                       else if (samp2 == 0x00)                         // RLE compression...
-                                       {
-                                               sample2 += (float)voice_rom[spos2++] * sampleBase;      // # of repeats
-                                               samp2   = prevSamp2;
-                                       }
-                                       else
-                                               sample2 += sampleBase;
+                                       chan2_go = false;
+                                       samp2 = 128;                                    // Kill channel 2 if done...
                                }
+                               else if (samp2 == 0x00)                         // RLE compression...
+                               {
+                                       sample2 += (float)voice_rom[spos2++] * sampleBase;      // # of repeats
+                                       samp2   = prevSamp2;
+                               }
+                               else
+                                       sample2 += sampleBase;
+                       }
 
 // Delta-X values were making the samples sound like crap...
 //                             start_samp2 += delta_x2;
-                               prevSamp2 = samp2;
-                               sample2 -= 1.0;
-                       }
+                       prevSamp2 = samp2;
+                       sample2 -= 1.0;
+               }
 
-                       if (chan3_go)
-                       {
-                               samp3 = sndp3[spos3++];
+               if (chan3_go)
+               {
+                       samp3 = (psg1go ? sndp3[spos3++] : sndp3[spos3]);
+                       psg1go = !psg1go;
 
-                               if (spos3 == end_pos3)
-                               {
-                                       chan3_go = false;
-                                       samp3 = 128; // Kill channel 3 if done...
-                               }
+                       if (spos3 == end_pos3)
+                       {
+                               chan3_go = false;
+                               samp3 = 128; // Kill channel 3 if done...
                        }
+               }
 
-                       if (chan4_go)
-                       {
-                               samp4 = sndp4[spos4++];
+               if (chan4_go)
+               {
+                       samp4 = (psg2go ? sndp4[spos4++] : sndp4[spos4]);
+                       psg2go = !psg2go;
 
-                               if (spos4 == end_pos4)
-                               {
-                                       chan4_go = false;
-                                       samp4 = 128; // Kill channel 4 if done...
-                               }
+                       if (spos4 == end_pos4)
+                       {
+                               chan4_go = false;
+                               samp4 = 128; // Kill channel 4 if done...
                        }
+               }
+#if 0
+               if (chan5_go)
+               {
+                       samp5 = sndp5[spos5++];
 
-                       if (chan5_go)
+                       if (spos5 == end_pos5)
                        {
-                               samp5 = sndp5[spos5++];
-
-                               if (spos5 == end_pos5)
-                               {
-                                       chan5_go = false;
-                                       samp5 = 128; // Kill channel 5 if done...
-                               }
+                               chan5_go = false;
+                               samp5 = 128; // Kill channel 5 if done...
                        }
+               }
+#endif
+               if (chan6_go)
+               {
+                       samp6 = sndp6[spos6++];
 
-                       if (chan6_go)
+                       if (spos6 == end_pos6)
                        {
-                               samp6 = sndp6[spos6++];
-
-                               if (spos6 == end_pos6)
-                               {
-                                       chan6_go = false;
-                                       samp6 = 128;            // Kill channel 6...
-                               }
+                               chan6_go = false;
+                               samp6 = 128;            // Kill channel 6...
                        }
+               }
 
-                       // Mix 'em...
-                       sample = samp1 + samp2 + samp3 + samp4 + samp5 + samp6 - 640;
+               // Mix 'em...
+//             sample = samp1 + samp2 + samp3 + samp4 + samp5 + samp6 - 640;
+               sample = samp1 + samp2 + samp3 + samp4 + samp6 - (5 * 128);
 
-                       // If it overflowed, clip it
-                       if (sample & 0xFF00)
-                               sample = (sample & 0x8000 ? 0x00 : 0xFF);
+               // If it overflowed, clip it
+               if (sample & 0xFF00)
+//                     sample = (sample & 0x8000 ? 0x00 : 0xFF);
+                       sample = (sample & 0x8000 ? 0x0000 : 0xFFFF);
 
-                       buff[cnt++] = sample;
-               }
+               ((uint16_t *)buff)[cnt++] += sample << 7;
        }
 }