From 99231e5593697803cc5d53a17a6e8f858d830b05 Mon Sep 17 00:00:00 2001
From: jpbl <jpbl>
Date: Thu, 12 Jan 2006 22:12:05 +0000
Subject: [PATCH] added example03.cpp

---
 src/audio/OpenAL/Makefile.am      |  3 +-
 src/audio/OpenAL/NullSource.hpp   |  1 +
 src/audio/OpenAL/OpenALSource.cpp |  8 +++
 src/audio/OpenAL/OpenALSource.hpp |  1 +
 src/audio/OpenAL/README.txt       |  4 +-
 src/audio/OpenAL/Source.hpp       |  1 +
 src/audio/OpenAL/example02.cpp    |  9 +---
 src/audio/OpenAL/example03.cpp    | 84 +++++++++++++++++++++++++++++++
 8 files changed, 102 insertions(+), 9 deletions(-)
 create mode 100644 src/audio/OpenAL/example03.cpp

diff --git a/src/audio/OpenAL/Makefile.am b/src/audio/OpenAL/Makefile.am
index 1b04bc25bb..5871378bf1 100644
--- a/src/audio/OpenAL/Makefile.am
+++ b/src/audio/OpenAL/Makefile.am
@@ -1,5 +1,5 @@
 if MAINTENER_CODE
-noinst_PROGRAMS = example01 example02
+noinst_PROGRAMS = example01 example02 example03
 noinst_LTLIBRARIES = libsflaudio.la
 
 libsflaudio_la_SOURCES = \
@@ -29,5 +29,6 @@ LDADD = libsflaudio.la $(PORTAUDIO_LIBS) -lopenal -lportaudio
 
 example01_SOURCES = example01.cpp
 example02_SOURCES = example02.cpp
+example03_SOURCES = example03.cpp
 endif
 
diff --git a/src/audio/OpenAL/NullSource.hpp b/src/audio/OpenAL/NullSource.hpp
index d5f51efea1..4b0c24f3b8 100644
--- a/src/audio/OpenAL/NullSource.hpp
+++ b/src/audio/OpenAL/NullSource.hpp
@@ -33,6 +33,7 @@ namespace SFLAudio
     virtual bool isNull() {return true;};
     virtual bool isPlaying();
     virtual void play(void *data, int size);
+    virtual void stop();
   };
 }
 
diff --git a/src/audio/OpenAL/OpenALSource.cpp b/src/audio/OpenAL/OpenALSource.cpp
index ff91892bda..8f5a90a7d1 100644
--- a/src/audio/OpenAL/OpenALSource.cpp
+++ b/src/audio/OpenAL/OpenALSource.cpp
@@ -170,6 +170,8 @@ SFLAudio::OpenALSource::play(void *data, int size)
 {
   ALboolean loop;
 
+  alGetError();
+
   // Copy test.wav data into AL Buffer 0
   alBufferData(mBuffer, getFormat(), data, size, getFrequency());
   ALenum error = alGetError();
@@ -179,3 +181,9 @@ SFLAudio::OpenALSource::play(void *data, int size)
 
   alSourcePlay(mSource);
 }
+
+void
+SFLAudio::OpenALSource::stop()
+{
+  alSourceStop(mSource);
+}
diff --git a/src/audio/OpenAL/OpenALSource.hpp b/src/audio/OpenAL/OpenALSource.hpp
index 051f95492f..c3d30b6b5b 100644
--- a/src/audio/OpenAL/OpenALSource.hpp
+++ b/src/audio/OpenAL/OpenALSource.hpp
@@ -42,6 +42,7 @@ namespace SFLAudio
     // Source functions
     virtual bool isPlaying();
     virtual void play(void *data, int size);
+    virtual void stop();
 
   private:
     static bool genBuffer(ALuint &buffer);
