summaryrefslogtreecommitdiff
path: root/src/i_sound.c
blob: 17f5e7d0df3972efe22290847ee34b81b9baf60c (plain)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// $Id: i_sound.c 426 2006-03-18 23:24:04Z fraggle $
//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 2005 Simon Howard
//
// 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; either version 2
// of the License, or (at your option) any later version.
//
// 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.
//
// $Log$
// Revision 1.27  2006/01/23 01:40:24  fraggle
// Fix bug when expanding large sound effects with odd sample rates
//
// Revision 1.26  2006/01/22 21:20:20  fraggle
// Dehacked string replacements for sound and music lump names
//
// Revision 1.25  2006/01/10 22:14:13  fraggle
// Shut up compiler warnings
//
// Revision 1.24  2006/01/08 17:51:53  fraggle
// Add S_MusicPlaying function to query if music is still playing.
//
// Revision 1.23  2006/01/07 16:26:50  fraggle
// Fix the behavior when expanding sound effects (again).  Doom actually
// does play sounds of any sample rate, but the sound effects in
// Scientist 2 are corrupted.  Add some tests to check that the sound
// effect header is correct, and generic sound rate conversion code.
//
// Revision 1.22  2005/10/23 18:39:45  fraggle
// Reproduce the behavior when playing a sound at a sample rate which
// is not 11025 or 22050Hz.  This is to "fix" a bug in Scientist 2:
// however, it does not fix the playing of sounds, only silence
// them.  I tested Vanilla Doom and this is how it behaves when it
// receives sound effects with odd sample rates.  The bug here is
// actually in the Scientist 2 WAD, which has sound effects that
// have the wrong sample rate.
//
// Revision 1.21  2005/10/15 16:58:31  fraggle
// Fix MIDI music not pausing when using SDL_mixer's native MIDI playback.
// The SDL_mixer native MIDI code does not pause music properly - use
// a workaround of setting the volume to 0.
//
// Revision 1.20  2005/10/02 20:23:04  fraggle
// Guard against music lumps containing non-MUS data, document in bugs list
//
// Revision 1.19  2005/09/11 23:57:08  fraggle
// Remove temporary MIDI files generated by sound code.
//
// Revision 1.18  2005/09/11 20:53:17  fraggle
// Fix sounds playing at the wrong volume (too quiet)
//
// Revision 1.17  2005/09/07 22:24:26  fraggle
// Modify the sound effect caching behaviour: sounds which are not playing
// are now marked as PU_CACHE; it is otherwise possible to run out of memory.
//
// Revision 1.16  2005/09/06 22:39:43  fraggle
// Restore -nosound, -nosfx, -nomusic
//
// Revision 1.15  2005/09/06 21:40:28  fraggle
// Setting music volume
//
// Revision 1.14  2005/09/06 21:11:23  fraggle
// Working music!
//
// Revision 1.13  2005/09/05 22:50:56  fraggle
// Add mmus2mid code from prboom.  Use 'void *' for music handles.  Pass
// length of data when registering music.
//
// Revision 1.12  2005/09/05 21:03:43  fraggle
// 16-bit sound
//
// Revision 1.11  2005/09/05 20:32:18  fraggle
// Use the system-nonspecific sound code to assign the channel number used
// by SDL.  Remove handle tagging stuff.
//
// Revision 1.10  2005/08/19 21:55:51  fraggle
// Make sounds louder.  Use the correct maximum of 15 when doing sound
// calculations.
//
// Revision 1.9  2005/08/07 19:21:01  fraggle
// Cycle round sound channels to stop reuse and conflicts of channel
// numbers.  Add debug to detect when incorrect sound handles are used.
//
// Revision 1.8  2005/08/06 17:05:51  fraggle
// Remove debug messages, send error messages to stderr
// Fix overflow when playing large sound files
//
// Revision 1.7  2005/08/05 17:53:07  fraggle
// More sensible defaults
//
// Revision 1.6  2005/08/04 21:48:32  fraggle
// Turn on compiler optimisation and warning options
// Add SDL_mixer sound code
//
// Revision 1.5  2005/07/23 21:32:47  fraggle
// Add missing errno.h, fix crash on startup when no IWAD present
//
// Revision 1.4  2005/07/23 19:17:11  fraggle
// Use ANSI-standard limit constants.  Remove LINUX define.
//
// Revision 1.3  2005/07/23 17:21:35  fraggle
// Remove step table (unused, adds dependency on pow function)
//
// Revision 1.2  2005/07/23 16:44:55  fraggle
// Update copyright to GNU GPL
//
// Revision 1.1.1.1  2005/07/23 16:20:46  fraggle
// Initial import
//
//
// DESCRIPTION:
//	System interface for sound.
//
//-----------------------------------------------------------------------------

