doom / src / linux_x / sndserv / soundsrv.c

  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
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// $Id: soundsrv.c 538 2009-09-23 23:24:07Z smite-meister $
//
// Copyright (C) 1993-1996 by id Software, Inc.
// Portions Copyright (C) 1998-2000 by DooM Legacy Team.
//
// 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.
//
//
// $Log: soundsrv.c,v $
// Revision 1.8  2001/05/29 22:18:41  bock
// Small BSD commit: build r_opengl.so, sndserver
//
// Revision 1.7  2000/09/01 19:34:37  bpereira
// no message
//
// Revision 1.6  2000/04/30 19:50:37  metzgermeister
// no message
//
// Revision 1.5  2000/04/28 19:26:10  metzgermeister
// musserver fixed, sndserver amplified accordingly
//
// Revision 1.4  2000/04/22 20:30:00  metzgermeister
// fix amplification by 4
//
// Revision 1.3  2000/03/28 16:18:42  linuxcub
// Added a command to the Linux sound-server which sets a master volume.
//
// Revision 1.2  2000/02/27 00:42:12  hurdler
// fix CR+LF problem
//
// Revision 1.1.1.1  2000/02/22 20:32:33  hurdler
// Initial import into CVS (v1.29 pr3)
//
//
// DESCRIPTION:
//      UNIX soundserver, run as a separate process,
//       started by DOOM program.
//      Originally conceived fopr SGI Irix,
//       mostly used with Linux voxware.
//
//-----------------------------------------------------------------------------


#include <math.h>
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#ifndef FREEBSD
#include <malloc.h>
#endif
#include <sys/stat.h>
#include <sys/time.h>

#include "sounds.h"
#include "soundsrv.h"



extern int audio_8bit_flag;


//
// Department of Redundancy Department.
//
typedef struct wadinfo_struct
{
    // should be IWAD
    char        identification[4];      
    int         numlumps;
    int         infotableofs;
    
} wadinfo_t;


typedef struct filelump_struct
{
    int         filepos;
    int         size;
    char        name[8];
    
} filelump_t;


// an internal time keeper
static int      mytime = 0;

// number of sound effects
int             numsounds;

// longest sound effect
int             longsound;

// lengths of all sound effects
int             lengths[NUMSFX];

// mixing buffer
signed short    mixbuffer[MIXBUFFERSIZE];

// file descriptor of sfx device
int             sfxdevice;                      

// file descriptor of music device
int             musdevice;                      

// the channel data pointers
unsigned char*  channels[8];

// the channel step amount
unsigned int    channelstep[8];

// 0.16 bit remainder of last step
unsigned int    channelstepremainder[8];

// the channel data end pointers
unsigned char*  channelsend[8];

// time that the channel started playing
int             channelstart[8];

// the channel handles
int             channelhandles[8];

// the channel left volume lookup
// int*            channelleftvol_lookup[8];

// the channel right volume lookup
// int*            channelrightvol_lookup[8];

// sfx id of the playing sound effect
int             channelids[8];                  

int             snd_verbose=1;

int             steptable[256];

// int             vol_lookup[128*256];
int             volume_lookup[128][256];

int             master_volume=31; /* 0..31 */

int             left_volume[8],right_volume[8];

static void derror(char* msg)
{
    fprintf(stderr, "error: %s\n", msg);
    exit(-1);
}

