aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorILOVEPIE <thehairyrock@gmail.com>2013-03-31 09:20:48 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-09-24 16:56:44 -0700
commitdcbdbb420fe38fa173710d3de8adce9e684bf9b0 (patch)
tree96b3d6765c6ca7eca7bf1e0032e8a555742b97ca
parent0462f14bd6b6bdd9ad6adee0e30bddebab6da144 (diff)
Fixed some stuff I forgot to change.
Signed-off-by: ILOVEPIE <thehairyrock@gmail.com>
-rw-r--r--src/library_sdl.js27
-rw-r--r--tests/sdl_audio_beep.cpp128
2 files changed, 143 insertions, 12 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js
index a2d0457b..0e5a5df4 100644
--- a/src/library_sdl.js
+++ b/src/library_sdl.js
@@ -1465,6 +1465,10 @@ var LibrarySDL = {
samples: {{{ makeGetValue('desired', 'SDL.structs.AudioSpec.samples', 'i16', 0, 1) }}},
callback: {{{ makeGetValue('desired', 'SDL.structs.AudioSpec.callback', 'void*', 0, 1) }}},
userdata: {{{ makeGetValue('desired', 'SDL.structs.AudioSpec.userdata', 'void*', 0, 1) }}},
+ soundSource: new Array(),
+ nextSoundSource: 0,
+ lastSoundSource: -1,
+ nextPlayTime: 0,
paused: true,
timer: null
};
@@ -1503,10 +1507,11 @@ var LibrarySDL = {
}else{
if (typeof(AudioContext) === "function") {
SDL.audio.context = new AudioContext();
- SDL.audio.soundSource = SDL.audio.context.createBufferSource();
} else if (typeof(webkitAudioContext) === "function") {
SDL.audio.context = new webkitAudioContext();
- }
+ } else {
+ throw "no sound!";
+ }
SDL.audio.nextSoundSource = 0;
SDL.audio.soundSource = new Array();
SDL.audio.nextPlayTime = 0;
@@ -1531,11 +1536,8 @@ var LibrarySDL = {
}
SDL.audio.nextPlayTime = SDL.audio.context.currentTime+SDL.audio.soundSource[SDL.audio.nextSoundSource].buffer.duration;
- if(typeof(SDL.audio.soundSource.start)=== "function"){
- SDL.audio.soundSource[SDL.audio.nextSoundSource].start(SDL.audio.nextPlayTime);
- }else{
- SDL.audio.soundSource[SDL.audio.nextSoundSource].noteOn(SDL.audio.nextPlayTime);
- }
+
+ SDL.audio.soundSource[SDL.audio.nextSoundSource].start(SDL.audio.nextPlayTime);
SDL.audio.lastSoundSource = SDL.Audio.nextSoundSource;
SDL.Audio.nextSoundSource++;
@@ -1559,12 +1561,13 @@ var LibrarySDL = {
SDL_CloseAudio: function() {
if (SDL.audio) {
try{
- if(typeof(SDL.audio.soundSource.stop)=== "function"){
- SDL.audio.soundSource.stop(0);
- }else{
- SDL.audio.soundSource.noteOff(0);
- }
+ for(var i = 0; i<SDL.audio.soundSource.length;i++){
+ if(!(typeof(SDL.audio.soundSource[i]==="undefined"))){
+ SDL.audio.soundSource[i].stop(0);
+ }
+ }
}catch(e){}
+ SDL.audo.soundSource = null;
_SDL_PauseAudio(1);
_free(SDL.audio.buffer);
SDL.audio = null;
diff --git a/tests/sdl_audio_beep.cpp b/tests/sdl_audio_beep.cpp
new file mode 100644
index 00000000..4bcc3b85
--- /dev/null
+++ b/tests/sdl_audio_beep.cpp
@@ -0,0 +1,128 @@
+#include <SDL/SDL.h>
+#include <SDL/SDL_audio.h>
+#include <queue>
+#include <cmath>
+
+const int AMPLITUDE = 28000;
+const int FREQUENCY = 44100;
+
+struct BeepObject
+{
+ double freq;
+ int samplesLeft;
+};
+
+class Beeper
+{
+private:
+ double v;
+ std::queue<BeepObject> beeps;
+public:
+ Beeper();
+ ~Beeper();
+ void beep(double freq, int duration);
+ void generateSamples(Sint16 *stream, int length);
+ void wait();
+};
+
+void audio_callback(void*, Uint8*, int);
+
+Beeper::Beeper()
+{
+ SDL_AudioSpec desiredSpec;
+
+ desiredSpec.freq = FREQUENCY;
+ desiredSpec.format = AUDIO_S16SYS;
+ desiredSpec.channels = 1;
+ desiredSpec.samples = 2048;
+ desiredSpec.callback = audio_callback;
+ desiredSpec.userdata = this;
+
+ SDL_AudioSpec obtainedSpec;
+
+ // you might want to look for errors here
+ SDL_OpenAudio(&desiredSpec, &obtainedSpec);
+
+ // start play audio
+ SDL_PauseAudio(0);
+}
+
+Beeper::~Beeper()
+{
+ SDL_CloseAudio();
+}
+
+void Beeper::generateSamples(Sint16 *stream, int length)
+{
+ int i = 0;
+ while (i < length) {
+
+ if (beeps.empty()) {
+ while (i < length) {
+ stream[i] = 0;
+ i++;
+ }
+ return;
+ }
+ BeepObject& bo = beeps.front();
+
+ int samplesToDo = std::min(i + bo.samplesLeft, length);
+ bo.samplesLeft -= samplesToDo - i;
+
+ while (i < samplesToDo) {
+ stream[i] = AMPLITUDE * std::sin(v * 2 * M_PI / FREQUENCY);
+ i++;
+ v += bo.freq;
+ }
+
+ if (bo.samplesLeft == 0) {
+ beeps.pop();
+ }
+ }
+}
+
+void Beeper::beep(double freq, int duration)
+{
+ BeepObject bo;
+ bo.freq = freq;
+ bo.samplesLeft = duration * FREQUENCY / 1000;
+
+ SDL_LockAudio();
+ beeps.push(bo);
+ SDL_UnlockAudio();
+}
+
+void Beeper::wait()
+{
+ int size;
+ do {
+ SDL_Delay(20);
+ SDL_LockAudio();
+ size = beeps.size();
+ SDL_UnlockAudio();
+ } while (size > 0);
+
+}
+
+void audio_callback(void *_beeper, Uint8 *_stream, int _length)
+{
+ Sint16 *stream = (Sint16*) _stream;
+ int length = _length / 2;
+ Beeper* beeper = (Beeper*) _beeper;
+
+ beeper->generateSamples(stream, length);
+}
+
+int main(int argc, char* argv[])
+{
+ SDL_Init(SDL_INIT_AUDIO);
+
+ int duration = 1000;
+ double Hz = 440;
+
+ Beeper b;
+ b.beep(Hz, duration);
+ b.wait();
+
+ return 0;
+} \ No newline at end of file