Subversion Repositories Kolibri OS

Rev

Rev 374 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 374 Rev 376
1
// Emacs style mode select   -*- C++ -*- 
1
// Emacs style mode select   -*- C++ -*- 
2
//-----------------------------------------------------------------------------
2
//-----------------------------------------------------------------------------
3
//
3
//
4
// $Id:$
4
// $Id:$
5
//
5
//
6
// Copyright (C) 1993-1996 by id Software, Inc.
6
// Copyright (C) 1993-1996 by id Software, Inc.
7
//
7
//
8
// This source is available for distribution and/or modification
8
// This source is available for distribution and/or modification
9
// only under the terms of the DOOM Source Code License as
9
// only under the terms of the DOOM Source Code License as
10
// published by id Software. All rights reserved.
10
// published by id Software. All rights reserved.
11
//
11
//
12
// The source is distributed in the hope that it will be useful,
12
// The source is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
14
// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
15
// for more details.
15
// for more details.
16
//
16
//
17
// $Log:$
17
// $Log:$
18
//
18
//
19
// DESCRIPTION:  none
19
// DESCRIPTION:  none
20
//
20
//
21
//-----------------------------------------------------------------------------
21
//-----------------------------------------------------------------------------
22
 
22
 
23
 
23
 
24
static const char
24
static const char
25
rcsid[] = "$Id: s_sound.c,v 1.6 1997/02/03 22:45:12 b1 Exp $";
25
rcsid[] = "$Id: s_sound.c,v 1.6 1997/02/03 22:45:12 b1 Exp $";
26
 
26
 
27
 
27
 
28
#include 
28
#include 
29
#include 
29
#include 
30
 
30
 
31
#include "i_system.h"
31
#include "i_system.h"
32
#include "i_sound.h"
32
#include "i_sound.h"
33
#include "sounds.h"
33
#include "sounds.h"
34
#include "s_sound.h"
34
#include "s_sound.h"
35
 
35
 
36
#include "z_zone.h"
36
#include "z_zone.h"
37
#include "m_random.h"
37
#include "m_random.h"
38
#include "w_wad.h"
38
#include "w_wad.h"
39
 
39
 
40
#include "doomdef.h"
40
#include "doomdef.h"
41
#include "p_local.h"
41
#include "p_local.h"
42
//#include "m_music.h"
42
//#include "m_music.h"
43
 
43
 
44
#include "doomstat.h"
44
#include "doomstat.h"
45
 
45
 
46
//#include "qmus2mid.h"
46
//#include "qmus2mid.h"
47
 
47
 
48
 
48
 
49
#include "kolibri.h"
49
#include "kolibri.h"
50
#include "sound.h"
50
#include "sound.h"
51
 
51
 
52
void WriteDebug(char *);
52
void WriteDebug(char *);
53
 
53
 
54
// Purpose?
54
// Purpose?
55
const char snd_prefixen[]
55
const char snd_prefixen[]
56
= { 'P', 'P', 'A', 'S', 'S', 'S', 'M', 'M', 'M', 'S', 'S', 'S' };
56
= { 'P', 'P', 'A', 'S', 'S', 'S', 'M', 'M', 'M', 'S', 'S', 'S' };
57
 
57
 
58
#define S_MAX_VOLUME            127
58
#define S_MAX_VOLUME            127
59
 
59
 
60
// when to clip out sounds
60
// when to clip out sounds
61
// Does not fit the large outdoor areas.
61
// Does not fit the large outdoor areas.
62
#define S_CLIPPING_DIST         (1200*0x10000)
62
#define S_CLIPPING_DIST         (1200*0x10000)
63
 
63
 
64
// Distance tp origin when sounds should be maxed out.
64
// Distance tp origin when sounds should be maxed out.
65
// This should relate to movement clipping resolution
65
// This should relate to movement clipping resolution
66
// (see BLOCKMAP handling).
66
// (see BLOCKMAP handling).
67
// Originally: (200*0x10000).
67
// Originally: (200*0x10000).
68
#define S_CLOSE_DIST            (160*0x10000)
68
#define S_CLOSE_DIST            (160*0x10000)
69
 
69
 
70
 
70
 
71
#define S_ATTENUATOR            ((S_CLIPPING_DIST-S_CLOSE_DIST)>>FRACBITS)
71
#define S_ATTENUATOR            ((S_CLIPPING_DIST-S_CLOSE_DIST)>>FRACBITS)
72
 
72
 
73
// Adjustable by menu.
73
// Adjustable by menu.
74
#define NORM_VOLUME             snd_MaxVolume
74
#define NORM_VOLUME             snd_MaxVolume
75
 
75
 
76
#define NORM_PITCH              128
76
#define NORM_PITCH              128
77
#define NORM_PRIORITY           64
77
#define NORM_PRIORITY           64
78
#define NORM_SEP                128
78
#define NORM_SEP                128
79
 
79
 
80
#define S_PITCH_PERTURB         1
80
#define S_PITCH_PERTURB         1
81
#define S_STEREO_SWING          (96*0x10000)
81
#define S_STEREO_SWING          (96*0x10000)
82
 
82
 
83
// percent attenuation from front to back
83
// percent attenuation from front to back
84
#define S_IFRACVOL              30
84
#define S_IFRACVOL              30
85
 
85
 
86
#define NA                      0
86
#define NA                      0
87
//#define S_NUMCHANNELS         2
87
//#define S_NUMCHANNELS         2
88
#define NUM_CHANNELS    16
88
#define NUM_CHANNELS    16
89
 
89
 
90
// Current music/sfx card - index useless
90
// Current music/sfx card - index useless
91
//  w/o a reference LUT in a sound module.
91
//  w/o a reference LUT in a sound module.
92
extern int snd_MusicDevice;
92
extern int snd_MusicDevice;
93
extern int snd_SfxDevice;
93
extern int snd_SfxDevice;
94
// Config file? Same disclaimer as above.
94
// Config file? Same disclaimer as above.
95
extern int snd_DesiredMusicDevice;
95
extern int snd_DesiredMusicDevice;
96
extern int snd_DesiredSfxDevice;
96
extern int snd_DesiredSfxDevice;
97
 
97
 
98
 
98
 
99
typedef struct
99
typedef struct
100
{
100
{
101
    // sound information (if null, channel avail.)
101
    // sound information (if null, channel avail.)
102
    sfxinfo_t*  sfxinfo;
102
    sfxinfo_t*  sfxinfo;
103
 
103
 
104
    // origin of sound
104
    // origin of sound
105
    void*       origin;
105
    void*       origin;
106
 
106
 
107
    // handle of the sound being played
107
    // handle of the sound being played
108
    int         handle;
108
    int         handle;
109
    
109
    
110
} channel_t;
110
} channel_t;
111
 
111
 
112
 
112
 
113
// the set of channels available
113
// the set of channels available
114
static channel_t*       channels;
114
static channel_t*       channels;
115
 
115
 
116
// These are not used, but should be (menu).
116
// These are not used, but should be (menu).
117
// Maximum volume of a sound effect.
117
// Maximum volume of a sound effect.
118
// Internal default is max out of 0-15.
118
// Internal default is max out of 0-15.
119
int             snd_SfxVolume = 15;
119
int             snd_SfxVolume = 15;
120
 
