From 0e576348ff143c1a6d2296e27f624f886e5eb86d Mon Sep 17 00:00:00 2001
From: jpbl <jpbl>
Date: Fri, 13 Jan 2006 16:47:37 +0000
Subject: [PATCH] added the Emitter functionnality

---
 src/audio/OpenAL/Context.cpp      |  8 +++-
 src/audio/OpenAL/Context.hpp      |  2 +
 src/audio/OpenAL/Emitter.cpp      | 66 +++++++++++++++++++++++++++++++
 src/audio/OpenAL/Emitter.hpp      | 55 ++++++++++++++++++++++++++
 src/audio/OpenAL/Makefile.am      |  9 +++--
 src/audio/OpenAL/OpenALDevice.cpp |  5 ++-
 src/audio/OpenAL/OpenALLayer.cpp  | 23 +++++++++++
 src/audio/OpenAL/OpenALLayer.hpp  |  1 +
 src/audio/OpenAL/WavEmitter.cpp   | 62 +++++++++++++++++++++++++++++
 src/audio/OpenAL/WavEmitter.hpp   | 46 +++++++++++++++++++++
 src/audio/OpenAL/example04.cpp    | 46 +++++++++++++++++++++
 11 files changed, 318 insertions(+), 5 deletions(-)
 create mode 100644 src/audio/OpenAL/Emitter.cpp
 create mode 100644 src/audio/OpenAL/Emitter.hpp
 create mode 100644 src/audio/OpenAL/WavEmitter.cpp
 create mode 100644 src/audio/OpenAL/WavEmitter.hpp
 create mode 100644 src/audio/OpenAL/example04.cpp

diff --git a/src/audio/OpenAL/Context.cpp b/src/audio/OpenAL/Context.cpp
index 189a2d7171..dd33338afc 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 be9d254cc3..7cf28e683b 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 0000000000..6fb1aa262f
--- /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 0000000000..f4b9b9acdf
--- /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 5871378bf1..45b4349f96 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 91ea6fbcf4..ef6ff8612a 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 bc530a2af4..b6bbfdc30a 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 1a04b027f0..de35ab4e7c 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 0000000000..9bd47b34da
--- /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 0000000000..34af517b0e
--- /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 0000000000..85548ad44c
--- /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();
+}
-- 
GitLab