chiark / gitweb /
Proper Subversion configuration.
[newkind] / sound.c
1 /*
2  * Elite - The New Kind.
3  *
4  * Reverse engineered from the BBC disk version of Elite.
5  * Additional material by C.J.Pinder.
6  *
7  * The original Elite code is (C) I.Bell & D.Braben 1984.
8  * This version re-engineered in C by C.J.Pinder 1999-2001.
9  *
10  * email: <christian@newkind.co.uk>
11  *
12  *
13  */
14
15 /*
16  * sound.c
17  */
18
19 #include <stdlib.h>
20 #include <allegro.h>
21 #include "sound.h"
22 #include "alg_data.h" 
23
24 #define NUM_SAMPLES 14 
25
26 extern DATAFILE *datafile;
27
28 static int sound_on;
29
30 struct sound_sample
31 {
32         SAMPLE *sample;
33         char filename[256];
34         int runtime;
35         int timeleft;
36 };
37
38 struct sound_sample sample_list[NUM_SAMPLES] =
39 {
40         {NULL, "launch.wav",    32, 0},
41         {NULL, "crash.wav",      7, 0},
42         {NULL, "dock.wav",      36, 0},
43         {NULL, "gameover.wav",  24, 0},
44         {NULL, "pulse.wav",      4, 0},
45         {NULL, "hitem.wav",              4, 0},
46         {NULL, "explode.wav",   23, 0},
47         {NULL, "ecm.wav",               23, 0},
48         {NULL, "missile.wav",   25, 0},
49         {NULL, "hyper.wav",         37, 0},
50         {NULL, "incom1.wav",     4, 0},
51         {NULL, "incom2.wav",     5, 0},
52         {NULL, "beep.wav",               2, 0},
53         {NULL, "boop.wav",               7, 0},
54 };
55  
56  
57 void snd_sound_startup (void)
58 {
59         int i;
60
61         /* Install a sound driver.. */
62         sound_on = 1;
63         
64         if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, ".") != 0)
65         {
66                 sound_on = 0;
67                 return;
68         }
69
70         /* Load the sound samples... */
71
72         for (i = 0; i < NUM_SAMPLES; i++)
73         {
74                 sample_list[i].sample = load_sample(sample_list[i].filename);
75         }
76 }
77  
78
79 void snd_sound_shutdown (void)
80 {
81         int i;
82
83         if (!sound_on)
84                 return;
85
86         for (i = 0; i < NUM_SAMPLES; i++)
87         {
88                 if (sample_list[i].sample != NULL)
89                 {
90                         destroy_sample (sample_list[i].sample);
91                         sample_list[i].sample = NULL;
92                 }
93         }
94 }
95
96
97 void snd_play_sample (int sample_no)
98 {
99         if (!sound_on)
100                 return;
101
102         if (sample_list[sample_no].timeleft != 0)
103                 return;
104
105         sample_list[sample_no].timeleft = sample_list[sample_no].runtime;
106                 
107         play_sample (sample_list[sample_no].sample, 255, 128, 1000, FALSE);
108 }
109
110
111 void snd_update_sound (void)
112 {
113         int i;
114         
115         for (i = 0; i < NUM_SAMPLES; i++)
116         {
117                 if (sample_list[i].timeleft > 0)
118                         sample_list[i].timeleft--;
119         }
120 }
121
122
123 void snd_play_midi (int midi_no, int repeat)
124 {
125         if (!sound_on)
126                 return;
127         
128         switch (midi_no)
129         {
130                 case SND_ELITE_THEME:
131                         play_midi (datafile[THEME].dat, repeat);
132                         break;
133                 
134                 case SND_BLUE_DANUBE:
135                         play_midi (datafile[DANUBE].dat, repeat);
136                         break;
137         }
138 }
139
140
141 void snd_stop_midi (void)
142 {
143         if (sound_on);
144                 play_midi (NULL, TRUE);
145 }