|
|
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;
30: static int have_sound;
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.2 ! root 51: int setup_sound (void)
1.1 root 52: {
53: sound_fd = open ("/dev/dsp", O_WRONLY);
54: have_sound = !(sound_fd < 0);
1.1.1.2 ! root 55: if (! have_sound)
1.1 root 56: return 0;
57:
58: if (ioctl (sound_fd, SNDCTL_DSP_GETFMTS, &formats) == -1) {
1.1.1.2 ! root 59: fprintf (stderr, "ioctl failed - can't use sound.\n");
1.1 root 60: close(sound_fd);
61: have_sound = 0;
62: return 0;
63: }
64:
65: sound_available = 1;
66: return 1;
67: }
68:
69: int init_sound (void)
70: {
71: int tmp;
72: int rate;
73: int dspbits;
74:
75: if (currprefs.sound_maxbsiz < 128 || currprefs.sound_maxbsiz > 16384) {
1.1.1.2 ! root 76: fprintf (stderr, "Sound buffer size %d out of range.\n", currprefs.sound_maxbsiz);
1.1 root 77: currprefs.sound_maxbsiz = 8192;
78: }
79:
1.1.1.2 ! root 80: tmp = 0x00040000 + exact_log2 (currprefs.sound_maxbsiz);
1.1 root 81: ioctl (sound_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
82: ioctl (sound_fd, SNDCTL_DSP_GETBLKSIZE, &sndbufsize);
83:
84: dspbits = currprefs.sound_bits;
85: ioctl (sound_fd, SNDCTL_DSP_SAMPLESIZE, &dspbits);
86: ioctl (sound_fd, SOUND_PCM_READ_BITS, &dspbits);
87: if (dspbits != currprefs.sound_bits) {
1.1.1.2 ! root 88: fprintf (stderr, "Can't use sound with %d bits\n", currprefs.sound_bits);
1.1 root 89: return 0;
90: }
91:
92: tmp = currprefs.stereo;
93: ioctl (sound_fd, SNDCTL_DSP_STEREO, &tmp);
94:
95: rate = currprefs.sound_freq;
96: ioctl (sound_fd, SNDCTL_DSP_SPEED, &rate);
97: ioctl (sound_fd, SOUND_PCM_READ_RATE, &rate);
98: /* Some soundcards have a bit of tolerance here. */
99: if (rate < currprefs.sound_freq * 90 / 100 || rate > currprefs.sound_freq * 110 / 100) {
1.1.1.2 ! root 100: fprintf (stderr, "Can't use sound with desired frequency %d\n", currprefs.sound_freq);
1.1 root 101: return 0;
102: }
103:
104: sample_evtime = (long)maxhpos * maxvpos * 50 / rate;
105:
106: if (dspbits == 16) {
107: /* Will this break horribly on bigendian machines? Possible... */
108: if (!(formats & AFMT_S16_LE))
109: return 0;
110: init_sound_table16 ();
1.1.1.2 ! root 111: sample_handler = currprefs.stereo ? sample16s_handler : sample16_handler;
1.1 root 112: } else {
113: if (!(formats & AFMT_U8))
114: return 0;
115: init_sound_table8 ();
1.1.1.2 ! root 116: sample_handler = currprefs.stereo ? sample8s_handler : sample8_handler;
1.1 root 117: }
118: sound_available = 1;
119: printf ("Sound driver found and configured for %d bits at %d Hz, buffer is %d bytes\n",
120: dspbits, rate, sndbufsize);
121: sndbufpt = sndbuffer;
122:
123: #ifdef FRAME_RATE_HACK
124: vsynctime = vsynctime * 9 / 10;
125: #endif
126:
127: return 1;
128: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.