VOX
A little voxel engine
Loading...
Searching...
No Matches
SoundEngine.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "AudioData.hpp"
4#include "SoundList.hpp"
5#include "logger.hpp"
6
7#include <portaudio.h>
8
9#include <memory>
10#include <vector>
11#include <mutex>
12
13#define PA_THROW_CHECK(expr, msg) \
14{ \
15 PaError err = expr; \
16 if (err != paNoError) \
17 { \
18 throw std::runtime_error(std::string("Portaudio: ") + msg + " (" + std::string(Pa_GetErrorText(err)) + ")"); \
19 } \
20}
21
22#define PA_WARNING_CHECK(expr, msg) \
23{ \
24 PaError err = expr; \
25 if (err != paNoError) \
26 { \
27 LOG_WARNING(std::string("Portaudio: ") + msg + " (" + std::string(Pa_GetErrorText(err)) + ")"); \
28 } \
29}
30
31namespace Sound
32{
33 struct Instance
34 {
35 Data *audio_data = nullptr;
36 uint32_t cursor = 0;
37 bool loop = false;
38 bool ended = false;
39 };
40
41 class Engine
42 {
43
44 public:
45
46 Engine();
47 ~Engine();
48
49 void playSound(const SoundName audio_index, bool loop = false);
50
51 private:
52
53 PaStream *m_stream = nullptr;
54
55 std::vector<std::unique_ptr<Data>> m_audio_datas;
56
57 std::mutex m_sound_instances_mutex;
58 std::vector<Instance> m_sound_instances;
59
60 friend int paCallback_stereo(
61 const void *inputBuffer,
62 void *outputBuffer,
63 unsigned long framesPerBuffer,
64 const PaStreamCallbackTimeInfo *timeInfo,
65 PaStreamCallbackFlags statusFlags,
66 void *userData
67 );
68 };
69
70}
SoundName
Definition: SoundList.hpp:14
Definition: AudioData.hpp:12
Definition: SoundEngine.hpp:42
friend int paCallback_stereo(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
Definition: SoundEngine.cpp:6
Engine()
Definition: SoundEngine.cpp:69
~Engine()
Definition: SoundEngine.cpp:111
void playSound(const SoundName audio_index, bool loop=false)
Definition: SoundEngine.cpp:118
Definition: AudioData.cpp:7
Definition: SoundEngine.hpp:34
Data * audio_data
Definition: SoundEngine.hpp:35
bool ended
Definition: SoundEngine.hpp:38
bool loop
Definition: SoundEngine.hpp:37
uint32_t cursor
Definition: SoundEngine.hpp:36