1
/* this file is a part of amp software, (C) tomislav uzelac 1996,1997
3
Origional code by: Lutz Vieweg
5
* Andrew Richards - moved code from audio.c
10
#include <sys/audio.h>
12
#include <sys/ioctl.h>
18
/* declare these static to effectively isolate the audio device */
24
/* should open the audio device, perform any special initialization */
25
/* Set the frequency, no of channels and volume. Volume is only set if */
29
if ((audio_fd = open("/dev/audio",O_RDWR))==-1)
30
die(" unable to open the audio device\n");
32
DB(audio, msg("Audio device opened on %d\n",audio_fd); )
37
audioInit(int sampleSize,int frequency, int stereo)
43
if ((flags = fcntl (audio_fd, F_GETFL, 0)) < 0) {
44
die("unable to set non-blocking mode for /dev/audio\n");
47
if (fcntl (audio_fd, F_SETFL, flags) < 0) {
48
die("unable to set non-blocking mode for /dev/audio\n");
51
if ( ioctl(audio_fd, AUDIO_SET_DATA_FORMAT, AUDIO_FORMAT_LINEAR16BIT) < 0 ||
52
ioctl(audio_fd, AUDIO_SET_CHANNELS, stereo ? 2 : 1) < 0 ||
53
ioctl(audio_fd, AUDIO_SET_OUTPUT, AUDIO_OUT_SPEAKER | AUDIO_OUT_HEADPHONE
54
| AUDIO_OUT_LINE) < 0 ||
55
ioctl(audio_fd, AUDIO_SET_SAMPLE_RATE, frequency) < 0) {
59
struct audio_describe description;
60
struct audio_gains gains;
61
float fvolume = (float)volume / 100.0f;
62
if (ioctl(audio_fd, AUDIO_DESCRIBE, &description)) {
65
if (ioctl (audio_fd, AUDIO_GET_GAINS, &gains)) {
69
gains.transmit_gain = (int)((float)description.min_transmit_gain +
70
(float)(description.max_transmit_gain
71
- description.min_transmit_gain)
74
/* gains.monitor_gain = description.min_monitor_gain; */ /* don't monitor ! */
76
if (ioctl (audio_fd, AUDIO_SET_GAINS, &gains)) {
81
if (ioctl(audio_fd, AUDIO_SET_TXBUFSIZE, 4096 * 8)) {
85
die(" unable to setup /dev/audio\n");
89
/* audioSetVolume - only code this if your system can change the volume while */
90
/* playing. sets the output volume 0-100 */
93
audioSetVolume(int volume)
95
struct audio_describe description;
96
struct audio_gains gains;
98
float fvolume = ((float)volume) / 100.0f;
99
if (ioctl(audio_fd, AUDIO_DESCRIBE, &description)) {
102
if (ioctl (audio_fd, AUDIO_GET_GAINS, &gains)) {
106
gains.transmit_gain = (int)((float)description.min_transmit_gain +
107
(float)(description.max_transmit_gain
108
- description.min_transmit_gain)
110
if (ioctl (audio_fd, AUDIO_SET_GAINS, &gains)) {
114
/* could evaluate "failed" here - but who cares? */
116
DB(audio, msg("volume set to %d%%\n",volume); )
121
/* should flush the audio device */
126
DB(audio, msg("audio: flush %d\n",audio_fd) );
131
/* should close the audio device and perform any special shutdown */
137
DB(audio, msg("audio: closed %d\n",audio_fd) );
142
/* writes count bytes from buffer to the audio device */
143
/* returns the number of bytes actually written */
145
int audioWrite(char *buffer, int count)
147
DB(audio, msg("audio: Writing %d bytes to audio descriptor %d\n",count,getAudioFd()) );
148
return(write(audio_fd,buffer,count));
152
/* Let buffer.c have the audio descriptor so it can select on it. This means */
153
/* that the program is dependent on an file descriptor to work. Should really */
154
/* move the select's etc (with inlines of course) in here so that this is the */
155
/* ONLY file which has hardware dependent audio stuff in it */
164
Try to set the priority of this process to a value which
165
allows us to play without buffering, thus saving memory
166
and avoiding cache-misses.
167
If we cannot get any priority high enough to allow for
168
undisturbed replay (because we don't have sufficient
169
priviledges), return a zero, otherwise, return a one.
171
int audioSetPriority(void) {
173
/* try to lock process in physical memory, just ignore if this fails */
174
plock(PROCSHLIBLOCK);
176
/* try to set a realtime-priority of 64 */
177
if (-1 != rtprio(0, 64)) {
178
DB(audio, msg("using real-time priority\n"); )
182
/* try to set a nice-level of -20 */
183
if (-1 != nice(-20)) {
184
DB(audio, msg("using nice-level -20\n"); )
188
DB(audio, msg("using buffered output\n"); )
189
return 0; /* need to use a buffer */