int mix(void)
{

    register unsigned int       sample;
    register int                dl;
    register int                dr;
    unsigned short              sdl;
    unsigned short              sdr;
    
    signed short*               leftout;
    signed short*               rightout;
    signed short*               leftend;
    unsigned char*              bothout;
    
    int                         step;
    int                         i;
    int                         leftv[8],rightv[8];
    
    for( i=0; i<8; ++i ) {
        leftv[i] = left_volume[i]*master_volume/31;
        rightv[i] = right_volume[i]*master_volume/31;
    }

    leftout = mixbuffer;
    rightout = mixbuffer+1;
    bothout = (unsigned char *)mixbuffer;
    step = 2;

    leftend = mixbuffer + SAMPLECOUNT*step;

    // mix into the mixing buffer
    while (leftout != leftend)
    {

        dl = 0;
        dr = 0;

        if (channels[0])
        {
            sample = *channels[0];
            // dl += channelleftvol_lookup[0][sample];
            dl += volume_lookup[leftv[0]][sample];
            // dr += channelrightvol_lookup[0][sample];
            dr += volume_lookup[rightv[0]][sample];
            channelstepremainder[0] += channelstep[0];
            channels[0] += channelstepremainder[0] >> 16;
            channelstepremainder[0] &= 65536-1;

            if (channels[0] >= channelsend[0])
                channels[0] = 0;
        }

        if (channels[1])
        {
            sample = *channels[1];
            // dl += channelleftvol_lookup[1][sample];
            dl += volume_lookup[leftv[1]][sample];
            // dr += channelrightvol_lookup[1][sample];
            dr += volume_lookup[rightv[1]][sample];
            channelstepremainder[1] += channelstep[1];
            channels[1] += channelstepremainder[1] >> 16;
            channelstepremainder[1] &= 65536-1;

            if (channels[1] >= channelsend[1])
                channels[1] = 0;
        }

        if (channels[2])
        {
            sample = *channels[2];
            // dl += channelleftvol_lookup[2][sample];
            dl += volume_lookup[leftv[2]][sample];
            // dr += channelrightvol_lookup[2][sample];
            dr += volume_lookup[rightv[2]][sample];
            channelstepremainder[2] += channelstep[2];
            channels[2] += channelstepremainder[2] >> 16;
            channelstepremainder[2] &= 65536-1;

            if (channels[2] >= channelsend[2])
                channels[2] = 0;
        }
        
        if (channels[3])
        {
            sample = *channels[3];
            // dl += channelleftvol_lookup[3][sample];
            dl += volume_lookup[leftv[3]][sample];
            // dr += channelrightvol_lookup[3][sample];
            dr += volume_lookup[rightv[3]][sample];
            channelstepremainder[3] += channelstep[3];
            channels[3] += channelstepremainder[3] >> 16;
            channelstepremainder[3] &= 65536-1;

            if (channels[3] >= channelsend[3])
                channels[3] = 0;
        }
        
        if (channels[4])
        {
            sample = *channels[4];
            // dl += channelleftvol_lookup[4][sample];
            dl += volume_lookup[leftv[4]][sample];
            // dr += channelrightvol_lookup[4][sample];
            dr += volume_lookup[rightv[4]][sample];
            channelstepremainder[4] += channelstep[4];
            channels[4] += channelstepremainder[4] >> 16;
            channelstepremainder[4] &= 65536-1;

            if (channels[4] >= channelsend[4])
                channels[4] = 0;
        }
        
        if (channels[5])
        {
            sample = *channels[5];
            // dl += channelleftvol_lookup[5][sample];
            dl += volume_lookup[leftv[5]][sample];
            // dr += channelrightvol_lookup[5][sample];
            dr += volume_lookup[rightv[5]][sample];
            channelstepremainder[5] += channelstep[5];
            channels[5] += channelstepremainder[5] >> 16;
            channelstepremainder[5] &= 65536-1;

            if (channels[5] >= channelsend[5])
                channels[5] = 0;
        }
        
        if (channels[6])
        {
            sample = *channels[6];
            // dl += channelleftvol_lookup[6][sample];
            dl += volume_lookup[leftv[6]][sample];
            // dr += channelrightvol_lookup[6][sample];
            dr += volume_lookup[rightv[6]][sample];
            channelstepremainder[6] += channelstep[6];
            channels[6] += channelstepremainder[6] >> 16;
            channelstepremainder[6] &= 65536-1;

            if (channels[6] >= channelsend[6])
                channels[6] = 0;
        }
        if (channels[7])
        {
            sample = *channels[7];
            // dl += channelleftvol_lookup[7][sample];
            dl += volume_lookup[leftv[7]][sample];
            // dr += channelrightvol_lookup[7][sample];
            dr += volume_lookup[rightv[7]][sample];
            channelstepremainder[7] += channelstep[7];
            channels[7] += channelstepremainder[7] >> 16;
            channelstepremainder[7] &= 65536-1;

            if (channels[7] >= channelsend[7])
                channels[7] = 0;
        }

        // Has been char instead of short.
        // if (dl > 127) *leftout = 127;
        // else if (dl < -128) *leftout = -128;
        // else *leftout = dl;

        // if (dr > 127) *rightout = 127;
        // else if (dr < -128) *rightout = -128;
        // else *rightout = dr;
        
        dl <<= 3;
        dr <<= 3;

        if (!audio_8bit_flag)
        {
            if (dl > 0x7fff)
                *leftout = 0x7fff;
            else if (dl < -0x8000)
                *leftout = -0x8000;
            else
                *leftout = dl;

            if (dr > 0x7fff)
                *rightout = 0x7fff;
            else if (dr < -0x8000)
                *rightout = -0x8000;
            else
                *rightout = dr;
        }
        else
        {
            if (dl > 0x7fff)
                dl = 0x7fff;
            else if (dl < -0x8000)
                dl = -0x8000;
            sdl = dl ^ 0xfff8000;

            if (dr > 0x7fff)
                dr = 0x7fff;
            else if (dr < -0x8000)
                dr = -0x8000;
            sdr = dr ^ 0xfff8000;

            *bothout++ = (((sdr + sdl) / 2) >> 8);
        }

        leftout += step;
        rightout += step;

    }
    return 1;
}

