Subversion Repositories eduke32

Rev

Rev 1904 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1247 helixhorne 1
//-------------------------------------------------------------------------
2
/*
1652 terminx 3
Copyright (C) 2010 EDuke32 developers and contributors
1247 helixhorne 4
 
1652 terminx 5
This file is part of EDuke32.
1247 helixhorne 6
 
7
EDuke32 is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public License version 2
9
as published by the Free Software Foundation.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 
15
See the GNU General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
*/
21
//-------------------------------------------------------------------------
22
 
23
// Stripped sounds.c for use in Mapster32, breaks all ties to game & music
24
 
25
#include <stdio.h>
26
#include <string.h>
1346 terminx 27
 
1468 terminx 28
#include "compat.h"
1886 helixhorne 29
#include "baselayer.h"
1468 terminx 30
 
1247 helixhorne 31
#include "fx_man.h"
32
#include "osd.h"
33
 
34
#include "cache1d.h"
35
#include "macros.h"
36
#include "mathutil.h"
37
#include "build.h"  // vec3_t
1886 helixhorne 38
#include "editor.h"
1247 helixhorne 39
 
1672 terminx 40
#ifdef WIN32
41
#include "winlayer.h"
42
#endif
43
 
1247 helixhorne 44
#include "sounds_mapster32.h"
45
 
46
#define LOUDESTVOLUME 150
47
#define MUSICANDSFX 5
48
 
49
static char SM32_havesound = 0;
1454 terminx 50
 
1251 helixhorne 51
char SoundToggle = 1;
52
int32_t NumVoices = 32;
1697 helixhorne 53
int32_t MixRate = 44100;
1247 helixhorne 54
 
55
int32_t backflag,g_numEnvSoundsPlaying;
56
 
57
void MUSIC_Update(void) {}  // needed when linking
58
 
1599 terminx 59
void S_Callback(uint32_t);
1247 helixhorne 60
 
61
/*
62
===================
63
=
64
= SoundStartup
65
=
66
===================
67
*/
68
 
1468 terminx 69
 
1247 helixhorne 70
int32_t S_SoundStartup(void)
71
{
1468 terminx 72
    int32_t status;
73
    int32_t fxdevicetype;
1677 terminx 74
    void *initdata = 0;
1247 helixhorne 75
 
76
    // TODO: read config
1697 helixhorne 77
    int32_t FXVolume=220, /*NumVoices=32,*/ NumChannels=2, NumBits=16, ReverseStereo=0;
78
 
1468 terminx 79
    fxdevicetype = ASS_AutoDetect;
1247 helixhorne 80
 
1468 terminx 81
#ifdef WIN32
82
    initdata = (void *) win_gethwnd();
83
#endif
1247 helixhorne 84
 
1468 terminx 85
    status = FX_Init(fxdevicetype, NumVoices, NumChannels, NumBits, MixRate, initdata);
1247 helixhorne 86
    if (status == FX_Ok)
87
    {
88
        FX_SetVolume(FXVolume);
1468 terminx 89
        FX_SetReverseStereo(ReverseStereo);
1599 terminx 90
        status = FX_SetCallBack(S_Callback);
1247 helixhorne 91
    }
92
 
93
    if (status != FX_Ok)
94
    {
95
        initprintf("Sound startup error: %s", FX_ErrorString(FX_Error));
96
        return -2;
97
    }
98
 
99
    SM32_havesound = 1;
1468 terminx 100
 
1247 helixhorne 101
    return 0;
102
}
103
 
104
/*
105
===================
106
=
107
= SoundShutdown
108
=
109
===================
110
*/
111
 
112
void S_SoundShutdown(void)
113
{
114
    int32_t status;
115
 
116
    if (!SM32_havesound)
117
        return;
118
 
119
    status = FX_Shutdown();
120
    if (status != FX_Ok)
121
        initprintf("Sound shutdown error: %s", FX_ErrorString(FX_Error));
122
}
123
 
