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