ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/audio_oss_esd.cpp
Revision: 1.8
Committed: 2001-07-05T21:24:45Z (23 years, 4 months ago) by cebix
Branch: MAIN
Changes since 1.7: +41 -30 lines
Log Message:
some cleanups

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * audio_oss_esd.cpp - Audio support, implementation for OSS and ESD (Linux and FreeBSD)
3     *
4 cebix 1.6 * Basilisk II (C) 1997-2001 Christian Bauer
5 cebix 1.1 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #include "sysdeps.h"
22    
23     #include <sys/ioctl.h>
24     #include <unistd.h>
25     #include <errno.h>
26     #include <pthread.h>
27     #include <semaphore.h>
28    
29     #ifdef __linux__
30     #include <linux/soundcard.h>
31     #endif
32    
33     #ifdef __FreeBSD__
34     #include <machine/soundcard.h>
35     #endif
36    
37     #include "cpu_emulation.h"
38     #include "main.h"
39     #include "prefs.h"
40     #include "user_strings.h"
41     #include "audio.h"
42     #include "audio_defs.h"
43    
44 cebix 1.5 #ifdef ENABLE_ESD
45 cebix 1.1 #include <esd.h>
46     #endif
47    
48     #define DEBUG 0
49     #include "debug.h"
50    
51    
52 cebix 1.7 // The currently selected audio parameters (indices in audio_sample_rates[] etc. vectors)
53     static int audio_sample_rate_index = 0;
54     static int audio_sample_size_index = 0;
55     static int audio_channel_count_index = 0;
56 cebix 1.1
57     // Constants
58     #define DSP_NAME "/dev/dsp"
59    
60     // Global variables
61     static int audio_fd = -1; // fd of /dev/dsp or ESD
62     static int mixer_fd = -1; // fd of /dev/mixer
63     static sem_t audio_irq_done_sem; // Signal from interrupt to streaming thread: data block read
64     static bool sem_inited = false; // Flag: audio_irq_done_sem initialized
65     static int sound_buffer_size; // Size of sound buffer in bytes
66     static bool little_endian = false; // Flag: DSP accepts only little-endian 16-bit sound data
67 cebix 1.2 static uint8 silence_byte; // Byte value to use to fill sound buffers with silence
68 cebix 1.1 static pthread_t stream_thread; // Audio streaming thread
69     static pthread_attr_t stream_thread_attr; // Streaming thread attributes
70     static bool stream_thread_active = false; // Flag: streaming thread installed
71     static volatile bool stream_thread_cancel = false; // Flag: cancel streaming thread
72    
73     // Prototypes
74     static void *stream_func(void *arg);
75    
76    
77     /*
78     * Initialization
79     */
80    
81 cebix 1.2 // Set AudioStatus to reflect current audio stream format
82     static void set_audio_status_format(void)
83     {
84 cebix 1.7 AudioStatus.sample_rate = audio_sample_rates[audio_sample_rate_index];
85     AudioStatus.sample_size = audio_sample_sizes[audio_sample_size_index];
86     AudioStatus.channels = audio_channel_counts[audio_channel_count_index];
87 cebix 1.2 }
88    
89 cebix 1.1 // Init using /dev/dsp, returns false on error
90 cebix 1.7 static bool open_dsp(void)
91 cebix 1.1 {
92 cebix 1.7 // Open /dev/dsp
93     audio_fd = open(DSP_NAME, O_WRONLY);
94     if (audio_fd < 0) {
95     fprintf(stderr, "WARNING: Cannot open %s (%s)\n", DSP_NAME, strerror(errno));
96     return false;
97     }
98    
99 cebix 1.1 printf("Using " DSP_NAME " audio output\n");
100    
101     // Get supported sample formats
102 cebix 1.8 if (audio_sample_sizes.empty()) {
103 cebix 1.7 unsigned long format;
104     ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &format);
105     if (format & AFMT_U8)
106     audio_sample_sizes.push_back(8);
107     if (format & (AFMT_S16_BE | AFMT_S16_LE))
108     audio_sample_sizes.push_back(16);
109    
110     int stereo = 0;
111     if (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) == 0 && stereo == 0)
112     audio_channel_counts.push_back(1);
113     stereo = 1;
114     if (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) == 0 && stereo == 1)
115     audio_channel_counts.push_back(2);
116    
117     if (audio_sample_sizes.empty() || audio_channel_counts.empty()) {
118     WarningAlert(GetString(STR_AUDIO_FORMAT_WARN));
119     close(audio_fd);
120     audio_fd = -1;
121     return false;
122     }
123    
124     audio_sample_rates.push_back(11025 << 16);
125     audio_sample_rates.push_back(22050 << 16);
126     int rate = 44100;
127     ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate);
128     if (rate > 22050)
129     audio_sample_rates.push_back(rate << 16);
130    
131     // Default to highest supported values
132     audio_sample_rate_index = audio_sample_rates.size() - 1;
133     audio_sample_size_index = audio_sample_sizes.size() - 1;
134     audio_channel_count_index = audio_channel_counts.size() - 1;
135     }
136    
137     // Set DSP parameters
138 cebix 1.1 unsigned long format;
139 cebix 1.7 if (audio_sample_sizes[audio_sample_size_index] == 8) {
140     format = AFMT_U8;
141     little_endian = false;
142     silence_byte = 0x80;
143     } else {
144     unsigned long sup_format;
145     ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &format);
146     if (sup_format & AFMT_S16_BE) {
147     little_endian = false;
148     format = AFMT_S16_BE;
149     } else {
150     little_endian = true;
151     format = AFMT_S16_LE;
152     }
153 cebix 1.2 silence_byte = 0;
154     }
155 cebix 1.1 ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format);
156     int frag = 0x0004000c; // Block size: 4096 frames
157     ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag);
158 cebix 1.7 int stereo = (audio_channel_counts[audio_channel_count_index] == 2);
159 cebix 1.1 ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo);
160 cebix 1.7 int rate = audio_sample_rates[audio_sample_rate_index] >> 16;
161 cebix 1.1 ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate);
162    
163     // Get sound buffer size
164     ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &audio_frames_per_block);
165     D(bug("DSP_GETBLKSIZE %d\n", audio_frames_per_block));
166     return true;
167     }
168    
169     // Init using ESD, returns false on error
170 cebix 1.7 static bool open_esd(void)
171 cebix 1.1 {
172 cebix 1.5 #ifdef ENABLE_ESD
173 cebix 1.8 int rate;
174     esd_format_t format = ESD_STREAM | ESD_PLAY;
175    
176     if (audio_sample_sizes.empty()) {
177    
178     // Default values
179     rate = 44100;
180     format |= (ESD_BITS16 | ESD_STEREO);
181    
182     } else {
183 cebix 1.7
184 cebix 1.8 rate = audio_sample_rates[audio_sample_rate_index] >> 16;
185     if (audio_sample_sizes[audio_sample_size_index] == 8)
186     format |= ESD_BITS8;
187     else
188     format |= ESD_BITS16;
189     if (audio_channel_counts[audio_channel_count_index] == 1)
190     format |= ESD_MONO;
191     else
192     format |= ESD_STEREO;
193 cebix 1.7 }
194 cebix 1.1
195     #if WORDS_BIGENDIAN
196     little_endian = false;
197     #else
198     little_endian = true;
199     #endif
200 cebix 1.2 silence_byte = 0; // Is this correct for 8-bit mode?
201 cebix 1.1
202     // Open connection to ESD server
203 cebix 1.8 audio_fd = esd_play_stream(format, rate, NULL, NULL);
204 cebix 1.1 if (audio_fd < 0) {
205 cebix 1.7 fprintf(stderr, "WARNING: Cannot open ESD connection\n");
206 cebix 1.1 return false;
207     }
208    
209 cebix 1.7 printf("Using ESD audio output\n");
210 cebix 1.8
211     // ESD supports a variety of twisted little audio formats, all different
212     if (audio_sample_sizes.empty()) {
213    
214     // The reason we do this here is that we don't want to add sample
215     // rates etc. unless the ESD server connection could be opened
216     // (if ESD fails, /dev/dsp might be tried next)
217     audio_sample_rates.push_back(11025 << 16);
218     audio_sample_rates.push_back(22050 << 16);
219     audio_sample_rates.push_back(44100 << 16);
220     audio_sample_sizes.push_back(8);
221     audio_sample_sizes.push_back(16);
222     audio_channel_counts.push_back(1);
223     audio_channel_counts.push_back(2);
224    
225     // Default to highest supported values
226     audio_sample_rate_index = audio_sample_rates.size() - 1;
227     audio_sample_size_index = audio_sample_sizes.size() - 1;
228     audio_channel_count_index = audio_channel_counts.size() - 1;
229     }
230 cebix 1.7
231 cebix 1.1 // Sound buffer size = 4096 frames
232     audio_frames_per_block = 4096;
233     return true;
234     #endif
235     }
236    
237 cebix 1.7 static bool open_audio(void)
238 cebix 1.1 {
239 cebix 1.7 #ifdef ENABLE_ESD
240     // If ESPEAKER is set, the user probably wants to use ESD, so try that first
241     if (getenv("ESPEAKER"))
242     if (open_esd())
243     goto dev_opened;
244     #endif
245 cebix 1.1
246 cebix 1.7 // Try to open /dev/dsp
247     if (open_dsp())
248     goto dev_opened;
249 cebix 1.1
250 cebix 1.5 #ifdef ENABLE_ESD
251 cebix 1.7 // Hm, /dev/dsp failed so we try ESD again if ESPEAKER wasn't set
252     if (!getenv("ESPEAKER"))
253     if (open_esd())
254     goto dev_opened;
255 cebix 1.1 #endif
256    
257 cebix 1.7 // No audio device succeeded
258     WarningAlert(GetString(STR_NO_AUDIO_WARN));
259     return false;
260 cebix 1.1
261 cebix 1.7 // Device opened, set AudioStatus
262     dev_opened:
263     sound_buffer_size = (audio_sample_sizes[audio_sample_size_index] >> 3) * audio_channel_counts[audio_channel_count_index] * audio_frames_per_block;
264     set_audio_status_format();
265 cebix 1.1
266     // Start streaming thread
267     pthread_attr_init(&stream_thread_attr);
268     #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
269     if (geteuid() == 0) {
270     pthread_attr_setinheritsched(&stream_thread_attr, PTHREAD_EXPLICIT_SCHED);
271     pthread_attr_setschedpolicy(&stream_thread_attr, SCHED_FIFO);
272     struct sched_param fifo_param;
273     fifo_param.sched_priority = (sched_get_priority_min(SCHED_FIFO) + sched_get_priority_max(SCHED_FIFO)) / 2;
274     pthread_attr_setschedparam(&stream_thread_attr, &fifo_param);
275     }
276     #endif
277 cebix 1.7 stream_thread_cancel = false;
278 cebix 1.1 stream_thread_active = (pthread_create(&stream_thread, &stream_thread_attr, stream_func, NULL) == 0);
279    
280 cebix 1.7 // Everything went fine
281 cebix 1.1 audio_open = true;
282 cebix 1.7 return true;
283     }
284    
285     void AudioInit(void)
286     {
287     char str[256];
288    
289     // Init audio status (reasonable defaults) and feature flags
290     AudioStatus.sample_rate = 44100 << 16;
291     AudioStatus.sample_size = 16;
292     AudioStatus.channels = 2;
293     AudioStatus.mixer = 0;
294     AudioStatus.num_sources = 0;
295     audio_component_flags = cmpWantsRegisterMessage | kStereoOut | k16BitOut;
296    
297     // Sound disabled in prefs? Then do nothing
298     if (PrefsFindBool("nosound"))
299     return;
300    
301     // Init semaphore
302     if (sem_init(&audio_irq_done_sem, 0, 0) < 0)
303     return;
304     sem_inited = true;
305    
306     // Try to open /dev/mixer
307     mixer_fd = open("/dev/mixer", O_RDWR);
308     if (mixer_fd < 0)
309     printf("WARNING: Cannot open /dev/mixer (%s)", strerror(errno));
310    
311     // Open and initialize audio device
312     open_audio();
313 cebix 1.1 }
314    
315    
316     /*
317     * Deinitialization
318     */
319    
320 cebix 1.7 static void close_audio(void)
321 cebix 1.1 {
322     // Stop stream and delete semaphore
323     if (stream_thread_active) {
324     stream_thread_cancel = true;
325     #ifdef HAVE_PTHREAD_CANCEL
326     pthread_cancel(stream_thread);
327     #endif
328     pthread_join(stream_thread, NULL);
329     stream_thread_active = false;
330     }
331    
332 cebix 1.7 // Close /dev/dsp or ESD socket
333     if (audio_fd >= 0) {
334 cebix 1.1 close(audio_fd);
335 cebix 1.7 audio_fd = -1;
336     }
337    
338     audio_open = false;
339     }
340    
341     void AudioExit(void)
342     {
343     if (sem_inited) {
344     sem_destroy(&audio_irq_done_sem);
345     sem_inited = false;
346     }
347 cebix 1.1
348     // Close /dev/mixer
349 cebix 1.7 if (mixer_fd >= 0) {
350 cebix 1.1 close(mixer_fd);
351 cebix 1.7 mixer_fd = -1;
352     }
353 cebix 1.1 }
354    
355    
356     /*
357     * First source added, start audio stream
358     */
359    
360     void audio_enter_stream()
361     {
362     // Streaming thread is always running to avoid clicking noises
363     }
364    
365    
366     /*
367     * Last source removed, stop audio stream
368     */
369    
370     void audio_exit_stream()
371     {
372     // Streaming thread is always running to avoid clicking noises
373     }
374    
375    
376     /*
377     * Streaming function
378     */
379    
380     static uint32 apple_stream_info; // Mac address of SoundComponentData struct describing next buffer
381    
382     static void *stream_func(void *arg)
383     {
384     int16 *silent_buffer = new int16[sound_buffer_size / 2];
385     int16 *last_buffer = new int16[sound_buffer_size / 2];
386 cebix 1.2 memset(silent_buffer, silence_byte, sound_buffer_size);
387 cebix 1.1
388     while (!stream_thread_cancel) {
389     if (AudioStatus.num_sources) {
390    
391     // Trigger audio interrupt to get new buffer
392     D(bug("stream: triggering irq\n"));
393     SetInterruptFlag(INTFLAG_AUDIO);
394     TriggerInterrupt();
395     D(bug("stream: waiting for ack\n"));
396     sem_wait(&audio_irq_done_sem);
397     D(bug("stream: ack received\n"));
398    
399     // Get size of audio data
400     uint32 apple_stream_info = ReadMacInt32(audio_data + adatStreamInfo);
401     if (apple_stream_info) {
402     int work_size = ReadMacInt32(apple_stream_info + scd_sampleCount) * (AudioStatus.sample_size >> 3) * AudioStatus.channels;
403     D(bug("stream: work_size %d\n", work_size));
404     if (work_size > sound_buffer_size)
405     work_size = sound_buffer_size;
406     if (work_size == 0)
407     goto silence;
408    
409     // Send data to DSP
410     if (work_size == sound_buffer_size && !little_endian)
411     write(audio_fd, Mac2HostAddr(ReadMacInt32(apple_stream_info + scd_buffer)), sound_buffer_size);
412     else {
413     // Last buffer or little-endian DSP
414     if (little_endian) {
415     int16 *p = (int16 *)Mac2HostAddr(ReadMacInt32(apple_stream_info + scd_buffer));
416     for (int i=0; i<work_size/2; i++)
417     last_buffer[i] = ntohs(p[i]);
418     } else
419 cebix 1.3 Mac2Host_memcpy(last_buffer, ReadMacInt32(apple_stream_info + scd_buffer), work_size);
420 cebix 1.2 memset((uint8 *)last_buffer + work_size, silence_byte, sound_buffer_size - work_size);
421 cebix 1.1 write(audio_fd, last_buffer, sound_buffer_size);
422     }
423     D(bug("stream: data written\n"));
424     } else
425     goto silence;
426    
427     } else {
428    
429     // Audio not active, play silence
430     silence: write(audio_fd, silent_buffer, sound_buffer_size);
431     }
432     }
433     delete[] silent_buffer;
434     delete[] last_buffer;
435     return NULL;
436     }
437    
438    
439     /*
440     * MacOS audio interrupt, read next data block
441     */
442    
443     void AudioInterrupt(void)
444     {
445     D(bug("AudioInterrupt\n"));
446    
447     // Get data from apple mixer
448     if (AudioStatus.mixer) {
449     M68kRegisters r;
450     r.a[0] = audio_data + adatStreamInfo;
451     r.a[1] = AudioStatus.mixer;
452     Execute68k(audio_data + adatGetSourceData, &r);
453     D(bug(" GetSourceData() returns %08lx\n", r.d[0]));
454     } else
455     WriteMacInt32(audio_data + adatStreamInfo, 0);
456    
457     // Signal stream function
458     sem_post(&audio_irq_done_sem);
459     D(bug("AudioInterrupt done\n"));
460     }
461    
462    
463     /*
464     * Set sampling parameters
465 cebix 1.7 * "index" is an index into the audio_sample_rates[] etc. vectors
466 cebix 1.1 * It is guaranteed that AudioStatus.num_sources == 0
467     */
468    
469 cebix 1.7 bool audio_set_sample_rate(int index)
470 cebix 1.1 {
471 cebix 1.7 close_audio();
472     audio_sample_rate_index = index;
473     return open_audio();
474 cebix 1.1 }
475    
476 cebix 1.7 bool audio_set_sample_size(int index)
477 cebix 1.1 {
478 cebix 1.7 close_audio();
479     audio_sample_size_index = index;
480     return open_audio();
481 cebix 1.1 }
482    
483 cebix 1.7 bool audio_set_channels(int index)
484 cebix 1.1 {
485 cebix 1.7 close_audio();
486     audio_channel_count_index = index;
487     return open_audio();
488 cebix 1.1 }
489    
490    
491     /*
492     * Get/set volume controls (volume values received/returned have the left channel
493     * volume in the upper 16 bits and the right channel volume in the lower 16 bits;
494     * both volumes are 8.8 fixed point values with 0x0100 meaning "maximum volume"))
495     */
496    
497     bool audio_get_main_mute(void)
498     {
499     return false;
500     }
501    
502     uint32 audio_get_main_volume(void)
503     {
504     if (mixer_fd >= 0) {
505     int vol;
506     if (ioctl(mixer_fd, SOUND_MIXER_READ_PCM, &vol) == 0) {
507     int left = vol >> 8;
508     int right = vol & 0xff;
509     return ((left * 256 / 100) << 16) | (right * 256 / 100);
510     }
511     }
512     return 0x01000100;
513     }
514    
515     bool audio_get_speaker_mute(void)
516     {
517     return false;
518     }
519    
520     uint32 audio_get_speaker_volume(void)
521     {
522     if (mixer_fd >= 0) {
523     int vol;
524     if (ioctl(mixer_fd, SOUND_MIXER_READ_VOLUME, &vol) == 0) {
525     int left = vol >> 8;
526     int right = vol & 0xff;
527     return ((left * 256 / 100) << 16) | (right * 256 / 100);
528     }
529     }
530     return 0x01000100;
531     }
532    
533     void audio_set_main_mute(bool mute)
534     {
535     }
536    
537     void audio_set_main_volume(uint32 vol)
538     {
539     if (mixer_fd >= 0) {
540     int left = vol >> 16;
541     int right = vol & 0xffff;
542     int p = ((left * 100 / 256) << 8) | (right * 100 / 256);
543     ioctl(mixer_fd, SOUND_MIXER_WRITE_PCM, &p);
544     }
545     }
546    
547     void audio_set_speaker_mute(bool mute)
548     {
549     }
550    
551     void audio_set_speaker_volume(uint32 vol)
552     {
553     if (mixer_fd >= 0) {
554     int left = vol >> 16;
555     int right = vol & 0xffff;
556     int p = ((left * 100 / 256) << 8) | (right * 100 / 256);
557     ioctl(mixer_fd, SOUND_MIXER_WRITE_VOLUME, &p);
558     }
559     }