124
int32_t S_LoadSound(uint32_t num)
125
{
126
    int32_t   fp = -1, l;
127
 
128
    if (!SM32_havesound) return 0;
129
    if (num >= MAXSOUNDS || SoundToggle == 0) return 0;
130
 
131
    if (g_sounds[num].filename == NULL)
132
    {
133
        OSD_Printf(OSD_ERROR "Sound (#%d) not defined!\n",num);
134
        return 0;
135
    }
136
 
1904 helixhorne 137
    if (g_sounds[num].filename1) fp = kopen4loadfrommod(g_sounds[num].filename1,0);//pathsearchmode
138
    if (fp == -1) fp = kopen4loadfrommod(g_sounds[num].filename,0);
1247 helixhorne 139
    if (fp == -1)
140
    {
141
        OSD_Printf(OSDTEXT_RED "Sound %s(#%d) not found!\n",g_sounds[num].filename,num);
142
        return 0;
143
    }
144
 
145
    l = kfilelength(fp);
146
    g_sounds[num].soundsiz = l;
147
 
148
    g_sounds[num].lock = 200;
149
 
150
    allocache((intptr_t *)&g_sounds[num].ptr,l,(char *)&g_sounds[num].lock);
151
    kread(fp, g_sounds[num].ptr , l);
152
    kclose(fp);
153
    return 1;
154
}
155
 
