summaryrefslogtreecommitdiff
path: root/servers/audio/sample_manager_sw.cpp
blob: 5fa3c834c465ee927a2c27063f327de2dc54b35d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*************************************************************************/
/*  sample_manager_sw.cpp                                                */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur.                 */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#include "sample_manager_sw.h"

#include "print_string.h"

SampleManagerSW::~SampleManagerSW()
{
}



RID SampleManagerMallocSW::sample_create(AS::SampleFormat p_format, bool p_stereo, int p_length) {

	ERR_EXPLAIN("IMA-ADPCM and STEREO are not a valid combination for sample format.");
	ERR_FAIL_COND_V( p_format == AS::SAMPLE_FORMAT_IMA_ADPCM && p_stereo,RID());
	Sample *s = memnew( Sample );
	int datalen = p_length;
	if (p_stereo)
		datalen*=2;
	if (p_format==AS::SAMPLE_FORMAT_PCM16)
		datalen*=2;
	else if (p_format==AS::SAMPLE_FORMAT_IMA_ADPCM) {
		if (datalen&1) {
			datalen++;
		}
		datalen/=2;
		datalen+=4;
	}
#define SAMPLE_EXTRA 16

	s->data = memalloc(datalen+SAMPLE_EXTRA); //help the interpolator by allocating a little more..
	for(int i=0;i<SAMPLE_EXTRA;i++) {

		uint8_t *data = (uint8_t*)s->data;
		data[datalen+i]=0;
	}
	if (!s->data) {

		memdelete(s);
		ERR_EXPLAIN("Cannot allocate sample of requested size.");
		ERR_FAIL_V(RID());
	}

	s->format=p_format;
	s->length=p_length;
	s->length_bytes=datalen;
	s->stereo=p_stereo;
	s->loop_begin=0;
	s->loop_end=0;
	s->loop_format=AS::SAMPLE_LOOP_NONE;
	s->mix_rate=44100;

	AudioServer::get_singleton()->lock();
	RID rid = sample_owner.make_rid(s);
	AudioServer::get_singleton()->unlock();

	return rid;
}

void SampleManagerMallocSW::sample_set_description(RID p_sample, const String& p_description) {

	Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND(!s);

	s->description=p_description;
}

String SampleManagerMallocSW::sample_get_description(RID p_sample) const {

	const Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND_V(!s,String());

	return s->description;
}


AS::SampleFormat SampleManagerMallocSW::sample_get_format(RID p_sample) const {

	const Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND_V(!s,AS::SAMPLE_FORMAT_PCM8);

	return s->format;
}

bool SampleManagerMallocSW::sample_is_stereo(RID p_sample) const {

	const Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND_V(!s,false);

	return s->stereo;

}
int SampleManagerMallocSW::sample_get_length(RID p_sample) const {

	const Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND_V(!s,-1);

	return s->length;
}

void SampleManagerMallocSW::sample_set_data(RID p_sample, const DVector<uint8_t>& p_buffer) {

	Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND(!s);

	int buff_size=p_buffer.size();
	ERR_FAIL_COND(buff_size==0);


	ERR_EXPLAIN("Sample buffer size does not match sample size.");
	ERR_FAIL_COND(s->length_bytes!=buff_size);
	DVector<uint8_t>::Read buffer_r=p_buffer.read();
	const uint8_t *src = buffer_r.ptr();
	uint8_t *dst = (uint8_t*)s->data;
	//print_line("set data: "+itos(s->length_bytes));

	for(int i=0;i<s->length_bytes;i++) {

		dst[i]=src[i];
	}

	switch(s->format) {

		case AS::SAMPLE_FORMAT_PCM8: {

			if (s->stereo) {
				dst[s->length]=dst[s->length-2];
				dst[s->length+1]=dst[s->length-1];
			} else {

				dst[s->length]=dst[s->length-1];
			}

		} break;
		case AS::SAMPLE_FORMAT_PCM16: {

			if (s->stereo) {
				dst[s->length]=dst[s->length-4];
				dst[s->length+1]=dst[s->length-3];
				dst[s->length+2]=dst[s->length-2];
				dst[s->length+3]=dst[s->length-1];
			} else {

				dst[s->length]=dst[s->length-2];
				dst[s->length+1]=dst[s->length-1];
			}

		} break;

	}



}

const DVector<uint8_t> SampleManagerMallocSW::sample_get_data(RID p_sample) const {

	Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND_V(!s,DVector<uint8_t>());

	DVector<uint8_t> ret_buffer;
	ret_buffer.resize(s->length_bytes);
	DVector<uint8_t>::Write buffer_w=ret_buffer.write();
	uint8_t *dst = buffer_w.ptr();
	const uint8_t *src = (const uint8_t*)s->data;

	for(int i=0;i<s->length_bytes;i++) {

		dst[i]=src[i];
	}

	buffer_w = DVector<uint8_t>::Write(); //unlock

	return ret_buffer;
}

void *SampleManagerMallocSW::sample_get_data_ptr(RID p_sample) const {

	const Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND_V(!s,NULL);

	return s->data;

}

void SampleManagerMallocSW::sample_set_mix_rate(RID p_sample,int p_rate) {

	ERR_FAIL_COND(p_rate<1);

	Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND(!s);

	s->mix_rate=p_rate;


}
int SampleManagerMallocSW::sample_get_mix_rate(RID p_sample) const {

	const Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND_V(!s,-1);

	return s->mix_rate;

}
void SampleManagerMallocSW::sample_set_loop_format(RID p_sample,AS::SampleLoopFormat p_format) {

	Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND(!s);

	s->loop_format=p_format;

}
AS::SampleLoopFormat SampleManagerMallocSW::sample_get_loop_format(RID p_sample) const {

	const Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND_V(!s,AS::SAMPLE_LOOP_NONE);

	return s->loop_format;
}

void SampleManagerMallocSW::sample_set_loop_begin(RID p_sample,int p_pos) {

	Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND(!s);
	ERR_FAIL_INDEX(p_pos,s->length);

	s->loop_begin=p_pos;
}
int SampleManagerMallocSW::sample_get_loop_begin(RID p_sample) const {

	const Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND_V(!s,-1);

	return s->loop_begin;
}

void SampleManagerMallocSW::sample_set_loop_end(RID p_sample,int p_pos) {

	Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND(!s);
	if (p_pos>s->length)
		p_pos=s->length;
	s->loop_end=p_pos;

}
int SampleManagerMallocSW::sample_get_loop_end(RID p_sample) const {

	const Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND_V(!s,-1);

	return s->loop_end;
}

bool SampleManagerMallocSW::is_sample(RID p_sample) const {

	return sample_owner.owns(p_sample);

}
void SampleManagerMallocSW::free(RID p_sample) {

	Sample *s = sample_owner.get(p_sample);
	ERR_FAIL_COND(!s);
	AudioServer::get_singleton()->lock();
	sample_owner.free(p_sample);
	AudioServer::get_singleton()->unlock();

	memfree(s->data);
	memdelete(s);

}

SampleManagerMallocSW::SampleManagerMallocSW() {


}

SampleManagerMallocSW::~SampleManagerMallocSW() {

	// check for sample leakage
	List<RID> owned_list;
	sample_owner.get_owned_list(&owned_list);

	while(owned_list.size()) {

		Sample *s = sample_owner.get(owned_list.front()->get());
		String err="Leaked sample of size: "+itos(s->length_bytes)+" description: "+s->description;
		ERR_PRINT(err.utf8().get_data());
		free(owned_list.front()->get());
		owned_list.pop_front();
	}

}