Allolib  1.0
C++ Components For Interactive Multimedia
al_SoundFile.hpp
1 #ifndef INCLUDE_AL_SOUNDFILE_HPP
2 #define INCLUDE_AL_SOUNDFILE_HPP
3 
4 #include <atomic>
5 #include <vector>
6 
7 namespace al {
8 
16 struct SoundFile {
17  std::vector<float> data;
18  int sampleRate = 0;
19  int channels = 0;
20  long long int frameCount = 0;
21 
22  // In case of adding some constructor other than default constructor,
23  // remember to implement or explicitly specify related functions
24  // Related concept: `Rule of 5`
25 
26  // SoundFile() = default;
27  // SoundFile(const SoundFile&) = default;
28  // SoundFile(SoundFile&&) noexcept = default;
29  //
30  // SoundFile& operator=(const SoundFile&) = default;
31  // SoundFile& operator=(SoundFile&&) noexcept = default;
32  //
33  // ~SoundFile() = default;
34 
35  bool open(const char* path);
36  float* getFrame(long long int frame); // unsafe, without frameCount check
37 };
38 
39 SoundFile getResampledSoundFile(SoundFile* toConvert,
40  unsigned int newSampleRate);
41 
45  long long int frame = 0;
46  bool pause = true;
47  bool loop = false;
48  SoundFile* soundFile = nullptr; // non-owning
49 
50  // In case of adding some constructor other than default constructor,
51  // remember to implement or explicitly specify related functions
52  // Related concept: `Rule of 5`
53 
54  // SoundFilePlayer() = default;
55  // SoundFilePlayer(const SoundFilePlayer&) = default;
56  // SoundFilePlayer(SoundFilePlayer&&) noexcept = default;
57  //
58  // SoundFilePlayer& operator=(const SoundFilePlayer&) = default;
59  // SoundFilePlayer& operator=(SoundFilePlayer&&) noexcept = default;
60  //
61  // ~SoundFilePlayer() = default;
62 
63  void getFrames(uint64_t numFrames, float* buffer, int bufferLength);
64 };
65 
74  public:
75  SoundFileStreaming(const char* path = nullptr);
77 
78  bool isOpen() { return mImpl != nullptr; }
79 
81  uint32_t sampleRate();
83  uint64_t totalFrames();
85  uint16_t numChannels();
86 
88  bool open(const char* path);
90  void close();
92  uint64_t getFrames(uint64_t numFrames, float* buffer);
93 
94  private:
95  void* mImpl{nullptr};
96 };
97 
101  SoundFile soundFile;
102  SoundFilePlayer player;
103  std::atomic<bool> pauseSignal;
104  std::atomic<bool> loopSignal;
105  std::atomic<bool> rewindSignal;
106  // TODO - volume and fading
107 
108  // In case of adding some constructor other than default constructor,
109  // remember to implement or explicitly specify related functions
110  // Related concept: `Rule of 5`
111 
112  // SoundFilePlayerTS() = default;
113  // SoundFilePlayerTS(const SoundFilePlayerTS&) = default;
114  // SoundFilePlayerTS(SoundFilePlayerTS&&) noexcept = default;
115  //
116  // SoundFilePlayerTS& operator=(const SoundFilePlayerTS&) = default;
117  // SoundFilePlayerTS& operator=(SoundFilePlayerTS&&) noexcept = default;
118  //
119  // ~SoundFilePlayerTS() = default;
120 
121  bool open(const char* path) {
122  bool ret = soundFile.open(path);
123  player.soundFile = &soundFile;
124  return ret;
125  }
126 
127  void setPlay() { pauseSignal.store(false); }
128  void setPause() { pauseSignal.store(true); }
129  void togglePause() { pauseSignal.store(!pauseSignal.load()); }
130 
131  void setRewind() { rewindSignal.store(true); }
132 
133  void setLoop() { loopSignal.store(true); }
134  void setNoLoop() { loopSignal.store(false); }
135 
136  void getFrames(int numFrames, float* buffer, int bufferLength) {
137  player.pause = pauseSignal.load();
138  player.loop = loopSignal.load();
139  if (rewindSignal.exchange(false)) {
140  player.frame = 0;
141  }
142  player.getFrames(numFrames, buffer, bufferLength);
143  }
144 };
145 
146 } // namespace al
147 
148 #endif
The SoundFileStreaming class provides reading soundifle directly from disk one buffer at a time.
uint64_t getFrames(uint64_t numFrames, float *buffer)
Read interleaved frames into preallocated buffer;.
uint64_t totalFrames()
Total number of frames in file. Call after open has returned true.
void close()
Close file and cleanup.
uint32_t sampleRate()
Sampling rate of file. Call after open has returned true.
bool open(const char *path)
Open file for reading.
uint16_t numChannels()
Number of channels in file. Call after open has returned true.
Definition: al_App.hpp:23
Read sound file and store the data in float array (interleaved)
Soundfile player class.
Soundfile player class with thread-safe access to playback controls.