Sound.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 #ifndef H_SOUND_INCLUDED
34 #define H_SOUND_INCLUDED
35 
36 
37 
38 #include <Dynacoe/Backends/AudioManager/AudioManager.h>
39 #include <Dynacoe/AudioSample.h>
40 #include <Dynacoe/Modules/Assets.h>
41 #include <Dynacoe/AudioEffect.h>
42 #include <Dynacoe/Sequence.h>
43 #include <Dynacoe/Util/Table.h>
44 
45 
46 class AudioClient;
47 
48 namespace Dynacoe {
49 class AudioManager;
50 class AudioBlock;
51 
52 
53 
63 class ActiveSound {
64  public:
65  ActiveSound();
66  ActiveSound(const ActiveSound &);
67  ActiveSound & operator=(const ActiveSound &);
68 
72  void SetVolume(float v);
73 
77  void SetPanning(float v);
78 
83  void SetRepeat(bool b);
84 
91  void Seek(float f);
92 
96  void SetChannel(uint32_t i);
97 
100  void Stop();
101 
104  void Pause();
105 
108  void Resume();
109 
116  bool Valid();
117 
118  private:
119  friend class Sound;
120 
121  uint8_t * Updated();
122  ActiveSound(LookupID stateID);
123  LookupID state;
124 };
125 
126 
129 class Sound : public Module {
130 
131  public:
132 
133 
134 
135 
136 
139  static bool IsPlaying(AssetID index);
140 
141 
147  static ActiveSound PlayAudio(AssetID, uint8_t effectChannel=0, float volume = 1.f, float panning = .5f);
148 
149 
150 
151 
152 
153 
154 
155  /* Channels */
174  //{
175 
180  static void ChannelAddEffect(const AudioEffect *, uint8_t channel);
181 
184  static void ChannelRemoveEffect(const AudioEffect *, uint8_t channel);
185 
188  static void ChannelReset(uint8_t channel);
189 
196  static void ChannelKeepAwake(uint8_t, bool doIt);
197 
202  static void ChannelSetVolume(uint8_t channel, float);
203 
208  static void ChannelSetPanning(uint8_t channel, float);
210 
211 
212  static AudioManager * GetManager();
213 
214 
215 
216 
217 
218  private:
219 
220  friend class AudioBlock;
221 
222  static AudioBlock * uncompressAudio(const std::string & path);
223  static AudioClient * a;
224  static void AudioClientThreadMain();
225  static void * audioClientThread;
226  static void initBase();
227 
228  public:
229  std::string GetName() {return "Sound";}
230  void Init(); void InitAfter(); void RunBefore(); void RunAfter(); void DrawBefore(); void DrawAfter();
231  Backend * GetBackend();
232 };
233 
234 struct Export_d {
235  Sound * s;
236  std::string fileName;
237 };
238 
239 
240 
241 
242 
243 
244 };
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 #endif
void SetChannel(uint32_t i)
Alters the channel that the sound plays on.
Backend.
Definition: Backend.h:56
void Seek(float f)
Sets the progress in the sound to play from.
Base class that applies effects through the Sound module.
Definition: AudioEffect.h:40
A generalized structure representing an audio waveform.
Definition: AudioBlock.h:64
void SetRepeat(bool b)
Sets whether the sound should be replayed once it finishes.
void SetVolume(float v)
Sets the volume of the ActiveSound.
Definition: AssetID.h:37
static void ChannelRemoveEffect(const AudioEffect *, uint8_t channel)
Unregisters an AudioEffect instance.
void Stop()
Halts the sound.
Assets are referred to by an AssetID. The AssetID uniquely refers to an Asset stored within memory...
Definition: AssetID.h:42
void SetPanning(float v)
Sets the panning.
static void ChannelKeepAwake(uint8_t, bool doIt)
Keeps the stream active for the given channel. By default, channels will only be active if there are ...
Allows for interaction with a playing sound.
Definition: Sound.h:63
static AudioManager * GetManager()
}
static void ChannelSetVolume(uint8_t channel, float)
Sets the volume for the given channel 0.f denotes minimum volume and 1.f maximum. The values are clip...
static ActiveSound PlayAudio(AssetID, uint8_t effectChannel=0, float volume=1.f, float panning=.5f)
Queues audio for immediate playback.
static void ChannelReset(uint8_t channel)
Removes all effects from a channel. This does not delete the AudioEffects given to the channel...
void Pause()
Pauses the sound if it was playing.
bool Valid()
Returns whether this ActiveSound actually refers to something that is playing back.
void Resume()
Resumes the sound's playback if it was Pause()ed.
static void ChannelSetPanning(uint8_t channel, float)
Sets the panning for the entire channel. 0.f denotes all the way to the left and 1.f all the way to the right. The values are clipped if they are beyond these bounds.
static bool IsPlaying(AssetID index)
Returns whether or not an instance of the sample is playing.
static void ChannelAddEffect(const AudioEffect *, uint8_t channel)
Registers an AudioEffect instance with an associated effect channel. All further samples that play on...
Module that handles all audio related functionality.
Definition: Sound.h:129