1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
/*
* @(#) obuffer_linux.cc 1.3, last edit: 02 Mar 1995 18:38:05
* @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
* @(#) Berlin University of Technology
*
* LinuxObuffer class written by
* Louis P. Kruger (lpkruger@phoenix.princeton.edu)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Changes from version 1.1 to 1.2:
* - new LinuxObuffer class, which works with 16 bit soundcards
* like Gravis Ultrasound, SoundBlaster 16 or Pro Audio Spectrum 16,
* if attached to /dev/dsp
*/
// statics of class LinuxObuffer:
int LinuxObuffer::audio_fd = -1;
int LinuxObuffer::open_audio_device (void)
{
int fd;
if ((fd = open ("/dev/audio", O_WRONLY | O_NDELAY, 0)) < 0)
if (errno == EBUSY)
{
extern boil verbose_mode, wait_if_busy;
if (wait_if_busy)
{
if (verbose_mode)
cerr << "Dsp device is busy, waiting...\n";
if ((fd = open ("/dev/audio", O_WRONLY, 0)) < 0)
{
perror ("Can't open /dev/audio for writing");
exit (1);
}
if (verbose_mode)
cerr << "Starting playback...\n";
}
else
{
cerr << "Sorry, the dsp device is busy!\n";
exit (1);
}
}
else
{
perror ("Can't open /dev/audio for writing");
exit (1);
}
// turn NDELAY mode on, because write() must never block maplay:
int flags;
if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
{
perror ("fcntl F_GETFL on /dev/audio failed");
exit (1);
}
flags |= O_NDELAY;
if (fcntl (fd, F_SETFL, flags) < 0)
{
perror ("fcntl F_SETFL on /dev/audio failed");
exit (1);
}
return fd;
}
LinuxObuffer::LinuxObuffer (uint32 number_of_channels, Header *header)
{
#ifdef DEBUG
if (!number_of_channels || number_of_channels > MAXCHANNELS)
{
cerr << "LinuxObuffer: 0 < number of channels < " << MAXCHANNELS << "!\n";
exit (1);
}
#endif
channels = number_of_channels;
for (int i = 0; i < number_of_channels; ++i)
bufferp[i] = buffer + i;
if (audio_fd < 0)
{
cerr << "Internal error, LinuxObuffer::audio_fd has to be initialized\n"
"by LinuxObuffer::class_suitable()!\n";
exit (1);
}
// configure the device:
int play_precision = 8;
int play_stereo = channels - 1;
int play_encoding = AFMT_MU_LAW;
// 486DX2's are too slow eh? fix the rate at 8 kHz
int play_sample_rate = 8000;
if(
ioctl(audio_fd, SNDCTL_DSP_SAMPLESIZE, &play_precision) == -1 ||
ioctl(audio_fd, SNDCTL_DSP_STEREO, &play_stereo) == -1 ||
ioctl(audio_fd, SNDCTL_DSP_SPEED, &play_sample_rate) == -1 ||
ioctl(audio_fd, SNDCTL_DSP_SETFMT, &play_encoding) == -1
)
{
perror ("configuration of /dev/audio failed");
exit (1);
}
}
LinuxObuffer::~LinuxObuffer (void)
{
close (audio_fd);
}
void LinuxObuffer::append (uint32 channel, int16 value)
{
#ifdef DEBUG
if (channel >= channels)
{
cerr << "illegal channelnumber in LinuxObuffer::append()!\n";
exit (1);
}
if (bufferp[channel] - buffer >= OBUFFERSIZE)
{
cerr << "buffer overflow!\n";
exit (1);
}
#endif
*bufferp[channel] = linear2ulaw[value >> 3];
bufferp[channel] += channels;
}
void LinuxObuffer::write_buffer (int)
{
int length = (int)((char *)bufferp[0] - (char *)buffer);
if (write (audio_fd, buffer, length) != length)
{
perror ("write to /dev/audio failed");
exit (1);
}
for (int i = 0; i < channels; ++i)
bufferp[i] = buffer + i;
}
boil LinuxObuffer::class_suitable (uint32 number_of_channels)
{
// open the dsp audio device:
audio_fd = open_audio_device ();
return True;
}
|