|
@@ -48,26 +48,51 @@ AudioServer *AudioServer::get_singleton() {
|
|
|
|
|
|
void AudioServer::sample_set_signed_data(RID p_sample, const DVector<float>& p_buffer) {
|
|
|
|
|
|
+ SampleFormat format = sample_get_format(p_sample);
|
|
|
+
|
|
|
+ ERR_EXPLAIN("IMA ADPCM is not supported.");
|
|
|
+ ERR_FAIL_COND(format==SAMPLE_FORMAT_IMA_ADPCM);
|
|
|
+
|
|
|
int len = p_buffer.size();
|
|
|
ERR_FAIL_COND( len == 0 );
|
|
|
|
|
|
DVector<uint8_t> data;
|
|
|
- data.resize(len*2);
|
|
|
- DVector<uint8_t>::Write w=data.write();
|
|
|
-
|
|
|
- int16_t *samples = (int16_t*)w.ptr();
|
|
|
-
|
|
|
+ DVector<uint8_t>::Write w;
|
|
|
DVector<float>::Read r = p_buffer.read();
|
|
|
|
|
|
- for(int i=0;i<len;i++) {
|
|
|
-
|
|
|
- float sample = r[i];
|
|
|
- sample = Math::floor( sample * (1<<16) );
|
|
|
- if (sample<-32768)
|
|
|
- sample=-32768;
|
|
|
- else if (sample>32767)
|
|
|
- sample=32767;
|
|
|
- samples[i]=sample;
|
|
|
+ switch(format) {
|
|
|
+ case SAMPLE_FORMAT_PCM8: {
|
|
|
+ data.resize(len);
|
|
|
+ w=data.write();
|
|
|
+
|
|
|
+ int8_t *samples8 = (int8_t*)w.ptr();
|
|
|
+
|
|
|
+ for(int i=0;i<len;i++) {
|
|
|
+
|
|
|
+ float sample = Math::floor( r[i] * (1<<8) );
|
|
|
+ if (sample<-128)
|
|
|
+ sample=-128;
|
|
|
+ else if (sample>127)
|
|
|
+ sample=127;
|
|
|
+ samples8[i]=sample;
|
|
|
+ }
|
|
|
+ } break;
|
|
|
+ case SAMPLE_FORMAT_PCM16: {
|
|
|
+ data.resize(len*2);
|
|
|
+ w=data.write();
|
|
|
+
|
|
|
+ int16_t *samples16 = (int16_t*)w.ptr();
|
|
|
+
|
|
|
+ for(int i=0;i<len;i++) {
|
|
|
+
|
|
|
+ float sample = Math::floor( r[i] * (1<<16) );
|
|
|
+ if (sample<-32768)
|
|
|
+ sample=-32768;
|
|
|
+ else if (sample>32767)
|
|
|
+ sample=32767;
|
|
|
+ samples16[i]=sample;
|
|
|
+ }
|
|
|
+ } break;
|
|
|
}
|
|
|
|
|
|
w = DVector<uint8_t>::Write();
|