summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt6
-rw-r--r--cmake/FindLibSndFile.cmake23
-rw-r--r--cmake/FindPortaudio.cmake106
-rw-r--r--res/Randomize87.wavbin47368 -> 29620 bytes
-rw-r--r--src/components.cpp3
-rw-r--r--src/game.cpp2
-rw-r--r--src/main.cpp3
-rw-r--r--src/muxer.cpp111
-rw-r--r--src/muxer.h8
9 files changed, 260 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 82ac3cb..0b28397 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -25,15 +25,19 @@ endif()
25find_package(OpenGL REQUIRED) 25find_package(OpenGL REQUIRED)
26find_package(GLEW REQUIRED) 26find_package(GLEW REQUIRED)
27find_package(GLFW REQUIRED) 27find_package(GLFW REQUIRED)
28find_package(portaudio REQUIRED)
29find_package(libsndfile REQUIRED)
28 30
29set(ALL_LIBS 31set(ALL_LIBS
30 ${OPENGL_LIBRARY} 32 ${OPENGL_LIBRARY}
31 ${GLEW_LIBRARY} 33 ${GLEW_LIBRARY}
32 ${GLFW_LIBRARY} 34 ${GLFW_LIBRARY}
35 ${PORTAUDIO_LIBRARIES}
36 ${LIBSNDFILE_LIBRARY}
33) 37)
34 38
35# include_directories(${SDL2_INCLUDE_DIR}) 39# include_directories(${SDL2_INCLUDE_DIR})
36set(CMAKE_BUILD_TYPE Debug) 40set(CMAKE_BUILD_TYPE Debug)
37add_executable(Aromatherapy src/main.cpp src/map.cpp src/renderer.cpp src/entity.cpp src/components.cpp src/game.cpp) 41add_executable(Aromatherapy src/main.cpp src/map.cpp src/renderer.cpp src/entity.cpp src/components.cpp src/game.cpp src/muxer.cpp)
38target_link_libraries(Aromatherapy ${ALL_LIBS}) 42target_link_libraries(Aromatherapy ${ALL_LIBS})
39install(TARGETS Aromatherapy RUNTIME DESTINATION ${BIN_DIR}) 43install(TARGETS Aromatherapy RUNTIME DESTINATION ${BIN_DIR})
diff --git a/cmake/FindLibSndFile.cmake b/cmake/FindLibSndFile.cmake new file mode 100644 index 0000000..a177bde --- /dev/null +++ b/cmake/FindLibSndFile.cmake
@@ -0,0 +1,23 @@
1# Base Io build system
2# Written by Jeremy Tregunna <jeremy.tregunna@me.com>
3#
4# Find libsndfile.
5
6FIND_PATH(LIBSNDFILE_INCLUDE_DIR sndfile.h)
7
8SET(LIBSNDFILE_NAMES ${LIBSNDFILE_NAMES} sndfile libsndfile)
9FIND_LIBRARY(LIBSNDFILE_LIBRARY NAMES ${LIBSNDFILE_NAMES} PATH)
10
11IF(LIBSNDFILE_INCLUDE_DIR AND LIBSNDFILE_LIBRARY)
12 SET(LIBSNDFILE_FOUND TRUE)
13ENDIF(LIBSNDFILE_INCLUDE_DIR AND LIBSNDFILE_LIBRARY)
14
15IF(LIBSNDFILE_FOUND)
16 IF(NOT LibSndFile_FIND_QUIETLY)
17 MESSAGE(STATUS "Found LibSndFile: ${LIBSNDFILE_LIBRARY}")
18 ENDIF (NOT LibSndFile_FIND_QUIETLY)
19ELSE(LIBSNDFILE_FOUND)
20 IF(LibSndFile_FIND_REQUIRED)
21 MESSAGE(FATAL_ERROR "Could not find sndfile")
22 ENDIF(LibSndFile_FIND_REQUIRED)
23ENDIF (LIBSNDFILE_FOUND) \ No newline at end of file
diff --git a/cmake/FindPortaudio.cmake b/cmake/FindPortaudio.cmake new file mode 100644 index 0000000..aa46935 --- /dev/null +++ b/cmake/FindPortaudio.cmake
@@ -0,0 +1,106 @@
1# - Try to find Portaudio
2# Once done this will define
3#
4# PORTAUDIO_FOUND - system has Portaudio
5# PORTAUDIO_INCLUDE_DIRS - the Portaudio include directory
6# PORTAUDIO_LIBRARIES - Link these to use Portaudio
7# PORTAUDIO_DEFINITIONS - Compiler switches required for using Portaudio
8# PORTAUDIO_VERSION - Portaudio version
9#
10# Copyright (c) 2006 Andreas Schneider <mail@cynapses.org>
11#
12# Redistribution and use is allowed according to the terms of the New BSD license.
13# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
14#
15
16
17if (PORTAUDIO_LIBRARIES AND PORTAUDIO_INCLUDE_DIRS)
18 # in cache already
19 set(PORTAUDIO_FOUND TRUE)
20else (PORTAUDIO_LIBRARIES AND PORTAUDIO_INCLUDE_DIRS)
21 if (NOT WIN32)
22 include(FindPkgConfig)
23 pkg_check_modules(PORTAUDIO2 portaudio-2.0)
24 endif (NOT WIN32)
25
26 if (PORTAUDIO2_FOUND)
27 set(PORTAUDIO_INCLUDE_DIRS
28 ${PORTAUDIO2_INCLUDE_DIRS}
29 )
30 if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
31 set(PORTAUDIO_LIBRARIES "${PORTAUDIO2_LIBRARY_DIRS}/lib${PORTAUDIO2_LIBRARIES}.dylib")
32 else (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
33 set(PORTAUDIO_LIBRARIES
34 ${PORTAUDIO2_LIBRARIES}
35 )
36 endif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
37 set(PORTAUDIO_VERSION
38 19
39 )
40 set(PORTAUDIO_FOUND TRUE)
41 else (PORTAUDIO2_FOUND)
42 find_path(PORTAUDIO_INCLUDE_DIR
43 NAMES
44 portaudio.h
45 PATHS
46 /usr/include
47 /usr/local/include
48 /opt/local/include
49 /sw/include
50 )
51
52 find_library(PORTAUDIO_LIBRARY
53 NAMES
54 portaudio
55 PATHS
56 /usr/lib
57 /usr/local/lib
58 /opt/local/lib
59 /sw/lib
60 )
61
62 find_path(PORTAUDIO_LIBRARY_DIR
63 NAMES
64 portaudio
65 PATHS
66 /usr/lib
67 /usr/local/lib
68 /opt/local/lib
69 /sw/lib
70 )
71
72 set(PORTAUDIO_INCLUDE_DIRS
73 ${PORTAUDIO_INCLUDE_DIR}
74 )
75 set(PORTAUDIO_LIBRARIES
76 ${PORTAUDIO_LIBRARY}
77 )
78
79 set(PORTAUDIO_LIBRARY_DIRS
80 ${PORTAUDIO_LIBRARY_DIR}
81 )
82
83 set(PORTAUDIO_VERSION
84 18
85 )
86
87 if (PORTAUDIO_INCLUDE_DIRS AND PORTAUDIO_LIBRARIES)
88 set(PORTAUDIO_FOUND TRUE)
89 endif (PORTAUDIO_INCLUDE_DIRS AND PORTAUDIO_LIBRARIES)
90
91 if (PORTAUDIO_FOUND)
92 if (NOT Portaudio_FIND_QUIETLY)
93 message(STATUS "Found Portaudio: ${PORTAUDIO_LIBRARIES}")
94 endif (NOT Portaudio_FIND_QUIETLY)
95 else (PORTAUDIO_FOUND)
96 if (Portaudio_FIND_REQUIRED)
97 message(FATAL_ERROR "Could not find Portaudio")
98 endif (Portaudio_FIND_REQUIRED)
99 endif (PORTAUDIO_FOUND)
100 endif (PORTAUDIO2_FOUND)
101
102
103 # show the PORTAUDIO_INCLUDE_DIRS and PORTAUDIO_LIBRARIES variables only in the advanced view
104 mark_as_advanced(PORTAUDIO_INCLUDE_DIRS PORTAUDIO_LIBRARIES)
105
106endif (PORTAUDIO_LIBRARIES AND PORTAUDIO_INCLUDE_DIRS)
diff --git a/res/Randomize87.wav b/res/Randomize87.wav index 6338faf..14cf907 100644 --- a/res/Randomize87.wav +++ b/res/Randomize87.wav
Binary files differ
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 @@
1#include "components.h" 1#include "components.h"
2#include "game.h" 2#include "game.h"
3#include "muxer.h"
3 4
4// User movement component 5// User movement component
5 6
@@ -215,6 +216,8 @@ void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg)
215 velocity.second = 0.0; 216 velocity.second = 0.0;
216 } else if (msg.type == Message::Type::jump) 217 } else if (msg.type == Message::Type::jump)
217 { 218 {
219 playSound("../res/Randomize87.wav");
220
218 velocity.second = jump_velocity; 221 velocity.second = jump_velocity;
219 accel.second = jump_gravity; 222 accel.second = jump_gravity;
220 } else if (msg.type == Message::Type::stopJump) 223 } 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)
160 160
161void Game::schedule(int frames, std::function<void ()>&& callback) 161void Game::schedule(int frames, std::function<void ()>&& callback)
162{ 162{
163 scheduled.emplace(begin(scheduled), frames, callback); 163 scheduled.emplace_front(frames, callback);
164} 164}
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 @@
3#include "renderer.h" 3#include "renderer.h"
4#include <cstdlib> 4#include <cstdlib>
5#include "game.h" 5#include "game.h"
6#include "muxer.h"
6 7
7int main() 8int main()
8{ 9{
9 srand(time(NULL)); 10 srand(time(NULL));
10 11
11 GLFWwindow* window = initRenderer(); 12 GLFWwindow* window = initRenderer();
13 initMuxer();
12 14
13 // Put this in a block so game goes out of scope before we destroy the renderer 15 // Put this in a block so game goes out of scope before we destroy the renderer
14 { 16 {
@@ -16,6 +18,7 @@ int main()
16 game.execute(window); 18 game.execute(window);
17 } 19 }
18 20
21 destroyMuxer();
19 destroyRenderer(); 22 destroyRenderer();
20 23
21 return 0; 24 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 @@
1#include "muxer.h"
2#include <cstdlib>
3#include <sndfile.h>
4#include <portaudio.h>
5#include <list>
6
7#define SAMPLE_RATE (44100)
8
9class Sound {
10 public:
11 Sound(const char* filename);
12 ~Sound();
13
14 float* ptr;
15 unsigned long pos;
16 unsigned long len;
17};
18
19struct Muxer {
20 std::list<Sound> playing;
21 PaStream* stream;
22};
23
24inline void dealWithPaError(PaError err)
25{
26 if (err != paNoError)
27 {
28 printf("PortAudio error: %s\n", Pa_GetErrorText(err));
29 exit(-1);
30 }
31}
32
33int paMuxerCallback(const void*, void* outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void* userData)
34{
35 Muxer* muxer = (Muxer*) userData;
36 float* out = (float*) outputBuffer;
37
38 for (unsigned long i = 0; i<framesPerBuffer; i++)
39 {
40 unsigned long curAmount = 0;
41 *out = 0;
42
43 for (auto& sound : muxer->playing)
44 {
45 if (sound.pos < sound.len)
46 {
47 *out *= curAmount++;
48 *out += sound.ptr[sound.pos++];
49 *out /= (float) curAmount;
50 }
51 }
52
53 out++;
54 }
55
56 return 0;
57}
58
59static Muxer* muxer;
60
61void initMuxer()
62{
63 muxer = new Muxer();
64
65 dealWithPaError(Pa_Initialize());
66 dealWithPaError(Pa_OpenDefaultStream(&(muxer->stream), 0, 1, paFloat32, SAMPLE_RATE, paFramesPerBufferUnspecified, paMuxerCallback, muxer));
67 dealWithPaError(Pa_StartStream(muxer->stream));
68}
69
70void destroyMuxer()
71{
72 dealWithPaError(Pa_AbortStream(muxer->stream));
73 dealWithPaError(Pa_CloseStream(muxer->stream));
74 dealWithPaError(Pa_Terminate());
75
76 delete muxer;
77 muxer = 0;
78}
79
80void playSound(const char* filename)
81{
82 // First, clear out any sounds that have finished playing
83 muxer->playing.remove_if([] (Sound& value) { return value.pos >= value.len; });
84
85 // Then, add the new sound
86 muxer->playing.emplace_back(filename);
87}
88
89Sound::Sound(const char* filename)
90{
91 SF_INFO info;
92 SNDFILE* file = sf_open(filename, SFM_READ, &info);
93 if (file == nullptr)
94 {
95 printf("LibSndFile error: %s\n", sf_strerror(file));
96 exit(-1);
97 }
98
99 ptr = (float*) malloc(info.frames * info.channels * sizeof(float));
100 len = info.frames * info.channels;
101 pos = 0;
102
103 sf_readf_float(file, ptr, info.frames);
104
105 sf_close(file);
106}
107
108Sound::~Sound()
109{
110 free(ptr);
111}
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 @@
1#ifndef MUXER_H
2#define MUXER_H
3
4void initMuxer();
5void destroyMuxer();
6void playSound(const char* filename);
7
8#endif