1 /*************************************************************************/
2 /*  sample_player.h                                                      */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 #ifndef SAMPLE_PLAYER_H
31 #define SAMPLE_PLAYER_H
32 
33 #include "scene/main/node.h"
34 #include "scene/resources/sample_library.h"
35 
36 class SamplePlayer : public Node {
37 
38 	OBJ_TYPE(SamplePlayer, Node);
39 	OBJ_CATEGORY("Audio Nodes");
40 
41 public:
42 	enum FilterType {
43 		FILTER_NONE,
44 		FILTER_LOWPASS,
45 		FILTER_BANDPASS,
46 		FILTER_HIPASS,
47 		FILTER_NOTCH,
48 		FILTER_PEAK,
49 		FILTER_BANDLIMIT, ///< cutoff is LP resonace is HP
50 		FILTER_LOW_SHELF,
51 		FILTER_HIGH_SHELF,
52 	};
53 
54 	enum ReverbRoomType {
55 
56 		REVERB_SMALL,
57 		REVERB_MEDIUM,
58 		REVERB_LARGE,
59 		REVERB_HALL
60 	};
61 
62 	enum {
63 
64 		INVALID_VOICE_ID = 0xFFFFFFFF
65 	};
66 
67 	typedef uint32_t VoiceID;
68 
69 private:
70 	Ref<SampleLibrary> library;
71 
72 	struct Voice {
73 
74 		RID voice;
75 		uint32_t check;
76 		bool active;
77 
78 		int priority;
79 		int sample_mix_rate;
80 		int mix_rate;
81 		float volume;
82 		float pan;
83 		float pan_depth;
84 		float pan_height;
85 		FilterType filter_type;
86 		float filter_cutoff;
87 		float filter_resonance;
88 		float filter_gain;
89 		float chorus_send;
90 		ReverbRoomType reverb_room;
91 		float reverb_send;
92 
93 		void clear();
94 		Voice();
95 		~Voice();
96 	};
97 
98 	Vector<Voice> voices;
99 
100 	struct Default {
101 
102 		float reverb_send;
103 		float pitch_scale;
104 		float volume_db;
105 		float pan;
106 		float depth;
107 		float height;
108 		FilterType filter_type;
109 		float filter_cutoff;
110 		float filter_resonance;
111 		float filter_gain;
112 		float chorus_send;
113 		ReverbRoomType reverb_room;
114 
115 	} _default;
116 
117 	uint32_t last_id;
118 	uint16_t last_check;
119 	String played_back;
120 
121 protected:
122 	bool _set(const StringName &p_name, const Variant &p_value);
123 	bool _get(const StringName &p_name, Variant &r_ret) const;
124 	void _get_property_list(List<PropertyInfo> *p_list) const;
125 
126 	static void _bind_methods();
127 
128 public:
129 	void set_sample_library(const Ref<SampleLibrary> &p_library);
130 	Ref<SampleLibrary> get_sample_library() const;
131 
132 	void set_polyphony(int p_voice_count);
133 	int get_polyphony() const;
134 
135 	VoiceID play(const String &p_name, bool unique = false);
136 	void stop(VoiceID p_voice);
137 	void stop_all();
138 	bool is_voice_active(VoiceID) const;
139 	bool is_active() const;
140 
141 	void set_mix_rate(VoiceID p_voice, int p_mix_rate);
142 	void set_pitch_scale(VoiceID p_voice, float p_pitch_scale);
143 	void set_volume(VoiceID p_voice, float p_volume);
144 	void set_volume_db(VoiceID p_voice, float p_db);
145 	void set_pan(VoiceID p_voice, float p_pan, float p_pan_depth = 0, float p_pan_height = 0);
146 	void set_filter(VoiceID p_voice, FilterType p_filter, float p_cutoff, float p_resonance, float p_gain);
147 	void set_chorus(VoiceID p_voice, float p_send);
148 	void set_reverb(VoiceID p_voice, ReverbRoomType p_room, float p_send);
149 
150 	int get_mix_rate(VoiceID p_voice) const;
151 	float get_pitch_scale(VoiceID p_voice) const;
152 	float get_volume(VoiceID p_voice) const;
153 	float get_volume_db(VoiceID p_voice) const;
154 
155 	float get_pan(VoiceID p_voice) const;
156 	float get_pan_depth(VoiceID p_voice) const;
157 	float get_pan_height(VoiceID p_voice) const;
158 	FilterType get_filter_type(VoiceID p_voice) const;
159 	float get_filter_cutoff(VoiceID p_voice) const;
160 	float get_filter_resonance(VoiceID p_voice) const;
161 	float get_filter_gain(VoiceID p_voice) const;
162 	float get_chorus(VoiceID p_voice) const;
163 	ReverbRoomType get_reverb_room(VoiceID p_voice) const;
164 	float get_reverb(VoiceID p_voice) const;
165 
166 	void set_default_pitch_scale(float p_pitch_scale);
167 	void set_default_volume(float p_volume);
168 	void set_default_volume_db(float p_db);
169 	void set_default_pan(float p_pan, float p_pan_depth = 0, float p_pan_height = 0);
170 	void set_default_filter(FilterType p_filter, float p_cutoff, float p_resonance, float p_gain);
171 	void set_default_chorus(float p_send);
172 	void set_default_reverb(ReverbRoomType p_room, float p_send);
173 
174 	float get_default_volume() const;
175 	float get_default_volume_db() const;
176 	float get_default_pitch_scale() const;
177 	float get_default_pan() const;
178 	float get_default_pan_depth() const;
179 	float get_default_pan_height() const;
180 	FilterType get_default_filter_type() const;
181 	float get_default_filter_cutoff() const;
182 	float get_default_filter_resonance() const;
183 	float get_default_filter_gain() const;
184 	float get_default_chorus() const;
185 	ReverbRoomType get_default_reverb_room() const;
186 	float get_default_reverb() const;
187 
188 	String get_configuration_warning() const;
189 
190 	SamplePlayer();
191 	~SamplePlayer();
192 };
193 
194 VARIANT_ENUM_CAST(SamplePlayer::FilterType);
195 VARIANT_ENUM_CAST(SamplePlayer::ReverbRoomType);
196 
197 #endif // SAMPLE_PLAYER_H
198