virtualx-engine/scene/resources/audio_stream_sample.cpp

664 lines
20 KiB
C++
Raw Permalink Normal View History

/**************************************************************************/
/* audio_stream_sample.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 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. */
/**************************************************************************/
2017-02-03 02:51:26 +01:00
#include "audio_stream_sample.h"
#include "core/io/marshalls.h"
#include "core/os/file_access.h"
2017-02-03 02:51:26 +01:00
void AudioStreamPlaybackSample::start(float p_from_pos) {
if (base->format == AudioStreamSample::FORMAT_IMA_ADPCM) {
//no seeking in IMA_ADPCM
for (int i = 0; i < 2; i++) {
ima_adpcm[i].step_index = 0;
ima_adpcm[i].predictor = 0;
ima_adpcm[i].loop_step_index = 0;
ima_adpcm[i].loop_predictor = 0;
ima_adpcm[i].last_nibble = -1;
ima_adpcm[i].loop_pos = 0x7FFFFFFF;
ima_adpcm[i].window_ofs = 0;
}
offset = 0;
} else {
seek(p_from_pos);
2017-02-03 02:51:26 +01:00
}
sign = 1;
active = true;
2017-02-03 02:51:26 +01:00
}
void AudioStreamPlaybackSample::stop() {
active = false;
2017-02-03 02:51:26 +01:00
}
bool AudioStreamPlaybackSample::is_playing() const {
return active;
}
int AudioStreamPlaybackSample::get_loop_count() const {
return 0;
}
float AudioStreamPlaybackSample::get_playback_position() const {
return float(offset >> MIX_FRAC_BITS) / base->mix_rate;
2017-02-03 02:51:26 +01:00
}
void AudioStreamPlaybackSample::seek(float p_time) {
if (base->format == AudioStreamSample::FORMAT_IMA_ADPCM) {
2017-02-03 02:51:26 +01:00
return; //no seeking in ima-adpcm
}
2017-02-03 02:51:26 +01:00
2018-01-22 20:35:33 +01:00
float max = base->get_length();
if (p_time < 0) {
p_time = 0;
} else if (p_time >= max) {
p_time = max - 0.001;
2017-02-03 02:51:26 +01:00
}
offset = uint64_t(p_time * base->mix_rate) << MIX_FRAC_BITS;
2017-02-03 02:51:26 +01:00
}
template <class Depth, bool is_stereo, bool is_ima_adpcm>
void AudioStreamPlaybackSample::do_resample(const Depth *p_src, AudioFrame *p_dst, int64_t &offset, int32_t &increment, uint32_t amount, IMA_ADPCM_State *ima_adpcm) {
2017-02-03 02:51:26 +01:00
// this function will be compiled branchless by any decent compiler
int32_t final, final_r, next, next_r;
while (amount) {
amount--;
int64_t pos = offset >> MIX_FRAC_BITS;
if (is_stereo && !is_ima_adpcm) {
pos <<= 1;
}
2017-02-03 02:51:26 +01:00
if (is_ima_adpcm) {
int64_t sample_pos = pos + ima_adpcm[0].window_ofs;
while (sample_pos > ima_adpcm[0].last_nibble) {
2017-02-03 02:51:26 +01:00
static const int16_t _ima_adpcm_step_table[89] = {
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
};
static const int8_t _ima_adpcm_index_table[16] = {
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8
};
for (int i = 0; i < (is_stereo ? 2 : 1); i++) {
int16_t nibble, diff, step;
2017-02-03 02:51:26 +01:00
ima_adpcm[i].last_nibble++;
const uint8_t *src_ptr = (const uint8_t *)base->data;
src_ptr += AudioStreamSample::DATA_PAD;
2017-02-03 02:51:26 +01:00
uint8_t nbb = src_ptr[(ima_adpcm[i].last_nibble >> 1) * (is_stereo ? 2 : 1) + i];
nibble = (ima_adpcm[i].last_nibble & 1) ? (nbb >> 4) : (nbb & 0xF);
step = _ima_adpcm_step_table[ima_adpcm[i].step_index];
2017-02-03 02:51:26 +01:00
ima_adpcm[i].step_index += _ima_adpcm_index_table[nibble];
if (ima_adpcm[i].step_index < 0) {
ima_adpcm[i].step_index = 0;
}
if (ima_adpcm[i].step_index > 88) {
ima_adpcm[i].step_index = 88;
}
2017-02-03 02:51:26 +01:00
diff = step >> 3;
if (nibble & 1) {
diff += step >> 2;
}
if (nibble & 2) {
diff += step >> 1;
}
if (nibble & 4) {
diff += step;
}
if (nibble & 8) {
diff = -diff;
}
2017-02-03 02:51:26 +01:00
ima_adpcm[i].predictor += diff;
if (ima_adpcm[i].predictor < -0x8000) {
ima_adpcm[i].predictor = -0x8000;
} else if (ima_adpcm[i].predictor > 0x7FFF) {
ima_adpcm[i].predictor = 0x7FFF;
}
2017-02-03 02:51:26 +01:00
/* store loop if there */
if (ima_adpcm[i].last_nibble == ima_adpcm[i].loop_pos) {
2017-02-03 02:51:26 +01:00
ima_adpcm[i].loop_step_index = ima_adpcm[i].step_index;
ima_adpcm[i].loop_predictor = ima_adpcm[i].predictor;
}
//printf("%i - %i - pred %i\n",int(ima_adpcm[i].last_nibble),int(nibble),int(ima_adpcm[i].predictor));
}
}
final = ima_adpcm[0].predictor;
2017-02-03 02:51:26 +01:00
if (is_stereo) {
final_r = ima_adpcm[1].predictor;
2017-02-03 02:51:26 +01:00
}
} else {
final = p_src[pos];
if (is_stereo) {
final_r = p_src[pos + 1];
}
2017-02-03 02:51:26 +01:00
if (sizeof(Depth) == 1) { /* conditions will not exist anymore when compiled! */
final <<= 8;
if (is_stereo) {
final_r <<= 8;
}
2017-02-03 02:51:26 +01:00
}
if (is_stereo) {
next = p_src[pos + 2];
next_r = p_src[pos + 3];
2017-02-03 02:51:26 +01:00
} else {
next = p_src[pos + 1];
2017-02-03 02:51:26 +01:00
}
if (sizeof(Depth) == 1) {
next <<= 8;
if (is_stereo) {
next_r <<= 8;
}
2017-02-03 02:51:26 +01:00
}
int32_t frac = int64_t(offset & MIX_FRAC_MASK);
2017-02-03 02:51:26 +01:00
final = final + ((next - final) * frac >> MIX_FRAC_BITS);
if (is_stereo) {
final_r = final_r + ((next_r - final_r) * frac >> MIX_FRAC_BITS);
}
2017-02-03 02:51:26 +01:00
}
if (!is_stereo) {
final_r = final; //copy to right channel if stereo
2017-02-03 02:51:26 +01:00
}
p_dst->l = final / 32767.0;
p_dst->r = final_r / 32767.0;
2017-02-03 02:51:26 +01:00
p_dst++;
offset += increment;
2017-02-03 02:51:26 +01:00
}
}
void AudioStreamPlaybackSample::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
2017-02-03 02:51:26 +01:00
if (!base->data || !active) {
for (int i = 0; i < p_frames; i++) {
p_buffer[i] = AudioFrame(0, 0);
2017-02-03 02:51:26 +01:00
}
return;
}
int len = base->data_bytes;
switch (base->format) {
case AudioStreamSample::FORMAT_8_BITS:
len /= 1;
break;
case AudioStreamSample::FORMAT_16_BITS:
len /= 2;
break;
case AudioStreamSample::FORMAT_IMA_ADPCM:
len *= 2;
break;
2017-02-03 02:51:26 +01:00
}
if (base->stereo) {
len /= 2;
2017-02-03 02:51:26 +01:00
}
/* some 64-bit fixed point precaches */
int64_t loop_begin_fp = ((int64_t)base->loop_begin << MIX_FRAC_BITS);
int64_t loop_end_fp = ((int64_t)base->loop_end << MIX_FRAC_BITS);
int64_t length_fp = ((int64_t)len << MIX_FRAC_BITS);
int64_t begin_limit = (base->loop_mode != AudioStreamSample::LOOP_DISABLED) ? loop_begin_fp : 0;
int64_t end_limit = (base->loop_mode != AudioStreamSample::LOOP_DISABLED) ? loop_end_fp : length_fp;
bool is_stereo = base->stereo;
2017-02-03 02:51:26 +01:00
int32_t todo = p_frames;
2017-02-03 02:51:26 +01:00
if (base->loop_mode == AudioStreamSample::LOOP_BACKWARD) {
sign = -1;
}
float global_rate_scale = AudioServer::get_singleton()->get_global_rate_scale();
float base_rate = AudioServer::get_singleton()->get_mix_rate() * global_rate_scale;
2017-02-03 02:51:26 +01:00
float srate = base->mix_rate;
srate *= p_rate_scale;
2017-02-03 02:51:26 +01:00
float fincrement = srate / base_rate;
int32_t increment = int32_t(MAX(fincrement * MIX_FRAC_LEN, 1));
increment *= sign;
2017-02-03 02:51:26 +01:00
//looping
AudioStreamSample::LoopMode loop_format = base->loop_mode;
2017-02-03 02:51:26 +01:00
AudioStreamSample::Format format = base->format;
/* audio data */
uint8_t *dataptr = (uint8_t *)base->data;
const void *data = dataptr + AudioStreamSample::DATA_PAD;
AudioFrame *dst_buff = p_buffer;
2017-02-03 02:51:26 +01:00
if (format == AudioStreamSample::FORMAT_IMA_ADPCM) {
if (loop_format != AudioStreamSample::LOOP_DISABLED) {
ima_adpcm[0].loop_pos = loop_begin_fp >> MIX_FRAC_BITS;
ima_adpcm[1].loop_pos = loop_begin_fp >> MIX_FRAC_BITS;
loop_format = AudioStreamSample::LOOP_FORWARD;
2017-02-03 02:51:26 +01:00
}
}
while (todo > 0) {
int64_t limit = 0;
int32_t target = 0, aux = 0;
2017-02-03 02:51:26 +01:00
/** LOOP CHECKING **/
if (increment < 0) {
2017-02-03 02:51:26 +01:00
/* going backwards */
if (loop_format != AudioStreamSample::LOOP_DISABLED && offset < loop_begin_fp) {
2017-02-03 02:51:26 +01:00
/* loopstart reached */
if (loop_format == AudioStreamSample::LOOP_PING_PONG) {
2017-02-03 02:51:26 +01:00
/* bounce ping pong */
offset = loop_begin_fp + (loop_begin_fp - offset);
increment = -increment;
sign *= -1;
2017-02-03 02:51:26 +01:00
} else {
/* go to loop-end */
offset = loop_end_fp - (loop_begin_fp - offset);
2017-02-03 02:51:26 +01:00
}
} else {
/* check for sample not reaching beginning */
if (offset < 0) {
active = false;
2017-02-03 02:51:26 +01:00
break;
}
}
} else {
/* going forward */
if (loop_format != AudioStreamSample::LOOP_DISABLED && offset >= loop_end_fp) {
2017-02-03 02:51:26 +01:00
/* loopend reached */
if (loop_format == AudioStreamSample::LOOP_PING_PONG) {
2017-02-03 02:51:26 +01:00
/* bounce ping pong */
offset = loop_end_fp - (offset - loop_end_fp);
increment = -increment;
sign *= -1;
2017-02-03 02:51:26 +01:00
} else {
/* go to loop-begin */
if (format == AudioStreamSample::FORMAT_IMA_ADPCM) {
for (int i = 0; i < 2; i++) {
ima_adpcm[i].step_index = ima_adpcm[i].loop_step_index;
ima_adpcm[i].predictor = ima_adpcm[i].loop_predictor;
ima_adpcm[i].last_nibble = loop_begin_fp >> MIX_FRAC_BITS;
2017-02-03 02:51:26 +01:00
}
offset = loop_begin_fp;
2017-02-03 02:51:26 +01:00
} else {
offset = loop_begin_fp + (offset - loop_end_fp);
2017-02-03 02:51:26 +01:00
}
}
} else {
/* no loop, check for end of sample */
if (offset >= length_fp) {
active = false;
2017-02-03 02:51:26 +01:00
break;
}
}
}
/** MIXCOUNT COMPUTING **/
/* next possible limit (looppoints or sample begin/end */
limit = (increment < 0) ? begin_limit : end_limit;
2017-02-03 02:51:26 +01:00
/* compute what is shorter, the todo or the limit? */
aux = (limit - offset) / increment + 1;
target = (aux < todo) ? aux : todo; /* mix target is the shorter buffer */
2017-02-03 02:51:26 +01:00
/* check just in case */
if (target <= 0) {
active = false;
2017-02-03 02:51:26 +01:00
break;
}
todo -= target;
2017-02-03 02:51:26 +01:00
switch (base->format) {
2017-02-03 02:51:26 +01:00
case AudioStreamSample::FORMAT_8_BITS: {
if (is_stereo) {
do_resample<int8_t, true, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
} else {
do_resample<int8_t, false, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
}
2017-02-03 02:51:26 +01:00
} break;
case AudioStreamSample::FORMAT_16_BITS: {
if (is_stereo) {
do_resample<int16_t, true, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm);
} else {
do_resample<int16_t, false, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm);
}
2017-02-03 02:51:26 +01:00
} break;
case AudioStreamSample::FORMAT_IMA_ADPCM: {
if (is_stereo) {
do_resample<int8_t, true, true>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
} else {
do_resample<int8_t, false, true>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm);
}
2017-02-03 02:51:26 +01:00
} break;
}
dst_buff += target;
2017-02-03 02:51:26 +01:00
}
if (todo) {
//bit was missing from mix
int todo_ofs = p_frames - todo;
for (int i = todo_ofs; i < p_frames; i++) {
p_buffer[i] = AudioFrame(0, 0);
}
}
2017-02-03 02:51:26 +01:00
}
AudioStreamPlaybackSample::AudioStreamPlaybackSample() {
active = false;
offset = 0;
sign = 1;
2017-02-03 02:51:26 +01:00
}
/////////////////////
void AudioStreamSample::set_format(Format p_format) {
format = p_format;
2017-02-03 02:51:26 +01:00
}
AudioStreamSample::Format AudioStreamSample::get_format() const {
2017-02-03 02:51:26 +01:00
return format;
}
void AudioStreamSample::set_loop_mode(LoopMode p_loop_mode) {
loop_mode = p_loop_mode;
2017-02-03 02:51:26 +01:00
}
AudioStreamSample::LoopMode AudioStreamSample::get_loop_mode() const {
2017-02-03 02:51:26 +01:00
return loop_mode;
}
void AudioStreamSample::set_loop_begin(int p_frame) {
loop_begin = p_frame;
2017-02-03 02:51:26 +01:00
}
int AudioStreamSample::get_loop_begin() const {
2017-02-03 02:51:26 +01:00
return loop_begin;
}
void AudioStreamSample::set_loop_end(int p_frame) {
loop_end = p_frame;
2017-02-03 02:51:26 +01:00
}
int AudioStreamSample::get_loop_end() const {
2017-02-03 02:51:26 +01:00
return loop_end;
}
void AudioStreamSample::set_mix_rate(int p_hz) {
ERR_FAIL_COND(p_hz == 0);
mix_rate = p_hz;
2017-02-03 02:51:26 +01:00
}
int AudioStreamSample::get_mix_rate() const {
2017-02-03 02:51:26 +01:00
return mix_rate;
}
void AudioStreamSample::set_stereo(bool p_enable) {
stereo = p_enable;
2017-02-03 02:51:26 +01:00
}
bool AudioStreamSample::is_stereo() const {
2017-02-03 02:51:26 +01:00
return stereo;
}
2018-01-22 20:35:33 +01:00
float AudioStreamSample::get_length() const {
int len = data_bytes;
switch (format) {
case AudioStreamSample::FORMAT_8_BITS:
len /= 1;
break;
case AudioStreamSample::FORMAT_16_BITS:
len /= 2;
break;
case AudioStreamSample::FORMAT_IMA_ADPCM:
len *= 2;
break;
2018-01-22 20:35:33 +01:00
}
if (stereo) {
len /= 2;
}
return float(len) / mix_rate;
}
void AudioStreamSample::set_data(const PoolVector<uint8_t> &p_data) {
2017-02-03 02:51:26 +01:00
AudioServer::get_singleton()->lock();
if (data) {
AudioServer::get_singleton()->audio_data_free(data);
data = nullptr;
data_bytes = 0;
2017-02-03 02:51:26 +01:00
}
int datalen = p_data.size();
if (datalen) {
PoolVector<uint8_t>::Read r = p_data.read();
int alloc_len = datalen + DATA_PAD * 2;
2017-02-03 02:51:26 +01:00
data = AudioServer::get_singleton()->audio_data_alloc(alloc_len); //alloc with some padding for interpolation
memset(data, 0, alloc_len);
uint8_t *dataptr = (uint8_t *)data;
memcpy(dataptr + DATA_PAD, r.ptr(), datalen);
data_bytes = datalen;
2017-02-03 02:51:26 +01:00
}
AudioServer::get_singleton()->unlock();
}
PoolVector<uint8_t> AudioStreamSample::get_data() const {
2017-02-03 02:51:26 +01:00
PoolVector<uint8_t> pv;
if (data) {
pv.resize(data_bytes);
{
PoolVector<uint8_t>::Write w = pv.write();
uint8_t *dataptr = (uint8_t *)data;
memcpy(w.ptr(), dataptr + DATA_PAD, data_bytes);
2017-02-03 02:51:26 +01:00
}
}
return pv;
}
Error AudioStreamSample::save_to_wav(const String &p_path) {
if (format == AudioStreamSample::FORMAT_IMA_ADPCM) {
2021-06-18 13:26:58 +02:00
WARN_PRINT("Saving IMA_ADPC samples are not supported yet");
return ERR_UNAVAILABLE;
}
int sub_chunk_2_size = data_bytes; //Subchunk2Size = Size of data in bytes
// Format code
// 1:PCM format (for 8 or 16 bit)
// 3:IEEE float format
int format_code = (format == FORMAT_IMA_ADPCM) ? 3 : 1;
int n_channels = stereo ? 2 : 1;
long sample_rate = mix_rate;
int byte_pr_sample = 0;
switch (format) {
case AudioStreamSample::FORMAT_8_BITS:
byte_pr_sample = 1;
break;
case AudioStreamSample::FORMAT_16_BITS:
byte_pr_sample = 2;
break;
case AudioStreamSample::FORMAT_IMA_ADPCM:
byte_pr_sample = 4;
break;
}
String file_path = p_path;
if (!(file_path.substr(file_path.length() - 4, 4) == ".wav")) {
file_path += ".wav";
}
FileAccessRef file = FileAccess::open(file_path, FileAccess::WRITE); //Overrides existing file if present
ERR_FAIL_COND_V(!file, ERR_FILE_CANT_WRITE);
// Create WAV Header
file->store_string("RIFF"); //ChunkID
file->store_32(sub_chunk_2_size + 36); //ChunkSize = 36 + SubChunk2Size (size of entire file minus the 8 bits for this and previous header)
file->store_string("WAVE"); //Format
file->store_string("fmt "); //Subchunk1ID
file->store_32(16); //Subchunk1Size = 16
file->store_16(format_code); //AudioFormat
file->store_16(n_channels); //Number of Channels
file->store_32(sample_rate); //SampleRate
file->store_32(sample_rate * n_channels * byte_pr_sample); //ByteRate
file->store_16(n_channels * byte_pr_sample); //BlockAlign = NumChannels * BytePrSample
file->store_16(byte_pr_sample * 8); //BitsPerSample
file->store_string("data"); //Subchunk2ID
file->store_32(sub_chunk_2_size); //Subchunk2Size
// Add data
PoolVector<uint8_t> data = get_data();
PoolVector<uint8_t>::Read read_data = data.read();
switch (format) {
case AudioStreamSample::FORMAT_8_BITS:
Fix warnings for comparison between signed and unsigned integers [-Wsign-compare] Also turn off -Wsign-compare warnings in the future, we do not consider them important. Fixes the following GCC 5 warnings: ``` core/node_path.cpp:279:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] core/oa_hash_map.h:169:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] core/oa_hash_map.h:314:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/gles2/shader_gles2.cpp:985:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/gles3/rasterizer_storage_gles3.cpp:1075:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/pulseaudio/audio_driver_pulseaudio.cpp:343:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/editor_plugin.cpp:525:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/editor_properties_array_dict.cpp:747:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/plugins/spatial_editor_plugin.cpp:2078:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/plugins/spatial_editor_plugin.cpp:4096:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/plugins/sprite_editor_plugin.cpp:100:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/cvtt/image_compress_cvtt.cpp:122:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/cvtt/image_compress_cvtt.cpp:134:77: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/cvtt/image_compress_cvtt.cpp:339:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/etc/image_etc.cpp:222:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/gdnative/register_types.cpp:242:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/gdnative/register_types.cpp:258:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/opensimplex/simplex_noise.cpp:200:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/opensimplex/simplex_noise.cpp:222:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/opensimplex/simplex_noise.cpp:246:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/android/export/export.cpp:1085:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/android/export/export.cpp:1489:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/android/export/export.cpp:1623:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/iphone/export/export.cpp:206:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/iphone/export/export.cpp:356:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/iphone/export/export.cpp:406:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/iphone/export/export.cpp:493:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/audio_stream_player_3d.cpp:420:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/resources/audio_stream_sample.cpp:565:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/resources/audio_stream_sample.cpp:571:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio/audio_rb_resampler.cpp:156:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] ``` The following warnings were not fixed, as they implied casting for no gain: ``` core/io/packet_peer.cpp:228:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] core/io/resource_format_binary.cpp:109:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/gles2/rasterizer_scene_gles2.cpp:144:57: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/unix/file_access_unix.cpp:249:46: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/voxel_light_baker.cpp:889:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/voxel_light_baker.cpp:1020:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/voxel_light_baker.cpp:1154:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/voxel_light_baker.cpp:2255:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/resources/bit_mask.cpp:336:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio/audio_stream.cpp:141:49: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio/audio_stream.cpp:150:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio/audio_stream.cpp:154:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio_server.cpp:86:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio_server.cpp:89:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] ```
2018-09-26 17:38:02 +02:00
for (unsigned int i = 0; i < data_bytes; i++) {
uint8_t data_point = (read_data[i] + 128);
file->store_8(data_point);
}
break;
case AudioStreamSample::FORMAT_16_BITS:
Fix warnings for comparison between signed and unsigned integers [-Wsign-compare] Also turn off -Wsign-compare warnings in the future, we do not consider them important. Fixes the following GCC 5 warnings: ``` core/node_path.cpp:279:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] core/oa_hash_map.h:169:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] core/oa_hash_map.h:314:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/gles2/shader_gles2.cpp:985:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/gles3/rasterizer_storage_gles3.cpp:1075:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/pulseaudio/audio_driver_pulseaudio.cpp:343:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/editor_plugin.cpp:525:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/editor_properties_array_dict.cpp:747:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/plugins/spatial_editor_plugin.cpp:2078:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/plugins/spatial_editor_plugin.cpp:4096:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] editor/plugins/sprite_editor_plugin.cpp:100:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/cvtt/image_compress_cvtt.cpp:122:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/cvtt/image_compress_cvtt.cpp:134:77: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/cvtt/image_compress_cvtt.cpp:339:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/etc/image_etc.cpp:222:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/gdnative/register_types.cpp:242:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/gdnative/register_types.cpp:258:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/opensimplex/simplex_noise.cpp:200:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/opensimplex/simplex_noise.cpp:222:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] modules/opensimplex/simplex_noise.cpp:246:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/android/export/export.cpp:1085:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/android/export/export.cpp:1489:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/android/export/export.cpp:1623:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/iphone/export/export.cpp:206:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/iphone/export/export.cpp:356:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/iphone/export/export.cpp:406:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] platform/iphone/export/export.cpp:493:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/audio_stream_player_3d.cpp:420:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/resources/audio_stream_sample.cpp:565:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/resources/audio_stream_sample.cpp:571:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio/audio_rb_resampler.cpp:156:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] ``` The following warnings were not fixed, as they implied casting for no gain: ``` core/io/packet_peer.cpp:228:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] core/io/resource_format_binary.cpp:109:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/gles2/rasterizer_scene_gles2.cpp:144:57: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] drivers/unix/file_access_unix.cpp:249:46: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/voxel_light_baker.cpp:889:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/voxel_light_baker.cpp:1020:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/voxel_light_baker.cpp:1154:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/3d/voxel_light_baker.cpp:2255:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] scene/resources/bit_mask.cpp:336:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio/audio_stream.cpp:141:49: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio/audio_stream.cpp:150:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio/audio_stream.cpp:154:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio_server.cpp:86:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] servers/audio_server.cpp:89:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] ```
2018-09-26 17:38:02 +02:00
for (unsigned int i = 0; i < data_bytes / 2; i++) {
uint16_t data_point = decode_uint16(&read_data[i * 2]);
file->store_16(data_point);
}
break;
case AudioStreamSample::FORMAT_IMA_ADPCM:
//Unimplemented
break;
}
file->close();
return OK;
}
2017-02-03 02:51:26 +01:00
Ref<AudioStreamPlayback> AudioStreamSample::instance_playback() {
Ref<AudioStreamPlaybackSample> sample;
sample.instance();
sample->base = Ref<AudioStreamSample>(this);
2017-02-03 02:51:26 +01:00
return sample;
}
String AudioStreamSample::get_stream_name() const {
return "";
}
void AudioStreamSample::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamSample::set_data);
ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamSample::get_data);
ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioStreamSample::set_format);
ClassDB::bind_method(D_METHOD("get_format"), &AudioStreamSample::get_format);
2017-02-03 02:51:26 +01:00
ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &AudioStreamSample::set_loop_mode);
ClassDB::bind_method(D_METHOD("get_loop_mode"), &AudioStreamSample::get_loop_mode);
2017-02-03 02:51:26 +01:00
ClassDB::bind_method(D_METHOD("set_loop_begin", "loop_begin"), &AudioStreamSample::set_loop_begin);
ClassDB::bind_method(D_METHOD("get_loop_begin"), &AudioStreamSample::get_loop_begin);
2017-02-03 02:51:26 +01:00
ClassDB::bind_method(D_METHOD("set_loop_end", "loop_end"), &AudioStreamSample::set_loop_end);
ClassDB::bind_method(D_METHOD("get_loop_end"), &AudioStreamSample::get_loop_end);
2017-02-03 02:51:26 +01:00
ClassDB::bind_method(D_METHOD("set_mix_rate", "mix_rate"), &AudioStreamSample::set_mix_rate);
ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioStreamSample::get_mix_rate);
2017-02-03 02:51:26 +01:00
ClassDB::bind_method(D_METHOD("set_stereo", "stereo"), &AudioStreamSample::set_stereo);
ClassDB::bind_method(D_METHOD("is_stereo"), &AudioStreamSample::is_stereo);
2017-02-03 02:51:26 +01:00
ClassDB::bind_method(D_METHOD("save_to_wav", "path"), &AudioStreamSample::save_to_wav);
ADD_PROPERTY(PropertyInfo(Variant::POOL_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_data", "get_data");
ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA-ADPCM"), "set_format", "get_format");
ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "Disabled,Forward,Ping-Pong,Backward"), "set_loop_mode", "get_loop_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_begin"), "set_loop_begin", "get_loop_begin");
ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_end"), "set_loop_end", "get_loop_end");
ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_rate"), "set_mix_rate", "get_mix_rate");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stereo"), "set_stereo", "is_stereo");
BIND_ENUM_CONSTANT(FORMAT_8_BITS);
BIND_ENUM_CONSTANT(FORMAT_16_BITS);
BIND_ENUM_CONSTANT(FORMAT_IMA_ADPCM);
BIND_ENUM_CONSTANT(LOOP_DISABLED);
BIND_ENUM_CONSTANT(LOOP_FORWARD);
BIND_ENUM_CONSTANT(LOOP_PING_PONG);
BIND_ENUM_CONSTANT(LOOP_BACKWARD);
2017-02-03 02:51:26 +01:00
}
AudioStreamSample::AudioStreamSample() {
format = FORMAT_8_BITS;
loop_mode = LOOP_DISABLED;
stereo = false;
loop_begin = 0;
loop_end = 0;
mix_rate = 44100;
data = nullptr;
data_bytes = 0;
2017-02-03 02:51:26 +01:00
}
AudioStreamSample::~AudioStreamSample() {
2017-02-03 02:51:26 +01:00
if (data) {
AudioServer::get_singleton()->audio_data_free(data);
data = nullptr;
data_bytes = 0;
2017-02-03 02:51:26 +01:00
}
}