diff --git a/src/audio/OpenAL/README.txt b/src/audio/OpenAL/README.txt
index 6ed8af5f60..fc595d4d2d 100644
--- a/src/audio/OpenAL/README.txt
+++ b/src/audio/OpenAL/README.txt
@@ -1 +1,3 @@
-example01.cpp: This is an enumeration of device example.
\ No newline at end of file
+example01.cpp: This is an enumeration of device example.
+example02.cpp: A simple WAV player.
+example03.cpp: A simple WAV player, with 2 files playing at the same time.
\ No newline at end of file
diff --git a/src/audio/OpenAL/Source.hpp b/src/audio/OpenAL/Source.hpp
index 234167b69a..862bce2e44 100644
--- a/src/audio/OpenAL/Source.hpp
+++ b/src/audio/OpenAL/Source.hpp
@@ -34,6 +34,7 @@ namespace SFLAudio
     virtual bool isNull() {return false;}
     virtual bool isPlaying() = 0;
     virtual void play(void *data, int size) = 0;
+    virtual void stop() = 0;
 
     int getFrequency() {return mFreq;}
     int getFormat() {return mFormat;}
diff --git a/src/audio/OpenAL/example02.cpp b/src/audio/OpenAL/example02.cpp
index 00ca61a6f9..785eda71cc 100644
--- a/src/audio/OpenAL/example02.cpp
+++ b/src/audio/OpenAL/example02.cpp
@@ -39,12 +39,8 @@ int main(int, char* [])
   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);
@@ -55,17 +51,16 @@ int main(int, char* [])
   }
 
   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);
+  std::cin.get();
   error = alGetError();
+
   if (error != AL_NO_ERROR) {
     std::cerr << "OpenAL: unloadWAV : " << alGetString(error);
   }
 
 
-  std::cin.get();
 }
diff --git a/src/audio/OpenAL/example03.cpp b/src/audio/OpenAL/example03.cpp
new file mode 100644
index 0000000000..603f5501fe
--- /dev/null
+++ b/src/audio/OpenAL/example03.cpp
@@ -0,0 +1,84 @@
+/*
+ *  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 <AL/al.h>
+#include <AL/alc.h>
+#include <AL/alut.h>
+
+#include "SFLAudio.hpp"
+
+using namespace SFLAudio;
+
+int main(int, char* []) 
+{
+  ALenum format1;
+  ALvoid *data1;
+  ALsizei size1;
+  ALsizei freq1;
+  ALboolean loop1;
+
+  ALenum format2;
+  ALvoid *data2;
+  ALsizei size2;
+  ALsizei freq2;
+  ALboolean loop2;
+
+  AudioLayer *layer = SFLAudio::AudioManager::instance().currentLayer();
+  Device *device = layer->openDevice();
+  Context *context = device->createContext();
+
+  // Load test.wav
+  alutLoadWAVFile("test.wav",&format1,&data1,&size1,&freq1,&loop1);
+  ALenum error = alGetError();
+  if (error != AL_NO_ERROR) {
+    std::cerr << "OpenAL: loadWAVFile : " << alGetString(error);
+    return 1;
+  }
+
+  // Load test2.wav
+  alutLoadWAVFile("test2.wav",&format2,&data2,&size2,&freq2,&loop2);
+  error = alGetError();
+  if (error != AL_NO_ERROR) {
+    std::cerr << "OpenAL: loadWAVFile : " << alGetString(error);
+    return 1;
+  }
+
+  Source *source1 = context->createSource(format1, freq1);
+  source1->play(data1, size1);
+  Source *source2 = context->createSource(format2, freq2);
+  source2->play(data2, size2);
+
+  // Unload test.wav and test2.wav
+  alutUnloadWAV(format1, data1, size1, freq1);
+  alutUnloadWAV(format2, data2, size2, freq2);
+  std::cin.get();
+  error = alGetError();
+
+  if (error != AL_NO_ERROR) {
+    std::cerr << "OpenAL: unloadWAV : " << alGetString(error);
+  }
+
+
+}
+
-- 
GitLab