aboutsummaryrefslogtreecommitdiff
path: root/scene/io/resource_format_wav.cpp
diff options
context:
space:
mode:
authorRémi Verschelde2017-03-19 00:36:26 +0100
committerRémi Verschelde2017-03-19 00:36:26 +0100
commitf8db8a3faa30b71dca33ced38be16d3f93f43e8a (patch)
tree3b798318132cca7eccfbca5818ab55656a2896d7 /scene/io/resource_format_wav.cpp
parent1d418afe863c9e553b69174ce63aef203c46d2f0 (diff)
downloadgodot-f8db8a3faa30b71dca33ced38be16d3f93f43e8a.tar.gz
godot-f8db8a3faa30b71dca33ced38be16d3f93f43e8a.tar.zst
godot-f8db8a3faa30b71dca33ced38be16d3f93f43e8a.zip
Bring that Whole New World to the Old Continent too
Applies the clang-format style to the 2.1 branch as done for master in 5dbf1809c6e3e905b94b8764e99491e608122261.
Diffstat (limited to '')
-rw-r--r--scene/io/resource_format_wav.cpp167
1 files changed, 74 insertions, 93 deletions
diff --git a/scene/io/resource_format_wav.cpp b/scene/io/resource_format_wav.cpp
index 3720aa13f..80e280ebb 100644
--- a/scene/io/resource_format_wav.cpp
+++ b/scene/io/resource_format_wav.cpp
@@ -30,73 +30,67 @@
#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) {
+RES ResourceFormatLoaderWAV::load(const String &p_path, const String &p_original_path, Error *r_error) {
if (r_error)
- *r_error=ERR_FILE_CANT_OPEN;
+ *r_error = ERR_FILE_CANT_OPEN;
Error err;
- FileAccess *file=FileAccess::open(p_path, FileAccess::READ,&err);
+ FileAccess *file = FileAccess::open(p_path, FileAccess::READ, &err);
- ERR_FAIL_COND_V( err!=OK, RES() );
+ ERR_FAIL_COND_V(err != OK, RES());
if (r_error)
- *r_error=ERR_FILE_CORRUPT;
+ *r_error = ERR_FILE_CORRUPT;
/* CHECK RIFF */
char riff[5];
- riff[4]=0;
- file->get_buffer((uint8_t*)&riff,4); //RIFF
+ riff[4] = 0;
+ file->get_buffer((uint8_t *)&riff, 4); //RIFF
- if (riff[0]!='R' || riff[1]!='I' || riff[2]!='F' || riff[3]!='F') {
+ if (riff[0] != 'R' || riff[1] != 'I' || riff[2] != 'F' || riff[3] != 'F') {
file->close();
memdelete(file);
- ERR_FAIL_V( RES() );
+ ERR_FAIL_V(RES());
}
-
/* GET FILESIZE */
- uint32_t filesize=file->get_32();
+ uint32_t filesize = file->get_32();
/* CHECK WAVE */
char wave[4];
- file->get_buffer((uint8_t*)&wave,4); //RIFF
-
- if (wave[0]!='W' || wave[1]!='A' || wave[2]!='V' || wave[3]!='E') {
+ file->get_buffer((uint8_t *)&wave, 4); //RIFF
+ if (wave[0] != 'W' || wave[1] != 'A' || wave[2] != 'V' || wave[3] != 'E') {
file->close();
memdelete(file);
ERR_EXPLAIN("Not a WAV file (no WAVE RIFF Header)")
- ERR_FAIL_V( RES() );
+ ERR_FAIL_V(RES());
}
- 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;
-
-
- Ref<Sample> sample( memnew( Sample ) );
+ 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;
+ Ref<Sample> sample(memnew(Sample));
while (!file->eof_reached()) {
-
/* chunk */
char chunkID[4];
- file->get_buffer((uint8_t*)&chunkID,4); //RIFF
+ file->get_buffer((uint8_t *)&chunkID, 4); //RIFF
/* 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
+ uint32_t chunksize = file->get_32();
+ uint32_t file_pos = file->get_pos(); //save file pos, so we can skip to next chunk safely
if (file->eof_reached()) {
@@ -104,122 +98,113 @@ RES ResourceFormatLoaderWAV::load(const String &p_path, const String& p_original
break;
}
- if (chunkID[0]=='f' && chunkID[1]=='m' && chunkID[2]=='t' && chunkID[3]==' ' && !format_found) {
+ if (chunkID[0] == 'f' && chunkID[1] == 'm' && chunkID[2] == 't' && chunkID[3] == ' ' && !format_found) {
/* IS FORMAT CHUNK */
- uint16_t compression_code=file->get_16();
-
+ uint16_t compression_code = file->get_16();
- if (compression_code!=1) {
+ if (compression_code != 1) {
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) {
+ format_channels = file->get_16();
+ if (format_channels != 1 && format_channels != 2) {
ERR_PRINT("Format not supported for WAVE file (not stereo or mono)");
break;
-
}
- format_freq=file->get_32(); //sampling rate
+ format_freq = file->get_32(); //sampling rate
file->get_32(); // average bits/second (unused)
file->get_16(); // block align (unused)
- format_bits=file->get_16(); // bits per sample
+ format_bits = file->get_16(); // bits per sample
- if (format_bits%8) {
+ if (format_bits % 8) {
ERR_PRINT("Strange number of bits in sample (not 8,16,24,32)");
break;
}
/* Dont need anything else, continue */
- format_found=true;
+ format_found = true;
}
-
- if (chunkID[0]=='d' && chunkID[1]=='a' && chunkID[2]=='t' && chunkID[3]=='a' && !data_found) {
+ if (chunkID[0] == 'd' && chunkID[1] == 'a' && chunkID[2] == 't' && chunkID[3] == 'a' && !data_found) {
/* IS FORMAT CHUNK */
- data_found=true;
+ data_found = true;
if (!format_found) {
ERR_PRINT("'data' chunk before 'format' chunk found.");
break;
-
}
- int frames=chunksize;
+ int frames = chunksize;
- frames/=format_channels;
- frames/=(format_bits>>3);
+ frames /= format_channels;
+ frames /= (format_bits >> 3);
/*print_line("chunksize: "+itos(chunksize));
print_line("channels: "+itos(format_channels));
print_line("bits: "+itos(format_bits));
*/
sample->create(
- (format_bits==8) ? Sample::FORMAT_PCM8 : Sample::FORMAT_PCM16,
- (format_channels==2)?true:false,
- frames );
- sample->set_mix_rate( format_freq );
+ (format_bits == 8) ? Sample::FORMAT_PCM8 : Sample::FORMAT_PCM16,
+ (format_channels == 2) ? true : false,
+ frames);
+ sample->set_mix_rate(format_freq);
- int len=frames;
- if (format_channels==2)
- len*=2;
- if (format_bits>8)
- len*=2;
+ int len = frames;
+ if (format_channels == 2)
+ len *= 2;
+ if (format_bits > 8)
+ len *= 2;
DVector<uint8_t> data;
data.resize(len);
DVector<uint8_t>::Write dataw = data.write();
- void * data_ptr = dataw.ptr();
+ void *data_ptr = dataw.ptr();
- for (int i=0;i<frames;i++) {
+ for (int i = 0; i < frames; i++) {
+ for (int c = 0; c < format_channels; c++) {
- for (int c=0;c<format_channels;c++) {
-
-
- if (format_bits==8) {
+ if (format_bits == 8) {
// 8 bit samples are UNSIGNED
uint8_t s = file->get_8();
- s-=128;
- int8_t *sp=(int8_t*)&s;
+ s -= 128;
+ int8_t *sp = (int8_t *)&s;
- int8_t *data_ptr8=&((int8_t*)data_ptr)[i*format_channels+c];
+ int8_t *data_ptr8 = &((int8_t *)data_ptr)[i * format_channels + c];
- *data_ptr8=*sp;
+ *data_ptr8 = *sp;
} else {
//16+ bits samples are SIGNED
// if sample is > 16 bits, just read extra bytes
- uint32_t data=0;
- for (int b=0;b<(format_bits>>3);b++) {
+ uint32_t data = 0;
+ for (int b = 0; b < (format_bits >> 3); b++) {
- data|=((uint32_t)file->get_8())<<(b*8);
+ data |= ((uint32_t)file->get_8()) << (b * 8);
}
- data<<=(32-format_bits);
-
+ data <<= (32 - format_bits);
- int32_t s=data;
+ int32_t s = data;
- int16_t *data_ptr16=&((int16_t*)data_ptr)[i*format_channels+c];
+ int16_t *data_ptr16 = &((int16_t *)data_ptr)[i * format_channels + c];
- *data_ptr16=s>>16;
+ *data_ptr16 = s >> 16;
}
}
-
}
- dataw=DVector<uint8_t>::Write();
+ dataw = DVector<uint8_t>::Write();
sample->set_data(data);
-
if (file->eof_reached()) {
file->close();
memdelete(file);
@@ -228,18 +213,17 @@ RES ResourceFormatLoaderWAV::load(const String &p_path, const String& p_original
}
}
- if (chunkID[0]=='s' && chunkID[1]=='m' && chunkID[2]=='p' && chunkID[3]=='l') {
+ if (chunkID[0] == 's' && chunkID[1] == 'm' && chunkID[2] == 'p' && chunkID[3] == 'l') {
//loop point info!
- for(int i=0;i<10;i++)
+ for (int i = 0; i < 10; i++)
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();
-
+ loop = file->get_32() ? Sample::LOOP_PING_PONG : Sample::LOOP_FORWARD;
+ loop_begin = file->get_32();
+ loop_end = file->get_32();
}
- file->seek( file_pos+chunksize );
+ file->seek(file_pos + chunksize);
}
sample->set_loop_format(loop);
@@ -250,25 +234,22 @@ RES ResourceFormatLoaderWAV::load(const String &p_path, const String& p_original
memdelete(file);
if (r_error)
- *r_error=OK;
-
+ *r_error = OK;
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 {
+bool ResourceFormatLoaderWAV::handles_type(const String &p_type) const {
- return (p_type=="Sample");
+ return (p_type == "Sample");
}
String ResourceFormatLoaderWAV::get_resource_type(const String &p_path) const {
- if (p_path.extension().to_lower()=="wav")
+ if (p_path.extension().to_lower() == "wav")
return "Sample";
return "";
}
-