Annotation of uae/src/sd-uss/sound.c, revision 1.1.1.3

1.1       root        1:  /* 
                      2:   * UAE - The Un*x Amiga Emulator
                      3:   * 
                      4:   * Support for Linux/USS sound
                      5:   * 
                      6:   * Copyright 1997 Bernd Schmidt
                      7:   */
                      8: 
                      9: #include "sysconfig.h"
                     10: #include "sysdeps.h"
                     11: 
                     12: #include "config.h"
                     13: #include "options.h"
                     14: #include "memory.h"
                     15: #include "custom.h"
                     16: #include "gensound.h"
                     17: #include "sounddep/sound.h"
                     18: 
                     19: #include <sys/ioctl.h>
                     20: 
                     21: #ifdef HAVE_SYS_SOUNDCARD_H
                     22: #include <sys/soundcard.h>
                     23: #elif defined HAVE_MACHINE_SOUNDCARD_H
                     24: #include <machine/soundcard.h>
                     25: #else
                     26: #error "Something went wrong during configuration."
                     27: #endif
                     28: 
                     29: int sound_fd;
1.1.1.3 ! root       30: static int have_sound = 0;
1.1       root       31: static unsigned long formats;
                     32: 
                     33: uae_u16 sndbuffer[44100];
                     34: uae_u16 *sndbufpt;
                     35: int sndbufsize;
                     36: 
1.1.1.2   root       37: static int exact_log2 (int v)
1.1       root       38: {
                     39:     int l = 0;
                     40:     while ((v >>= 1) != 0)
                     41:        l++;
                     42:     return l;
                     43: }
                     44: 
1.1.1.2   root       45: void close_sound (void)
1.1       root       46: {
                     47:     if (have_sound)
1.1.1.2   root       48:        close (sound_fd);
1.1       root       49: }
                     50: 
1.1.1.3 ! root       51: /* Try to determine whether sound is available.  This is only for GUI purposes.  */
1.1.1.2   root       52: int setup_sound (void)
1.1       root       53: {
                     54:     sound_fd = open ("/dev/dsp", O_WRONLY);
1.1.1.3 ! root       55: 
        !            56:     sound_available = 0;
        !            57: 
        !            58:     if (sound_fd < 0) {
        !            59:        perror ("Can't open /dev/dsp");
        !            60:        if (errno == EBUSY) {
        !            61:            /* We can hope, can't we ;) */
        !            62:            sound_available = 1;
        !            63:            return 1;
        !            64:        }
1.1       root       65:        return 0;
1.1.1.3 ! root       66:     }
1.1       root       67: 
                     68:     if (ioctl (sound_fd, SNDCTL_DSP_GETFMTS, &formats) == -1) {
1.1.1.3 ! root       69:        perror ("ioctl failed - can't use sound");
        !            70:        close (sound_fd);
1.1       root       71:        return 0;
                     72:     }
                     73: 
                     74:     sound_available = 1;
1.1.1.3 ! root       75:     close (sound_fd);
1.1       root       76:     return 1;
                     77: }
                     78: 
                     79: int init_sound (void)
                     80: {
                     81:     int tmp;
                     82:     int rate;
                     83:     int dspbits;
                     84: 
1.1.1.3 ! root       85:     sound_fd = open ("/dev/dsp", O_WRONLY);
        !            86:     have_sound = !(sound_fd < 0);
        !            87:     if (! have_sound) {
        !            88:        perror ("Can't open /dev/dsp");
        !            89:        if (errno != EBUSY)
        !            90:            sound_available = 0;
        !            91:        return 0;
        !            92:     }
        !            93:     if (ioctl (sound_fd, SNDCTL_DSP_GETFMTS, &formats) == -1) {
        !            94:        perror ("ioctl failed - can't use sound");
        !            95:        close (sound_fd);
        !            96:        have_sound = 0;
        !            97:        return 0;
        !            98:     }
        !            99: 
1.1       root      100:     if (currprefs.sound_maxbsiz < 128 || currprefs.sound_maxbsiz > 16384) {
1.1.1.2   root      101:        fprintf (stderr, "Sound buffer size %d out of range.\n", currprefs.sound_maxbsiz);
1.1       root      102:        currprefs.sound_maxbsiz = 8192;
                    103:     }
                    104: 
1.1.1.2   root      105:     tmp = 0x00040000 + exact_log2 (currprefs.sound_maxbsiz);
1.1       root      106:     ioctl (sound_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
                    107:     ioctl (sound_fd, SNDCTL_DSP_GETBLKSIZE, &sndbufsize);
                    108: 
                    109:     dspbits = currprefs.sound_bits;
                    110:     ioctl (sound_fd, SNDCTL_DSP_SAMPLESIZE, &dspbits);
                    111:     ioctl (sound_fd, SOUND_PCM_READ_BITS, &dspbits);
                    112:     if (dspbits != currprefs.sound_bits) {
1.1.1.2   root      113:        fprintf (stderr, "Can't use sound with %d bits\n", currprefs.sound_bits);
1.1       root      114:        return 0;
                    115:     }
                    116: 
                    117:     tmp = currprefs.stereo;
                    118:     ioctl (sound_fd, SNDCTL_DSP_STEREO, &tmp);
                    119: 
                    120:     rate = currprefs.sound_freq;
                    121:     ioctl (sound_fd, SNDCTL_DSP_SPEED, &rate);
                    122:     ioctl (sound_fd, SOUND_PCM_READ_RATE, &rate);
                    123:     /* Some soundcards have a bit of tolerance here. */
                    124:     if (rate < currprefs.sound_freq * 90 / 100 || rate > currprefs.sound_freq * 110 / 100) {
1.1.1.2   root      125:        fprintf (stderr, "Can't use sound with desired frequency %d\n", currprefs.sound_freq);
1.1       root      126:        return 0;
                    127:     }
                    128: 
                    129:     sample_evtime = (long)maxhpos * maxvpos * 50 / rate;
                    130: 
                    131:     if (dspbits == 16) {
                    132:        /* Will this break horribly on bigendian machines? Possible... */
                    133:        if (!(formats & AFMT_S16_LE))
                    134:            return 0;
                    135:        init_sound_table16 ();
1.1.1.2   root      136:        sample_handler = currprefs.stereo ? sample16s_handler : sample16_handler;
1.1       root      137:     } else {
                    138:        if (!(formats & AFMT_U8))
                    139:            return 0;
                    140:        init_sound_table8 ();
1.1.1.2   root      141:        sample_handler = currprefs.stereo ? sample8s_handler : sample8_handler;
1.1       root      142:     }
                    143:     sound_available = 1;
                    144:     printf ("Sound driver found and configured for %d bits at %d Hz, buffer is %d bytes\n",
                    145:            dspbits, rate, sndbufsize);
                    146:     sndbufpt = sndbuffer;
                    147:     
                    148: #ifdef FRAME_RATE_HACK
                    149:     vsynctime = vsynctime * 9 / 10;
                    150: #endif 
                    151: 
                    152:     return 1;
                    153: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.