static struct timeval           last={0,0};
//static struct timeval         now;

static struct timezone          whocares;

void updatesounds(void)
{

    mix();
    I_SubmitOutputBuffer(mixbuffer, SAMPLECOUNT);

}

int
addsfx
( int           sfxid,
  int           volume,
  int           step,
  int           seperation )
{
    static unsigned short       handlenums = 0;
 
    int         i;
    int         rc = -1;
    
    int         oldest = mytime;
    int         oldestnum = 0;
    int         slot;
    int         rightvol;
    int         leftvol;

    // play these sound effects
    //  only one at a time
    if ( sfxid == sfx_sawup
         || sfxid == sfx_sawidl
         || sfxid == sfx_sawful
         || sfxid == sfx_sawhit
         || sfxid == sfx_stnmov
         || sfxid == sfx_pistol )
    {
        for (i=0 ; i<8 ; i++)
        {
            if (channels[i] && channelids[i] == sfxid)
            {
                channels[i] = 0;
                break;
            }
        }
    }

    for (i=0 ; i<8 && channels[i] ; i++)
    {
        if (channelstart[i] < oldest)
        {
            oldestnum = i;
            oldest = channelstart[i];
        }
    }

    if (i == 8)
        slot = oldestnum;
    else
        slot = i;

    channels[slot] = (unsigned char *) S_sfx[sfxid].data;
    channelsend[slot] = channels[slot] + lengths[sfxid];

    if (!handlenums)
        handlenums = 100;
    
    channelhandles[slot] = rc = handlenums++;
    channelstep[slot] = step;
    channelstepremainder[slot] = 0;
    channelstart[slot] = mytime;

    // (range: 1 - 256)
    seperation += 1;

    // (x^2 seperation)
    leftvol =
        volume - (volume*seperation*seperation)/(256*256);

    seperation = seperation - 257;

    // (x^2 seperation)
    rightvol =
        volume - (volume*seperation*seperation)/(256*256);      

    // sanity check
    if (rightvol < 0 || rightvol > 127)
        derror("rightvol out of bounds");
    
    if (leftvol < 0 || leftvol > 127)
        derror("leftvol out of bounds");
    
    // get the proper lookup table piece
    //  for this volume level
    // channelleftvol_lookup[slot] = &vol_lookup[(leftvol*master_volume/31)*256];
    // channelrightvol_lookup[slot] = &vol_lookup[(rightvol*master_volume/31)*256];
    left_volume[slot] = leftvol;
    right_volume[slot] = rightvol;

    channelids[slot] = sfxid;

    return rc;

}


void outputushort(int num)
{

    static unsigned char        buff[5] = { 0, 0, 0, 0, '\n' };
    static char*                badbuff = "xxxx\n";

    // outputs a 16-bit # in hex or "xxxx" if -1.
    if (num < 0)
    {
        write(1, badbuff, 5);
    }
    else
    {
        buff[0] = num>>12;
        buff[0] += buff[0] > 9 ? 'a'-10 : '0';
        buff[1] = (num>>8) & 0xf;
        buff[1] += buff[1] > 9 ? 'a'-10 : '0';
        buff[2] = (num>>4) & 0xf;
        buff[2] += buff[2] > 9 ? 'a'-10 : '0';
        buff[3] = num & 0xf;
        buff[3] += buff[3] > 9 ? 'a'-10 : '0';
        write(1, buff, 5);
    }
}

void initdata(void)
{

    int         i;
    int         j;
    
    int*        steptablemid = steptable + 128;

    for (i=0 ;
         i<sizeof(channels)/sizeof(unsigned char *) ;
         i++)
    {
        channels[i] = 0;
    }
    
    gettimeofday(&last, &whocares);

    for (i=-128 ; i<128 ; i++)
        steptablemid[i] = pow(2.0, (i/64.0))*65536.0;

    // generates volume lookup tables
    //  which also turn the unsigned samples
    //  into signed samples
    // for (i=0 ; i<128 ; i++)
    // for (j=0 ; j<256 ; j++)
    // vol_lookup[i*256+j] = (i*(j-128))/127;
    
    for (i=0 ; i<128 ; i++)
        for (j=0 ; j<256 ; j++)
            // vol_lookup[i*256+j] = (i*(j-128)*256)/127;
            volume_lookup[i][j] = (i*(j-128)*256)/127;

}




