godot/scene/io/resource_format_wav.cpp

262 lines
7.8 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* resource_format_wav.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 01:10:30 +00: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 "resource_format_wav.h"
#include "os/file_access.h"
#include "scene/resources/sample.h"
RES ResourceFormatLoaderWAV::load(const String &p_path, const String &p_original_path, Error *r_error) {
if (r_error)
*r_error = ERR_FILE_CANT_OPEN;
2014-02-10 01:10:30 +00:00
Error err;
FileAccess *file = FileAccess::open(p_path, FileAccess::READ, &err);
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V(err != OK, RES());
2014-02-10 01:10:30 +00:00
if (r_error)
*r_error = ERR_FILE_CORRUPT;
2014-02-10 01:10:30 +00:00
/* CHECK RIFF */
char riff[5];
riff[4] = 0;
file->get_buffer((uint8_t *)&riff, 4); //RIFF
2014-02-10 01:10:30 +00:00
if (riff[0] != 'R' || riff[1] != 'I' || riff[2] != 'F' || riff[3] != 'F') {
2014-02-10 01:10:30 +00:00
file->close();
memdelete(file);
ERR_FAIL_V(RES());
2014-02-10 01:10:30 +00:00
}
/* GET FILESIZE */
uint32_t filesize = file->get_32();
2014-02-10 01:10:30 +00:00
/* CHECK WAVE */
char wave[4];
file->get_buffer((uint8_t *)&wave, 4); //RIFF
2014-02-10 01:10:30 +00:00
if (wave[0] != 'W' || wave[1] != 'A' || wave[2] != 'V' || wave[3] != 'E') {
2014-02-10 01:10:30 +00:00
file->close();
memdelete(file);
ERR_EXPLAIN("Not a WAV file (no WAVE RIFF Header)")
ERR_FAIL_V(RES());
2014-02-10 01:10:30 +00:00
}
uint16_t compression_code = 1;
bool format_found = false;
bool data_found = false;
int format_bits = 0;
int format_channels = 0;
int format_freq = 0;
Sample::LoopFormat loop = Sample::LOOP_NONE;
int loop_begin = 0;
int loop_end = 0;
2014-02-10 01:10:30 +00:00
Ref<Sample> sample(memnew(Sample));
2014-02-10 01:10:30 +00:00
while (!file->eof_reached()) {
/* chunk */
char chunkID[4];
file->get_buffer((uint8_t *)&chunkID, 4); //RIFF
2014-02-10 01:10:30 +00:00
/* chunk size */
uint32_t chunksize = file->get_32();
uint32_t file_pos = file->get_pos(); //save file pos, so we can skip to next chunk safely
2014-02-10 01:10:30 +00:00
if (file->eof_reached()) {
//ERR_PRINT("EOF REACH");
break;
}
if (chunkID[0] == 'f' && chunkID[1] == 'm' && chunkID[2] == 't' && chunkID[3] == ' ' && !format_found) {
2014-02-10 01:10:30 +00:00
/* IS FORMAT CHUNK */
compression_code = file->get_16();
if (compression_code != 1 && compression_code != 3) {
2014-02-10 01:10:30 +00:00
ERR_PRINT("Format not supported for WAVE file (not PCM). Save WAVE files as uncompressed PCM instead.");
break;
}
format_channels = file->get_16();
if (format_channels != 1 && format_channels != 2) {
2014-02-10 01:10:30 +00:00
ERR_PRINT("Format not supported for WAVE file (not stereo or mono)");
break;
}
format_freq = file->get_32(); //sampling rate
2014-02-10 01:10:30 +00:00
file->get_32(); // average bits/second (unused)
file->get_16(); // block align (unused)
format_bits = file->get_16(); // bits per sample
2014-02-10 01:10:30 +00:00
if (format_bits % 8) {
2014-02-10 01:10:30 +00:00
ERR_PRINT("Strange number of bits in sample (not 8,16,24,32)");
break;
}
/* Dont need anything else, continue */
format_found = true;
2014-02-10 01:10:30 +00:00
}
if (chunkID[0] == 'd' && chunkID[1] == 'a' && chunkID[2] == 't' && chunkID[3] == 'a' && !data_found) {
2014-02-10 01:10:30 +00:00
/* IS FORMAT CHUNK */
data_found = true;
2014-02-10 01:10:30 +00:00
if (!format_found) {
ERR_PRINT("'data' chunk before 'format' chunk found.");
break;
}
int frames = chunksize;
2015-05-20 02:37:04 +00:00
frames /= format_channels;
frames /= (format_bits >> 3);
2014-02-10 01:10:30 +00:00
/*print_line("chunksize: "+itos(chunksize));
2015-05-20 02:37:04 +00:00
print_line("channels: "+itos(format_channels));
print_line("bits: "+itos(format_bits));
*/
2014-02-10 01:10:30 +00:00
sample->create(
(format_bits == 8) ? Sample::FORMAT_PCM8 : Sample::FORMAT_PCM16,
(format_channels == 2) ? true : false,
frames);
sample->set_mix_rate(format_freq);
2014-02-10 01:10:30 +00:00
int len = frames;
if (format_channels == 2)
len *= 2;
if (format_bits > 8)
len *= 2;
2015-05-20 02:37:04 +00:00
2014-02-10 01:10:30 +00:00
DVector<uint8_t> data;
2015-05-20 02:37:04 +00:00
data.resize(len);
2014-02-10 01:10:30 +00:00
DVector<uint8_t>::Write dataw = data.write();
void *data_ptr = dataw.ptr();
2014-02-10 01:10:30 +00:00
if (format_bits == 8) {
int8_t *data_ptr8 = (int8_t *)data_ptr;
for (int i = 0; i < frames * format_channels; i++) {
// 8 bit samples are UNSIGNED
2014-02-10 01:10:30 +00:00
uint8_t s = file->get_8();
data_ptr8[i] = (int8_t)(s - 128);
}
} else if (format_bits == 32 && compression_code == 3) {
int16_t *data_ptr16 = (int16_t *)data_ptr;
for (int i = 0; i < frames * format_channels; i++) {
//32 bit IEEE Float
2014-02-10 01:10:30 +00:00
float s = file->get_float();
data_ptr16[i] = (int32_t)(s * 32768.f);
}
} else if (format_bits == 16) {
int16_t *data_ptr16 = (int16_t *)data_ptr;
for (int i = 0; i < frames * format_channels; i++) {
//16 bit SIGNED
2014-02-10 01:10:30 +00:00
data_ptr16[i] = file->get_16();
}
} else {
int16_t *data_ptr16 = (int16_t *)data_ptr;
for (int i = 0; i < frames * format_channels; i++) {
//16+ bits samples are SIGNED
// if sample is > 16 bits, just read extra bytes
2014-02-10 01:10:30 +00:00
uint32_t s = 0;
for (int b = 0; b < (format_bits >> 3); b++) {
2014-02-10 01:10:30 +00:00
s |= ((uint32_t)file->get_8()) << (b * 8);
2014-02-10 01:10:30 +00:00
}
s <<= (32 - format_bits);
data_ptr16[i] = s >> 16;
2014-02-10 01:10:30 +00:00
}
}
dataw = DVector<uint8_t>::Write();
2014-02-10 01:10:30 +00:00
sample->set_data(data);
if (file->eof_reached()) {
file->close();
memdelete(file);
ERR_EXPLAIN("Premature end of file.");
ERR_FAIL_V(RES());
}
}
if (chunkID[0] == 's' && chunkID[1] == 'm' && chunkID[2] == 'p' && chunkID[3] == 'l') {
2014-02-10 01:10:30 +00:00
//loop point info!
for (int i = 0; i < 10; i++)
2014-02-10 01:10:30 +00:00
file->get_32(); // i wish to know why should i do this... no doc!
loop = file->get_32() ? Sample::LOOP_PING_PONG : Sample::LOOP_FORWARD;
loop_begin = file->get_32();
loop_end = file->get_32();
2014-02-10 01:10:30 +00:00
}
file->seek(file_pos + chunksize);
2014-02-10 01:10:30 +00:00
}
sample->set_loop_format(loop);
sample->set_loop_begin(loop_begin);
sample->set_loop_end(loop_end);
file->close();
memdelete(file);
if (r_error)
*r_error = OK;
2014-02-10 01:10:30 +00:00
return sample;
}
void ResourceFormatLoaderWAV::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back("wav");
}
bool ResourceFormatLoaderWAV::handles_type(const String &p_type) const {
2014-02-10 01:10:30 +00:00
return (p_type == "Sample");
2014-02-10 01:10:30 +00:00
}
String ResourceFormatLoaderWAV::get_resource_type(const String &p_path) const {
if (p_path.extension().to_lower() == "wav")
2014-02-10 01:10:30 +00:00
return "Sample";
return "";
}