1 /* Sarien - A Sierra AGI resource interpreter engine
2 * Copyright (C) 1999,2001 Stuart George and Claudio Matsuoka
3 *
4 * $Id: sound_bsd.c,v 1.3 2001/06/22 14:45:04 cmatsuoka Exp $
5 *
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; see docs/COPYING for further details.
9 */
10
11 /*
12 * BSD sound driver by Claudio Matsuoka <claudio@helllabs.org>
13 */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <sys/ioctl.h>
22 #include <sys/audioio.h>
23
24 #include "typedef.h"
25 #include "sound.h"
26
27 static int bsd_init_sound (SINT16 *);
28 static void bsd_close_sound (void);
29 static void dump_buffer (void);
30 static SINT16 *buffer;
31
32 static struct sound_driver sound_bsd = {
33 "BSD /dev/audio sound output",
34 bsd_init_sound,
35 bsd_close_sound,
36 };
37
38 static int audio_fd;
39
40
41 #include <pthread.h>
42
43 static pthread_t thread;
44
45
sound_thread(void * arg)46 static void *sound_thread (void *arg)
47 {
48 while (42) {
49 play_sound ();
50 mix_sound ();
51 dump_buffer ();
52 }
53 }
54
55
__init_sound()56 void __init_sound ()
57 {
58 snd = &sound_bsd;
59 }
60
61
bsd_init_sound(SINT16 * b)62 static int bsd_init_sound (SINT16 *b)
63 {
64 audio_info_t ainfo;
65
66 buffer = b;
67
68 if ((audio_fd = open ("/dev/audio", O_WRONLY)) < 0)
69 return err_Unk;
70
71 AUDIO_INITINFO (&ainfo);
72 ainfo.play.sample_rate = 22050;
73 ainfo.play.channels = 1;
74 ainfo.play.precision = 16;
75 ainfo.play.encoding = AUDIO_ENCODING_LINEAR;
76 ainfo.play.buffer_size = 16384;
77
78 if (ioctl (audio_fd, AUDIO_SETINFO, &ainfo) == -1)
79 return err_Unk;
80
81 report ("BSD sound driver written by claudio@helllabs.org.\n");
82
83 /* Set sound device to 16 bit, 22 kHz mono */
84
85 pthread_create (&thread, NULL, sound_thread, NULL);
86 pthread_detach (thread);
87
88 return err_OK;
89 }
90
91
bsd_close_sound()92 static void bsd_close_sound ()
93 {
94 close (audio_fd);
95 }
96
97
dump_buffer()98 static void dump_buffer ()
99 {
100 int i = BUFFER_SIZE << 1, j;
101 SINT16 *b = buffer;
102
103 do {
104 if ((j = write (audio_fd, b, i)) > 0) {
105 i -= j;
106 b += j;
107 }
108 } while (i);
109 }
110
111