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