void quit(void)
{
    I_ShutdownMusic();
    I_ShutdownSound();
    exit(0);
}



fd_set          fdset;
fd_set          scratchset;



int
main
( int           c,
  char**        v )
{

    int         done = 0;
    int         rc;
    int         nrc;
    int         sndnum;
    int         sndcnt = 0;
    int         handle = 0;
    
    unsigned char       commandbuf[10];
    struct timeval      zerowait = { 0, 0 };

    
    int         step;
    int         vol;
    int         sep;
    
    int         i;
    int         waitingtofinish=0;

    // init any data
    initdata();         

    I_InitSound(11025, 16);

    I_InitMusic();

    if (snd_verbose)
        fprintf(stderr, "ready\n");
    
    // parse commands and play sounds until done
    FD_ZERO(&fdset);
    FD_SET(0, &fdset);

    while (!done)
    {
        mytime++;

        if (!waitingtofinish)
        {
            do {
                scratchset = fdset;
                rc = select(FD_SETSIZE, &scratchset, 0, 0, &zerowait);

                if (rc > 0)
                {
                    //  fprintf(stderr, "select is true\n");
                    // got a command
                    nrc = read(0, commandbuf, 1);

                    if (!nrc)
                    {
                        done = 1;
                        rc = 0;
                    }
                    else
                    {
                        //if (snd_verbose)
                        //    fprintf(stderr, "cmd: %c", commandbuf[0]);

                        switch (commandbuf[0])
                        {
                        case 'v':
                            // get master volume
                            read(0, commandbuf, 1);
                            master_volume = (commandbuf[0] & 0x1f);
                            break;
                            
                        case 'p':
                            // play a new sound effect
                            read(0, commandbuf, 4);

                            //if (snd_verbose)
                            //{
                            //  commandbuf[9]=0;
                            //  fprintf(stderr, "%s\n", commandbuf);
                            //}

                            //  p<snd#><step><vol><sep>
                            sndnum = commandbuf[0];
                            step = commandbuf[1];
                            step = steptable[step];
                            vol = commandbuf[2];
                            sep = commandbuf[3];

                            handle = addsfx(sndnum, vol, step, sep);
                            // returns the handle
                            //  outputushort(handle);
                            break;

                        case 'l': {
                            int bln,tlen;
                            read(0, &bln, sizeof(int));
                            //fprintf(stderr,"%d in...\n",bln);
                            S_sfx[sndcnt].data = malloc(bln);
                            // hey, read on a pipewill not always
                            // fill the whole buffer 19990203 by Kin
                            for(tlen = 0; tlen < bln;) {
                                tlen+=read(0, S_sfx[sndcnt].data+tlen, bln-tlen);
                            }
                            lengths[sndcnt] = bln;
                            sndcnt++;
                            break;
                        }
                        case 'q':
                            // no '\n' 19990201 by Kin
                            //read(0, commandbuf, 1);
                            waitingtofinish = 1; rc = 0;
                            break;
                            
                            //case 's':
                            //{
                            //  int fd;
                            //  read(0, commandbuf, 3);
                            //  commandbuf[2] = 0;
                            //  fd = open((char*)commandbuf, O_CREAT|O_WRONLY, 0644);
                            //  commandbuf[0] -= commandbuf[0]>='a' ? 'a'-10 : '0';
                            //  commandbuf[1] -= commandbuf[1]>='a' ? 'a'-10 : '0';
                            //  sndnum = (commandbuf[0]<<4) + commandbuf[1];
                            //  write(fd, S_sfx[sndnum].data, lengths[sndnum]);
                            //  close(fd);
                            //}
                            //break;
                        default:
                            fprintf(stderr, "Did not recognize command %d\n",commandbuf[0]);
                            break;
                        }
                    }
                }
                else if (rc < 0)
                {
                    quit();
                }
            } while (rc > 0);
        }

        updatesounds();

        if (waitingtofinish)
        {
            for(i=0 ; i<8 && !channels[i] ; i++);
            
            if (i==8)
                done=1;
        }

    }

    quit();
    return 0;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.