static const char
rcsid[] = "$Id: i_sound.c 426 2006-03-18 23:24:04Z fraggle $";

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_mixer.h>

#ifndef _WIN32
#include <unistd.h>
#endif

#include "mmus2mid.h"
#include "z_zone.h"

#include "i_system.h"
#include "i_sound.h"
#include "deh_main.h"
#include "m_argv.h"
#include "m_misc.h"
#include "m_swap.h"
#include "w_wad.h"

#include "doomdef.h"

#define NUM_CHANNELS		16

static boolean sound_initialised = false;
static boolean music_initialised = false;

static Mix_Chunk sound_chunks[NUMSFX];
static int channels_playing[NUM_CHANNELS];


// When a sound stops, check if it is still playing.  If it is not, 
// we can mark the sound data as CACHE to be freed back for other
// means.

void ReleaseSoundOnChannel(int channel)
{
    int i;
    int id = channels_playing[channel];

    if (!id)
        return;

    channels_playing[channel] = sfx_None;
    
    for (i=0; i<NUM_CHANNELS; ++i)
    {
        // Playing on this channel? if so, don't release.

        if (channels_playing[i] == id)
            return;
    }

    // Not used on any channel, and can be safely released
    
    Z_ChangeTag(sound_chunks[id].abuf, PU_CACHE);
}

// Expands the 11025Hz, 8bit, mono sound effects in Doom to
// 22050Hz, 16bit stereo

static void ExpandSoundData(byte *data, int samplerate, int length,
                            Mix_Chunk *destination)
{
    byte *expanded = (byte *) destination->abuf;
    int expanded_length;
    int expand_ratio;
    int i;

    if (samplerate == 11025)
    {
        // Most of Doom's sound effects are 11025Hz

        // need to expand to 2 channels, 11025->22050 and 8->16 bit

        for (i=0; i<length; ++i)
        {
            Uint16 sample;

            sample = data[i] | (data[i] << 8);
            sample -= 32768;

            expanded[i * 8] = expanded[i * 8 + 2]
              = expanded[i * 8 + 4] = expanded[i * 8 + 6] = sample & 0xff;
            expanded[i * 8 + 1] = expanded[i * 8 + 3]
              = expanded[i * 8 + 5] = expanded[i * 8 + 7] = (sample >> 8) & 0xff;
        }
    }
    else if (samplerate == 22050)
    {
        for (i=0; i<length; ++i)
        {
            Uint16 sample;

            sample = data[i] | (data[i] << 8);
            sample -= 32768;

            expanded[i * 4] = expanded[i * 4 + 2] = sample & 0xff;
            expanded[i * 4 + 1] = expanded[i * 4 + 3] = (sample >> 8) & 0xff;
        }
    }
    else
    {
        // Generic expansion function for all other sample rates

        // number of samples in the converted sound

        expanded_length = (length * 22050) / samplerate;
        expand_ratio = (length << 8) / expanded_length;

        for (i=0; i<expanded_length; ++i)
        {
            Uint16 sample;
            int src;

            src = (i * expand_ratio) >> 8;

            sample = data[src] | (data[src] << 8);
            sample -= 32768;

            // expand 8->16 bits, mono->stereo

            expanded[i * 4] = expanded[i * 4 + 2] = sample & 0xff;
            expanded[i * 4 + 1] = expanded[i * 4 + 3] = (sample >> 8) & 0xff;
        }
    }
}

// Load and convert a sound effect
// Returns true if successful

static boolean CacheSFX(int sound)
{
    int lumpnum;
    int lumplen;
    int samplerate;
    int length;
    int expanded_length;
    byte *data;

    // need to load the sound

    lumpnum = I_GetSfxLumpNum(&S_sfx[sound]);
    data = W_CacheLumpNum(lumpnum, PU_STATIC);
    lumplen = W_LumpLength(lumpnum);

    // Check the header, and ensure this is a valid sound

    if (lumplen < 8
     || data[0] != 0x03 || data[1] != 0x00
     || data[6] != 0x00 || data[7] != 0x00)
    {
        // Invalid sound

        return false;
    }
    
    samplerate = (data[3] << 8) | data[2];
    length = (data[5] << 8) | data[4];

    // If the header specifies that the length of the sound is greater than
    // the length of the lump itself, this is an invalid sound lump

    if (length - 8 > lumplen)
    {
        return false;
    }

    expanded_length = (length * 22050) / (samplerate / 4);

    sound_chunks[sound].allocated = 1;
    sound_chunks[sound].alen = expanded_length;
    sound_chunks[sound].abuf 
        = Z_Malloc(expanded_length, PU_STATIC, &sound_chunks[sound].abuf);
    sound_chunks[sound].volume = MIX_MAX_VOLUME;

    ExpandSoundData(data + 8, samplerate, length, &sound_chunks[sound]);

    // don't need the original lump any more
  
    Z_ChangeTag(data, PU_CACHE);

    return true;
}

