]> Shamusworld >> Repos - thunder/blob - test/sound.cpp
Replaced old v6809 core with new updated one. :-)
[thunder] / test / sound.cpp
1 // Sound player
2 // (Last build: 7/1/1998)
3 //
4 // by James L. Hammons
5 //
6 // (c) 1998 Underground Software
7
8 #include <dos.h>
9 #include <pc.h>
10 #include <go32.h>
11 #include <dpmi.h>
12 #include <conio.h>
13 #include <fstream.h>
14 #include <string.h>
15 #include <iomanip.h>
16 #include <iostream.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <time.h>
20 #include <new.h>
21 #include "keyboard.h"
22 #include "sbdrv.h"    // Goes before GUI so it can have access to uint..
23
24 #define ROM21 "RT1-21.ROM"
25 #define ROM22 "RT2-22.ROM"
26
27 #define BYTE  unsigned char
28 #define WORD  unsigned short int
29 #define DWORD unsigned long int
30
31 // Global defines
32
33 BYTE * voice_rom;
34
35 fstream tr;    // Tracelog hook
36
37 bool snd_go = false;     // for sound routines...
38 uint32 start_pos, end_pos, rom_pos;
39 uint8 sample;
40 int8 delta_x;
41
42 //
43 // Sound stuff (Will go elsewhere???)
44 //
45 void SpawnSound(void)
46 {
47   snd_go = true;  rom_pos = start_pos;  sample = 0;  
48 }
49 void SoundFunc(uint8 * buff, uint16 num)
50 {
51   uint16 cnt = 0;              // 0-22 different sounds...
52   uint8 start_samp, end_samp;
53
54   memset(buff, 128, num);  // Kill sound...
55   if (snd_go)
56   {
57     while (cnt != num)
58     {
59       if (sample == 0)
60       {
61         start_samp = voice_rom[rom_pos++];
62         end_samp   = voice_rom[rom_pos];
63         delta_x    = (end_samp - start_samp) / 4;  // Interpolation value...
64         sample     = 4;
65       }
66       buff[cnt++] = start_samp;
67       start_samp += delta_x;
68       sample--;
69       if (rom_pos == end_pos)  // Reached cutoff yet?
70       {
71         snd_go = false;
72         cnt = num;
73       }
74     }
75   }
76 }
77 //
78 // Generic Load file into image space
79 // (No error checking performed!  Responsibility of caller!)
80 //
81 bool LoadImg(char * filename, BYTE * mem, DWORD address, DWORD length)
82 {
83   ifstream ff;
84   char ch;
85
86   ff.open(filename, ios::binary | ios::in);  // Open 'da file...
87   if (ff)
88   {                                          
89     for(DWORD i=0; i<length; i++)             // Read it in...
90     {
91       ff.get(ch);  mem[address+i] = ch;
92     }
93     ff.close();                              // Close 'da file...
94   }
95   return(ff);
96 }
97 //
98 // Show screen routine
99 //
100 void ShowScreen(void)
101 {
102   cout << "Start: " << hex << start_pos << "  End: " << end_pos << " "
103        << endl;
104 }
105 //
106 // Main loop
107 //
108 int main(int /*argc*/, char * /*argv[]*/)
109 {
110   char lbuff[80];
111   fstream ff;                       // Declare fstream without file hooks...
112   bool running = true;              // CPU running state flag...
113   DWORD debounce = 0;               // Key de-bounce counter
114   BYTE x;                           // General placeholder...
115
116   tr.open("sound.log", ios::binary | ios::out); // Tracelog
117
118   cout << "Looking for soundcard..." << endl;
119   if (!OpenSB(22050, 800))  { cerr << "No soundcard found!" << endl
120                                    << "Aborting!" << endl;  return -1; }
121   Start_audio_output(AUTO_DMA, SoundFunc);    // Start sound...
122   
123   cout << "Allocating memory..." << endl;
124   set_new_handler(0);    // Make 'new' return NULL on failure...
125   voice_rom = new BYTE[0x20000];
126   if (voice_rom == NULL)  { cerr << "Could not allocate ROM voice data!" << endl
127                       << "Aborting!" << endl;  return -1; }
128
129   cout << "Loading ROMs..." << endl;
130   if (!LoadImg(ROM21, voice_rom, 0, 0x10000))  // Load 21st ROM
131   { cerr << "Could not open file '" << ROM21 << "'!" << endl;  return -1; }
132
133   if (!LoadImg(ROM22, voice_rom, 0x10000, 0x10000))  // Load 22nd ROM
134   { cerr << "Could not open file '" << ROM22 << "'!" << endl;  return -1; }
135      
136   for(int i=0; i<128; i++)  keys[i] = 0;    // Clear keyboard buffer...
137   SnagKeyboard();                           // Snag the interrupt...
138   ChainKeyboard(0);                         // No chaining!
139   start_pos = 0;  end_pos = 1;
140
141   while (running)
142   {
143     if (keys[0x01])  running = false;      // ESC to exit...
144
145     if (debounce)  debounce--;
146     if (keys[57] && !debounce)
147     {
148       SpawnSound();                   // Space
149       debounce = 0x100000;
150     }
151     if (keys[12])  { start_pos--;  ShowScreen(); } // '-'
152     if (keys[13])  { start_pos++;  ShowScreen(); } // '+'
153     if (keys[26])  { end_pos--;  ShowScreen(); }    // '['
154     if (keys[27])  { end_pos++;  ShowScreen(); }    // ']'
155     if (keys[19])  tr << hex << start_pos << ":" << end_pos << endl; // 'R'
156   }
157   ReleaseKeyboard();               // Release the interrupt...
158   Stop_audio_output();
159   CloseSB();                       // Shut down sound card
160   delete[] voice_rom;
161   tr.close(); // Close tracelog
162 }