The small3d framework
Sound.hpp
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <unordered_map>
13 
14 #if defined(__ANDROID__)
15 #include <aaudio/AAudio.h>
16 #elif defined(SMALL3D_IOS)
17 #include <OpenAL/al.h>
18 #include <OpenAL/alc.h>
19 #else
20 #include <portaudio.h>
21 #endif
22 
23 #include <vector>
24 #include <string>
25 
26 namespace small3d {
27 
33  class Sound {
34 
35  private:
36 
37  struct SoundData {
38  int channels;
39  int rate;
40  long samples;
41  long size;
42  double duration;
43  double startTime;
44  bool repeat;
45  unsigned long currentFrame;
46  std::vector<char> data;
47  };
48 
49  SoundData soundData;
50 
51 #if !defined(__ANDROID__) && !defined(SMALL3D_IOS)
52  PaStream *stream;
53 #elif defined(__ANDROID__)
54  AAudioStreamBuilder *streamBuilder;
55  AAudioStream *stream;
56 #elif defined(SMALL3D_IOS)
57 static ALCdevice *openalDevice;
58 static ALCcontext *openalContext;
59  ALuint openalSource;
60  ALuint openalBuffer;
61 #endif
62 
63  static bool noOutputDevice;
64 
65  static unsigned int numInstances;
66 
67 #if !defined(SMALL3D_IOS) && !defined(__ANDROID__)
68  static PaDeviceIndex defaultOutput;
69  static int audioCallback(const void *inputBuffer, void *outputBuffer,
70  unsigned long framesPerBuffer,
71  const PaStreamCallbackTimeInfo *timeInfo,
72  PaStreamCallbackFlags statusFlags,
73  void *userData);
74 #elif defined(__ANDROID__)
75  static aaudio_data_callback_result_t audioCallback (
76  AAudioStream *stream,
77  void *userData,
78  void *audioData,
79  int32_t numFrames);
80 #endif
81 
82  void load(const std::string soundFilePath);
83  void openStream();
84 
85  public:
89  Sound();
90 
95  Sound(const std::string soundFilePath);
96 
100  ~Sound();
101 
106  void play(const bool repeat=false);
107 
111  void stop();
112 
116  Sound(const Sound& other);
117 
121  Sound(const Sound&& other);
122 
126  Sound& operator=(const Sound& other);
127 
131  Sound& operator=(const Sound&& other);
132 
133  };
134 
135 }
Sound()
Default constructor.
Definition: Sound.cpp:208
Sound & operator=(const Sound &other)
Copy assignment.
Definition: Sound.cpp:532
void play(const bool repeat=false)
Play the sound.
Definition: Sound.cpp:447
~Sound()
Destructor.
Definition: Sound.cpp:265
void stop()
Stop playing the sound.
Definition: Sound.cpp:488
Class that loads and plays a sound from an ogg file.
Definition: Sound.hpp:33