2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2011-02-25 01:05:47 +03:00
|
|
|
// $Id$
|
2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2011-01-24 23:35:51 +03:00
|
|
|
// Copyright (C) 2001-2011 The Bochs Project
|
2001-04-10 05:04:59 +04:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library 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
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
2009-02-08 12:05:52 +03:00
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2008-02-16 01:05:43 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-07-12 19:21:36 +04:00
|
|
|
// Josef Drexler coded the original version of the lowlevel sound support
|
|
|
|
// for Linux using OSS. The current version also supports OSS on FreeBSD and
|
|
|
|
// ALSA PCM output on Linux.
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-02-15 00:14:20 +03:00
|
|
|
#include "iodev.h"
|
2005-12-10 21:37:35 +03:00
|
|
|
|
2011-03-19 15:57:13 +03:00
|
|
|
#if (defined(linux) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)) && BX_SUPPORT_SOUNDLOW
|
2005-12-10 21:37:35 +03:00
|
|
|
|
2011-01-26 02:29:08 +03:00
|
|
|
#define LOG_THIS device->
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-02-11 01:58:22 +03:00
|
|
|
#include "soundmod.h"
|
2004-09-05 14:30:19 +04:00
|
|
|
#include "soundlnx.h"
|
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/soundcard.h>
|
|
|
|
|
2011-02-11 01:58:22 +03:00
|
|
|
bx_sound_linux_c::bx_sound_linux_c(logfunctions *dev)
|
2011-03-19 15:57:13 +03:00
|
|
|
:bx_sound_lowlevel_c(dev)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2008-07-12 19:21:36 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
2008-07-19 16:01:54 +04:00
|
|
|
alsa_seq.handle = NULL;
|
|
|
|
alsa_pcm.handle = NULL;
|
2008-07-12 19:21:36 +04:00
|
|
|
alsa_buffer = NULL;
|
2008-07-13 19:37:19 +04:00
|
|
|
#endif
|
2008-07-19 16:01:54 +04:00
|
|
|
midi = NULL;
|
2001-04-10 05:04:59 +04:00
|
|
|
wavedevice = NULL;
|
|
|
|
wave = -1;
|
2011-01-26 02:29:08 +03:00
|
|
|
BX_INFO(("Sound output module 'linux' initialized"));
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bx_sound_linux_c::~bx_sound_linux_c()
|
|
|
|
{
|
|
|
|
// nothing for now
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int bx_sound_linux_c::waveready()
|
|
|
|
{
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_linux_c::midiready()
|
|
|
|
{
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2008-07-19 16:01:54 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
2011-02-13 20:26:52 +03:00
|
|
|
int bx_sound_linux_c::alsa_seq_open(const char *alsadev)
|
2008-07-19 16:01:54 +04:00
|
|
|
{
|
|
|
|
char *mididev, *ptr;
|
|
|
|
int client, port, ret = 0;
|
2011-01-26 02:29:08 +03:00
|
|
|
int length = strlen(alsadev) + 1;
|
2008-07-19 16:01:54 +04:00
|
|
|
|
|
|
|
mididev = new char[length];
|
|
|
|
|
|
|
|
if (mididev == NULL)
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-07-19 16:01:54 +04:00
|
|
|
|
2011-01-26 02:29:08 +03:00
|
|
|
strcpy(mididev, alsadev);
|
2008-07-19 16:01:54 +04:00
|
|
|
ptr = strtok(mididev, ":");
|
|
|
|
if (ptr == NULL) {
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_ERROR(("ALSA sequencer setup: missing client parameters"));
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-07-19 16:01:54 +04:00
|
|
|
}
|
|
|
|
client = atoi(ptr);
|
|
|
|
ptr = strtok(NULL, ":");
|
|
|
|
if (ptr == NULL) {
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_ERROR(("ALSA sequencer setup: missing port parameter"));
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-07-19 16:01:54 +04:00
|
|
|
}
|
|
|
|
port = atoi(ptr);
|
|
|
|
|
|
|
|
delete(mididev);
|
|
|
|
|
|
|
|
if (snd_seq_open(&alsa_seq.handle, "default", SND_SEQ_OPEN_OUTPUT, 0) < 0) {
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_ERROR(("Couldn't open ALSA sequencer for midi output"));
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-07-19 16:01:54 +04:00
|
|
|
}
|
|
|
|
ret = snd_seq_create_simple_port(alsa_seq.handle, NULL,
|
|
|
|
SND_SEQ_PORT_CAP_WRITE |
|
|
|
|
SND_SEQ_PORT_CAP_SUBS_WRITE |
|
|
|
|
SND_SEQ_PORT_CAP_READ,
|
|
|
|
SND_SEQ_PORT_TYPE_MIDI_GENERIC);
|
|
|
|
if (ret < 0) {
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_ERROR(("ALSA sequencer: error creating port %s", snd_strerror(errno)));
|
2008-07-19 16:01:54 +04:00
|
|
|
} else {
|
|
|
|
alsa_seq.source_port = ret;
|
|
|
|
ret = snd_seq_connect_to(alsa_seq.handle, alsa_seq.source_port, client, port);
|
|
|
|
if (ret < 0) {
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_ERROR(("ALSA sequencer: could not connect to port %d:%d", client, port));
|
2008-07-19 16:01:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
snd_seq_close(alsa_seq.handle);
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-07-19 16:01:54 +04:00
|
|
|
} else {
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2008-07-19 16:01:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-02-13 20:26:52 +03:00
|
|
|
int bx_sound_linux_c::openmidioutput(const char *mididev)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2011-01-26 02:29:08 +03:00
|
|
|
if ((mididev == NULL) || (strlen(mididev) < 1))
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-07-19 16:01:54 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
2011-01-26 02:29:08 +03:00
|
|
|
use_alsa_seq = !strncmp(mididev, "alsa:", 5);
|
2008-07-19 16:01:54 +04:00
|
|
|
if (use_alsa_seq) {
|
2011-01-26 02:29:08 +03:00
|
|
|
return alsa_seq_open(mididev+5);
|
2008-07-19 16:01:54 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-26 02:29:08 +03:00
|
|
|
midi = fopen(mididev,"w");
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-02-11 01:58:22 +03:00
|
|
|
if (midi == NULL) {
|
|
|
|
BX_ERROR(("Couldn't open midi output device %s: %s",
|
|
|
|
mididev, strerror(errno)));
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-19 16:01:54 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
|
|
|
int bx_sound_linux_c::alsa_seq_output(int delta, int command, int length, Bit8u data[])
|
|
|
|
{
|
|
|
|
int cmd, chan, value;
|
|
|
|
snd_seq_event_t ev;
|
|
|
|
|
|
|
|
snd_seq_ev_clear(&ev);
|
|
|
|
snd_seq_ev_set_source(&ev, alsa_seq.source_port);
|
|
|
|
snd_seq_ev_set_subs(&ev);
|
|
|
|
snd_seq_ev_set_direct(&ev);
|
|
|
|
cmd = command & 0xf0;
|
|
|
|
chan = command & 0x0f;
|
|
|
|
switch (cmd) {
|
|
|
|
case 0x80:
|
|
|
|
ev.type = SND_SEQ_EVENT_NOTEOFF;
|
|
|
|
ev.data.note.channel = chan;
|
|
|
|
ev.data.note.note = data[0];
|
|
|
|
ev.data.note.velocity = data[1];
|
|
|
|
ev.data.note.duration = delta;
|
|
|
|
break;
|
|
|
|
case 0x90:
|
|
|
|
ev.type = SND_SEQ_EVENT_NOTEON;
|
|
|
|
ev.data.note.channel = chan;
|
|
|
|
ev.data.note.note = data[0];
|
|
|
|
ev.data.note.velocity = data[1];
|
|
|
|
ev.data.note.duration = 0;
|
|
|
|
break;
|
|
|
|
case 0xa0:
|
|
|
|
ev.type = SND_SEQ_EVENT_KEYPRESS;
|
|
|
|
ev.data.control.channel = chan;
|
|
|
|
ev.data.control.param = data[0];
|
|
|
|
ev.data.control.value = data[1];
|
|
|
|
break;
|
|
|
|
case 0xb0:
|
|
|
|
ev.type = SND_SEQ_EVENT_CONTROLLER;
|
|
|
|
ev.data.control.channel = chan;
|
|
|
|
ev.data.control.param = data[0];
|
|
|
|
ev.data.control.value = data[1];
|
|
|
|
break;
|
|
|
|
case 0xc0:
|
|
|
|
ev.type = SND_SEQ_EVENT_PGMCHANGE;
|
|
|
|
ev.data.control.channel = chan;
|
|
|
|
ev.data.control.value = data[0];
|
|
|
|
break;
|
|
|
|
case 0xd0:
|
|
|
|
ev.type = SND_SEQ_EVENT_CHANPRESS;
|
|
|
|
ev.data.control.channel = chan;
|
|
|
|
ev.data.control.value = data[0];
|
|
|
|
break;
|
|
|
|
case 0xe0:
|
|
|
|
ev.type = SND_SEQ_EVENT_PITCHBEND;
|
|
|
|
ev.data.control.channel = chan;
|
|
|
|
value = data[0] | (data[1] << 7);
|
|
|
|
value -= 0x2000;
|
|
|
|
ev.data.control.value = value;
|
|
|
|
break;
|
|
|
|
case 0xf0:
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_ERROR(("alsa_seq_output(): SYSEX not implemented, length=%d", length));
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-07-19 16:01:54 +04:00
|
|
|
default:
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_ERROR(("alsa_seq_output(): unknown command 0x%02x, length=%d", command, length));
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-07-19 16:01:54 +04:00
|
|
|
}
|
|
|
|
snd_seq_event_output(alsa_seq.handle, &ev);
|
|
|
|
snd_seq_drain_output(alsa_seq.handle);
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2008-07-19 16:01:54 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
int bx_sound_linux_c::sendmidicommand(int delta, int command, int length, Bit8u data[])
|
|
|
|
{
|
2008-07-19 16:01:54 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
|
|
|
if ((use_alsa_seq) && (alsa_seq.handle != NULL)) {
|
|
|
|
return alsa_seq_output(delta, command, length, data);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
UNUSED(delta);
|
|
|
|
|
|
|
|
fputc(command, midi);
|
|
|
|
fwrite(data, 1, length, midi);
|
|
|
|
fflush(midi); // to start playing immediately
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int bx_sound_linux_c::closemidioutput()
|
|
|
|
{
|
2008-07-19 16:01:54 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
|
|
|
if ((use_alsa_seq) && (alsa_seq.handle != NULL)) {
|
|
|
|
snd_seq_close(alsa_seq.handle);
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2008-07-19 16:01:54 +04:00
|
|
|
}
|
|
|
|
#endif
|
2001-04-10 05:04:59 +04:00
|
|
|
fclose(midi);
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-13 20:26:52 +03:00
|
|
|
int bx_sound_linux_c::openwaveoutput(const char *wavedev)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2008-07-13 19:37:19 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
2011-01-26 02:29:08 +03:00
|
|
|
use_alsa_pcm = !strcmp(wavedev, "alsa");
|
2008-07-13 19:37:19 +04:00
|
|
|
if (use_alsa_pcm) {
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2008-07-13 19:37:19 +04:00
|
|
|
}
|
|
|
|
#endif
|
2011-01-26 02:29:08 +03:00
|
|
|
int length = strlen(wavedev) + 1;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
if (wavedevice != NULL)
|
|
|
|
delete(wavedevice);
|
|
|
|
|
|
|
|
wavedevice = new char[length];
|
|
|
|
|
|
|
|
if (wavedevice == NULL)
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-01-26 02:29:08 +03:00
|
|
|
strncpy(wavedevice, wavedev, length);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2008-07-12 19:21:36 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
2011-03-20 21:02:12 +03:00
|
|
|
int bx_sound_linux_c::alsa_pcm_open(int frequency, int bits, bx_bool stereo, int format)
|
2008-07-12 19:21:36 +04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
snd_pcm_format_t fmt;
|
|
|
|
snd_pcm_hw_params_t *params;
|
|
|
|
unsigned int size, freq;
|
|
|
|
int signeddata = format & 1;
|
|
|
|
|
|
|
|
audio_bufsize = 0;
|
|
|
|
|
2008-07-19 16:01:54 +04:00
|
|
|
if (alsa_pcm.handle == NULL) {
|
2011-02-19 11:48:53 +03:00
|
|
|
ret = snd_pcm_open(&alsa_pcm.handle, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
|
2008-07-12 19:21:36 +04:00
|
|
|
if (ret < 0) {
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-07-12 19:21:36 +04:00
|
|
|
}
|
2011-02-19 11:48:53 +03:00
|
|
|
BX_INFO(("ALSA: opened default PCM output device"));
|
2008-07-12 19:21:36 +04:00
|
|
|
}
|
|
|
|
snd_pcm_hw_params_alloca(¶ms);
|
2008-07-19 16:01:54 +04:00
|
|
|
snd_pcm_hw_params_any(alsa_pcm.handle, params);
|
|
|
|
snd_pcm_hw_params_set_access(alsa_pcm.handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
|
2008-07-12 19:21:36 +04:00
|
|
|
|
|
|
|
if ((frequency == oldfreq) &&
|
|
|
|
(bits == oldbits) &&
|
|
|
|
(stereo == oldstereo) &&
|
|
|
|
(format == oldformat))
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK; // nothing to do
|
2008-07-12 19:21:36 +04:00
|
|
|
|
|
|
|
oldfreq = frequency;
|
|
|
|
oldbits = bits;
|
|
|
|
oldstereo = stereo;
|
|
|
|
oldformat = format;
|
|
|
|
|
|
|
|
freq = (unsigned int)frequency;
|
|
|
|
|
|
|
|
if (bits == 16) {
|
|
|
|
if (signeddata == 1)
|
|
|
|
fmt = SND_PCM_FORMAT_S16_LE;
|
|
|
|
else
|
|
|
|
fmt = SND_PCM_FORMAT_U16_LE;
|
|
|
|
size = 2;
|
|
|
|
} else if (bits == 8) {
|
|
|
|
if (signeddata == 1)
|
|
|
|
fmt = SND_PCM_FORMAT_S8;
|
|
|
|
else
|
|
|
|
fmt = SND_PCM_FORMAT_U8;
|
|
|
|
size = 1;
|
|
|
|
} else
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-07-12 19:21:36 +04:00
|
|
|
|
|
|
|
if (stereo) size *= 2;
|
|
|
|
|
2008-07-19 16:01:54 +04:00
|
|
|
snd_pcm_hw_params_set_format(alsa_pcm.handle, params, fmt);
|
|
|
|
snd_pcm_hw_params_set_channels(alsa_pcm.handle, params, (stereo != 0) ? 2 : 1);
|
|
|
|
snd_pcm_hw_params_set_rate_near(alsa_pcm.handle, params, &freq, &dir);
|
2008-07-12 19:21:36 +04:00
|
|
|
|
2008-07-19 16:01:54 +04:00
|
|
|
alsa_pcm.frames = 32;
|
|
|
|
snd_pcm_hw_params_set_period_size_near(alsa_pcm.handle, params, &alsa_pcm.frames, &dir);
|
2008-07-12 19:21:36 +04:00
|
|
|
|
2008-07-19 16:01:54 +04:00
|
|
|
ret = snd_pcm_hw_params(alsa_pcm.handle, params);
|
2008-07-12 19:21:36 +04:00
|
|
|
if (ret < 0) {
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-07-12 19:21:36 +04:00
|
|
|
}
|
2008-07-19 16:01:54 +04:00
|
|
|
snd_pcm_hw_params_get_period_size(params, &alsa_pcm.frames, &dir);
|
|
|
|
alsa_bufsize = alsa_pcm.frames * size;
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("ALSA: buffer size set to %d", alsa_bufsize));
|
2008-07-12 19:21:36 +04:00
|
|
|
if (alsa_buffer != NULL) {
|
|
|
|
free(alsa_buffer);
|
|
|
|
alsa_buffer = NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2008-07-12 19:21:36 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
int bx_sound_linux_c::startwaveplayback(int frequency, int bits, bx_bool stereo, int format)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
int fmt, ret;
|
|
|
|
int signeddata = format & 1;
|
|
|
|
|
2008-07-12 19:21:36 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
2008-07-13 19:37:19 +04:00
|
|
|
if (use_alsa_pcm) {
|
|
|
|
return alsa_pcm_open(frequency, bits, stereo, format);
|
|
|
|
}
|
|
|
|
#endif
|
2008-02-16 01:05:43 +03:00
|
|
|
if ((wavedevice == NULL) || (strlen(wavedevice) < 1))
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-07-12 19:21:36 +04:00
|
|
|
if (wave == -1) {
|
2001-04-10 05:04:59 +04:00
|
|
|
wave = open(wavedevice, O_WRONLY);
|
2008-07-12 19:21:36 +04:00
|
|
|
if (wave == -1) {
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-07-12 19:21:36 +04:00
|
|
|
} else {
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_INFO(("OSS: opened output device %s", wavedevice));
|
2008-07-12 19:21:36 +04:00
|
|
|
}
|
|
|
|
} else {
|
2008-02-16 01:05:43 +03:00
|
|
|
if ((frequency == oldfreq) &&
|
|
|
|
(bits == oldbits) &&
|
|
|
|
(stereo == oldstereo) &&
|
|
|
|
(format == oldformat))
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK; // nothing to do
|
2008-07-12 19:21:36 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
oldfreq = frequency;
|
|
|
|
oldbits = bits;
|
|
|
|
oldstereo = stereo;
|
|
|
|
oldformat = format;
|
|
|
|
|
|
|
|
if (bits == 16)
|
|
|
|
if (signeddata == 1)
|
|
|
|
fmt = AFMT_S16_LE;
|
|
|
|
else
|
|
|
|
fmt = AFMT_U16_LE;
|
|
|
|
else if (bits == 8)
|
|
|
|
if (signeddata == 1)
|
|
|
|
fmt = AFMT_S8;
|
|
|
|
else
|
|
|
|
fmt = AFMT_U8;
|
|
|
|
else
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
// set frequency etc.
|
|
|
|
ret = ioctl(wave, SNDCTL_DSP_RESET);
|
|
|
|
if (ret != 0)
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("ioctl(SNDCTL_DSP_RESET): %s", strerror(errno)));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
ret = ioctl(wave, SNDCTL_DSP_SETFRAGMENT, &fragment);
|
|
|
|
if (ret != 0)
|
2008-02-16 01:05:43 +03:00
|
|
|
WRITELOG(WAVELOG(4), "ioctl(SNDCTL_DSP_SETFRAGMENT, %d): %s",
|
|
|
|
fragment, strerror(errno));
|
2001-04-10 05:04:59 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = ioctl(wave, SNDCTL_DSP_SETFMT, &fmt);
|
|
|
|
if (ret != 0) // abort if the format is unknown, to avoid playing noise
|
2008-02-16 01:05:43 +03:00
|
|
|
{
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("ioctl(SNDCTL_DSP_SETFMT, %d): %s",
|
|
|
|
fmt, strerror(errno)));
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
ret = ioctl(wave, SNDCTL_DSP_STEREO, &stereo);
|
|
|
|
if (ret != 0)
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("ioctl(SNDCTL_DSP_STEREO, %d): %s",
|
|
|
|
stereo, strerror(errno)));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
ret = ioctl(wave, SNDCTL_DSP_SPEED, &frequency);
|
|
|
|
if (ret != 0)
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_DEBUG(("ioctl(SNDCTL_DSP_SPEED, %d): %s",
|
|
|
|
frequency, strerror(errno)));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
// ioctl(wave, SNDCTL_DSP_GETBLKSIZE, &fragment);
|
2011-02-19 11:48:53 +03:00
|
|
|
// BX_DEBUG(("current output block size is %d", fragment));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2008-07-12 19:21:36 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
|
|
|
int bx_sound_linux_c::alsa_pcm_write()
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (alsa_buffer == NULL) {
|
|
|
|
alsa_buffer = (char *)malloc(alsa_bufsize);
|
|
|
|
}
|
|
|
|
while (audio_bufsize >= alsa_bufsize) {
|
|
|
|
memcpy(alsa_buffer, audio_buffer, alsa_bufsize);
|
2008-07-19 16:01:54 +04:00
|
|
|
ret = snd_pcm_writei(alsa_pcm.handle, alsa_buffer, alsa_pcm.frames);
|
2011-02-19 13:25:18 +03:00
|
|
|
if (ret == -EAGAIN)
|
|
|
|
continue;
|
2008-07-12 19:21:36 +04:00
|
|
|
if (ret == -EPIPE) {
|
|
|
|
/* EPIPE means underrun */
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_ERROR(("ALSA: underrun occurred"));
|
2008-07-19 16:01:54 +04:00
|
|
|
snd_pcm_prepare(alsa_pcm.handle);
|
2008-07-12 19:21:36 +04:00
|
|
|
} else if (ret < 0) {
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_ERROR(("ALSA: error from writei: %s", snd_strerror(ret)));
|
2008-07-19 16:01:54 +04:00
|
|
|
} else if (ret != (int)alsa_pcm.frames) {
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_ERROR(("ALSA: short write, write %d frames", ret));
|
2008-07-12 19:21:36 +04:00
|
|
|
}
|
|
|
|
audio_bufsize -= alsa_bufsize;
|
|
|
|
memcpy(audio_buffer, audio_buffer+alsa_bufsize, audio_bufsize);
|
|
|
|
}
|
|
|
|
if ((audio_bufsize == 0) && (alsa_buffer != NULL)) {
|
|
|
|
free(alsa_buffer);
|
|
|
|
alsa_buffer = NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2008-07-12 19:21:36 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
int bx_sound_linux_c::sendwavepacket(int length, Bit8u data[])
|
|
|
|
{
|
2008-07-12 19:21:36 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
2008-07-13 19:37:19 +04:00
|
|
|
if (use_alsa_pcm) {
|
|
|
|
if ((audio_bufsize+length) <= BX_SOUND_LINUX_BUFSIZE) {
|
|
|
|
memcpy(audio_buffer+audio_bufsize, data, length);
|
|
|
|
audio_bufsize += length;
|
|
|
|
} else {
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_ERROR(("ALSA: audio buffer overflow"));
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-07-13 19:37:19 +04:00
|
|
|
}
|
|
|
|
if (audio_bufsize < alsa_bufsize) {
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2008-07-13 19:37:19 +04:00
|
|
|
} else {
|
|
|
|
return alsa_pcm_write();
|
|
|
|
}
|
2008-07-12 19:21:36 +04:00
|
|
|
}
|
2008-07-13 19:37:19 +04:00
|
|
|
#endif
|
2008-02-16 01:05:43 +03:00
|
|
|
int ret = write(wave, data, length);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-07-12 19:21:36 +04:00
|
|
|
if (ret == length) {
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2008-07-12 19:21:36 +04:00
|
|
|
} else {
|
2011-02-11 01:58:22 +03:00
|
|
|
BX_ERROR(("OSS: write error"));
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_ERR;
|
2008-07-12 19:21:36 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_linux_c::stopwaveplayback()
|
|
|
|
{
|
2008-07-12 19:21:36 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
2008-07-13 19:37:19 +04:00
|
|
|
if (use_alsa_pcm && (audio_bufsize > 0)) {
|
2008-07-12 19:21:36 +04:00
|
|
|
if (audio_bufsize < alsa_bufsize) {
|
|
|
|
memset(audio_buffer+audio_bufsize, 0, alsa_bufsize-audio_bufsize);
|
|
|
|
audio_bufsize = alsa_bufsize;
|
|
|
|
}
|
|
|
|
alsa_pcm_write();
|
|
|
|
}
|
2008-07-13 19:37:19 +04:00
|
|
|
#endif
|
2008-02-16 01:05:43 +03:00
|
|
|
// ioctl(wave, SNDCTL_DSP_SYNC);
|
|
|
|
// close(wave);
|
|
|
|
// wave = -1;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int bx_sound_linux_c::closewaveoutput()
|
|
|
|
{
|
2008-07-12 19:21:36 +04:00
|
|
|
#if BX_HAVE_ALSASOUND
|
2008-07-19 16:01:54 +04:00
|
|
|
if (use_alsa_pcm && (alsa_pcm.handle != NULL)) {
|
|
|
|
snd_pcm_drain(alsa_pcm.handle);
|
|
|
|
snd_pcm_close(alsa_pcm.handle);
|
|
|
|
alsa_pcm.handle = NULL;
|
2008-07-12 19:21:36 +04:00
|
|
|
}
|
2008-07-13 19:37:19 +04:00
|
|
|
#endif
|
2001-04-10 05:04:59 +04:00
|
|
|
if (wavedevice != NULL)
|
|
|
|
delete(wavedevice);
|
|
|
|
|
|
|
|
if (wave != -1)
|
2008-02-16 01:05:43 +03:00
|
|
|
{
|
2001-04-10 05:04:59 +04:00
|
|
|
close(wave);
|
|
|
|
wave = -1;
|
2008-02-16 01:05:43 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
wavedevice = NULL;
|
|
|
|
|
2011-03-20 21:02:12 +03:00
|
|
|
return BX_SOUNDLOW_OK;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2008-07-12 19:21:36 +04:00
|
|
|
#endif
|