From 4c0869841d9ee6f8cc567b3e9eb2249193fc9f83 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 12 Mar 2015 16:43:49 -0400 Subject: Play a sound when you jump --- src/components.cpp | 3 ++ src/game.cpp | 2 +- src/main.cpp | 3 ++ src/muxer.cpp | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/muxer.h | 8 ++++ 5 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 src/muxer.cpp create mode 100644 src/muxer.h (limited to 'src') diff --git a/src/components.cpp b/src/components.cpp index e2eb2ed..941d02e 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -1,5 +1,6 @@ #include "components.h" #include "game.h" +#include "muxer.h" // User movement component @@ -215,6 +216,8 @@ void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg) velocity.second = 0.0; } else if (msg.type == Message::Type::jump) { + playSound("../res/Randomize87.wav"); + velocity.second = jump_velocity; accel.second = jump_gravity; } else if (msg.type == Message::Type::stopJump) diff --git a/src/game.cpp b/src/game.cpp index 043c82b..50c01f5 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -160,5 +160,5 @@ void Game::loadGame(const Map& curMap) void Game::schedule(int frames, std::function&& callback) { - scheduled.emplace(begin(scheduled), frames, callback); + scheduled.emplace_front(frames, callback); } diff --git a/src/main.cpp b/src/main.cpp index e37c2dd..96eb7da 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,12 +3,14 @@ #include "renderer.h" #include #include "game.h" +#include "muxer.h" int main() { srand(time(NULL)); GLFWwindow* window = initRenderer(); + initMuxer(); // Put this in a block so game goes out of scope before we destroy the renderer { @@ -16,6 +18,7 @@ int main() game.execute(window); } + destroyMuxer(); destroyRenderer(); return 0; diff --git a/src/muxer.cpp b/src/muxer.cpp new file mode 100644 index 0000000..0844faa --- /dev/null +++ b/src/muxer.cpp @@ -0,0 +1,111 @@ +#include "muxer.h" +#include +#include +#include +#include + +#define SAMPLE_RATE (44100) + +class Sound { + public: + Sound(const char* filename); + ~Sound(); + + float* ptr; + unsigned long pos; + unsigned long len; +}; + +struct Muxer { + std::list playing; + PaStream* stream; +}; + +inline void dealWithPaError(PaError err) +{ + if (err != paNoError) + { + printf("PortAudio error: %s\n", Pa_GetErrorText(err)); + exit(-1); + } +} + +int paMuxerCallback(const void*, void* outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void* userData) +{ + Muxer* muxer = (Muxer*) userData; + float* out = (float*) outputBuffer; + + for (unsigned long i = 0; iplaying) + { + if (sound.pos < sound.len) + { + *out *= curAmount++; + *out += sound.ptr[sound.pos++]; + *out /= (float) curAmount; + } + } + + out++; + } + + return 0; +} + +static Muxer* muxer; + +void initMuxer() +{ + muxer = new Muxer(); + + dealWithPaError(Pa_Initialize()); + dealWithPaError(Pa_OpenDefaultStream(&(muxer->stream), 0, 1, paFloat32, SAMPLE_RATE, paFramesPerBufferUnspecified, paMuxerCallback, muxer)); + dealWithPaError(Pa_StartStream(muxer->stream)); +} + +void destroyMuxer() +{ + dealWithPaError(Pa_AbortStream(muxer->stream)); + dealWithPaError(Pa_CloseStream(muxer->stream)); + dealWithPaError(Pa_Terminate()); + + delete muxer; + muxer = 0; +} + +void playSound(const char* filename) +{ + // First, clear out any sounds that have finished playing + muxer->playing.remove_if([] (Sound& value) { return value.pos >= value.len; }); + + // Then, add the new sound + muxer->playing.emplace_back(filename); +} + +Sound::Sound(const char* filename) +{ + SF_INFO info; + SNDFILE* file = sf_open(filename, SFM_READ, &info); + if (file == nullptr) + { + printf("LibSndFile error: %s\n", sf_strerror(file)); + exit(-1); + } + + ptr = (float*) malloc(info.frames * info.channels * sizeof(float)); + len = info.frames * info.channels; + pos = 0; + + sf_readf_float(file, ptr, info.frames); + + sf_close(file); +} + +Sound::~Sound() +{ + free(ptr); +} diff --git a/src/muxer.h b/src/muxer.h new file mode 100644 index 0000000..b0f5378 --- /dev/null +++ b/src/muxer.h @@ -0,0 +1,8 @@ +#ifndef MUXER_H +#define MUXER_H + +void initMuxer(); +void destroyMuxer(); +void playSound(const char* filename); + +#endif -- cgit 1.4.1