1487 terminx 156
int32_t S_PlaySound3D(int32_t num, int32_t i, const vec3_t *pos)
1247 helixhorne 157
{
1251 helixhorne 158
    int32_t sndist, cx, cy, cz, j/*,k*/;
1247 helixhorne 159
    int32_t pitche,pitchs,cs;
160
    int32_t voice, sndang, ca, pitch;
161
 
162
    //    if(num != 358) return 0;
163
 
164
    if (num >= MAXSOUNDS ||
1316 terminx 165
            !SM32_havesound ||
1247 helixhorne 166
//        ((g_sounds[num].m&8) && ud.lockout) ||
1316 terminx 167
            SoundToggle == 0 ||
168
            g_sounds[num].num > 3 ||
169
            FX_VoiceAvailable(g_sounds[num].pr) == 0)
170
        return -1;
1247 helixhorne 171
 
172
    if (g_sounds[num].m&128)
173
    {
174
        S_PlaySound(num);
175
        return 0;
176
    }
177
 
178
    if (g_sounds[num].m&4)
179
    {
180
        for (j=0; j<MAXSOUNDS; j++)
181
//            for (k=0; k<g_sounds[j].num; k++)
1316 terminx 182
            if ((g_sounds[j].num > 0) && (g_sounds[j].m&4))
183
                return -1;
1247 helixhorne 184
    }
185
 
186
    cx = pos->x;
187
    cy = pos->y;
188
    cz = pos->z;
189
    cs = cursectnum;
190
    ca = ang;
191
 
192
    sndist = FindDistance3D((cx-pos->x),(cy-pos->y),(cz-pos->z)>>4);
193
 
194
    if (i >= 0 && (g_sounds[num].m&16) == 0 && PN == MUSICANDSFX && SLT < 999 && (sector[SECT].lotag&0xff) < 9)
195
        sndist = divscale14(sndist,(SHT+1));
196
 
197
    pitchs = g_sounds[num].ps;
198
    pitche = g_sounds[num].pe;
199
    cx = klabs(pitche-pitchs);
200
 
201
    if (cx)
202
    {
203
        if (pitchs < pitche)
204
            pitch = pitchs + (rand()%cx);
205
        else pitch = pitche + (rand()%cx);
206
    }
207
    else pitch = pitchs;
208
 
209
    sndist += g_sounds[num].vo;
210
    if (sndist < 0) sndist = 0;
211
    if (cs > -1 && sndist && PN != MUSICANDSFX && !cansee(cx,cy,cz-(24<<8),cs,SX,SY,SZ-(24<<8),SECT))
212
        sndist += sndist>>5;
1316 terminx 213
    /*
214
        switch (num)
215
        {
216
        case PIPEBOMB_EXPLODE:
217
        case LASERTRIP_EXPLODE:
218
        case RPG_EXPLODE:
219
            if (sndist > (6144))
220
                sndist = 6144;
221
            if (g_player[screenpeek].ps->cursectnum > -1 && sector[g_player[screenpeek].ps->cursectnum].lotag == 2)
222
                pitch -= 1024;
223
            break;
224
        default:
225
    */
226
    if (cursectnum > -1 && sector[cursectnum].lotag == 2 && (g_sounds[num].m&4) == 0)
227
        pitch = -768;
228
    if (sndist > 31444 && PN != MUSICANDSFX)
229
        return -1;
1247 helixhorne 230
//        break;
231
//    }
232
 
233
    if (g_sounds[num].num > 0 && PN != MUSICANDSFX)
234
    {
235
        if (g_sounds[num].SoundOwner[0].i == i) S_StopSound(num);
236
        else if (g_sounds[num].num > 1) S_StopSound(num);
237
//        else if (A_CheckEnemySprite(&sprite[i]) && sprite[i].extra <= 0) S_StopSound(num);
238
    }
239
 
240
    sndang = 2048 + ca - getangle(cx-pos->x,cy-pos->y);
241
    sndang &= 2047;
242
 
243
    if (g_sounds[num].ptr == 0)
244
    {
245
        if (S_LoadSound(num) == 0) return 0;
246
    }
247
    else
248
    {
249
        if (g_sounds[num].lock < 200)
250
            g_sounds[num].lock = 200;
251
        else g_sounds[num].lock++;
252
    }
253
 
254
    if (g_sounds[num].m&16) sndist = 0;
255
 
256
    if (sndist < ((255-LOUDESTVOLUME)<<6))
257
        sndist = ((255-LOUDESTVOLUME)<<6);
258
 
259
    if (g_sounds[num].m&1)
260
    {
261
        if (g_sounds[num].num > 0) return -1;
262
 
1468 terminx 263
        voice = FX_PlayLoopedAuto(g_sounds[num].ptr, g_sounds[num].soundsiz, 0, -1,
264
                                  pitch,sndist>>6,sndist>>6,0,g_sounds[num].pr,num);
1247 helixhorne 265
    }
266
    else
267
    {
1468 terminx 268
        voice = FX_PlayAuto3D(g_sounds[ num ].ptr, g_sounds[num].soundsiz, pitch,sndang>>4,sndist>>6, g_sounds[num].pr, num);
1247 helixhorne 269
    }
270
 
1465 terminx 271
    if (voice >= FX_Ok)
1247 helixhorne 272
    {
273
        g_sounds[num].SoundOwner[g_sounds[num].num].i = i;
274
        g_sounds[num].SoundOwner[g_sounds[num].num].voice = voice;
275
        g_sounds[num].num++;
276
    }
277
    else g_sounds[num].lock--;
278
    return (voice);
279
}
280
 
