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 __OAMLTRACK_H__
24 #define __OAMLTRACK_H__
25 
26 class ByteBuffer;
27 class oamlAudio;
28 
29 class oamlTrack {
30 protected:
31 	bool verbose;
32 
33 	std::string name;
34 	std::vector<std::string> groups;
35 	std::vector<std::string> subgroups;
36 
37 	int lock;
38 	int fadeIn;
39 	int fadeOut;
40 	int xfadeIn;
41 	int xfadeOut;
42 	float volume;
43 
44 	int Random(int min, int max);
45 
46 	void ApplyVolPanTo(float *samlpes, int channels, float vol, float pan);
47 	float SafeAdd(float a, float b, bool debug);
48 	void MixAudio(oamlAudio *audio, float *samples, int channels, bool debug);
49 	unsigned int MixAudio(oamlAudio *audio, float *samples, int channels, bool debug, unsigned int pos);
50 
51 	oamlAudio* FindAudio(std::vector<oamlAudio*> *audios, std::string filename);
52 	oamlRC FindAudioAndRemove(std::vector<oamlAudio*> *audios, std::string filename);
53 	void ClearAudios(std::vector<oamlAudio*> *audios);
54 	void ReadAudiosInfo(std::vector<oamlAudio*> *audios, oamlTrackInfo *info);
55 	void FreeAudiosMemory(std::vector<oamlAudio*> *audios);
56 	void FillAudiosList(std::vector<oamlAudio*> *audios, std::vector<std::string>& list);
57 
58 	int GetFilesSamplesFor(std::vector<oamlAudio*> *audios);
59 	int LoadProgressFor(std::vector<oamlAudio*> *audios);
60 
61 public:
62 	oamlTrack();
63 	virtual ~oamlTrack();
64 
65 	void SetName(std::string trackName) { name = trackName; }
66 	void AddGroup(std::string trackGroup) { groups.push_back(trackGroup); }
67 	void AddSubgroup(std::string trackSubgroup) { subgroups.push_back(trackSubgroup); }
68 	void SetFadeIn(int trackFadeIn) { fadeIn = trackFadeIn; }
69 	void SetFadeOut(int trackFadeOut) { fadeOut = trackFadeOut; }
70 	void SetXFadeIn(int trackXFadeIn) { xfadeIn = trackXFadeIn; }
71 	void SetXFadeOut(int trackXFadeOut) { xfadeOut = trackXFadeOut; }
72 	void SetVolume(float trackVolume) { volume = trackVolume; }
73 
74 	virtual void ReadInfo(oamlTrackInfo *info);
75 
76 	const char *GetNameStr() const { return name.c_str(); }
77 	std::string GetName() const { return name; }
78 	std::vector<std::string> GetGroups() const { return groups; }
79 	std::vector<std::string> GetSubgroups() const { return subgroups; }
80 	bool HasGroup(std::string trackGroup) const { return std::find(groups.begin(), groups.end(), trackGroup) != groups.end(); }
81 	bool HasSubgroup(std::string trackSubgroup) const { return std::find(subgroups.begin(), subgroups.end(), trackSubgroup) != subgroups.end(); }
82 	int GetFadeIn() const { return fadeIn; }
83 	int GetFadeOut() const { return fadeOut; }
84 	int GetXFadeIn() const { return xfadeIn; }
85 	int GetXFadeOut() const { return xfadeOut; }
86 	float GetVolume() const { return volume; }
87 
88 	virtual void GetAudioList(std::vector<std::string>&) { }
89 	virtual void AddAudio(oamlAudio *) { }
90 	virtual oamlAudio* GetAudio(std::string) { return NULL; }
91 	virtual oamlRC RemoveAudio(std::string) { return OAML_NOT_FOUND; }
92 	virtual oamlRC Load() { return OAML_NOT_FOUND; }
93 	virtual float LoadProgress() { return -1.f; }
94 	virtual void Stop() { }
95 
96 	virtual bool IsPlaying() { return false; }
97 	void ShowPlaying();
98 	virtual std::string GetPlayingInfo() { return ""; }
99 
100 	virtual void Mix(float *, int, bool) { }
101 
102 	virtual void SetCondition(int, int) { }
103 
104 	virtual bool IsMusicTrack() const { return false; }
105 	virtual bool IsSfxTrack() const { return false; }
106 
107 	virtual void SetLayerGain(std::string, float) { }
108 
109 	virtual void FreeMemory() { }
110 };
111 
112 #endif
113