diff --git a/src/audio/OpenAL/Context.cpp b/src/audio/OpenAL/Context.cpp index 189a2d717140e7287625af18b4bae93f34341591..dd33338afc39662d3b1327629bf64e9d75fff944 100644 --- a/src/audio/OpenAL/Context.cpp +++ b/src/audio/OpenAL/Context.cpp @@ -19,5 +19,11 @@ */ #include "Context.hpp" -#include "NullSource.hpp" +#include "Emitter.hpp" +SFLAudio::Source * +SFLAudio::Context::createSource(SFLAudio::Emitter *emitter) +{ + Source *source = createSource(emitter->getFormat(), emitter->getFrequency()); + emitter->connect(source); +} diff --git a/src/audio/OpenAL/Context.hpp b/src/audio/OpenAL/Context.hpp index be9d254cc3660b0621ed288308c12177d96abac2..7cf28e683b657b9a6f61d03ca5839de76e59d261 100644 --- a/src/audio/OpenAL/Context.hpp +++ b/src/audio/OpenAL/Context.hpp @@ -23,6 +23,7 @@ namespace SFLAudio { + class Emitter; class Source; class Context @@ -34,6 +35,7 @@ namespace SFLAudio * Create a source for the context. */ virtual Source *createSource(int format, int freq) = 0; + Source *createSource(Emitter *emitter); }; } diff --git a/src/audio/OpenAL/Emitter.cpp b/src/audio/OpenAL/Emitter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6fb1aa262f252694f01b2b27003fdab32710fb4a --- /dev/null +++ b/src/audio/OpenAL/Emitter.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2004-2005 Savoir-Faire Linux inc. + * Author: Jean-Philippe Barrette-LaPierre + * <jean-philippe.barrette-lapierre@savoirfairelinux.com> + * + * 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. + */ + +#include "Context.hpp" +#include "Emitter.hpp" + + +SFLAudio::Emitter::Emitter() + : mSource(0) + , mFormat(0) + , mFreq(0) +{} + + +SFLAudio::Emitter::Emitter(int format, int freq) + : mSource(0) + , mFormat(format) + , mFreq(freq) +{} + + +int +SFLAudio::Emitter::getFrequency() +{return mFreq;} + +int +SFLAudio::Emitter::getFormat() +{return mFormat;} + +void +SFLAudio::Emitter::setFrequency(int freq) +{mFreq = freq;} + +void +SFLAudio::Emitter::setFormat(int format) +{mFormat = format;} + +void +SFLAudio::Emitter::connect(Source *source) +{mSource = source;} + +void +SFLAudio::Emitter::connect(Context *context) +{mSource = context->createSource(this);} + +SFLAudio::Source * +SFLAudio::Emitter::getSource() +{return mSource;} + diff --git a/src/audio/OpenAL/Emitter.hpp b/src/audio/OpenAL/Emitter.hpp new file mode 100644 index 0000000000000000000000000000000000000000..f4b9b9acdf1a5ad6d6795dc310ca3f5ada2f9957 --- /dev/null +++ b/src/audio/OpenAL/Emitter.hpp @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2004-2005 Savoir-Faire Linux inc. + * Author: Jean-Philippe Barrette-LaPierre + * <jean-philippe.barrette-lapierre@savoirfairelinux.com> + * + * 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. + */ + +#ifndef __SFLAUDIO_EMITTER_HPP__ +#define __SFLAUDIO_EMITTER_HPP__ + +namespace SFLAudio +{ + class Context; + class Source; + + class Emitter + { + public: + Emitter(); + Emitter(int format, int freq); + + int getFrequency(); + int getFormat(); + + void setFrequency(int freq); + void setFormat(int format); + + void connect(Source *source); + void connect(Context *context); + Source *getSource(); + + private: + Source *mSource; + + int mFormat; + int mFreq; + }; + +} + +#endif + diff --git a/src/audio/OpenAL/Makefile.am b/src/audio/OpenAL/Makefile.am index 5871378bf17447109e07b491352cb4bb966a1a1b..45b4349f96bbad637cf7605903ede60d36ae0d14 100644 --- a/src/audio/OpenAL/Makefile.am +++ b/src/audio/OpenAL/Makefile.am @@ -1,12 +1,13 @@ if MAINTENER_CODE -noinst_PROGRAMS = example01 example02 example03 +noinst_PROGRAMS = example01 example02 example03 example04 noinst_LTLIBRARIES = libsflaudio.la libsflaudio_la_SOURCES = \ AudioLayer.cpp AudioLayer.hpp \ AudioManagerImpl.cpp AudioManagerImpl.hpp \ - Context.hpp \ + Context.cpp Context.hpp \ Device.cpp Device.hpp \ + Emitter.cpp Emitter.hpp \ NullLayer.cpp NullLayer.hpp \ NullDevice.cpp NullDevice.hpp \ NullContext.cpp NullContext.hpp \ @@ -17,7 +18,8 @@ libsflaudio_la_SOURCES = \ OpenALSource.cpp OpenALSource.hpp \ PortAudioLayer.cpp PortAudioLayer.hpp \ SFLAudio.hpp \ - Source.cpp Source.hpp + Source.cpp Source.hpp \ + WavEmitter.cpp WavEmitter.hpp AM_CPPFLAGS = $(PORTAUDIO_CFLAGS) -I$(top_srcdir)/libs/portaudio/pa_common -I$(top_srcdir)/libs/ AM_CXXFLAGS = $(PORTAUDIO_CXXFLAGS) -I$(top_srcdir)/libs/ @@ -30,5 +32,6 @@ LDADD = libsflaudio.la $(PORTAUDIO_LIBS) -lopenal -lportaudio example01_SOURCES = example01.cpp example02_SOURCES = example02.cpp example03_SOURCES = example03.cpp +example04_SOURCES = example04.cpp endif diff --git a/src/audio/OpenAL/OpenALDevice.cpp b/src/audio/OpenAL/OpenALDevice.cpp index 91ea6fbcf451c9c414e14f7905683e21350c42f9..ef6ff8612ae9b7f979be66a0c2c6a15c52ce4638 100644 --- a/src/audio/OpenAL/OpenALDevice.cpp +++ b/src/audio/OpenAL/OpenALDevice.cpp @@ -41,7 +41,10 @@ SFLAudio::OpenALDevice::~OpenALDevice() void SFLAudio::OpenALDevice::unload() { if(mDevice) { - alcCloseDevice(mDevice); + if(alcCloseDevice(mDevice) == ALC_FALSE) { + ALenum error = alcGetError(mDevice); + std::cerr << "OpenAL::alcCloseDevice: " << alGetString(error) << std::endl; + } mDevice = 0; } } diff --git a/src/audio/OpenAL/OpenALLayer.cpp b/src/audio/OpenAL/OpenALLayer.cpp index bc530a2af49184f4d09b962bc0cd1e9aadf6bf70..b6bbfdc30a458701f8aac86a23b5206fe66d3205 100644 --- a/src/audio/OpenAL/OpenALLayer.cpp +++ b/src/audio/OpenAL/OpenALLayer.cpp @@ -27,6 +27,7 @@ #include <AL/alc.h> #define DEFAULT_DEVICE_NAME "default" +#define DEFAULT_CAPTURE_DEVICE_NAME "default" SFLAudio::OpenALLayer::OpenALLayer() : AudioLayer("openal") @@ -53,6 +54,28 @@ SFLAudio::OpenALLayer::getDevicesNames() } +std::list< std::string > +SFLAudio::OpenALLayer::getCaptureDevicesNames() +{ + std::list< std::string > devices; + if (alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT") == AL_TRUE) { + const ALCchar *devs = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER); + const ALCchar *devname = devs; + while(devname) { + devices.push_back(devname); + devname += sizeof(ALCchar) * (strlen(devname) + 1); + } + } + else { + devices.push_back(DEFAULT_CAPTURE_DEVICE_NAME); + } + + + return devices; + +} + + SFLAudio::Device * SFLAudio::OpenALLayer::openDevice() { diff --git a/src/audio/OpenAL/OpenALLayer.hpp b/src/audio/OpenAL/OpenALLayer.hpp index 1a04b027f08c3060151dbcfd4edb7e9a0c8a6114..de35ab4e7c7adb538c63da4593b125c9ea08e7a7 100644 --- a/src/audio/OpenAL/OpenALLayer.hpp +++ b/src/audio/OpenAL/OpenALLayer.hpp @@ -31,6 +31,7 @@ namespace SFLAudio OpenALLayer(); virtual std::list< std::string > getDevicesNames(); + virtual std::list< std::string > getCaptureDevicesNames(); virtual Device *openDevice(); virtual Device *openDevice(const std::string &name); }; diff --git a/src/audio/OpenAL/WavEmitter.cpp b/src/audio/OpenAL/WavEmitter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9bd47b34da1021b34ab11763e2b1e5c18fb7bb76 --- /dev/null +++ b/src/audio/OpenAL/WavEmitter.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2004-2005 Savoir-Faire Linux inc. + * Author: Jean-Philippe Barrette-LaPierre + * <jean-philippe.barrette-lapierre@savoirfairelinux.com> + * + * 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. + */ + +#include <AL/alut.h> +#include <iostream> + +#include "WavEmitter.hpp" +#include "Source.hpp" + +SFLAudio::WavEmitter::WavEmitter(char *filename) + : mData(0) + , mSize(0) +{ + ALenum format; + ALsizei freq; + ALboolean loop; + + // Load test.wav + alutLoadWAVFile(filename, &format,&mData,&mSize,&freq,&loop); + ALenum error = alGetError(); + if (error != AL_NO_ERROR) { + std::cerr << "OpenAL: loadWAVFile : " << alGetString(error); + mData = 0; + } + else { + setFrequency(freq); + setFormat(format); + } +} + +void +SFLAudio::WavEmitter::play() +{ + Source *source = getSource(); + if(source && mData) { + source->play(mData, mSize); + + // Unload test.wav + alutUnloadWAV(getFormat(), mData, mSize, getFrequency()); + ALenum error = alGetError(); + if (error != AL_NO_ERROR) { + std::cerr << "OpenAL: unloadWAV : " << alGetString(error); + } + } +} diff --git a/src/audio/OpenAL/WavEmitter.hpp b/src/audio/OpenAL/WavEmitter.hpp new file mode 100644 index 0000000000000000000000000000000000000000..34af517b0eeee5aed5046d9edf1e907bc51326e2 --- /dev/null +++ b/src/audio/OpenAL/WavEmitter.hpp @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2004-2005 Savoir-Faire Linux inc. + * Author: Jean-Philippe Barrette-LaPierre + * <jean-philippe.barrette-lapierre@savoirfairelinux.com> + * + * 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. + */ + +#ifndef __SFLAUDIO_WAV_EMITTER_HPP__ +#define __SFLAUDIO_WAV_EMITTER_HPP__ + +#include <AL/al.h> +#include "Emitter.hpp" + +namespace SFLAudio +{ + class Source; + + class WavEmitter : public Emitter + { + private: + WavEmitter(); + + public: + WavEmitter(char *filename); + void play(); + + private: + ALvoid *mData; + ALsizei mSize; + }; +} + +#endif diff --git a/src/audio/OpenAL/example04.cpp b/src/audio/OpenAL/example04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85548ad44c197e7d9c84e6b0ef01a93289086d48 --- /dev/null +++ b/src/audio/OpenAL/example04.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2004-2005 Savoir-Faire Linux inc. + * Author: Jean-Philippe Barrette-LaPierre + * <jean-philippe.barrette-lapierre@savoirfairelinux.com> + * + * 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. + */ + +#include <iostream> +#include <list> +#include <string> + +#include "SFLAudio.hpp" +#include "WavEmitter.hpp" + +using namespace SFLAudio; + +int main(int, char* []) +{ + + AudioLayer *layer = SFLAudio::AudioManager::instance().currentLayer(); + Device *device = layer->openDevice(); + Context *context = device->createContext(); + + // Load test.wav + SFLAudio::WavEmitter wav("test.wav"); + wav.connect(context); + + wav.play(); + + // Wait for user input. + std::cout << "Press any key to quit the program." << std::endl; + std::cin.get(); +}