281
void S_PlaySound(int32_t num)
282
{
283
    int32_t pitch,pitche,pitchs,cx;
284
    int32_t voice;
285
 
286
    if (!SM32_havesound) return;
287
    if (SoundToggle==0) return;
288
//    if ((g_sounds[num].m&8) && ud.lockout) return;
289
    if (FX_VoiceAvailable(g_sounds[num].pr) == 0) return;
290
    if (num < 0 || num > MAXSOUNDS-1 || !g_sounds[num].filename)
291
    {
292
        OSD_Printf("WARNING: invalid sound #%d\n",num);
293
        return;
294
    }
295
 
296
    pitchs = g_sounds[num].ps;
297
    pitche = g_sounds[num].pe;
298
    cx = klabs(pitche-pitchs);
299
 
300
    if (cx)
301
    {
302
        if (pitchs < pitche)
303
            pitch = pitchs + (rand()%cx);
304
        else pitch = pitche + (rand()%cx);
305
    }
306
    else pitch = pitchs;
307
 
308
    if (g_sounds[num].ptr == 0)
309
    {
310
        if (S_LoadSound(num) == 0) return;
311
    }
312
    else
313
    {
314
        if (g_sounds[num].lock < 200)
315
            g_sounds[num].lock = 200;
316
        else g_sounds[num].lock++;
317
    }
318
 
319
    if (g_sounds[num].m&1)
320
    {
1468 terminx 321
        voice = FX_PlayLoopedAuto(g_sounds[num].ptr, g_sounds[num].soundsiz, 0, -1,
322
                                  pitch,LOUDESTVOLUME,LOUDESTVOLUME,LOUDESTVOLUME,g_sounds[num].soundsiz,num);
1247 helixhorne 323
    }
324
    else
325
    {
1468 terminx 326
        voice = FX_PlayAuto3D(g_sounds[ num ].ptr, g_sounds[num].soundsiz, pitch,0,255-LOUDESTVOLUME,g_sounds[num].pr, num);
1247 helixhorne 327
    }
328
 
1465 terminx 329
    if (voice >= FX_Ok)// return;
1247 helixhorne 330
    {
331
        g_sounds[num].SoundOwner[g_sounds[num].num].voice = voice;
332
        g_sounds[num].num++;
333
        return;
334
    }
335
    g_sounds[num].lock--;
336
}
337
 
338
int32_t A_PlaySound(uint32_t num, int32_t i)
339
{
340
    if (num >= MAXSOUNDS) return -1;
341
    if (i < 0)
342
    {
343
        S_PlaySound(num);
344
        return 0;
345
    }
346
 
1487 terminx 347
    return S_PlaySound3D(num,i, (vec3_t *)&sprite[i]);
1247 helixhorne 348
}
349
 
350
void S_StopSound(int32_t num)
351
{
352
    if (num >= 0 && num < MAXSOUNDS)
353
        if (g_sounds[num].num > 0)
354
        {
355
            FX_StopSound(g_sounds[num].SoundOwner[g_sounds[num].num-1].voice);
1599 terminx 356
            S_Callback(num);
1247 helixhorne 357
        }
358
}
359
 
360
void S_StopEnvSound(int32_t num,int32_t i)
361
{
362
    int32_t j, k;
363
 
364
    if (num >= 0 && num < MAXSOUNDS)
365
        if (g_sounds[num].num > 0)
366
        {
367
            k = g_sounds[num].num;
368
            for (j=0; j<k; j++)
369
                if (g_sounds[num].SoundOwner[j].i == i)
370
                {
371
                    FX_StopSound(g_sounds[num].SoundOwner[j].voice);
372
                    break;
373
                }
374
        }
375
}
376
 