120
 
121
// Maximum volume of music. Useless so far.
121
// Maximum volume of music. Useless so far.
122
int             snd_MusicVolume = 15; 
122
int             snd_MusicVolume = 15; 
123
 
123
 
124
 
124
 
125
 
125
 
126
// whether songs are mus_paused
126
// whether songs are mus_paused
127
static boolean          mus_paused;     
127
static boolean          mus_paused;     
128
 
128
 
129
// music currently being played
129
// music currently being played
130
static musicinfo_t*     mus_playing=0;
130
static musicinfo_t*     mus_playing=0;
131
 
131
 
132
// following is set
132
// following is set
133
//  by the defaults code in M_misc:
133
//  by the defaults code in M_misc:
134
// number of channels available
134
// number of channels available
135
int                     numChannels;    
135
int                     numChannels;    
136
 
136
 
137
static int              nextcleanup;
137
static int              nextcleanup;
138
 
138
 
139
 
139
 
140
 
140
 
141
//
141
//
142
// Internals.
142
// Internals.
143
//
143
//
144
int
144
int
145
S_getChannel
145
S_getChannel
146
( void*         origin,
146
( void*         origin,
147
  sfxinfo_t*    sfxinfo,
147
  sfxinfo_t*    sfxinfo,
148
  int       sfxid );
148
  int       sfxid );
149
 
149
 
150
 
150
 
151
int
151
int
152
S_AdjustSoundParams
152
S_AdjustSoundParams
153
( mobj_t*       listener,
153
( mobj_t*       listener,
154
  mobj_t*       source,
154
  mobj_t*       source,
155
  int*          vol,
155
  int*          vol,
156
  int*          sep,
156
  int*          sep,
157
  int*          pitch );
157
  int*          pitch );
158
 
158
 
159
void S_StopChannel(int cnum);
159
void S_StopChannel(int cnum);
160
 
160
 
161
 
161
 
162
 
162
 
163
//
163
//
164
// Initializes sound stuff, including volume
164
// Initializes sound stuff, including volume
165
// Sets channels, SFX and music volume,
165
// Sets channels, SFX and music volume,
166
//  allocates channel buffer, sets S_sfx lookup.
166
//  allocates channel buffer, sets S_sfx lookup.
167
//
167
//
168
 
168
 
169
 
169
 
170
SNDBUF hMixBuff;
170
SNDBUF hMixBuff;
171
volatile int sound_state; 
171
volatile int sound_state; 
172
void sound_proc(void);
172
void sound_proc(void);
173
void I_UpdateSound( void );
173
void I_UpdateSound( void );
174
 
174
 
175
void S_Init
175
void S_Init
176
( int           sfxVolume,
176
( int           sfxVolume,
177
  int           musicVolume )
177
  int           musicVolume )
