2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_stream.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* 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 "audio_stream.h"
|
|
|
|
|
2015-09-09 23:50:52 +02:00
|
|
|
//////////////////////////////
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
void AudioStreamPlaybackResampled::_begin_resample() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
//clear cubic interpolation history
|
|
|
|
internal_buffer[0]=AudioFrame(0.0,0.0);
|
|
|
|
internal_buffer[1]=AudioFrame(0.0,0.0);
|
|
|
|
internal_buffer[2]=AudioFrame(0.0,0.0);
|
|
|
|
internal_buffer[3]=AudioFrame(0.0,0.0);
|
|
|
|
//mix buffer
|
|
|
|
_mix_internal(internal_buffer+4,INTERNAL_BUFFER_LEN);
|
|
|
|
mix_offset=0;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
void AudioStreamPlaybackResampled::mix(AudioFrame* p_buffer,float p_rate_scale,int p_frames) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
float target_rate = AudioServer::get_singleton()->get_mix_rate() * p_rate_scale;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
uint64_t mix_increment = uint64_t((get_stream_sampling_rate() / double(target_rate)) * double( FP_LEN ));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
for(int i=0;i<p_frames;i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
uint32_t idx = CUBIC_INTERP_HISTORY + uint32_t(mix_offset >> FP_BITS);
|
|
|
|
//standard cubic interpolation (great quality/performance ratio)
|
|
|
|
//this used to be moved to a LUT for greater performance, but nowadays CPU speed is generally faster than memory.
|
|
|
|
float mu = (mix_offset&FP_MASK)/float(FP_LEN);
|
|
|
|
AudioFrame y0 = internal_buffer[idx-3];
|
|
|
|
AudioFrame y1 = internal_buffer[idx-2];
|
|
|
|
AudioFrame y2 = internal_buffer[idx-1];
|
|
|
|
AudioFrame y3 = internal_buffer[idx-0];
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
float mu2 = mu*mu;
|
|
|
|
AudioFrame a0 = y3 - y2 - y0 + y1;
|
|
|
|
AudioFrame a1 = y0 - y1 - a0;
|
|
|
|
AudioFrame a2 = y2 - y0;
|
|
|
|
AudioFrame a3 = y1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
p_buffer[i] = (a0*mu*mu2 + a1*mu2 + a2*mu + a3);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-21 23:00:25 +01:00
|
|
|
mix_offset+=mix_increment;
|
|
|
|
|
|
|
|
while ( (mix_offset >> FP_BITS) >= INTERNAL_BUFFER_LEN ) {
|
|
|
|
|
|
|
|
internal_buffer[0]=internal_buffer[INTERNAL_BUFFER_LEN+0];
|
|
|
|
internal_buffer[1]=internal_buffer[INTERNAL_BUFFER_LEN+1];
|
|
|
|
internal_buffer[2]=internal_buffer[INTERNAL_BUFFER_LEN+2];
|
|
|
|
internal_buffer[3]=internal_buffer[INTERNAL_BUFFER_LEN+3];
|
|
|
|
_mix_internal(internal_buffer+4,INTERNAL_BUFFER_LEN);
|
|
|
|
mix_offset-=(INTERNAL_BUFFER_LEN<<FP_BITS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|