]> Shamusworld >> Repos - stargem2/blob - src/sound.cpp
Finalizing move to trunk...
[stargem2] / src / sound.cpp
1 //
2 // Sound Interface v2.0
3 //
4 // by James L. Hammons
5 // (c) 2006 Underground Software
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  06/15/2006  Added changelog ;-)
12 // JLH  06/15/2006  Scrubbed all BYTE, WORD & DWORD references from the code
13 //
14 // Notes:
15 //   The sound CPU (6808) runs at 894,750 (3,579,000 / 4) Hz.
16 //   At 44.1 KHz, this works out to 894750 / 44100 = 20.289115646... cycles per sample.
17 //
18
19 // Still need to add volume control...
20
21 #include "sound.h"
22
23 #include "SDL.h"
24 #include "types.h"
25 #include "log.h"
26 #include "v6808.h"
27
28 //using namespace std;
29
30 // Local variables
31
32 SDL_AudioSpec desired;
33 bool soundInitialized = false;
34
35 // Private function prototypes
36
37 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
38
39 //
40 // Initialize the SDL sound system
41 //
42 void SoundInit(void)
43 {
44 //      memory_malloc_secure((void **)&DACBuffer, BUFFER_SIZE * sizeof(uint16), "DAC buffer");
45
46         desired.freq = 44100;                                                   // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
47         desired.format = AUDIO_U8;                                              // This uses the native endian (for portability)...
48         desired.channels = 1;
49 //      desired.samples = 4096;                                                 // Let's try a 4K buffer (can always go lower)
50         desired.samples = 2048;                                                 // Let's try a 2K buffer (can always go lower)
51         desired.callback = SDLSoundCallback;
52
53         if (SDL_OpenAudio(&desired, NULL) < 0)                  // NULL means SDL guarantees what we want
54         {
55                 WriteLog("Sound: Failed to initialize SDL sound.\n");
56 //              exit(1);
57                 return;
58         }
59
60         SDL_PauseAudio(false);                                                  // Start playback!
61         soundInitialized = true;
62         WriteLog("Sound: Successfully initialized.\n");
63 }
64
65 //
66 // Close down the SDL sound subsystem
67 //
68 void SoundDone(void)
69 {
70         if (soundInitialized)
71         {
72                 SDL_PauseAudio(true);
73                 SDL_CloseAudio();
74                 WriteLog("Sound: Done.\n");
75         }
76 }
77
78 //
79 // Sound card callback handler
80 //
81 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length)
82 {
83         extern V6808REGS soundCPU;
84 //      extern uint8 * sram;
85         extern uint8 sram[];
86         int cnt = 0;
87
88         while (cnt != length)
89         {
90                 // This is close, but not cycle exact (exact would be 20.289115646...)
91
92 //Need to figure out how to get that fraction to execute... !!! FIX !!!         
93                 Execute6808(&soundCPU, 20);
94                 soundCPU.clock -= 20;
95                 buffer[cnt++] = sram[0x0400];                           // Fill the buffer with the PIA output value
96         }
97 }