diff --git a/src/audio/OpenAL/Context.cpp b/src/audio/OpenAL/Context.cpp index 47e03d498b493f50b5114080454482aba4c02c10..189a2d717140e7287625af18b4bae93f34341591 100644 --- a/src/audio/OpenAL/Context.cpp +++ b/src/audio/OpenAL/Context.cpp @@ -21,8 +21,3 @@ #include "Context.hpp" #include "NullSource.hpp" -SFLAudio::Source * -SFLAudio::Context::createSource() -{ - return new NullSource(); -} diff --git a/src/audio/OpenAL/Context.hpp b/src/audio/OpenAL/Context.hpp index e22378e05a1911460e923e0e62df65d48598161e..be9d254cc3660b0621ed288308c12177d96abac2 100644 --- a/src/audio/OpenAL/Context.hpp +++ b/src/audio/OpenAL/Context.hpp @@ -28,10 +28,12 @@ namespace SFLAudio class Context { public: + virtual bool isNull() {return false;} + /** * Create a source for the context. */ - Source *createSource(); + virtual Source *createSource(int format, int freq) = 0; }; } diff --git a/src/audio/OpenAL/Makefile.am b/src/audio/OpenAL/Makefile.am index cdafe1c688a3b796c18f61dff49a75bfa9cbafda..1b04bc25bbefcf4749134061de95d608c1b35639 100644 --- a/src/audio/OpenAL/Makefile.am +++ b/src/audio/OpenAL/Makefile.am @@ -5,17 +5,19 @@ noinst_LTLIBRARIES = libsflaudio.la libsflaudio_la_SOURCES = \ AudioLayer.cpp AudioLayer.hpp \ AudioManagerImpl.cpp AudioManagerImpl.hpp \ - Context.cpp Context.hpp \ + Context.hpp \ Device.cpp Device.hpp \ NullLayer.cpp NullLayer.hpp \ NullDevice.cpp NullDevice.hpp \ NullContext.cpp NullContext.hpp \ - NullSource.hpp \ + NullSource.cpp NullSource.hpp \ OpenALLayer.cpp OpenALLayer.hpp \ OpenALDevice.cpp OpenALDevice.hpp \ + OpenALContext.cpp OpenALContext.hpp \ + OpenALSource.cpp OpenALSource.hpp \ PortAudioLayer.cpp PortAudioLayer.hpp \ SFLAudio.hpp \ - Source.hpp + Source.cpp Source.hpp AM_CPPFLAGS = $(PORTAUDIO_CFLAGS) -I$(top_srcdir)/libs/portaudio/pa_common -I$(top_srcdir)/libs/ AM_CXXFLAGS = $(PORTAUDIO_CXXFLAGS) -I$(top_srcdir)/libs/ diff --git a/src/audio/OpenAL/NullContext.cpp b/src/audio/OpenAL/NullContext.cpp index d83499a3a335c8f7c356346da3de9700fc609561..c83df7887ce2eb974860b536736fb3eba56fb5d8 100644 --- a/src/audio/OpenAL/NullContext.cpp +++ b/src/audio/OpenAL/NullContext.cpp @@ -22,7 +22,7 @@ #include "NullSource.hpp" SFLAudio::Source * -SFLAudio::NullContext::createSource() +SFLAudio::NullContext::createSource(int, int) { return new NullSource(); } diff --git a/src/audio/OpenAL/NullContext.hpp b/src/audio/OpenAL/NullContext.hpp index 4588ff5f17d323f80b06a49ab2677070d7e76adf..a8393105792e3cd888270054bd7237754e63bdf2 100644 --- a/src/audio/OpenAL/NullContext.hpp +++ b/src/audio/OpenAL/NullContext.hpp @@ -28,7 +28,8 @@ namespace SFLAudio class NullContext : public Context { public: - Source *createSource(); + virtual bool isNull() {return true;} + Source *createSource(int format, int freq); }; } diff --git a/src/audio/OpenAL/NullSource.hpp b/src/audio/OpenAL/NullSource.hpp index 85c07f380ce22a64b81742a58ebf1ab9e6c2ab7e..d5f51efea1b72ff2f1b5f7f9eb5f96a9dd7d8516 100644 --- a/src/audio/OpenAL/NullSource.hpp +++ b/src/audio/OpenAL/NullSource.hpp @@ -27,6 +27,12 @@ namespace SFLAudio { class NullSource : public Source { + public: + NullSource(); + + virtual bool isNull() {return true;}; + virtual bool isPlaying(); + virtual void play(void *data, int size); }; } diff --git a/src/audio/OpenAL/OpenALContext.cpp b/src/audio/OpenAL/OpenALContext.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6cf92b65bba91db7732567081382196a97221fe --- /dev/null +++ b/src/audio/OpenAL/OpenALContext.cpp @@ -0,0 +1,43 @@ +/* + * 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 "OpenALContext.hpp" +#include "OpenALSource.hpp" +#include "NullSource.hpp" + +SFLAudio::OpenALContext::OpenALContext(ALCcontext *context) + : mContext(context) +{} + +SFLAudio::Source * +SFLAudio::OpenALContext::createSource(int format, int freq) +{ + Source *source = 0; + if(mContext == 0) { + source = new NullSource(); + } + else { + source = OpenALSource::create(this, format, freq); + } + + return source; +} diff --git a/src/audio/OpenAL/OpenALContext.hpp b/src/audio/OpenAL/OpenALContext.hpp new file mode 100644 index 0000000000000000000000000000000000000000..d287bccaf8814818676aa274f2735edab04f3a46 --- /dev/null +++ b/src/audio/OpenAL/OpenALContext.hpp @@ -0,0 +1,43 @@ +/* + * 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_OPENAL_CONTEXT_HPP__ +#define __SFLAUDIO_OPENAL_CONTEXT_HPP__ + +#include "Context.hpp" +#include "AL/alc.h" + +namespace SFLAudio +{ + class OpenALContext : public Context + { + private: + OpenALContext(); + + public: + OpenALContext(ALCcontext *context); + Source *createSource(int format, int freq); + + private: + ALCcontext *mContext; + }; +} + +#endif diff --git a/src/audio/OpenAL/OpenALDevice.cpp b/src/audio/OpenAL/OpenALDevice.cpp index 87d6234f0e2de7177c204acd9c2b2993687020d3..91ea6fbcf451c9c414e14f7905683e21350c42f9 100644 --- a/src/audio/OpenAL/OpenALDevice.cpp +++ b/src/audio/OpenAL/OpenALDevice.cpp @@ -22,7 +22,9 @@ #include <AL/al.h> #include <AL/alc.h> + #include "OpenALDevice.hpp" +#include "OpenALContext.hpp" #include "OpenALLayer.hpp" #include "NullContext.hpp" #include "NullDevice.hpp" @@ -66,5 +68,25 @@ SFLAudio::OpenALDevice::load() { SFLAudio::Context * SFLAudio::OpenALDevice::createContext() { - return new NullContext(); + SFLAudio::Context *context = NULL; + if (mDevice) { + ALCcontext *c = alcCreateContext(mDevice, NULL); + alcMakeContextCurrent(c); + if(c == NULL) { + ALenum error = alcGetError(mDevice); + if (error != AL_NO_ERROR) { + std::cerr << "OpenAL::alcCreateContext: " << alGetString(error) << std::endl; + } + context = new NullContext(); + } + else { + alcMakeContextCurrent(c); + context = new OpenALContext(c); + } + } + else { + context = new NullContext(); + } + + return context; } diff --git a/src/audio/OpenAL/OpenALSource.cpp b/src/audio/OpenAL/OpenALSource.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff91892bda5a83595c03201acfbccca9cb835f13 --- /dev/null +++ b/src/audio/OpenAL/OpenALSource.cpp @@ -0,0 +1,181 @@ +/* + * 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/al.h> +#include <AL/alc.h> +#include <AL/alut.h> +#include <iostream> +#include <unistd.h> + +#include "Context.hpp" +#include "NullSource.hpp" +#include "OpenALSource.hpp" + +SFLAudio::OpenALSource::OpenALSource(int format, int freq, ALuint buffer, ALuint source) + : Source(format, freq) + , mBuffer(buffer) + , mSource(source) +{} + +SFLAudio::OpenALSource::~OpenALSource() +{ + alGetError(); + + alDeleteSources(1, &mSource); + ALenum error = alGetError(); + if(error != AL_NO_ERROR) { + std::cerr << "OpenAL: alDeleteSources : " << alGetString(error) << std::endl; + } + + alDeleteBuffers(1, &mBuffer); + error = alGetError(); + if(error != AL_NO_ERROR) { + std::cerr << "OpenAL: alDeleteBuffers : " << alGetString(error) << std::endl; + } +} + + +bool +SFLAudio::OpenALSource::genBuffer(ALuint &buffer) { + // Generate buffer + alGetError(); // clear error code + alGenBuffers(1, &buffer); + ALenum error = alGetError(); + if (error != AL_NO_ERROR) { + std::cerr << "OpenAL: alGenBuffers : " << alGetString(error) << std::endl;; + return false; + } + + return true; +} + +bool +SFLAudio::OpenALSource::genSource(ALuint &source) { + // Generate buffer + alGetError(); // clear error code + alGenSources(1, &source); + ALenum error = alGetError(); + if (error != AL_NO_ERROR) { + std::cerr << "OpenAL: alGenSources : " << alGetString(error) << std::endl;; + return false; + } + + alSource3f(source, AL_POSITION, 0.0, 0.0, 0.0); + alSource3f(source, AL_VELOCITY, 0.0, 0.0, 0.0); + alSource3f(source, AL_DIRECTION, 0.0, 0.0, 0.0); + alSourcef (source, AL_ROLLOFF_FACTOR, 0.0); + alSourcei (source, AL_SOURCE_RELATIVE, AL_TRUE); + + return true; +} + +bool +SFLAudio::OpenALSource::deleteSource(ALuint &source) { + // Generate buffer + alGetError(); // clear error code + alDeleteSources(1, &source); + ALenum error = alGetError(); + if (error != AL_NO_ERROR) { + std::cerr << "OpenAL: alDeleteSources : " << alGetString(error) << std::endl;; + return false; + } + + return true; +} + +bool +SFLAudio::OpenALSource::deleteBuffer(ALuint &buffer) { + // Generate buffer + alGetError(); // clear error code + alDeleteBuffers(1, &buffer); + ALenum error = alGetError(); + if (error != AL_NO_ERROR) { + std::cerr << "OpenAL: alDeleteBuffers : " << alGetString(error) << std::endl;; + return false; + } + + return true; +} + +bool +SFLAudio::OpenALSource::attach(ALuint source, ALuint buffer) { + // Attach buffer to source + alSourcei(source, AL_BUFFER, buffer); + ALenum error = alGetError(); + if(error != AL_NO_ERROR) { + std::cerr << "OpenAL: alSourcei : " << alGetString(error); + return false; + } + + return true; +} + +SFLAudio::Source * +SFLAudio::OpenALSource::create(OpenALContext *, int format, int freq) { + ALuint buffer; + ALuint source; + + // Generate buffer + if(!genBuffer(buffer)){ + deleteBuffer(buffer); + return new NullSource(); + } + + // Generate source + if(!genSource(source)){ + deleteBuffer(buffer); + deleteSource(source); + return new NullSource(); + } + + // Attach buffer to source + if(!attach(source, buffer)) { + deleteBuffer(buffer); + deleteSource(source); + return new NullSource(); + } + + return new OpenALSource(format, freq, buffer, source); +} + + +bool +SFLAudio::OpenALSource::isPlaying() +{ + ALenum state; + alGetSourcei(mSource, AL_SOURCE_STATE, &state); + + return (state == AL_PLAYING); +} + +void +SFLAudio::OpenALSource::play(void *data, int size) +{ + ALboolean loop; + + // Copy test.wav data into AL Buffer 0 + alBufferData(mBuffer, getFormat(), data, size, getFrequency()); + ALenum error = alGetError(); + if (error != AL_NO_ERROR) { + std::cerr << "OpenAL: alBufferData : " << alGetString(error); + } + + alSourcePlay(mSource); +} diff --git a/src/audio/OpenAL/OpenALSource.hpp b/src/audio/OpenAL/OpenALSource.hpp new file mode 100644 index 0000000000000000000000000000000000000000..051f95492f3813f4941088dcd22831d3cbdaef43 --- /dev/null +++ b/src/audio/OpenAL/OpenALSource.hpp @@ -0,0 +1,63 @@ +/* + * 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_OPENAL_SOURCE_HPP__ +#define __SFLAUDIO_OPENAL_SOURCE_HPP__ + +#include <AL/alc.h> +#include "Source.hpp" + +namespace SFLAudio +{ + class OpenALContext; + + class OpenALSource : public Source + { + private: + OpenALSource(); + + public: + OpenALSource(int format, int freq, ALuint buffer, ALuint source); + ~OpenALSource(); + + static Source *create(OpenALContext *context, int format, int freq); + + // Source functions + virtual bool isPlaying(); + virtual void play(void *data, int size); + + private: + static bool genBuffer(ALuint &buffer); + static bool genSource(ALuint &source); + static bool deleteBuffer(ALuint &buffer); + static bool deleteSource(ALuint &source); + static bool attach(ALuint source, ALuint buffer); + + + private: + // Buffers to hold sound data. + ALuint mBuffer; + + // Sources are points of emitting sound. + ALuint mSource; + }; +} + +#endif diff --git a/src/audio/OpenAL/Source.cpp b/src/audio/OpenAL/Source.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd2e40003d64b7b9c3a69bf1e41b445307526ef7 --- /dev/null +++ b/src/audio/OpenAL/Source.cpp @@ -0,0 +1,34 @@ +/* + * 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/al.h> +#include <AL/alc.h> +#include <AL/alut.h> +#include <iostream> +#include <unistd.h> + +#include "Context.hpp" +#include "NullSource.hpp" +#include "OpenALSource.hpp" + +SFLAudio::Source::Source(int format, int freq) + : mFormat(format) + , mFreq(freq) +{} diff --git a/src/audio/OpenAL/Source.hpp b/src/audio/OpenAL/Source.hpp index 86ae4b5fd99e2e34c3c371292fca94dceef93b20..234167b69a35eee1271ed753cdceb35740abcff2 100644 --- a/src/audio/OpenAL/Source.hpp +++ b/src/audio/OpenAL/Source.hpp @@ -25,6 +25,22 @@ namespace SFLAudio { class Source { + private: + Source(); + + public: + Source(int format, int freq); + + virtual bool isNull() {return false;} + virtual bool isPlaying() = 0; + virtual void play(void *data, int size) = 0; + + int getFrequency() {return mFreq;} + int getFormat() {return mFormat;} + + private: + int mFormat; + int mFreq; }; } diff --git a/src/audio/OpenAL/example02.cpp b/src/audio/OpenAL/example02.cpp index 49de87c6ae16c51c68294bf631d6805a3c684322..00ca61a6f9232e072ea67ad09b988bff540fc136 100644 --- a/src/audio/OpenAL/example02.cpp +++ b/src/audio/OpenAL/example02.cpp @@ -22,23 +22,50 @@ #include <list> #include <string> +#include <AL/al.h> +#include <AL/alc.h> +#include <AL/alut.h> + #include "SFLAudio.hpp" using namespace SFLAudio; int main(int, char* []) { - std::list< SFLAudio::AudioLayer * > layers = - SFLAudio::AudioManager::instance().getLayers(); - for(std::list< SFLAudio::AudioLayer *>::iterator it = layers.begin(); - it != layers.end(); - it++) { - std::cout << "Layer: " << (*it)->getName() << std::endl; - - AudioLayer *layer = *it; - Device *device = layer->openDevice(); - std::cout << " Device: " << device->getName() << std::endl; - Context *context = device->createContext(); - Source *source = context->createSource(); + ALenum format; + ALvoid *data; + ALsizei size; + ALsizei freq; + ALboolean loop; + + AudioLayer *layer = SFLAudio::AudioManager::instance().currentLayer(); + std::cout << "Layer: " << layer->getName() << std::endl; + + Device *device = layer->openDevice(); + std::cout << " Device: " << device->getName() << std::endl; + Context *context = device->createContext(); + std::cout << " Context is null: " << (context->isNull() ? "true" : "false") << std::endl; + + // Load test.wav + alutLoadWAVFile("test.wav",&format,&data,&size,&freq,&loop); + ALenum error = alGetError(); + if (error != AL_NO_ERROR) { + std::cerr << "OpenAL: loadWAVFile : " << alGetString(error); + return 1; } + + Source *source = context->createSource(format, freq); + std::cout << " Source is null: " << (source->isNull() ? "true" : "false") << std::endl; + source->play(data, size); + + std::cout << "Unloading test.wav" << std::endl; + // Unload test.wav + alutUnloadWAV(format, data, size, freq); + error = alGetError(); + if (error != AL_NO_ERROR) { + std::cerr << "OpenAL: unloadWAV : " << alGetString(error); + } + + + std::cin.get(); } diff --git a/src/gui/qt/SFLPhoneApp.cpp b/src/gui/qt/SFLPhoneApp.cpp index f5ad6e3176549eed4f6a5109edf144eeba31c1d9..4d34156f6afd9970104148b65d1a71cebdc59a76 100644 --- a/src/gui/qt/SFLPhoneApp.cpp +++ b/src/gui/qt/SFLPhoneApp.cpp @@ -93,9 +93,9 @@ SFLPhoneApp::handleArg() void SFLPhoneApp::launch() { -// if(mLauncher) { -// mLauncher->start(); -// } + if(mLauncher) { + mLauncher->start(); + } } void