File: ssound.c

package info (click to toggle)
xscorch 0.2.0-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 4,636 kB
  • ctags: 3,399
  • sloc: ansic: 31,504; sh: 8,826; makefile: 467; perl: 16
file content (262 lines) | stat: -rw-r--r-- 5,655 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* $Header: /fridge/cvs/xscorch/ssound/ssound.c,v 1.8 2004/02/26 06:34:57 justins Exp $ */
/*
   
   xscorch - ssound.c         Copyright(c) 2000-2003 Justin David Smith
   justins(at)chaos2.org      http://chaos2.org/
    
   xscorch sound driver
   

   This program is free software; you can redistribute it and/or modify 
   it under the terms of the GNU General Public License as published by 
   the Free Software Foundation, version 2 of the License ONLY. 

   This program is distributed in the hope that it will be useful, 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/
#include <mikmod.h>

#include <ssound.h>

#include <libj/jstr/libjstr.h>



/* List of song names */
static const char *_sc_sound_names[] = {
   "prelude.xm",
   "inventory.xm",
   "round.xm",
   "endround.xm",
   "endgame.xm",
   NULL
};



static bool _sc_sound_try_load(sc_sound *s, sc_sound_music id) {
/* sc_sound_try_load
   Attempt to load the song given by the ID.  On success, true is returned
   and s->module will refer to the song.  On failure, false is returned.
   Note, Any previously loaded song must be unloaded before this function 
   is called.  */

   /* Temporary buffer to write filename into */
   char filename[SC_SOUND_BUFFER];

   /* Construct the suspected filename for the song */
   sbprintf(filename, sizeof(filename), "%s/%s/%s", SC_GLOBAL_DIR, SC_SOUND_DIR, _sc_sound_names[id]);
   
   /* Attempt to load the song */
   s->module = Player_Load(filename, 64, 0);
   if(s->module == NULL) {
      /* Failure; see what went wrong, then give up */
      /* TEMP TEMP TEMP TEMP TEMP
         fprintf(stderr, "Could not load module \"%s\": \"%s\"\n", _sc_sound_names[id], MikMod_strerror(MikMod_errno));
       */
      return(0);
   }
   
   /* Success: set module parametres and exit */
   s->module->wrap = 1;
   s->module->loop = 1;
   s->module->reppos = SC_SOUND_REVERB;
   return(!0);

}



void sc_sound_init(void) {
/* sc_sound_init
   This function must be called _exactly_ once, to initialise the sound 
   driver.  This function must be called before sound structures are
   created; for MikMod we'll just register everything.  */

   MikMod_RegisterAllDrivers();
   MikMod_RegisterAllLoaders();
   
}



static void _sc_sound_mdmode(sc_sound *s, int hqmixer) {
/* sc_sound_mdmode
   Set driver (global) options.  */

   s->hqmixer = hqmixer;
   md_mode |= DMODE_SOFT_MUSIC | DMODE_INTERP;
   if(hqmixer) {
      md_mode |= DMODE_HQMIXER;
   } else {
      md_mode &= (~DMODE_HQMIXER);
   }
   md_reverb = 0;

}



static void _sc_sound_reset(sc_sound *s) {
/* sc_sound_reset
   Reset the mikmod driver - Warning: all sound samples and modules must
   be reloaded after this call.  Otherwise, nasty segfaults will occur.  */

   /*
   if(s->playing) Player_Stop();
   if(s->module != NULL) Player_Free(s->module);
   */

   #if LIBMIKMOD_VERSION >= 0x030107
   if(MikMod_Reset("")) {
   #else /* Version is old */
   if(MikMod_Reset()) {
   #endif /* Libmikmod ok? */
      fprintf(stderr, "Could not reinitialise sound: \"%s\"\n", MikMod_strerror(MikMod_errno));
   }
   s->playing = 0;

}


   
sc_sound *sc_sound_new(int enable, int hqmixer) {

   sc_sound *s;

   if(!enable) return(NULL);

   s = (sc_sound *)malloc(sizeof(sc_sound));
   if(s == NULL) return(NULL);
   s->playing = 0;
   s->module = NULL;

   _sc_sound_mdmode(s, hqmixer);

   /* Beware the kludgery */
   #if LIBMIKMOD_VERSION >= 0x030107
   if(MikMod_Init("")) {
   #else /* Version is old */
   if(MikMod_Init()) {
   #endif /* Libmikmod ok? */
      fprintf(stderr, "Could not initialise sound: \"%s\"\n", MikMod_strerror(MikMod_errno));
      sc_sound_free(&s);
      return(NULL);
   }
   
   return(s);
   
}



void sc_sound_config(sc_sound **s, int enable, int hqmixer) {

   if(s == NULL) return;
   if(*s == NULL) {
      *s = sc_sound_new(enable, hqmixer);
   } else {
      if(enable) {
         _sc_sound_mdmode(*s, hqmixer);
         _sc_sound_reset(*s);
      } else {
         sc_sound_free(s);
      }
   }

}



void sc_sound_free(sc_sound **s) {

   if(s == NULL || *s == NULL) return;

   Player_Stop();
   if((*s)->module != NULL) {
      Player_Free((*s)->module);
   }
   MikMod_Exit();

   free(*s);
   *s = NULL;
   
}



void sc_sound_start(sc_sound *s, sc_sound_music id) {

   /*int voice;*/

   if(s == NULL) return;
   _sc_sound_reset(s);
   
   _sc_sound_try_load(s, id);
   if(s->module == NULL) return;
      
   Player_Start(s->module);
   /*
   Player_SetPosition(0);
   for(voice = 0; voice <= 99; ++voice) {
      Player_Unmute(voice);
      Voice_SetVolume(voice, 256);
      Voice_SetPanning(voice, PAN_SURROUND);
   }*/
   
   s->playing = 1;
   sc_sound_update(s);

}



void sc_sound_update(sc_sound *s) {
   
   if(s == NULL || s->module == NULL) return;
   if(s->playing) {
      MikMod_Update();
      if(!Player_Active()) {
         Player_SetPosition(0);
      }
   }

}



void sc_sound_stop(sc_sound *s) {

   if(s == NULL || s->module == NULL) return;
   Player_Stop();
   s->playing = 0;

}



void sc_sound_pause(sc_sound *s) {

   if(s == NULL || s->module == NULL) return;
   if(!Player_Paused()) Player_TogglePause();

}



void sc_sound_unpause(sc_sound *s) {

   if(s == NULL || s->module == NULL) return;
   if(Player_Paused()) Player_TogglePause();

}