1 /***************************************************************************
2                          sound_oss.c  -  description
3                              -------------------
4     begin                : Sun Jul 23 2000
5     copyright            : (C) 2000 by Claudio Matsuoka
6     email                : claudio@helllabs.org
7 
8     $Id: sound_oss.c,v 1.3 2000/11/22 22:43:25 claudio2 Exp $
9  ***************************************************************************/
10 
11 /***************************************************************************
12  *                                                                         *
13  *   This program is free software; you can redistribute it and/or modify  *
14  *   it under the terms of the GNU General Public License as published by  *
15  *   the Free Software Foundation; either version 2 of the License, or     *
16  *   (at your option) any later version.                                   *
17  *                                                                         *
18  ***************************************************************************/
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <sys/ioctl.h>
26 #include <sys/soundcard.h>
27 #include "config.h"
28 #include "soundplayer.h"
29 
30 #define FRAGNUM		(32 - 1)
31 #define FRAGSIZE	(8)
32 
33 static int oss_init_sound (void);
34 static void oss_close_sound (void);
35 static void dump_buffer (void);
36 
37 struct sound_driver sound_oss = {
38 	"OSS /dev/dsp sound output",
39 	oss_init_sound,
40 	oss_close_sound,
41 	dump_buffer
42 };
43 
44 static int audio_fd;
45 
46 
oss_init_sound()47 static int oss_init_sound ()
48 {
49 	int i;
50 	audio_buf_info info;
51 
52 	if ((audio_fd = open ("/dev/dsp", O_WRONLY)) == -1)
53 		return -1;
54 
55 	printf ("sound_oss: OSS support by claudio@helllabs.org\n");
56 
57 	/* Set sound device to 16 bit, 22 kHz mono */
58 
59 	i = (FRAGNUM << 16 | FRAGSIZE);
60 	ioctl (audio_fd, SNDCTL_DSP_SETFRAGMENT, &i);
61 
62 	ioctl (audio_fd, SNDCTL_DSP_GETOSPACE, &info);
63 	printf ("sound_oss: %d fragments of %d bytes\n",
64 		info.fragstotal, info.fragsize);
65 
66 	i = AFMT_S16_NE;
67 	ioctl (audio_fd, SNDCTL_DSP_SETFMT, &i);
68 	i = 0;
69 	ioctl (audio_fd, SNDCTL_DSP_STEREO, &i);
70 	i = 22050;
71 	ioctl (audio_fd, SNDCTL_DSP_SPEED, &i);
72 
73 	return 0;
74 }
75 
76 
oss_close_sound()77 static void oss_close_sound ()
78 {
79 	ioctl (audio_fd, SNDCTL_DSP_SYNC);
80 	close (audio_fd);
81 }
82 
83 
dump_buffer()84 static void dump_buffer ()
85 {
86 	int i = BUFFER_SIZE << 1, j;
87 	short *b = snd_buffer;
88 
89 	do {
90 		if ((j = write (audio_fd, b, i)) > 0) {
91 			i -= j;
92 			b += j;
93 		}
94 	} while (i);
95 }
96 
97