static Mix_Chunk *getsfx(int sound)
{
    if (sound_chunks[sound].abuf == NULL)
    {
        if (!CacheSFX(sound))
            return NULL;
    }
    else
    {
        // don't free the sound while it is playing!
   
        Z_ChangeTag(sound_chunks[sound].abuf, PU_STATIC);
    }

    return &sound_chunks[sound];
}

//
// SFX API
// Note: this was called by S_Init.
// However, whatever they did in the
// old DPMS based DOS version, this
// were simply dummies in the Linux
// version.
// See soundserver initdata().
//
void I_SetChannels()
{
}	

 
void I_SetSfxVolume(int volume)
{
    // Identical to DOS.
    // Basically, this should propagate
    //  the menu/config file setting
    //  to the state variable used in
    //  the mixing.
    snd_SfxVolume = volume;
}


//
// Retrieve the raw data lump index
//  for a given SFX name.
//
int I_GetSfxLumpNum(sfxinfo_t* sfx)
{
    char namebuf[9];
    sprintf(namebuf, "ds%s", DEH_String(sfx->name));
    return W_GetNumForName(namebuf);
}
//
// Starting a sound means adding it
//  to the current list of active sounds
//  in the internal channels.
// As the SFX info struct contains
//  e.g. a pointer to the raw data,
//  it is ignored.
// As our sound handling does not handle
//  priority, it is ignored.
// Pitching (that is, increased speed of playback)
//  is set, but currently not used by mixing.
//
int
I_StartSound
( int		id,
  int           channel,
  int		vol,
  int		sep,
  int		pitch,
  int		priority )
{
    Mix_Chunk *chunk;

    if (!sound_initialised)
        return 0;

    // Release a sound effect if there is already one playing
    // on this channel

    ReleaseSoundOnChannel(channel);

    // Get the sound data

    chunk = getsfx(id);

    if (chunk == NULL)
    {
        return -1;
    }

    // play sound

    Mix_PlayChannelTimed(channel, chunk, 0, -1);

    channels_playing[channel] = id;

    // set separation, etc.
 
    I_UpdateSoundParams(channel, vol, sep, pitch);

    return channel;
}

void I_StopSound (int handle)
{
    if (!sound_initialised)
        return;

    Mix_HaltChannel(handle);

    // Sound data is no longer needed; release the
    // sound data being used for this channel

    ReleaseSoundOnChannel(handle);
}


int I_SoundIsPlaying(int handle)
{
    if (!sound_initialised) 
        return false;

    if (handle < 0)
        return false;

    return Mix_Playing(handle);
}




// 
// Periodically called to update the sound system
//

void I_UpdateSound( void )
{
    int i;

    if (!sound_initialised)
        return;

    // Check all channels to see if a sound has finished

    for (i=0; i<NUM_CHANNELS; ++i)
    {
        if (channels_playing[i] && !I_SoundIsPlaying(i))
        {
            // Sound has finished playing on this channel,
            // but sound data has not been released to cache
            
            ReleaseSoundOnChannel(i);
        }
    }
}


// 
// This would be used to write out the mixbuffer
//  during each game loop update.
// Updates sound buffer and audio device at runtime. 
// It is called during Timer interrupt with SNDINTR.
// Mixing now done synchronous, and
//  only output be done asynchronous?
//
void
I_SubmitSound(void)
{
}



void 
I_UpdateSoundParams
( int	handle,
  int	vol,
  int	sep,
  int	pitch)
{
    int left, right;

    if (!sound_initialised)
        return;

    left = ((254 - sep) * vol) / 15;
    right = ((sep) * vol) / 15;

    Mix_SetPanning(handle, left, right);
}




void I_ShutdownSound(void)
{    
    if (!sound_initialised && !music_initialised)
        return;

    Mix_CloseAudio();
    SDL_QuitSubSystem(SDL_INIT_AUDIO);
}



