The small3d library
Tiny C++ 3D game development library for Win/MacOS/Linux/FreeBSD
Loading...
Searching...
No Matches
Sound.hpp
Go to the documentation of this file.
1
10#pragma once
11
12#include <unordered_map>
13#include <cstdint>
14
15#define WORD_SIZE 2
16
17#define SAMPLE_DATATYPE short
18
19#include <portaudio.h>
20
21
22#include <vector>
23#include <string>
24
25// This avoids a glitch on archlinux
26#ifdef __linux__
27#include <cstdint>
28#endif
29
30namespace small3d {
31
48 class Sound {
49
50 private:
51
52 struct SoundData {
53 int channels = 0;
54 int rate = 0;
55 long samples = 0;
56 long size = 0;
57 double duration = 0;
58 double startTime = 0;
59 bool repeat = false;
60 unsigned long currentFrame = 0;
61 std::vector<char> data;
62 bool playingRepeat = false;
63
64 template <class Archive>
65 void serialize(Archive& archive) {
66 archive(channels,
67 rate,
68 samples,
69 size,
70 duration,
71 startTime,
72 repeat,
73 currentFrame,
74 data,
75 playingRepeat
76 );
77 }
78 };
79
80 SoundData soundData;
81
82#if !defined(__ANDROID__) && !defined(SMALL3D_IOS)
83 PaStream *stream;
84#elif defined(__ANDROID__)
85 oboe::AudioStreamBuilder *streamBuilder;
86 oboe::AudioStream *stream;
87#elif defined(SMALL3D_IOS)
88static ALCdevice *openalDevice;
89static ALCcontext *openalContext;
90 ALuint openalSource;
91 ALuint openalBuffer;
92#endif
93
94 static bool noOutputDevice;
95
96 static unsigned int numInstances;
97
98#if !defined(SMALL3D_IOS) && !defined(__ANDROID__)
99 static PaDeviceIndex defaultOutput;
100 static int audioCallback(const void *inputBuffer, void *outputBuffer,
101 unsigned long framesPerBuffer,
102 const PaStreamCallbackTimeInfo *timeInfo,
103 PaStreamCallbackFlags statusFlags,
104 void *userData);
105#elif defined(__ANDROID__)
106
107 class AudioCallbackClass : public oboe::AudioStreamDataCallback {
108 private:
109 SoundData *soundData;
110 public:
111 AudioCallbackClass(SoundData *soundData) {
112 this->soundData = soundData;
113 }
114 oboe::DataCallbackResult onAudioReady(oboe::AudioStream *audioStream, void *audioData,
115 int32_t numFrames) {
116
117 auto *out = static_cast<SAMPLE_DATATYPE*>(audioData);
118
119 if (soundData->currentFrame * SAMPLES_PER_FRAME >= soundData->samples) {
120 if (soundData->repeat) {
121 soundData->currentFrame = 0;
122 }
123 else {
124 // Always write something to the stream
125 memset(out, 0, WORD_SIZE * numFrames * SAMPLES_PER_FRAME * soundData->channels);
126 return oboe::DataCallbackResult::Stop;
127 }
128 }
129
130 memcpy(out, &soundData->data.data()[WORD_SIZE * soundData->currentFrame *
131 SAMPLES_PER_FRAME * soundData->channels],
132 WORD_SIZE * numFrames * SAMPLES_PER_FRAME * soundData->channels);
133
134 soundData->currentFrame += numFrames;
135
136 return oboe::DataCallbackResult::Continue;
137 }
138 };
139
140 AudioCallbackClass audioCallbackObject;
141
142#endif
143
144 void load(const std::string& soundFilePath);
145 void openStream();
146
147 public:
151 Sound();
152
157 explicit Sound(const std::string& soundFilePath);
158
162 ~Sound();
163
168 void play(const bool repeat=false);
169
173 void stop();
174
180 void divideVolume(uint32_t divisor);
181
185 Sound(const Sound& other);
186
190 Sound(const Sound&& other);
191
195 Sound& operator=(const Sound& other);
196
200 Sound& operator=(const Sound&& other);
201
206 void saveBinary(const std::string& binaryFilePath);
207
208 };
209
210}
Class that loads and plays a sound from an ogg file, or a native sound file. In the latter case,...
Definition Sound.hpp:48
void play(const bool repeat=false)
Play the sound.
Definition Sound.cpp:352
void stop()
Stop playing the sound.
Definition Sound.cpp:387
Sound()
Default constructor.
Definition Sound.cpp:92
void saveBinary(const std::string &binaryFilePath)
Save sound data in binary format.
Definition Sound.cpp:462
void divideVolume(uint32_t divisor)
Divide the volume (in order to lower it).
Definition Sound.cpp:340
Sound & operator=(const Sound &other)
Copy assignment.
Definition Sound.cpp:426
~Sound()
Destructor.
Definition Sound.cpp:137
Definition BinaryFile.hpp:15