1625 terminx 377
void S_Update(void)
1247 helixhorne 378
{
379
    int32_t sndist, sx, sy, sz, cx, cy, cz;
380
    int32_t sndang,ca,j,k,i,cs;
381
 
382
    g_numEnvSoundsPlaying = 0;
383
 
384
    cx = pos.x;
385
    cy = pos.y;
386
    cz = pos.z;
387
    cs = cursectnum;
388
    ca = ang;
389
 
390
    for (j=0; j<MAXSOUNDS; j++)
391
        for (k=0; k<g_sounds[j].num; k++)
392
        {
393
            i = g_sounds[j].SoundOwner[k].i;
394
 
395
            sx = sprite[i].x;
396
            sy = sprite[i].y;
397
            sz = sprite[i].z;
398
 
399
            sndang = 2048 + ca - getangle(cx-sx,cy-sy);
400
            sndang &= 2047;
401
            sndist = FindDistance3D((cx-sx),(cy-sy),(cz-sz)>>4);
402
            if (i >= 0 && (g_sounds[j].m&16) == 0 && PN == MUSICANDSFX && SLT < 999 && (sector[SECT].lotag&0xff) < 9)
403
                sndist = divscale14(sndist,(SHT+1));
404
 
405
            sndist += g_sounds[j].vo;
406
            if (sndist < 0) sndist = 0;
407
 
408
            if (cs > -1 && sndist && PN != MUSICANDSFX && !cansee(cx,cy,cz-(24<<8),cs,sx,sy,sz-(24<<8),SECT))
409
                sndist += sndist>>5;
410
 
411
            if (PN == MUSICANDSFX && SLT < 999)
412
                g_numEnvSoundsPlaying++;
1316 terminx 413
            /*
414
                        switch (j)
415
                        {
416
                        case PIPEBOMB_EXPLODE:
417
                        case LASERTRIP_EXPLODE:
418
                        case RPG_EXPLODE:
419
                            if (sndist > (6144)) sndist = (6144);
420
                            break;
421
                        default:
422
            */
423
            if (sndist > 31444 && PN != MUSICANDSFX)
1247 helixhorne 424
            {
1316 terminx 425
                S_StopSound(j);
426
                continue;
427
            }
1247 helixhorne 428
//            }
429
 
430
            if (g_sounds[j].ptr == 0 && S_LoadSound(j) == 0) continue;
431
            if (g_sounds[j].m&16) sndist = 0;
432
 
433
            if (sndist < ((255-LOUDESTVOLUME)<<6))
434
                sndist = ((255-LOUDESTVOLUME)<<6);
435
 
1467 helixhorne 436
            FX_Pan3D(g_sounds[j].SoundOwner[k].voice,sndang>>4,sndist>>6);
1247 helixhorne 437
        }
438
}
439
 
1599 terminx 440
void S_Callback(uint32_t num)
1247 helixhorne 441
{
442
    int32_t i,j,k;
443
 
444
    k = g_sounds[num].num;
445
 
446
    if (k > 0)
447
    {
448
        if ((g_sounds[num].m&16) == 0)
449
            for (j=0; j<k; j++)
450
            {
451
                i = g_sounds[num].SoundOwner[j].i;
452
                if (sprite[i].picnum == MUSICANDSFX && sector[sprite[i].sectnum].lotag < 3 && sprite[i].lotag < 999)
453
                {
454
//                    ActorExtra[i].temp_data[0] = 0;
1251 helixhorne 455
                    sprite[i].filler &= (~1);
1247 helixhorne 456
                    if ((j + 1) < k)
457
                    {
458
                        g_sounds[num].SoundOwner[j].voice = g_sounds[num].SoundOwner[k-1].voice;
459
                        g_sounds[num].SoundOwner[j].i     = g_sounds[num].SoundOwner[k-1].i;
460
                    }
461
                    break;
462
                }
463
            }
464
 
465
        g_sounds[num].num--;
466
        g_sounds[num].SoundOwner[k-1].i = -1;
467
    }
468
 
469
    g_sounds[num].lock--;
470
}
471
 
472
void S_ClearSoundLocks(void)
473
{
474
    int32_t i;
475
 
476
    for (i=0; i<MAXSOUNDS; i++)
477
        if (g_sounds[i].lock >= 200)
478
            g_sounds[i].lock = 199;
479
}
480
 
481
int32_t A_CheckSoundPlaying(int32_t i, int32_t num)
482
{
483
    UNREFERENCED_PARAMETER(i);
484
    if (num < 0) num=0; // FIXME
485
    return (g_sounds[num].num > 0);
486
}
487
 
488
int32_t S_CheckSoundPlaying(int32_t i, int32_t num)
489
{
490
    if (i == -1)
491
    {
1492 terminx 492
        if (g_sounds[num].lock >= 200)
1247 helixhorne 493
            return 1;
494
        return 0;
495
    }
496
    return(g_sounds[num].num);
497
}