void
I_InitSound()
{ 
    int i;
    
    // No sounds yet

    for (i=0; i<NUMSFX; ++i)
    {
        sound_chunks[i].abuf = NULL;
    }

    for (i=0; i<NUM_CHANNELS; ++i)
    {
        channels_playing[i] = sfx_None;
    }
    
    // If music or sound is going to play, we need to at least
    // initialise SDL

    if (M_CheckParm("-nosound") 
     || (M_CheckParm("-nosfx") && M_CheckParm("-nomusic")))
        return;

    if (SDL_Init(SDL_INIT_AUDIO) < 0)
    {
        fprintf(stderr, "Unable to set up sound.\n");
        return;
    }

    if (Mix_OpenAudio(22050, AUDIO_S16LSB, 2, 1024) < 0)
    {
        fprintf(stderr, "Error initialising SDL_mixer: %s\n", SDL_GetError());
        return;
    }

    Mix_AllocateChannels(NUM_CHANNELS);
    
    SDL_PauseAudio(0);

    music_initialised = true;

    if (M_CheckParm("-nosound") || M_CheckParm("-nosfx"))
        return;

    sound_initialised = true;
}




//
// MUSIC API.
//

void I_InitMusic(void)		
{ 
}

void I_ShutdownMusic(void)	
{ 
    music_initialised = false;
}

static boolean  musicpaused = false;

//
// SDL_mixer's native MIDI music playing does not pause properly.
// As a workaround, set the volume to 0 when paused.
//

static void UpdateMusicVolume(void)
{
    int vol;

    if (musicpaused)
        vol = 0;
    else
        vol = (snd_MusicVolume * MIX_MAX_VOLUME) / 15;

    Mix_VolumeMusic(vol);
}

// MUSIC API - dummy. Some code from DOS version.
void I_SetMusicVolume(int volume)
{
    // Internal state variable.
    snd_MusicVolume = volume;

    UpdateMusicVolume();
}

void I_PlaySong(void *handle, int looping)
{
    Mix_Music *music = (Mix_Music *) handle;
    int loops;

    if (!music_initialised)
        return;

    if (handle == NULL)
        return;

    if (looping)
        loops = -1;
    else
        loops = 1;

    Mix_PlayMusic(music, loops);
}

void I_PauseSong (void *handle)
{
    if (!music_initialised)
        return;

    musicpaused = true;

    UpdateMusicVolume();
}

void I_ResumeSong (void *handle)
{
    if (!music_initialised)
        return;

    musicpaused = false;

    UpdateMusicVolume();
}

void I_StopSong(void *handle)
{
    if (!music_initialised)
        return;

    Mix_HaltMusic();
}

void I_UnRegisterSong(void *handle)
{
    Mix_Music *music = (Mix_Music *) handle;

    if (!music_initialised)
        return;

    if (handle == NULL)
        return;

    Mix_FreeMusic(music);
}

void *I_RegisterSong(void *data, int len)
{
    char filename[64];
    Mix_Music *music;
    MIDI *mididata;
    UBYTE *mid;
    int midlen;

    if (!music_initialised)
        return NULL;
    
    // MUS files begin with "MUS"
    // Reject anything which doesnt have this signature

    if (len < 3 || memcmp(data, "MUS", 3) != 0)
        return NULL;
    
#ifdef _WIN32
    sprintf(filename, "doom.mid");
#else
    sprintf(filename, "/tmp/doom-%i.mid", getpid());
#endif

    // Convert from mus to midi
    // Bits here came from PrBoom
  
    mididata = Z_Malloc(sizeof(MIDI), PU_STATIC, 0);
    mmus2mid(data, mididata, 89, 0);

    if (MIDIToMidi(mididata, &mid, &midlen))
    {
        // Error occurred

        fprintf(stderr, "Error converting MUS lump.\n");

        music = NULL;
    }
    else
    {
        // Write midi data to disk
       
        M_WriteFile(filename, mid, midlen);

        // Clean up
       
        free(mid);
        free_mididata(mididata);
        music = Mix_LoadMUS(filename);
        
        if (music == NULL)
        {
            // Failed to load

            fprintf(stderr, "Error loading midi\n");
        }

        // remove file now

        remove(filename);
    }

    Z_Free(mididata);
    
    return music;
}

// Is the song playing?
boolean I_QrySongPlaying(void *handle)
{
    if (!music_initialised)
        return false;

    return Mix_PlayingMusic();
}