1 //-----------------------------------------------------------------------------
2 // Copyright (c) 2015-2018 Marcelo Fernandez
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to
6 // deal in the Software without restriction, including without limitation the
7 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 // sell copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 // IN THE SOFTWARE.
21 //-----------------------------------------------------------------------------
22 
23 #ifndef __OAMLAUDIO_H__
24 #define __OAMLAUDIO_H__
25 
26 class ByteBuffer;
27 
28 class oamlAudio {
29 private:
30 	oamlBase *base;
31 	bool verbose;
32 	oamlFileCallbacks *fcbs;
33 
34 	std::vector<oamlAudioFile> files;
35 	std::string name;
36 	int type;
37 	int bars;
38 	float volume;
39 
40 	unsigned int samplesCount;
41 	unsigned int samplesPerSec;
42 	unsigned int samplesToEnd;
43 	unsigned int totalSamples;
44 	unsigned int filesSamples;
45 	unsigned int channelCount;
46 
47 	float bpm;
48 	int beatsPerBar;
49 	int minMovementBars;
50 	int randomChance;
51 	int playOrder;
52 
53 	unsigned int fadeIn;
54 	unsigned int fadeInSamples;
55 
56 	unsigned int fadeOut;
57 	unsigned int fadeOutSamples;
58 	unsigned int fadeOutCount;
59 
60 	unsigned int xfadeIn;
61 	unsigned int xfadeOut;
62 
63 	int condId;
64 	int condType;
65 	int condValue;
66 	int condValue2;
67 
68 	bool pickable;
69 
70 	void UpdateSamplesToEnd();
71 
72 public:
73 	oamlAudio(oamlBase *_base, oamlFileCallbacks *cbs, bool _verbose);
74 	~oamlAudio();
75 
SetName(std::string _name)76 	void SetName(std::string _name) { name = _name; }
SetType(int audioType)77 	void SetType(int audioType) { type = audioType; }
SetVolume(float audioVolume)78 	void SetVolume(float audioVolume) { volume = audioVolume; }
79 	void SetBPM(float _bpm);
80 	void SetBeatsPerBar(int _beatsPerBar);
81 	void SetBars(int _bars);
SetMinMovementBars(int audioMinMovementBars)82 	void SetMinMovementBars(int audioMinMovementBars) { minMovementBars = audioMinMovementBars; }
SetRandomChance(int audioRandomChance)83 	void SetRandomChance(int audioRandomChance) { randomChance = audioRandomChance; }
SetPlayOrder(int _playOrder)84 	void SetPlayOrder(int _playOrder) { playOrder = _playOrder; }
SetFadeIn(unsigned int audioFadeIn)85 	void SetFadeIn(unsigned int audioFadeIn) { fadeIn = audioFadeIn; }
SetFadeOut(unsigned int audioFadeOut)86 	void SetFadeOut(unsigned int audioFadeOut) { fadeOut = audioFadeOut; }
SetXFadeIn(unsigned int audioXFadeIn)87 	void SetXFadeIn(unsigned int audioXFadeIn) { xfadeIn = audioXFadeIn; }
SetXFadeOut(unsigned int audioXFadeOut)88 	void SetXFadeOut(unsigned int audioXFadeOut) { xfadeOut = audioXFadeOut; }
89 
SetCondId(int audioCondId)90 	void SetCondId(int audioCondId) { condId = audioCondId; }
SetCondType(int audioCondType)91 	void SetCondType(int audioCondType) { condType = audioCondType; }
SetCondValue(int audioCondValue)92 	void SetCondValue(int audioCondValue) { condValue = audioCondValue; }
SetCondValue2(int audioCondValue2)93 	void SetCondValue2(int audioCondValue2) { condValue2 = audioCondValue2; }
94 
95 	void SetCondition(int id, int type, int value, int value2 = 0);
96 	bool TestCondition(int id, int value);
HasCondition(int id)97 	bool HasCondition(int id) { return id == condId; }
98 
99 	bool HasFinished();
100 	bool HasFinishedTail(unsigned int pos);
101 
102 	oamlRC Open();
103 	oamlRC Load();
104 	int LoadProgress();
105 	float ReadFloat();
106 	float ReadFloat(unsigned int pos);
107 
108 	void ReadSamples(float *samples, int channels);
109 	unsigned int ReadSamples(float *samples, int channels, unsigned int pos);
110 
111 	void DoFadeIn(int msec);
112 	void DoFadeOut(int msec);
113 
114 	void ReadInfo(oamlAudioInfo *info);
115 
116 	void GetAudioFileList(std::vector<std::string>& list);
117 	bool HasAudioFile(std::string filename);
118 	void RemoveAudioFile(std::string filename);
119 	oamlAudioFile *GetAudioFile(std::string filename);
120 
121 	void AddAudioFile(std::string filename, std::string layer = "", int randomChance = -1);
GetName()122 	std::string GetName() const { return name; }
GetVolume()123 	float GetVolume() const { return volume; }
GetBPM()124 	float GetBPM() const { return bpm; }
GetBeatsPerBar()125 	int GetBeatsPerBar() const { return beatsPerBar; }
GetBars()126 	int GetBars() const { return bars; }
GetMinMovementBars()127 	int GetMinMovementBars() const { return minMovementBars; }
GetRandomChance()128 	int GetRandomChance() const { return randomChance; }
GetPlayOrder()129 	int GetPlayOrder() const { return playOrder; }
GetCondId()130 	int GetCondId() const { return condId; }
GetCondType()131 	int GetCondType() const { return condType; }
GetCondValue()132 	int GetCondValue() const { return condValue; }
GetCondValue2()133 	int GetCondValue2() const { return condValue2; }
GetType()134 	int GetType() const { return type; }
GetFadeIn()135 	unsigned int GetFadeIn() const { return fadeIn; }
GetFadeOut()136 	unsigned int GetFadeOut() const { return fadeOut; }
GetXFadeIn()137 	unsigned int GetXFadeIn() const { return xfadeIn; }
GetXFadeOut()138 	unsigned int GetXFadeOut() const { return xfadeOut; }
139 
140 	unsigned int GetBarsSamples(int bars);
GetSamplesCount()141 	unsigned int GetSamplesCount() const { return samplesCount; }
142 	unsigned int GetFilesSamples();
143 
144 	void SetLayerGain(std::string layer, float gain);
145 
SetPickable(bool value)146 	void SetPickable(bool value) { pickable = value; }
IsPickable()147 	bool IsPickable() const { return pickable; }
148 
149 	void SaveState(tinyxml2::XMLElement *node);
150 	void LoadState(tinyxml2::XMLElement *node);
151 
152 	void FreeMemory();
153 };
154 
155 #endif
156