ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/Solaris/audio_solaris.cpp
Revision: 1.10
Committed: 2004-01-12T15:54:46Z (20 years, 6 months ago) by cebix
Branch: MAIN
CVS Tags: nigel-build-16, nigel-build-15
Changes since 1.9: +1 -1 lines
Log Message:
Happy New Year!

File Contents

# Content
1 /*
2 * audio_solaris.cpp - Audio support, Solaris implementation
3 *
4 * Adapted from Frodo's Solaris sound routines by Marc Chabanas
5 *
6 * Basilisk II (C) 1997-2004 Christian Bauer
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #include "sysdeps.h"
24
25 #include <sys/ioctl.h>
26 #include <sys/audioio.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <pthread.h>
30 #include <semaphore.h>
31
32 #include "cpu_emulation.h"
33 #include "main.h"
34 #include "prefs.h"
35 #include "user_strings.h"
36 #include "audio.h"
37 #include "audio_defs.h"
38
39 #define DEBUG 0
40 #include "debug.h"
41
42
43 // Global variables
44 static int fd = -1; // fd of /dev/audio
45 static sem_t audio_irq_done_sem; // Signal from interrupt to streaming thread: data block read
46 static pthread_t stream_thread; // Audio streaming thread
47 static pthread_attr_t stream_thread_attr; // Streaming thread attributes
48 static bool stream_thread_active = false;
49 static int sound_buffer_size; // Size of sound buffer in bytes
50
51 // Prototypes
52 static void *stream_func(void *arg);
53
54
55 /*
56 * Initialization
57 */
58
59 // Set AudioStatus to reflect current audio stream format
60 static void set_audio_status_format(void)
61 {
62 AudioStatus.sample_rate = audio_sample_rates[0];
63 AudioStatus.sample_size = audio_sample_sizes[0];
64 AudioStatus.channels = audio_channel_counts[0];
65 }
66
67 void AudioInit(void)
68 {
69 char str[256];
70
71 // Init audio status and feature flags
72 audio_sample_rates.push_back(44100 << 16);
73 audio_sample_sizes.push_back(16);
74 audio_channel_counts.push_back(2);
75 set_audio_status_format();
76 AudioStatus.mixer = 0;
77 AudioStatus.num_sources = 0;
78 audio_component_flags = cmpWantsRegisterMessage | kStereoOut | k16BitOut;
79
80 // Sound disabled in prefs? Then do nothing
81 if (PrefsFindBool("nosound"))
82 return;
83
84 // Init semaphore
85 if (sem_init(&audio_irq_done_sem, 0, 0) < 0)
86 return;
87
88 // Open /dev/audio
89 fd = open("/dev/audio", O_WRONLY | O_NDELAY);
90 if (fd < 0) {
91 sprintf(str, GetString(STR_NO_AUDIO_DEV_WARN), "/dev/audio", strerror(errno));
92 WarningAlert(str);
93 sem_destroy(&audio_irq_done_sem);
94 return;
95 }
96
97 // Set audio parameters
98 struct audio_info info;
99 AUDIO_INITINFO(&info);
100 info.play.sample_rate = AudioStatus.sample_rate >> 16;
101 info.play.channels = AudioStatus.channels;
102 info.play.precision = AudioStatus.sample_size;
103 info.play.encoding = AUDIO_ENCODING_LINEAR;
104 info.play.port = AUDIO_SPEAKER;
105 if (ioctl(fd, AUDIO_SETINFO, &info)) {
106 WarningAlert(GetString(STR_AUDIO_FORMAT_WARN));
107 close(fd);
108 fd = -1;
109 sem_destroy(&audio_irq_done_sem);
110 return;
111 }
112
113 // 2048 frames per buffer
114 audio_frames_per_block = 2048;
115 sound_buffer_size = (AudioStatus.sample_size>>3) * AudioStatus.channels * audio_frames_per_block;
116
117 // Start audio thread
118 Set_pthread_attr(&stream_thread_attr, 0);
119 stream_thread_active = (pthread_create(&stream_thread, &stream_thread_attr, stream_func, NULL) == 0);
120
121 // Everything OK
122 audio_open = true;
123 }
124
125
126 /*
127 * Deinitialization
128 */
129
130 void AudioExit(void)
131 {
132 // Stop audio thread
133 if (stream_thread_active) {
134 pthread_cancel(stream_thread);
135 pthread_join(stream_thread, NULL);
136 sem_destroy(&audio_irq_done_sem);
137 stream_thread_active = false;
138 }
139
140 // Close /dev/audio
141 if (fd > 0) {
142 ioctl(fd, AUDIO_DRAIN);
143 close(fd);
144 }
145 }
146
147
148 /*
149 * First source added, start audio stream
150 */
151
152 void audio_enter_stream()
153 {
154 }
155
156
157 /*
158 * Last source removed, stop audio stream
159 */
160
161 void audio_exit_stream()
162 {
163 }
164
165
166 /*
167 * Streaming function
168 */
169
170 static uint32 apple_stream_info; // Mac address of SoundComponentData struct describing next buffer
171
172 static void *stream_func(void *arg)
173 {
174 int16 *silent_buffer = new int16[sound_buffer_size / 2];
175 int16 *last_buffer = new int16[sound_buffer_size / 2];
176 memset(silent_buffer, 0, sound_buffer_size);
177
178 uint_t sent = 0, delta;
179 struct audio_info status;
180
181 for (;;) {
182 if (AudioStatus.num_sources) {
183
184 // Trigger audio interrupt to get new buffer
185 D(bug("stream: triggering irq\n"));
186 SetInterruptFlag(INTFLAG_AUDIO);
187 TriggerInterrupt();
188 D(bug("stream: waiting for ack\n"));
189 sem_wait(&audio_irq_done_sem);
190 D(bug("stream: ack received\n"));
191
192 // Get size of audio data
193 uint32 apple_stream_info = ReadMacInt32(audio_data + adatStreamInfo);
194 if (apple_stream_info) {
195 int work_size = ReadMacInt32(apple_stream_info + scd_sampleCount) * (AudioStatus.sample_size >> 3) * AudioStatus.channels;
196 D(bug("stream: work_size %d\n", work_size));
197 if (work_size > sound_buffer_size)
198 work_size = sound_buffer_size;
199 if (work_size == 0)
200 goto silence;
201
202 // Send data to audio port
203 if (work_size == sound_buffer_size)
204 write(fd, Mac2HostAddr(ReadMacInt32(apple_stream_info + scd_buffer)), sound_buffer_size);
205 else {
206 // Last buffer
207 Mac2Host_memcpy(last_buffer, ReadMacInt32(apple_stream_info + scd_buffer), work_size);
208 memset((uint8 *)last_buffer + work_size, 0, sound_buffer_size - work_size);
209 write(fd, last_buffer, sound_buffer_size);
210 }
211 D(bug("stream: data written\n"));
212 } else
213 goto silence;
214
215 } else {
216
217 // Audio not active, play silence
218 silence: write(fd, silent_buffer, sound_buffer_size);
219 }
220
221 // We allow a maximum of three buffers to be sent
222 sent += audio_frames_per_block;
223 ioctl(fd, AUDIO_GETINFO, &status);
224 while ((delta = sent - status.play.samples) > (audio_frames_per_block * 3)) {
225 unsigned int sl = 1000000 * (delta - audio_frames_per_block * 3) / (AudioStatus.sample_rate >> 16);
226 usleep(sl);
227 ioctl(fd, AUDIO_GETINFO, &status);
228 }
229 }
230 return NULL;
231 }
232
233
234 /*
235 * MacOS audio interrupt, read next data block
236 */
237
238 void AudioInterrupt(void)
239 {
240 D(bug("AudioInterrupt\n"));
241
242 // Get data from apple mixer
243 if (AudioStatus.mixer) {
244 M68kRegisters r;
245 r.a[0] = audio_data + adatStreamInfo;
246 r.a[1] = AudioStatus.mixer;
247 Execute68k(audio_data + adatGetSourceData, &r);
248 D(bug(" GetSourceData() returns %08lx\n", r.d[0]));
249 } else
250 WriteMacInt32(audio_data + adatStreamInfo, 0);
251
252 // Signal stream function
253 sem_post(&audio_irq_done_sem);
254 D(bug("AudioInterrupt done\n"));
255 }
256
257
258 /*
259 * Set sampling parameters
260 * "index" is an index into the audio_sample_rates[] etc. arrays
261 * It is guaranteed that AudioStatus.num_sources == 0
262 */
263
264 bool audio_set_sample_rate(int index)
265 {
266 return true;
267 }
268
269 bool audio_set_sample_size(int index)
270 {
271 return true;
272 }
273
274 bool audio_set_channels(int index)
275 {
276 return true;
277 }
278
279
280 /*
281 * Get/set volume controls (volume values received/returned have the left channel
282 * volume in the upper 16 bits and the right channel volume in the lower 16 bits;
283 * both volumes are 8.8 fixed point values with 0x0100 meaning "maximum volume"))
284 */
285
286 bool audio_get_main_mute(void)
287 {
288 return false;
289 }
290
291 uint32 audio_get_main_volume(void)
292 {
293 return 0x01000100;
294 }
295
296 bool audio_get_speaker_mute(void)
297 {
298 return false;
299 }
300
301 uint32 audio_get_speaker_volume(void)
302 {
303 return 0x01000100;
304 }
305
306 void audio_set_main_mute(bool mute)
307 {
308 }
309
310 void audio_set_main_volume(uint32 vol)
311 {
312 }
313
314 void audio_set_speaker_mute(bool mute)
315 {
316 }
317
318 void audio_set_speaker_volume(uint32 vol)
319 {
320 }