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 "options.h"
13 #include "memory.h"
14 #include "custom.h"
15 #include "audio.h"
16 #include "gensound.h"
17 #include "sounddep/sound.h"
18 
19 #include <sys/ioctl.h>
20 #include <sys/soundcard.h>
21 
22 int sound_fd;
23 static int have_sound;
24 static unsigned long formats;
25 
26 unsigned long sndbuf_written;
27 
28 uae_u16 sndbuffer[44100];
29 uae_u16 *sndbufpt;
30 int sndbufsize;
31 
exact_log2(int v)32 static int exact_log2(int v)
33 {
34     int l = 0;
35     while ((v >>= 1) != 0)
36 	l++;
37     return l;
38 }
39 
close_sound(void)40 void close_sound(void)
41 {
42     int t;
43     uae_u32 v;
44     char buf[4];
45 
46     if (!have_sound)
47 	return;
48 
49     t = 0;
50     v = sndbuf_written;
51     buf[t] = v & 255;
52     buf[t+1] = (v>>8) & 255;
53     buf[t+2] = (v>>16) & 255;
54     buf[t+3] = (v>>24) & 255;
55     lseek (sound_fd, 40, SEEK_SET);
56     write (sound_fd, buf, 4);
57 
58     v += 36;
59     buf[t] = v & 255;
60     buf[t+1] = (v>>8) & 255;
61     buf[t+2] = (v>>16) & 255;
62     buf[t+3] = (v>>24) & 255;
63     lseek (sound_fd, 4, SEEK_SET);
64     write (sound_fd, buf, 4);
65 
66     close(sound_fd);
67 }
68 
setup_sound(void)69 int setup_sound(void)
70 {
71     sound_fd = open ("sound.output", O_CREAT|O_TRUNC|O_WRONLY, 0666);
72     have_sound = !(sound_fd < 0);
73     if (!have_sound)
74 	return 0;
75 
76     sound_available = 1;
77     return 1;
78 }
79 
init_sound(void)80 int init_sound (void)
81 {
82     int t;
83     uae_u32 v;
84     int tmp;
85 
86     char buf[200] = "RIFF    WAVEfmt                     data    ";
87 
88     /* Prepare a .WAV header */
89     sndbuf_written = 44;
90     t = 16; v = 16;
91     buf[t] = v & 255;
92     buf[t+1] = (v>>8) & 255;
93     buf[t+2] = (v>>16) & 255;
94     buf[t+3] = (v>>24) & 255;
95 
96     t = 20; v = 0x00010001 + (/* currprefs.stereo */ 1 ? 0x10000 : 0);
97     buf[t] = v & 255;
98     buf[t+1] = (v>>8) & 255;
99     buf[t+2] = (v>>16) & 255;
100     buf[t+3] = (v>>24) & 255;
101 
102     t = 24; v = currprefs.sound_freq;
103     buf[t] = v & 255;
104     buf[t+1] = (v>>8) & 255;
105     buf[t+2] = (v>>16) & 255;
106     buf[t+3] = (v>>24) & 255;
107     t = 32; v = (2 * 2) + 65536 * 16;
108     buf[t] = v & 255;
109     buf[t+1] = (v>>8) & 255;
110     buf[t+2] = (v>>16) & 255;
111     buf[t+3] = (v>>24) & 255;
112     t = 28; v = (currprefs.sound_freq * 2 * 2);
113     buf[t] = v & 255;
114     buf[t+1] = (v>>8) & 255;
115     buf[t+2] = (v>>16) & 255;
116     buf[t+3] = (v>>24) & 255;
117     write (sound_fd, buf, 44);
118 
119     obtained_freq = currprefs.sound_freq;
120 
121     init_sound_table16 ();
122     sample_handler = sample16s_handler;
123     sound_available = 1;
124     sndbufsize = 44100;
125     printf ("Writing sound into \"sound.output\"; 16 bits at %d Hz\n",
126 	    currprefs.sound_freq, sndbufsize);
127     sndbufpt = sndbuffer;
128     return 1;
129 }
130