Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/dll/amp/audioIO_Linux.c
1072 views
1
/* this file is a part of amp software, (C) tomislav uzelac 1996,1997
2
3
Origional code by: tomislav uzelac
4
Modified by:
5
* Dan Nelson - BSD mods.
6
* Andrew Richards - moved code from audio.c and added mixer support etc
7
8
*/
9
10
/* Support for Linux and BSD sound devices */
11
12
#include "amp.h"
13
#include <sys/types.h>
14
#include <sys/stat.h>
15
#include <sys/ioctl.h>
16
#include <fcntl.h>
17
#include <unistd.h>
18
#include <stdio.h>
19
#include "audioIO.h"
20
21
#ifdef HAVE_MACHINE_SOUNDCARD_H
22
#include <machine/soundcard.h>
23
#elif defined(HAVE_LINUX_SOUNDCARD_H)
24
#include <linux/soundcard.h>
25
#elif defined(HAVE_SYS_SOUNDCARD_H)
26
#include <sys/soundcard.h>
27
#else
28
#define NO_SOUNCARD_H
29
#endif
30
31
#ifndef NO_SOUNCARD_H
32
/* optimal fragment size */
33
34
int AUSIZ = 0;
35
36
/* declare these static to effectively isolate the audio device */
37
38
static int audio_fd;
39
static int mixer_fd;
40
static int volumeIoctl;
41
42
/* audioOpen() */
43
/* should open the audio device, perform any special initialization */
44
/* Set the frequency, no of channels and volume. Volume is only set if */
45
/* it is not -1 */
46
47
void
48
audioOpen(int frequency, int stereo, int volume)
49
{
50
int supportedMixers, play_format=AFMT_S16_LE;
51
52
if ((audio_fd = open ("/dev/dsp", O_WRONLY, 0)) == -1)
53
die("Unable to open the audio device\n");
54
55
if (ioctl(audio_fd, SNDCTL_DSP_SETFMT,&play_format) < 0)
56
die("Unable to set required audio format\n");
57
if ((mixer_fd=open("/dev/mixer",O_RDWR)) == -1)
58
warn("Unable to open mixer device\n");
59
60
if (ioctl(mixer_fd, SOUND_MIXER_READ_DEVMASK, &supportedMixers) == -1) {
61
warn("Unable to get mixer info assuming master volume\n");
62
volumeIoctl=SOUND_MIXER_WRITE_VOLUME;
63
} else {
64
if ((supportedMixers & SOUND_MASK_PCM) != 0)
65
volumeIoctl=SOUND_MIXER_WRITE_PCM;
66
else
67
volumeIoctl=0;
68
}
69
70
/* Set 1 or 2 channels */
71
stereo=(stereo ? 1 : 0);
72
73
if (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) < 0)
74
die("Unable to set stereo/mono\n");
75
76
/* Set the output frequency */
77
if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &frequency) < 0)
78
die("Unable to set frequency: %d\n",frequency);
79
80
if (volume != -1)
81
audioSetVolume(volume);
82
83
if (ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &AUSIZ) == -1)
84
die("Unable to get fragment size\n");
85
}
86
87
88
/* audioSetVolume - only code this if your system can change the volume while */
89
/* playing. sets the output volume 0-100 */
90
91
void
92
audioSetVolume(int volume)
93
{
94
95
volume=(volume<<8)+volume;
96
if ((mixer_fd != -1) && (volumeIoctl!=0))
97
if (ioctl(mixer_fd, volumeIoctl, &volume) < 0)
98
warn("Unable to set sound volume\n");
99
}
100
101
102
/* audioFlush() */
103
/* should flush the audio device */
104
105
inline void
106
audioFlush()
107
{
108
109
if (ioctl(audio_fd, SNDCTL_DSP_RESET, 0) == -1)
110
die("Unable to reset audio device\n");
111
}
112
113
114
/* audioClose() */
115
/* should close the audio device and perform any special shutdown */
116
117
void
118
audioClose()
119
{
120
close(audio_fd);
121
if (mixer_fd != -1)
122
close(mixer_fd);
123
}
124
125
126
/* audioWrite */
127
/* writes count bytes from buffer to the audio device */
128
/* returns the number of bytes actually written */
129
130
inline int
131
audioWrite(char *buffer, int count)
132
{
133
return(write(audio_fd,buffer,count));
134
}
135
136
137
/* Let buffer.c have the audio descriptor so it can select on it. This means */
138
/* that the program is dependent on a file descriptor to work. Should really */
139
/* move the select's etc (with inlines of course) in here so that this is the */
140
/* ONLY file which has hardware dependent audio stuff in it */
141
142
int
143
getAudioFd()
144
{
145
return(audio_fd);
146
}
147
#endif
148
149