1
/* this file is a part of amp software, (C) tomislav uzelac 1996,1997
3
Origional code by: tomislav uzelac
5
* Dan Nelson - BSD mods.
6
* Andrew Richards - moved code from audio.c and added mixer support etc
10
/* Support for Linux and BSD sound devices */
12
#include <sys/types.h>
14
#include <sys/ioctl.h>
27
// Why all these different system cannot make a standard where this
28
// soundcard.h file is ?
33
#if defined(HAVE_MACHINE_SOUNDCARD_H)
35
#include <machine/soundcard.h>
38
#if defined(HAVE_SYS_SOUNDCARD_H)
40
#include <sys/soundcard.h>
43
#include <linux/soundcard.h>
49
/* optimal fragment size */
53
// declare these static to effectively isolate the audio device
57
static int volumeIoctl;
61
int audioConstruct() {
68
void audioDestruct() {
75
should open the audio device, perform any special initialization
78
audio_fd = open ("/dev/dsp", O_WRONLY, 0);
80
perror("Unable to open the audio");
83
// Ok here something important if your programm forks:
85
if (fcntl(audio_fd,F_SETFD,true) < 0) {
86
perror("fcntl socket");exit(1);
90
return (audio_fd > 0);
93
inline void audioFlush() {
94
if (ioctl(audio_fd, SNDCTL_DSP_RESET, 0) == -1)
95
perror("Unable to reset audio device\n");
99
should close the audio device and perform any special shutdown
103
if (close(audio_fd) < 0) {
104
perror("error close audiodevice:");
109
void audioInit(int sampleSize,int frequency, int stereo, int sign, int big) {
113
"%s, %d: expecting signed audio data, "
114
"initialized unsigned (ignored)\n",
115
__FILE__, __LINE__ );
120
"%s, %d: expecting little endian audio data, "
121
"initialized big endian (ignored)\n",
122
__FILE__, __LINE__ );
125
int play_format=AFMT_S16_LE;
127
if (sampleSize == 8) {
130
ioctl(audio_fd,SNDCTL_DSP_RESET,NULL);
132
if (ioctl(audio_fd, SNDCTL_DSP_SETFMT,&play_format) < 0) {
133
perror("Unable to set required audio format\n");
136
/* Set 1 or 2 channels */
137
stereo=(stereo ? 1 : 0);
139
if (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) < 0) {
140
perror("Unable to set stereo/mono\n");
144
/* Set the output frequency */
145
if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &frequency) < 0) {
146
perror("Unable to set frequency");
150
if (ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &AUSIZ) == -1) {
151
perror("Unable to get fragment size\n");
157
int getAudioBufferSize() {
158
struct audio_buf_info buf_info;
160
if (ioctl(audio_fd,SNDCTL_DSP_GETOSPACE,&buf_info) == -1) {
161
perror("ioctl getAudioBufferSize using default");
172
if ((mixer_fd=open("/dev/mixer",O_RDWR)) == -1) {
173
perror("Unable to open mixer device");
176
// Ok here something important if your programm forks:
178
if (fcntl(mixer_fd,F_SETFD,true) < 0) {
179
perror("fcntl socket");exit(1);
183
if (ioctl(mixer_fd, SOUND_MIXER_READ_DEVMASK, &supportedMixers) == -1){
184
perror("Unable to get mixer info assuming master volume");
185
volumeIoctl=SOUND_MIXER_WRITE_VOLUME;
187
if ((supportedMixers & SOUND_MASK_PCM) != 0)
188
volumeIoctl=SOUND_MIXER_WRITE_PCM;
193
return (mixer_fd > 0);
198
if (mixer_fd != -1) {
204
only code this if your system can change the volume while
207
void mixerSetVolume(int leftVolume,int rightVolume) {
210
volume=leftVolume+(rightVolume<<8);
211
if ((mixer_fd != -1) && (volumeIoctl!=0)) {
212
if (ioctl(mixer_fd, volumeIoctl, &volume) < 0) {
213
perror("Unable to set sound volume");
220
int audioWrite(char *buffer, int count) {
221
return(write(audio_fd,buffer,count));