1 /*************************************************************************/
2 /*  sample_manager_sw.cpp                                                */
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 #include "sample_manager_sw.h"
31 
32 #include "print_string.h"
33 
~SampleManagerSW()34 SampleManagerSW::~SampleManagerSW() {
35 }
36 
sample_create(AS::SampleFormat p_format,bool p_stereo,int p_length)37 RID SampleManagerMallocSW::sample_create(AS::SampleFormat p_format, bool p_stereo, int p_length) {
38 
39 	Sample *s = memnew(Sample);
40 	int datalen = p_length;
41 	if (p_format == AS::SAMPLE_FORMAT_PCM16)
42 		datalen *= 2;
43 	else if (p_format == AS::SAMPLE_FORMAT_IMA_ADPCM) {
44 		if (datalen & 1) {
45 			datalen++;
46 		}
47 		datalen /= 2;
48 		datalen += 4;
49 	}
50 
51 	if (p_stereo)
52 		datalen *= 2;
53 
54 #define SAMPLE_EXTRA 16
55 
56 	s->data = memalloc(datalen + SAMPLE_EXTRA); //help the interpolator by allocating a little more..
57 	for (int i = 0; i < SAMPLE_EXTRA; i++) {
58 
59 		uint8_t *data = (uint8_t *)s->data;
60 		data[datalen + i] = 0;
61 	}
62 	if (!s->data) {
63 
64 		memdelete(s);
65 		ERR_EXPLAIN("Cannot allocate sample of requested size.");
66 		ERR_FAIL_V(RID());
67 	}
68 
69 	s->format = p_format;
70 	s->length = p_length;
71 	s->length_bytes = datalen;
72 	s->stereo = p_stereo;
73 	s->loop_begin = 0;
74 	s->loop_end = 0;
75 	s->loop_format = AS::SAMPLE_LOOP_NONE;
76 	s->mix_rate = 44100;
77 
78 	AudioServer::get_singleton()->lock();
79 	RID rid = sample_owner.make_rid(s);
80 	AudioServer::get_singleton()->unlock();
81 
82 	return rid;
83 }
84 
sample_set_description(RID p_sample,const String & p_description)85 void SampleManagerMallocSW::sample_set_description(RID p_sample, const String &p_description) {
86 
87 	Sample *s = sample_owner.get(p_sample);
88 	ERR_FAIL_COND(!s);
89 
90 	s->description = p_description;
91 }
92 
sample_get_description(RID p_sample) const93 String SampleManagerMallocSW::sample_get_description(RID p_sample) const {
94 
95 	const Sample *s = sample_owner.get(p_sample);
96 	ERR_FAIL_COND_V(!s, String());
97 
98 	return s->description;
99 }
100 
sample_get_format(RID p_sample) const101 AS::SampleFormat SampleManagerMallocSW::sample_get_format(RID p_sample) const {
102 
103 	const Sample *s = sample_owner.get(p_sample);
104 	ERR_FAIL_COND_V(!s, AS::SAMPLE_FORMAT_PCM8);
105 
106 	return s->format;
107 }
108 
sample_is_stereo(RID p_sample) const109 bool SampleManagerMallocSW::sample_is_stereo(RID p_sample) const {
110 
111 	const Sample *s = sample_owner.get(p_sample);
112 	ERR_FAIL_COND_V(!s, false);
113 
114 	return s->stereo;
115 }
sample_get_length(RID p_sample) const116 int SampleManagerMallocSW::sample_get_length(RID p_sample) const {
117 
118 	const Sample *s = sample_owner.get(p_sample);
119 	ERR_FAIL_COND_V(!s, -1);
120 
121 	return s->length;
122 }
123 
sample_set_data(RID p_sample,const DVector<uint8_t> & p_buffer)124 void SampleManagerMallocSW::sample_set_data(RID p_sample, const DVector<uint8_t> &p_buffer) {
125 
126 	Sample *s = sample_owner.get(p_sample);
127 	ERR_FAIL_COND(!s);
128 
129 	int buff_size = p_buffer.size();
130 	ERR_FAIL_COND(buff_size == 0);
131 
132 	ERR_EXPLAIN("Sample buffer size does not match sample size.");
133 	//print_line("len bytes: "+itos(s->length_bytes)+" bufsize: "+itos(buff_size));
134 	ERR_FAIL_COND(s->length_bytes != buff_size);
135 	DVector<uint8_t>::Read buffer_r = p_buffer.read();
136 	const uint8_t *src = buffer_r.ptr();
137 	uint8_t *dst = (uint8_t *)s->data;
138 	//print_line("set data: "+itos(s->length_bytes));
139 
140 	for (int i = 0; i < s->length_bytes; i++) {
141 
142 		dst[i] = src[i];
143 	}
144 
145 	switch (s->format) {
146 
147 		case AS::SAMPLE_FORMAT_PCM8: {
148 
149 			if (s->stereo) {
150 				dst[s->length] = dst[s->length - 2];
151 				dst[s->length + 1] = dst[s->length - 1];
152 			} else {
153 
154 				dst[s->length] = dst[s->length - 1];
155 			}
156 
157 		} break;
158 		case AS::SAMPLE_FORMAT_PCM16: {
159 
160 			if (s->stereo) {
161 				dst[s->length] = dst[s->length - 4];
162 				dst[s->length + 1] = dst[s->length - 3];
163 				dst[s->length + 2] = dst[s->length - 2];
164 				dst[s->length + 3] = dst[s->length - 1];
165 			} else {
166 
167 				dst[s->length] = dst[s->length - 2];
168 				dst[s->length + 1] = dst[s->length - 1];
169 			}
170 
171 		} break;
172 	}
173 }
174 
sample_get_data(RID p_sample) const175 const DVector<uint8_t> SampleManagerMallocSW::sample_get_data(RID p_sample) const {
176 
177 	Sample *s = sample_owner.get(p_sample);
178 	ERR_FAIL_COND_V(!s, DVector<uint8_t>());
179 
180 	DVector<uint8_t> ret_buffer;
181 	ret_buffer.resize(s->length_bytes);
182 	DVector<uint8_t>::Write buffer_w = ret_buffer.write();
183 	uint8_t *dst = buffer_w.ptr();
184 	const uint8_t *src = (const uint8_t *)s->data;
185 
186 	for (int i = 0; i < s->length_bytes; i++) {
187 
188 		dst[i] = src[i];
189 	}
190 
191 	buffer_w = DVector<uint8_t>::Write(); //unlock
192 
193 	return ret_buffer;
194 }
195 
sample_get_data_ptr(RID p_sample) const196 void *SampleManagerMallocSW::sample_get_data_ptr(RID p_sample) const {
197 
198 	const Sample *s = sample_owner.get(p_sample);
199 	ERR_FAIL_COND_V(!s, NULL);
200 
201 	return s->data;
202 }
203 
sample_set_mix_rate(RID p_sample,int p_rate)204 void SampleManagerMallocSW::sample_set_mix_rate(RID p_sample, int p_rate) {
205 
206 	ERR_FAIL_COND(p_rate < 1);
207 
208 	Sample *s = sample_owner.get(p_sample);
209 	ERR_FAIL_COND(!s);
210 
211 	s->mix_rate = p_rate;
212 }
sample_get_mix_rate(RID p_sample) const213 int SampleManagerMallocSW::sample_get_mix_rate(RID p_sample) const {
214 
215 	const Sample *s = sample_owner.get(p_sample);
216 	ERR_FAIL_COND_V(!s, -1);
217 
218 	return s->mix_rate;
219 }
sample_set_loop_format(RID p_sample,AS::SampleLoopFormat p_format)220 void SampleManagerMallocSW::sample_set_loop_format(RID p_sample, AS::SampleLoopFormat p_format) {
221 
222 	Sample *s = sample_owner.get(p_sample);
223 	ERR_FAIL_COND(!s);
224 
225 	s->loop_format = p_format;
226 }
sample_get_loop_format(RID p_sample) const227 AS::SampleLoopFormat SampleManagerMallocSW::sample_get_loop_format(RID p_sample) const {
228 
229 	const Sample *s = sample_owner.get(p_sample);
230 	ERR_FAIL_COND_V(!s, AS::SAMPLE_LOOP_NONE);
231 
232 	return s->loop_format;
233 }
234 
sample_set_loop_begin(RID p_sample,int p_pos)235 void SampleManagerMallocSW::sample_set_loop_begin(RID p_sample, int p_pos) {
236 
237 	Sample *s = sample_owner.get(p_sample);
238 	ERR_FAIL_COND(!s);
239 	ERR_FAIL_INDEX(p_pos, s->length);
240 
241 	s->loop_begin = p_pos;
242 }
sample_get_loop_begin(RID p_sample) const243 int SampleManagerMallocSW::sample_get_loop_begin(RID p_sample) const {
244 
245 	const Sample *s = sample_owner.get(p_sample);
246 	ERR_FAIL_COND_V(!s, -1);
247 
248 	return s->loop_begin;
249 }
250 
sample_set_loop_end(RID p_sample,int p_pos)251 void SampleManagerMallocSW::sample_set_loop_end(RID p_sample, int p_pos) {
252 
253 	Sample *s = sample_owner.get(p_sample);
254 	ERR_FAIL_COND(!s);
255 	if (p_pos > s->length)
256 		p_pos = s->length;
257 	s->loop_end = p_pos;
258 }
sample_get_loop_end(RID p_sample) const259 int SampleManagerMallocSW::sample_get_loop_end(RID p_sample) const {
260 
261 	const Sample *s = sample_owner.get(p_sample);
262 	ERR_FAIL_COND_V(!s, -1);
263 
264 	return s->loop_end;
265 }
266 
is_sample(RID p_sample) const267 bool SampleManagerMallocSW::is_sample(RID p_sample) const {
268 
269 	return sample_owner.owns(p_sample);
270 }
free(RID p_sample)271 void SampleManagerMallocSW::free(RID p_sample) {
272 
273 	Sample *s = sample_owner.get(p_sample);
274 	ERR_FAIL_COND(!s);
275 	AudioServer::get_singleton()->lock();
276 	sample_owner.free(p_sample);
277 	AudioServer::get_singleton()->unlock();
278 
279 	memfree(s->data);
280 	memdelete(s);
281 }
282 
SampleManagerMallocSW()283 SampleManagerMallocSW::SampleManagerMallocSW() {
284 }
285 
~SampleManagerMallocSW()286 SampleManagerMallocSW::~SampleManagerMallocSW() {
287 
288 	// check for sample leakage
289 	List<RID> owned_list;
290 	sample_owner.get_owned_list(&owned_list);
291 
292 	while (owned_list.size()) {
293 
294 		Sample *s = sample_owner.get(owned_list.front()->get());
295 		String err = "Leaked sample of size: " + itos(s->length_bytes) + " description: " + s->description;
296 		ERR_PRINT(err.utf8().get_data());
297 		free(owned_list.front()->get());
298 		owned_list.pop_front();
299 	}
300 }
301