178
{  
178
{  
179
  int           i;
179
  int           i;
180
  char *thread_stack;
180
  char *thread_stack;
181
  int ver;
-
 
182
 
181
 
183
  numChannels = NUM_CHANNELS;
182
  numChannels = NUM_CHANNELS;
184
 
183
 
185
  // Whatever these did with DMX, these are rather dummies now.
184
  // Whatever these did with DMX, these are rather dummies now.
186
  I_SetChannels();
185
  I_SetChannels();
187
  
186
  
188
  S_SetSfxVolume(sfxVolume);
187
  S_SetSfxVolume(sfxVolume);
189
  // No music with Linux - another dummy.
188
  // No music with Linux - another dummy.
190
  S_SetMusicVolume(musicVolume);
189
  S_SetMusicVolume(musicVolume);
191
 
190
 
192
  // Allocating the internal channels for mixing
191
  // Allocating the internal channels for mixing
193
  // (the maximum numer of sounds rendered
192
  // (the maximum numer of sounds rendered
194
  // simultaneously) within zone memory.
193
  // simultaneously) within zone memory.
195
  channels =
194
  channels =
196
    (channel_t *) Z_Malloc(numChannels*sizeof(channel_t), PU_STATIC, 0);
195
    (channel_t *) Z_Malloc(numChannels*sizeof(channel_t), PU_STATIC, 0);
197
  
196
  
198
  // Free all channels for use
197
  // Free all channels for use
199
  for (i=0 ; i
198
  for (i=0 ; i
200
    channels[i].sfxinfo = 0;
199
    channels[i].sfxinfo = 0;
201
  
200
  
202
  // no sounds are playing, and they are not mus_paused
201
  // no sounds are playing, and they are not mus_paused
203
  mus_paused = 0;
202
  mus_paused = 0;
204
 
203
 
205
  // Note that sounds have not been cached (yet).
204
  // Note that sounds have not been cached (yet).
206
  for (i=1 ; i
205
  for (i=1 ; i
207
    S_sfx[i].lumpnum = S_sfx[i].usefulness = -1;
206
    S_sfx[i].lumpnum = S_sfx[i].usefulness = -1;
208
 
207
 
209
 
208
 
210
/********
209
/********
211
  if((ver = InitSound())< SOUND_VERSION )
210
  if((ver = InitSound())< SOUND_VERSION )
212
  {  
211
  {  
213
     printf("Sound service version mismatch\n\r");
212
     printf("Sound service version mismatch\n\r");
214
     printf("Installed version: %d, required version %d\n\r",
213
     printf("Installed version: %d, required version %d\n\r",
215
             ver, SOUND_VERSION);
214
             ver, SOUND_VERSION);
216
  };
215
  };
217
 
216
 
218
  hMixBuff = CreateBuffer(PCM_2_16_11,0);
217
  hMixBuff = CreateBuffer(PCM_2_16_11,0);
219
 
218
 
220
*********/
219
*********/
221
 
220
 
222
   thread_stack = UserAlloc(4096);
221
   thread_stack = UserAlloc(4096);
223
   thread_stack+=4092;
222
   thread_stack+=4092;
224
 
223
 
225
   sound_state=1;  
224
   sound_state=1;  
226
   CreateThread(sound_proc, thread_stack);
225
   CreateThread(sound_proc, thread_stack);
227
}
226
}
228
 
227
 
229
typedef struct
228
typedef struct
230
{
229
{
231
  unsigned int  code;
230
  unsigned int  code;
232
  unsigned int  sender;
231
  unsigned int  sender;
233
  unsigned int  stream;
232
  unsigned int  stream;
234
  unsigned int  offset;
233
  unsigned int  offset;
235
  unsigned int  size;
234
  unsigned int  size;
236
  unsigned int  unused;
235
  unsigned int  unused;
237
}SND_EVENT;     
236
}SND_EVENT;     
238
 
237
 
239
unsigned int mix_offset;
238
unsigned int mix_offset;
240
int mix_size;
239
int mix_size;
241
extern signed short *mixbuffer;
240
extern signed short *mixbuffer;
242
 
241
 
243
void sound_proc(void)
242
void sound_proc(void)
244
{
243
{
245
  int ver;
244
  int ver;
-
 
245
  int err;
246
  SND_EVENT evnt;
246
  SND_EVENT evnt;
247
  int i;
247
  int i;
-
 
248
 
-
 
249
  if(err = InitSound(&ver))
-
 
250
  {  
-
 
251
     printf("Error %x Sound service not installed\n\r", err);
-
 
252
     _asm
-
 
253
     {
-
 
254
       mov eax, -1
-
 
255
       int 0x40
-
 
256
     };      
-
 
257
  }
-
 
258
 
248
    
259
    
249
  if((ver = InitSound())< SOUND_VERSION )
260
  if(ver< SOUND_VERSION)
250
  {  
261
  {  
251
     printf("Sound service version mismatch\n\r");
262
     printf("Sound service version mismatch\n\r");
252
     printf("Installed version: %d, required version %d\n\r",
263
     printf("Installed version: %d, required version %d\n\r",
253
             ver, SOUND_VERSION);
264
             ver, SOUND_VERSION);
-
 
265
     _asm
-
 
266
     {
-
 
267
       mov eax, -1
-
 
268
       int 0x40
-
 
269
     };      
254
  };
270
  };
255
 
271
 
-
 
272
  if (err = CreateBuffer(PCM_2_16_11|PCM_RING,0, &hMixBuff))
-
 
273
  {
-
 
274
    printf("Error %x sound not available\n\r", err);
-
 
275
    printf("handle = %x\n\r", hMixBuff);
-
 
276
    _asm
-
 
277
    {
-
 
278
      mov eax, -1
-
 
279
      int 0x40
-
 
280
    };      
256
  hMixBuff=CreateBuffer(PCM_2_16_11|PCM_RING,0);
281
  }
257
  
282
  
258
  if(!hMixBuff)
283
  if(err = GetBufferSize(hMixBuff, &mix_size))
-
 
284
  {
259
  {
285
    printf("Error %x get buffer size\n\r", err);
260
    printf("sound not available\n\r");
286
    printf("size = %x\n\r", mix_size);
261
    _asm
287
    _asm
262
    {
288
    {
263
      mov eax, -1
289
      mov eax, -1
264
      int 0x40
290
      int 0x40
265
    };      
291
    };      
266
  };
292
  };
-
 
293
  mix_size= mix_size/2;
267
 
-
 
268
  mix_size=GetBufferSize(hMixBuff)/2;
294
      
-
 
295
  printf("mixer size %d\n\r", mix_size);
-
 
296
 
-
 
297
  if(err=PlayBuffer(hMixBuff, 0))
-
 
298
  {
-
 
299
    printf("Error %x play buffer\n\r", err);
-
 
300
    _asm
-
 
301
    {
-
 
302
      mov eax, -1
-
 
303
      int 0x40
-
 
304
    };      
269
  printf("mixer size %d\n\r", mix_size);
305
  }
270
 
-
 
271
  mixbuffer = malloc(mix_size);
306
  
272
  
307
  mixbuffer = malloc(mix_size);
273
  PlayBuffer(hMixBuff, 0); 
308
  
274
  while(sound_state)
309
  while(sound_state)
275
  {
310
  {
276
     GetNotify(&evnt);
311
     GetNotify(&evnt);
277
 
312
 
278
     if(evnt.code != 0xFF000001)
313
     if(evnt.code != 0xFF000001)
279
     {
314
     {
280
       printf("invalid code %d\n\r", evnt.code);
315
       printf("invalid code %d\n\r", evnt.code);
281
       continue; 
316
       continue; 
282
     }    
317
     }    
283
     
318
     
284
     if(evnt.stream != hMixBuff)
319
     if(evnt.stream != hMixBuff)
285
     {
320
     {
286
       printf("invalid stream %d hMixBuff= %d\n\r", evnt.stream, hMixBuff);
321
       printf("invalid stream %d hMixBuff= %d\n\r", evnt.stream, hMixBuff);
287
       continue; 
322
       continue; 
288
     };
323
     };
289
     mix_offset= evnt.offset;
324
     mix_offset= evnt.offset;
290
     I_UpdateSound();
325
     I_UpdateSound();
291
  };
326
  };
292
 
327
 
293
  //flush sound buffers 
328
  //flush sound buffers 
294
 
329
 
295
  for(i=0; i<32; i++)
330
  for(i=0; i<32; i++)
296
  {
331
  {
297
     GetNotify(&evnt);
332
     GetNotify(&evnt);
298
 
333
 
299
     if(evnt.code != 0xFF000001)
334
     if(evnt.code != 0xFF000001)
300
     {
335
     {
301
       printf("invalid code %d\n\r", evnt.code);
336
       printf("invalid code %d\n\r", evnt.code);
302
       continue; 
337
       continue; 
303
     }    
338
     }    
304
     
339
     
305
     if(evnt.stream != hMixBuff)
340
     if(evnt.stream != hMixBuff)
306
     {
341
     {
307
       printf("invalid stream %d hMixBuff= %d\n\r", evnt.stream, hMixBuff);
342
       printf("invalid stream %d hMixBuff= %d\n\r", evnt.stream, hMixBuff);
308
       continue; 
343
       continue; 
309
     };
344
     };
310
     mix_offset= evnt.offset;
345
     mix_offset= evnt.offset;
311
     I_UpdateSound();
346
     I_UpdateSound();
312
  };  
347
  };  
313
  
348
  
314
  _asm
349
  _asm
315
  {
350
  {
316
    mov eax, -1
351
    mov eax, -1
317
    int 0x40
352
    int 0x40
318
  };      
353
  };      
319
 
354
 
320
};
355
};
321
 
356
 
322
//
357
//
323
// Per level startup code.
358
// Per level startup code.
324
// Kills playing sounds at start of level,
359
// Kills playing sounds at start of level,
325
//  determines music if any, changes music.
360
//  determines music if any, changes music.
326
//
361
//
327
void S_Start(void)
362
void S_Start(void)
328
{
363
{
329
  int cnum;
364
  int cnum;
330
  int mnum;
365
  int mnum;
331
 
366
 
332
  // kill all playing sounds at start of level
367
  // kill all playing sounds at start of level
333
  //  (trust me - a good idea)
368
  //  (trust me - a good idea)
334
  for (cnum=0 ; cnum
369
  for (cnum=0 ; cnum
335
    if (channels[cnum].sfxinfo)
370
    if (channels[cnum].sfxinfo)
336
      S_StopChannel(cnum);
371
      S_StopChannel(cnum);
337
  
372
  
338
  // start new music for the level
373
  // start new music for the level
339
  mus_paused = 0;
374
  mus_paused = 0;
340
  
375
  
341
  if (gamemode == commercial)
376
  if (gamemode == commercial)
342
          mnum = mus_runnin + gamemap - 1;
377
          mnum = mus_runnin + gamemap - 1;
343
  else
378
  else
344
  {
379
  {
345
          int spmus[]=
380
          int spmus[]=
346
          {
381
          {
347
                  // Song - Who? - Where?
382
                  // Song - Who? - Where?
348
                mus_e3m4,       // American     e4m1
383
                mus_e3m4,       // American     e4m1
349
                mus_e3m2,       // Romero       e4m2
384
                mus_e3m2,       // Romero       e4m2
350
                mus_e3m3,       // Shawn        e4m3
385
                mus_e3m3,       // Shawn        e4m3
351
                mus_e1m5,       // American     e4m4
386
                mus_e1m5,       // American     e4m4
352
                mus_e2m7,       // Tim  e4m5
387
                mus_e2m7,       // Tim  e4m5
353
                mus_e2m4,       // Romero       e4m6
388
                mus_e2m4,       // Romero       e4m6
354
                mus_e2m6,       // J.Anderson   e4m7 CHIRON.WAD
389
                mus_e2m6,       // J.Anderson   e4m7 CHIRON.WAD
355
                mus_e2m5,       // Shawn        e4m8
390
                mus_e2m5,       // Shawn        e4m8
356
                mus_e1m9        // Tim          e4m9
391
                mus_e1m9        // Tim          e4m9
357
          };
392
          };
358
          
393
          
359
          if (gameepisode < 4)
394
          if (gameepisode < 4)
360
                  mnum = mus_e1m1 + (gameepisode-1)*9 + gamemap-1;
395
                  mnum = mus_e1m1 + (gameepisode-1)*9 + gamemap-1;
361
          else
396
          else
362
                  mnum = spmus[gamemap-1];
397
                  mnum = spmus[gamemap-1];
363
  }     
398
  }     
364
  
399
  
365
  // HACK FOR COMMERCIAL
400
  // HACK FOR COMMERCIAL
366
  //  if (commercial && mnum > mus_e3m9)        
401
  //  if (commercial && mnum > mus_e3m9)        
367
  //      mnum -= mus_e3m9;
402
  //      mnum -= mus_e3m9;
368
  
403
  
369
  S_ChangeMusic(mnum, true);
404
  S_ChangeMusic(mnum, true);
370
  
405
  
371
  nextcleanup = 15;
406
  nextcleanup = 15;
372
}       
407
}       
373
 
408
 
374
void S_StartSoundAtVolume( void *origin_p, int sfx_id, int volume )
409
void S_StartSoundAtVolume( void *origin_p, int sfx_id, int volume )
375
   {
410
   {
376
    int        rc;
411
    int        rc;
377
    int        sep;
412
    int        sep;
378
    int        pitch;
413
    int        pitch;
379
    int        priority;
414
    int        priority;
380
    sfxinfo_t *sfx;
415
    sfxinfo_t *sfx;
381
    int        cnum;
416
    int        cnum;
382
    // int        chnum;  <-- 10.9.98 compiler warning
417
    // int        chnum;  <-- 10.9.98 compiler warning
383
  
418
  
384
    mobj_t*     origin = (mobj_t *)origin_p;
419
    mobj_t*     origin = (mobj_t *)origin_p;
385
  
420
  
386
    //WriteDebug("S_StartSoundAtVolume...\n");
421
    //WriteDebug("S_StartSoundAtVolume...\n");
387
  
422
  
388
    // Debug.
423
    // Debug.
389
    /*fprintf( stderr,"S_StartSoundAtVolume: playing sound %d (%s)\n",sfx_id, S_sfx[sfx_id].name );*/
424
    /*fprintf( stderr,"S_StartSoundAtVolume: playing sound %d (%s)\n",sfx_id, S_sfx[sfx_id].name );*/
390
  
425
  
391
    // check for bogus sound #
426
    // check for bogus sound #
392
    if (sfx_id < 1 || sfx_id > NUMSFX)
427
    if (sfx_id < 1 || sfx_id > NUMSFX)
393
        I_Error("Bad sfx #: %d", sfx_id);
428
        I_Error("Bad sfx #: %d", sfx_id);
394
  
429
  
395
    sfx = &S_sfx[sfx_id];
430
    sfx = &S_sfx[sfx_id];
396
  
431
  
397
    // Initialize sound parameters
432
    // Initialize sound parameters
398
    if (sfx->link)
433
    if (sfx->link)
399
       {
434
       {
400
        pitch = sfx->pitch;
435
        pitch = sfx->pitch;
401
        priority = sfx->priority;
436
        priority = sfx->priority;
402
        volume += sfx->volume;
437
        volume += sfx->volume;
403
    
438
    
404
        if (volume < 1)
439
        if (volume < 1)
405
           {
440
           {
406
            //WriteDebug("Volume off...\n");
441
            //WriteDebug("Volume off...\n");
407
            return;
442
            return;
408
           }
443
           }
409
    
444
    
410
        if (volume > snd_SfxVolume)
445
        if (volume > snd_SfxVolume)
411
            volume = snd_SfxVolume;
446
            volume = snd_SfxVolume;
412
       }        
447
       }        
413
    else
448
    else
414
       {
449
       {
415
        pitch = NORM_PITCH;
450
        pitch = NORM_PITCH;
416
        priority = NORM_PRIORITY;
451
        priority = NORM_PRIORITY;
417
       }
452
       }
418
 
453
 
419
 
454
 
420
    // Check to see if it is audible,
455
    // Check to see if it is audible,
421
    //  and if not, modify the params
456
    //  and if not, modify the params
422
    if (origin && origin != players[consoleplayer].mo)
457
    if (origin && origin != players[consoleplayer].mo)
423
       {
458
       {
424
        rc = S_AdjustSoundParams(players[consoleplayer].mo, origin, &volume, &sep, &pitch);
459
        rc = S_AdjustSoundParams(players[consoleplayer].mo, origin, &volume, &sep, &pitch);
425
        if ( origin->x == players[consoleplayer].mo->x && origin->y == players[consoleplayer].mo->y)
460
        if ( origin->x == players[consoleplayer].mo->x && origin->y == players[consoleplayer].mo->y)
426
           {    
461
           {    
427
            sep = NORM_SEP;
462
            sep = NORM_SEP;
428
           }
463
           }
429
        if (!rc)
464
        if (!rc)
430
           {
465
           {
431
            //WriteDebug("No rc from S_AdjustSoundParams...\n");
466
            //WriteDebug("No rc from S_AdjustSoundParams...\n");
432
            return;
467
            return;
433
           }
468
           }
434
       }        
469
       }        
435
    else
470
    else
436
       {
471
       {
437
        sep = NORM_SEP;
472
        sep = NORM_SEP;
438
       }
473
       }
439
  
474
  
440
    // hacks to vary the sfx pitches
475
    // hacks to vary the sfx pitches
441
    if (sfx_id >= sfx_sawup && sfx_id <= sfx_sawhit)
476
    if (sfx_id >= sfx_sawup && sfx_id <= sfx_sawhit)
442
       {        
477
       {        
443
        pitch += 8 - (M_Random()&15);
478
        pitch += 8 - (M_Random()&15);
444
        if (pitch < 0)
479
        if (pitch < 0)
445
            pitch = 0;
480
            pitch = 0;
446
        else
481
        else
447
        if (pitch>255)
482
        if (pitch>255)
448
            pitch = 255;
483
            pitch = 255;
449
       }
484
       }
450
    else
485
    else
451
    if (sfx_id != sfx_itemup && sfx_id != sfx_tink)
486
    if (sfx_id != sfx_itemup && sfx_id != sfx_tink)
452
       {
487
       {
453
        pitch += 16 - (M_Random()&31);
488
        pitch += 16 - (M_Random()&31);
454
        if (pitch<0)
489
        if (pitch<0)
455
            pitch = 0;
490
            pitch = 0;
456
        else
491
        else
457
        if (pitch > 255)
492
        if (pitch > 255)
458
            pitch = 255;
493
            pitch = 255;
459
       }
494
       }
460
 
495
 
461
  // kill old sound
496
  // kill old sound
462
  S_StopSound(origin);
497
  S_StopSound(origin);
463
 
498
 
464
  // try to find a channel
499
  // try to find a channel
465
  cnum = S_getChannel(origin, sfx, sfx_id);
500
  cnum = S_getChannel(origin, sfx, sfx_id);
466
  
501
  
467
  if (cnum<0)
502
  if (cnum<0)
468
     {
503
     {
469
        //WriteDebug("cnum < 0 -- no channel...\n");
504
        //WriteDebug("cnum < 0 -- no channel...\n");
470
    return;
505
    return;
471
     }
506
     }
472
 
507
 
473
  //
508
  //
474
  // This is supposed to handle the loading/caching.
509
  // This is supposed to handle the loading/caching.
475
  // For some odd reason, the caching is done nearly
510
  // For some odd reason, the caching is done nearly
476
  //  each time the sound is needed?
511
  //  each time the sound is needed?
477
  //
512
  //
478
  
513
  
479
  // get lumpnum if necessary
514
  // get lumpnum if necessary
480
  if (sfx->lumpnum < 0)
515
  if (sfx->lumpnum < 0)
481
    sfx->lumpnum = I_GetSfxLumpNum(sfx);
516
    sfx->lumpnum = I_GetSfxLumpNum(sfx);
482
 
517
 
483
  // cache data if necessary
518
  // cache data if necessary
484
  if (!sfx->data)
519
  if (!sfx->data)
485
  {
520
  {
486
    sfx->data = (void *) W_CacheLumpNum(sfx->lumpnum, PU_MUSIC);
521
    sfx->data = (void *) W_CacheLumpNum(sfx->lumpnum, PU_MUSIC);
487
    
522
    
488
  }
523
  }
489
  
524
  
490
  // increase the usefulness
525
  // increase the usefulness
491
  if (sfx->usefulness++ < 0)
526
  if (sfx->usefulness++ < 0)
492
      sfx->usefulness = 1;
527
      sfx->usefulness = 1;
493
  
528
  
494
  // Assigns the handle to one of the channels in the
529
  // Assigns the handle to one of the channels in the
495
  //  mix/output buffer.
530
  //  mix/output buffer.
496
  //WriteDebug("I_StartSound...\n");
531
  //WriteDebug("I_StartSound...\n");
497
  channels[cnum].handle = I_StartSound(sfx_id,volume,sep,pitch,priority);
532
  channels[cnum].handle = I_StartSound(sfx_id,volume,sep,pitch,priority);
498
  channels[cnum].handle = cnum;
533
  channels[cnum].handle = cnum;
499
  channels[cnum].sfxinfo = sfx;
534
  channels[cnum].sfxinfo = sfx;
500
  channels[cnum].origin = origin;
535
  channels[cnum].origin = origin;
501
}       
536
}       
502
 
537
 
503
void S_StartSound( void *origin, int sfx_id )
538
void S_StartSound( void *origin, int sfx_id )
504
   {
539
   {
505
#ifdef SAWDEBUG
540
#ifdef SAWDEBUG
506
    // if (sfx_id == sfx_sawful)
541
    // if (sfx_id == sfx_sawful)
507
    // sfx_id = sfx_itemup;
542
    // sfx_id = sfx_itemup;
508
#endif
543
#endif
509
 
544
 
510
    S_StartSoundAtVolume(origin, sfx_id, snd_SfxVolume);
545
    S_StartSoundAtVolume(origin, sfx_id, snd_SfxVolume);
511
 
546
 
512
 
547
 
513
    // UNUSED. We had problems, had we not?
548
    // UNUSED. We had problems, had we not?
514
#ifdef SAWDEBUG
549
#ifdef SAWDEBUG
515
{
550
{
516
    int i;
551
    int i;
517
    int n;
552
    int n;
518
        
553
        
519
    static mobj_t*      last_saw_origins[10] = {1,1,1,1,1,1,1,1,1,1};
554
    static mobj_t*      last_saw_origins[10] = {1,1,1,1,1,1,1,1,1,1};
520
    static int          first_saw=0;
555
    static int          first_saw=0;
521
    static int          next_saw=0;
556
    static int          next_saw=0;
522
        
557
        
523
    if (sfx_id == sfx_sawidl
558
    if (sfx_id == sfx_sawidl
524
        || sfx_id == sfx_sawful
559
        || sfx_id == sfx_sawful
525
        || sfx_id == sfx_sawhit)
560
        || sfx_id == sfx_sawhit)
526
    {
561
    {
527
        for (i=first_saw;i!=next_saw;i=(i+1)%10)
562
        for (i=first_saw;i!=next_saw;i=(i+1)%10)
528
            if (last_saw_origins[i] != origin)
563
            if (last_saw_origins[i] != origin)
529
                fprintf(stderr, "old origin 0x%lx != "
564
                fprintf(stderr, "old origin 0x%lx != "
530
                        "origin 0x%lx for sfx %d\n",
565
                        "origin 0x%lx for sfx %d\n",
531
                        last_saw_origins[i],
566
                        last_saw_origins[i],
532
                        origin,
567
                        origin,
533
                        sfx_id);
568
                        sfx_id);
534
            
569
            
535
        last_saw_origins[next_saw] = origin;
570
        last_saw_origins[next_saw] = origin;
536
        next_saw = (next_saw + 1) % 10;
571
        next_saw = (next_saw + 1) % 10;
537
        if (next_saw == first_saw)
572
        if (next_saw == first_saw)
538
            first_saw = (first_saw + 1) % 10;
573
            first_saw = (first_saw + 1) % 10;
539
            
574
            
540
        for (n=i=0; i
575
        for (n=i=0; i
541
        {
576
        {
542
            if (channels[i].sfxinfo == &S_sfx[sfx_sawidl]
577
            if (channels[i].sfxinfo == &S_sfx[sfx_sawidl]
543
                || channels[i].sfxinfo == &S_sfx[sfx_sawful]
578
                || channels[i].sfxinfo == &S_sfx[sfx_sawful]
544
                || channels[i].sfxinfo == &S_sfx[sfx_sawhit]) n++;
579
                || channels[i].sfxinfo == &S_sfx[sfx_sawhit]) n++;
545
        }
580
        }
546
            
581
            
547
        if (n>1)
582
        if (n>1)
548
        {
583
        {
549
            for (i=0; i
584
            for (i=0; i
550
            {
585
            {
551
                if (channels[i].sfxinfo == &S_sfx[sfx_sawidl]
586
                if (channels[i].sfxinfo == &S_sfx[sfx_sawidl]
552
                    || channels[i].sfxinfo == &S_sfx[sfx_sawful]
587
                    || channels[i].sfxinfo == &S_sfx[sfx_sawful]
553
                    || channels[i].sfxinfo == &S_sfx[sfx_sawhit])
588
                    || channels[i].sfxinfo == &S_sfx[sfx_sawhit])
554
                {
589
                {
555
                    fprintf(stderr,
590
                    fprintf(stderr,
556
                            "chn: sfxinfo=0x%lx, origin=0x%lx, "
591
                            "chn: sfxinfo=0x%lx, origin=0x%lx, "
557
                            "handle=%d\n",
592
                            "handle=%d\n",
558
                            channels[i].sfxinfo,
593
                            channels[i].sfxinfo,
559
                            channels[i].origin,
594
                            channels[i].origin,
560
                            channels[i].handle);
595
                            channels[i].handle);
561
                }
596
                }
562
            }
597
            }
563
            fprintf(stderr, "\n");
598
            fprintf(stderr, "\n");
564
        }
599
        }
565
    }
600
    }
566
}
601
}
567
#endif
602
#endif
568
 
603
 
569
}
604
}
570
 
605
 
571
 
606
 
572
 
607
 
573
 
608
 
574
void S_StopSound(void *origin)
609
void S_StopSound(void *origin)
575
{
610
{
576
 
611
 
577
    int cnum;
612
    int cnum;
578
 
613
 
579
    for (cnum=0 ; cnum
614
    for (cnum=0 ; cnum
580
    {
615
    {
581
        if (channels[cnum].sfxinfo && channels[cnum].origin == origin)
616
        if (channels[cnum].sfxinfo && channels[cnum].origin == origin)
582
        {
617
        {
583
            S_StopChannel(cnum);
618
            S_StopChannel(cnum);
584
            break;
619
            break;
585
        }
620
        }
586
    }
621
    }
587
}
622
}
588
 
623
 
589
 
624
 
590
 
625
 
591
 
626
 
592
 
627
 
593
 
628
 
594
 
629
 
595
 
630
 
596
 
631
 
597
//
632
//
598
// Stop and resume music, during game PAUSE.
633
// Stop and resume music, during game PAUSE.
599
//
634
//
600
void S_PauseSound(void)
635
void S_PauseSound(void)
601
{
636
{
602
    if (mus_playing && !mus_paused)
637
    if (mus_playing && !mus_paused)
603
    {
638
    {
604
        I_PauseSong(mus_playing->handle);
639
        I_PauseSong(mus_playing->handle);
605
        mus_paused = true;
640
        mus_paused = true;
606
    }
641
    }
607
}
642
}
608
 
643
 
609
void S_ResumeSound(void)
644
void S_ResumeSound(void)
610
{
645
{
611
    if (mus_playing && mus_paused)
646
    if (mus_playing && mus_paused)
612
    {
647
    {
613
        I_ResumeSong(mus_playing->handle);
648
        I_ResumeSong(mus_playing->handle);
614
        mus_paused = false;
649
        mus_paused = false;
615
    }
650
    }
616
}
651
}
617
 
652
 
618
 
653
 
619
//
654
//
620
// Updates music & sounds
655
// Updates music & sounds
621
//
656
//
622
void S_UpdateSounds(void* listener_p)
657
void S_UpdateSounds(void* listener_p)
623
{
658
{
624
    int         audible;
659
    int         audible;
625
    int         cnum;
660
    int         cnum;
626
    int         volume;
661
    int         volume;
627
    int         sep;
662
    int         sep;
628
    int         pitch;
663
    int         pitch;
629
    sfxinfo_t*  sfx;
664
    sfxinfo_t*  sfx;
630
    channel_t*  c;
665
    channel_t*  c;
631
    
666
    
632
    mobj_t*     listener = (mobj_t*)listener_p;
667
    mobj_t*     listener = (mobj_t*)listener_p;
633
 
668
 
634
 
669
 
635
    
670
    
636
    // Clean up unused data.
671
    // Clean up unused data.
637
    // This is currently not done for 16bit (sounds cached static).
672
    // This is currently not done for 16bit (sounds cached static).
638
    // DOS 8bit remains. 
673
    // DOS 8bit remains. 
639
    /*if (gametic > nextcleanup)
674
    /*if (gametic > nextcleanup)
640
    {
675
    {
641
        for (i=1 ; i
676
        for (i=1 ; i
642
        {
677
        {
643
            if (S_sfx[i].usefulness < 1
678
            if (S_sfx[i].usefulness < 1
644
                && S_sfx[i].usefulness > -1)
679
                && S_sfx[i].usefulness > -1)
645
            {
680
            {
646
                if (--S_sfx[i].usefulness == -1)
681
                if (--S_sfx[i].usefulness == -1)
647
                {
682
                {
648
                    Z_ChangeTag(S_sfx[i].data, PU_CACHE);
683
                    Z_ChangeTag(S_sfx[i].data, PU_CACHE);
649
                    S_sfx[i].data = 0;
684
                    S_sfx[i].data = 0;
650
                }
685
                }
651
            }
686
            }
652
        }
687
        }
653
        nextcleanup = gametic + 15;
688
        nextcleanup = gametic + 15;
654
    }*/
689
    }*/
655
    
690
    
656
    for (cnum=0 ; cnum
691
    for (cnum=0 ; cnum
657
    {
692
    {
658
        c = &channels[cnum];
693
        c = &channels[cnum];
659
        sfx = c->sfxinfo;
694
        sfx = c->sfxinfo;
660
 
695
 
661
        if (c->sfxinfo)
696
        if (c->sfxinfo)
662
        {
697
        {
663
            if (I_SoundIsPlaying(c->handle))
698
            if (I_SoundIsPlaying(c->handle))
664
            {
699
            {
665
                // initialize parameters
700
                // initialize parameters
666
                volume = snd_SfxVolume;
701
                volume = snd_SfxVolume;
667
                pitch = NORM_PITCH;
702
                pitch = NORM_PITCH;
668
                sep = NORM_SEP;
703
                sep = NORM_SEP;
669
 
704
 
670
                if (sfx->link)
705
                if (sfx->link)
671
                {
706
                {
672
                    pitch = sfx->pitch;
707
                    pitch = sfx->pitch;
673
                    volume += sfx->volume;
708
                    volume += sfx->volume;
674
                    if (volume < 1)
709
                    if (volume < 1)
675
                    {
710
                    {
676
                        S_StopChannel(cnum);
711
                        S_StopChannel(cnum);
677
                        continue;
712
                        continue;
678
                    }
713
                    }
679
                    else if (volume > snd_SfxVolume)
714
                    else if (volume > snd_SfxVolume)
680
                    {
715
                    {
681
                        volume = snd_SfxVolume;
716
                        volume = snd_SfxVolume;
682
                    }
717
                    }
683
                }
718
                }
684
 
719
 
685
                // check non-local sounds for distance clipping
720
                // check non-local sounds for distance clipping
686
                //  or modify their params
721
                //  or modify their params
687
                if (c->origin && listener_p != c->origin)
722
                if (c->origin && listener_p != c->origin)
688
                {
723
                {
689
                    audible = S_AdjustSoundParams(listener,
724
                    audible = S_AdjustSoundParams(listener,
690
                                                  c->origin,
725
                                                  c->origin,
691
                                                  &volume,
726
                                                  &volume,
692
                                                  &sep,
727
                                                  &sep,
693
                                                  &pitch);
728
                                                  &pitch);
694
                    
729
                    
695
                    if (!audible)
730
                    if (!audible)
696
                    {
731
                    {
697
                        S_StopChannel(cnum);
732
                        S_StopChannel(cnum);
698
                    }
733
                    }
699
                    else
734
                    else
700
                        I_UpdateSoundParams(c->handle, volume, sep, pitch);
735
                        I_UpdateSoundParams(c->handle, volume, sep, pitch);
701
                }
736
                }
702
            }
737
            }
703
            else
738
            else
704
            {
739
            {
705
                // if channel is allocated but sound has stopped,
740
                // if channel is allocated but sound has stopped,
706
                //  free it
741
                //  free it
707
                S_StopChannel(cnum);
742
                S_StopChannel(cnum);
708
            }
743
            }
709
        }
744
        }
710
    }
745
    }
711
    // kill music if it is a single-play && finished
746
    // kill music if it is a single-play && finished
712
    // if (     mus_playing
747
    // if (     mus_playing
713
    //      && !I_QrySongPlaying(mus_playing->handle)
748
    //      && !I_QrySongPlaying(mus_playing->handle)
714
    //      && !mus_paused )
749
    //      && !mus_paused )
715
    // S_StopMusic();
750
    // S_StopMusic();
716
}
751
}
717
 
752
 
718
 
753
 
719
void S_SetMusicVolume(int volume)
754
void S_SetMusicVolume(int volume)
720
{
755
{
721
    if (volume < 0 || volume > 127)
756
    if (volume < 0 || volume > 127)
722
    {
757
    {
723
        I_Error("Attempt to set music volume at %d",
758
        I_Error("Attempt to set music volume at %d",
724
                volume);
759
                volume);
725
    }    
760
    }    
726
 
761
 
727
    I_SetMusicVolume(127);
762
    I_SetMusicVolume(127);
728
    I_SetMusicVolume(volume);
763
    I_SetMusicVolume(volume);
729
    snd_MusicVolume = volume;
764
    snd_MusicVolume = volume;
730
}
765
}
731
 
766
 
732
 
767
 
733
 
768
 
734
void S_SetSfxVolume(int volume)
769
void S_SetSfxVolume(int volume)
735
{
770
{
736
 
771
 
737
    if (volume < 0 || volume > 127)
772
    if (volume < 0 || volume > 127)
738
        I_Error("Attempt to set sfx volume at %d", volume);
773
        I_Error("Attempt to set sfx volume at %d", volume);
739
 
774
 
740
    snd_SfxVolume = volume;
775
    snd_SfxVolume = volume;
741
 
776
 
742
}
777
}
743
 
778
 
744
//
779
//
745
// Starts some music with the music id found in sounds.h.
780
// Starts some music with the music id found in sounds.h.
746
//
781
//
747
void S_StartMusic(int m_id)
782
void S_StartMusic(int m_id)
748
{
783
{
749
//    S_ChangeMusic(m_id, false);
784
//    S_ChangeMusic(m_id, false);
750
}
785
}
751
 
786
 
752
 
787
 
753
// clean-up&code for orig midi 10.9.98-dlw
788
// clean-up&code for orig midi 10.9.98-dlw
754
void S_ChangeMusic(int musicnum, int looping)
789
void S_ChangeMusic(int musicnum, int looping)
755
{
790
{
756
 
791
 
757
    // I_PlaySong(music->handle, looping);
792
    // I_PlaySong(music->handle, looping);
758
//      mus_playing = music;
793
//      mus_playing = music;
759
}
794
}
760
 
795
 
761
 
796
 
762
void S_StopMusic(void)
797
void S_StopMusic(void)
763
{
798
{
764
    if (mus_playing)
799
    if (mus_playing)
765
    {
800
    {
766
                if (mus_paused) I_ResumeSong(mus_playing->handle);
801
                if (mus_paused) I_ResumeSong(mus_playing->handle);
767
                I_StopSong(mus_playing->handle);
802
                I_StopSong(mus_playing->handle);
768
                I_UnRegisterSong(mus_playing->handle);
803
                I_UnRegisterSong(mus_playing->handle);
769
                Z_ChangeTag(mus_playing->data, PU_CACHE);
804
                Z_ChangeTag(mus_playing->data, PU_CACHE);
770
                mus_playing->data = 0;
805
                mus_playing->data = 0;
771
                mus_playing = 0;
806
                mus_playing = 0;
772
    }
807
    }
773
}
808
}
774
 
809
 
775
void S_StopChannel(int cnum)
810
void S_StopChannel(int cnum)
776
{
811
{
777
 
812
 
778
    int         i;
813
    int         i;
779
    channel_t*  c = &channels[cnum];
814
    channel_t*  c = &channels[cnum];
780
 
815
 
781
    if (c->sfxinfo)
816
    if (c->sfxinfo)
782
    {
817
    {
783
        // stop the sound playing
818
        // stop the sound playing
784
        if (I_SoundIsPlaying(c->handle))
819
        if (I_SoundIsPlaying(c->handle))
785
        {
820
        {
786
#ifdef SAWDEBUG
821
#ifdef SAWDEBUG
787
            if (c->sfxinfo == &S_sfx[sfx_sawful])
822
            if (c->sfxinfo == &S_sfx[sfx_sawful])
788
                fprintf(stderr, "stopped\n");
823
                fprintf(stderr, "stopped\n");
789
#endif
824
#endif
790
            I_StopSound(c->handle);
825
            I_StopSound(c->handle);
791
        }
826
        }
792
 
827
 
793
        // check to see
828
        // check to see
794
        //  if other channels are playing the sound
829
        //  if other channels are playing the sound
795
        for (i=0 ; i
830
        for (i=0 ; i
796
        {
831
        {
797
            if (cnum != i
832
            if (cnum != i
798
                && c->sfxinfo == channels[i].sfxinfo)
833
                && c->sfxinfo == channels[i].sfxinfo)
799
            {
834
            {
800
                break;
835
                break;
801
            }
836
            }
802
        }
837
        }
803
        
838
        
804
        // degrade usefulness of sound data
839
        // degrade usefulness of sound data
805
        c->sfxinfo->usefulness--;
840
        c->sfxinfo->usefulness--;
806
 
841
 
807
        c->sfxinfo = 0;
842
        c->sfxinfo = 0;
808
    }
843
    }
809
}
844
}
810
 
845
 
811
 
846
 
812
//
847
//
813
// Changes volume, stereo-separation, and pitch variables
848
// Changes volume, stereo-separation, and pitch variables
814
//  from the norm of a sound effect to be played.
849
//  from the norm of a sound effect to be played.
815
// If the sound is not audible, returns a 0.
850
// If the sound is not audible, returns a 0.
816
// Otherwise, modifies parameters and returns 1.
851
// Otherwise, modifies parameters and returns 1.
817
//
852
//
818
int
853
int
819
S_AdjustSoundParams (mobj_t* listener, mobj_t* source, 
854
S_AdjustSoundParams (mobj_t* listener, mobj_t* source, 
820
                     int* vol, int* sep, int* pitch)
855
                     int* vol, int* sep, int* pitch)
821
{
856
{
822
    fixed_t     approx_dist;
857
    fixed_t     approx_dist;
823
    fixed_t     adx;
858
    fixed_t     adx;
824
    fixed_t     ady;
859
    fixed_t     ady;
825
    angle_t     angle;
860
    angle_t     angle;
826
 
861
 
827
    // calculate the distance to sound origin
862
    // calculate the distance to sound origin
828
    //  and clip it if necessary
863
    //  and clip it if necessary
829
    adx = abs(listener->x - source->x);
864
    adx = abs(listener->x - source->x);
830
    ady = abs(listener->y - source->y);
865
    ady = abs(listener->y - source->y);
831
 
866
 
832
    // From _GG1_ p.428. Appox. eucledian distance fast.
867
    // From _GG1_ p.428. Appox. eucledian distance fast.
833
    approx_dist = adx + ady - ((adx < ady ? adx : ady)>>1);
868
    approx_dist = adx + ady - ((adx < ady ? adx : ady)>>1);
834
    
869
    
835
    if (gamemap != 8
870
    if (gamemap != 8
836
        && approx_dist > S_CLIPPING_DIST)
871
        && approx_dist > S_CLIPPING_DIST)
837
    {
872
    {
838
        return 0;
873
        return 0;
839
    }
874
    }
840
    
875
    
841
    // angle of source to listener
876
    // angle of source to listener
842
    angle = R_PointToAngle2(listener->x,
877
    angle = R_PointToAngle2(listener->x,
843
                            listener->y,
878
                            listener->y,
844
                            source->x,
879
                            source->x,
845
                            source->y);
880
                            source->y);
846
 
881
 
847
    if (angle > listener->angle)
882
    if (angle > listener->angle)
848
        angle = angle - listener->angle;
883
        angle = angle - listener->angle;
849
    else
884
    else
850
        angle = angle + (0xffffffff - listener->angle);
885
        angle = angle + (0xffffffff - listener->angle);
851
 
886
 
852
    angle >>= ANGLETOFINESHIFT;
887
    angle >>= ANGLETOFINESHIFT;
853
 
888
 
854
    // stereo separation
889
    // stereo separation
855
    *sep = 128 - (FixedMul(S_STEREO_SWING,finesine[angle])>>FRACBITS);
890
    *sep = 128 - (FixedMul(S_STEREO_SWING,finesine[angle])>>FRACBITS);
856
 
891
 
857
    // volume calculation
892
    // volume calculation
858
    if (approx_dist < S_CLOSE_DIST)
893
    if (approx_dist < S_CLOSE_DIST)
859
    {
894
    {
860
        *vol = snd_SfxVolume;
895
        *vol = snd_SfxVolume;
861
    }
896
    }
862
    else if (gamemap == 8)
897
    else if (gamemap == 8)
863
    {
898
    {
864
        if (approx_dist > S_CLIPPING_DIST)
899
        if (approx_dist > S_CLIPPING_DIST)
865
            approx_dist = S_CLIPPING_DIST;
900
            approx_dist = S_CLIPPING_DIST;
866
 
901
 
867
        *vol = 15+ ((snd_SfxVolume-15)
902
        *vol = 15+ ((snd_SfxVolume-15)
868
                    *((S_CLIPPING_DIST - approx_dist)>>FRACBITS))
903
                    *((S_CLIPPING_DIST - approx_dist)>>FRACBITS))
869
            / S_ATTENUATOR;
904
            / S_ATTENUATOR;
870
    }
905
    }
871
    else
906
    else
872
    {
907
    {
873
        // distance effect
908
        // distance effect
874
        *vol = (snd_SfxVolume
909
        *vol = (snd_SfxVolume
875
                * ((S_CLIPPING_DIST - approx_dist)>>FRACBITS))
910
                * ((S_CLIPPING_DIST - approx_dist)>>FRACBITS))
876
            / S_ATTENUATOR; 
911
            / S_ATTENUATOR; 
877
    }
912
    }
878
    
913
    
879
    return (*vol > 0);
914
    return (*vol > 0);
880
}
915
}
881
 
916
 
882
//
917
//
883
// S_getChannel :
918
// S_getChannel :
884
//   If none available, return -1.  Otherwise channel #.
919
//   If none available, return -1.  Otherwise channel #.
885
//
920
//
886
int S_getChannel( void *origin, sfxinfo_t *sfxinfo, int sfxid )
921
int S_getChannel( void *origin, sfxinfo_t *sfxinfo, int sfxid )
887
   {
922
   {
888
    // channel number to use
923
    // channel number to use
889
    int         cnum;
924
    int         cnum;
890
        
925
        
891
    channel_t*  c;
926
    channel_t*  c;
892
 
927
 
893
    // Find an open channel
928
    // Find an open channel
894
    //for (cnum = 0; cnum < numChannels; cnum++)
929
    //for (cnum = 0; cnum < numChannels; cnum++)
895
    for (cnum = 0; cnum < NUM_CHANNELS; cnum++)
930
    for (cnum = 0; cnum < NUM_CHANNELS; cnum++)
896
       {
931
       {
897
        if (!channels[cnum].sfxinfo)
932
        if (!channels[cnum].sfxinfo)
898
            break;
933
            break;
899
        else
934
        else
900
        if (origin && channels[cnum].origin == origin)
935
        if (origin && channels[cnum].origin == origin)
901
           {
936
           {
902
            S_StopChannel(cnum);
937
            S_StopChannel(cnum);
903
            break;
938
            break;
904
           }
939
           }
905
       }
940
       }
906
 
941
 
907
    // None available
942
    // None available
908
    if (cnum == NUM_CHANNELS)
943
    if (cnum == NUM_CHANNELS)
909
       {
944
       {
910
        // Look for lower priority
945
        // Look for lower priority
911
        for (cnum = NUMSFX; cnum < NUM_CHANNELS; cnum++)
946
        for (cnum = NUMSFX; cnum < NUM_CHANNELS; cnum++)
912
             if (channels[cnum].sfxinfo->priority >= sfxinfo->priority)
947
             if (channels[cnum].sfxinfo->priority >= sfxinfo->priority)
913
                 break;
948
                 break;
914
 
949
 
915
        if (cnum == numChannels)
950
        if (cnum == numChannels)
916
           {
951
           {
917
            // FUCK!  No lower priority.  Sorry, Charlie.    
952
            // FUCK!  No lower priority.  Sorry, Charlie.    
918
            return -1;
953
            return -1;
919
           }
954
           }
920
        else
955
        else
921
           {
956
           {
922
            // Otherwise, kick out lower priority.
957
            // Otherwise, kick out lower priority.
923
            S_StopChannel(cnum);
958
            S_StopChannel(cnum);
924
           }
959
           }
925
       }
960
       }
926
 
961
 
927
    c = &channels[cnum];
962
    c = &channels[cnum];
928
 
963
 
929
    // channel is decided to be cnum.
964
    // channel is decided to be cnum.
930
    c->sfxinfo = sfxinfo;
965
    c->sfxinfo = sfxinfo;
931
    c->origin = origin;
966
    c->origin = origin;
932
 
967
 
933
    return cnum;
968
    return cnum;
934
   }
969
   }
935
>
970
>
936
>
971
>