From ddf592f8057f7c08f6158598e09f3b72b1642db3 Mon Sep 17 00:00:00 2001 From: Stefan Seyfried Date: Sat, 5 Feb 2011 16:02:34 +0100 Subject: [PATCH] libtriple: implement PCM playback in cAudio() --- libtriple/audio_td.cpp | 57 ++++++++++++++++++++++++++++++++++++++---- libtriple/audio_td.h | 2 ++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/libtriple/audio_td.cpp b/libtriple/audio_td.cpp index 49cf07d..e3ce4ed 100644 --- a/libtriple/audio_td.cpp +++ b/libtriple/audio_td.cpp @@ -10,11 +10,14 @@ #include "audio_td.h" #include "lt_debug.h" +#include + cAudio * audioDecoder = NULL; cAudio::cAudio(void *, void *, void *) { fd = -1; + clipfd = -1; openDevice(); Muted = false; } @@ -40,6 +43,9 @@ void cAudio::closeDevice(void) if (fd >= 0) close(fd); fd = -1; + if (clipfd >= 0) + close(clipfd); + clipfd = -1; } int cAudio::do_mute(bool enable, bool remember) @@ -152,21 +158,62 @@ int cAudio::setChannel(int /*channel*/) return 0; }; -int cAudio::PrepareClipPlay(int /*uNoOfChannels*/, int /*uSampleRate*/, int /*uBitsPerSample*/, int /*bLittleEndian*/) +int cAudio::PrepareClipPlay(int ch, int srate, int bits, int little_endian) { - lt_debug("cAudio::%s\n", __FUNCTION__); + int fmt; + lt_debug("cAudio::%s ch %d srate %d bits %d le %d\n", __FUNCTION__, ch, srate, bits, little_endian); + if (clipfd >= 0) { + fprintf(stderr, "cAudio::%s: clipfd already opened (%d)\n", __FUNCTION__, clipfd); + return -1; + } + /* the dsp driver seems to work only on the second open(). really. */ + clipfd = open("/dev/sound/dsp", O_WRONLY); + close(clipfd); + clipfd = open("/dev/sound/dsp", O_WRONLY); + if (clipfd < 0) { + perror("cAudio::PrepareClipPlay open /dev/sound/dsp"); + return -1; + } + /* no idea if we ever get little_endian == 0 */ + if (little_endian) + fmt = AFMT_S16_BE; + else + fmt = AFMT_S16_LE; + if (ioctl(clipfd, SNDCTL_DSP_SETFMT, &fmt)) + perror("SNDCTL_DSP_SETFMT"); + if (ioctl(clipfd, SNDCTL_DSP_CHANNELS, &ch)) + perror("SNDCTL_DSP_CHANNELS"); + if (ioctl(clipfd, SNDCTL_DSP_SPEED, &srate)) + perror("SNDCTL_DSP_SPEED"); + if (ioctl(clipfd, SNDCTL_DSP_RESET)) + perror("SNDCTL_DSP_RESET"); + return 0; }; -int cAudio::WriteClip(unsigned char * /*buffer*/, int /*size*/) +int cAudio::WriteClip(unsigned char *buffer, int size) { - lt_debug("cAudio::%s\n", __FUNCTION__); - return 0; + int ret; + // lt_debug("cAudio::%s\n", __FUNCTION__); + if (clipfd <= 0) { + fprintf(stderr, "cAudio::%s: clipfd not yet opened\n", __FUNCTION__); + return -1; + } + ret = write(clipfd, buffer, size); + if (ret < 0) + fprintf(stderr, "cAudio::%s: write error (%m)\n", __FUNCTION__); + return ret; }; int cAudio::StopClip() { lt_debug("cAudio::%s\n", __FUNCTION__); + if (clipfd <= 0) { + fprintf(stderr, "cAudio::%s: clipfd not yet opened\n", __FUNCTION__); + return -1; + } + close(clipfd); + clipfd = -1; return 0; }; diff --git a/libtriple/audio_td.h b/libtriple/audio_td.h index 7178ecf..d59fb7c 100644 --- a/libtriple/audio_td.h +++ b/libtriple/audio_td.h @@ -36,6 +36,8 @@ class cAudio int fd; bool Muted; + int clipfd; /* for pcm playback */ + AUDIO_FORMAT StreamType; AUDIO_SYNC_MODE SyncMode; bool started;