diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2017-03-05 16:44:50 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2017-03-05 16:44:50 +0100 |
commit | 5dbf1809c6e3e905b94b8764e99491e608122261 (patch) | |
tree | 5e5a5360db15d86d59ec8c6e4f7eb511388c5a9a /modules/opus | |
parent | 45438e9918d421b244bfd7776a30e67dc7f2d3e3 (diff) |
A Whole New World (clang-format edition)
I can show you the code
Pretty, with proper whitespace
Tell me, coder, now when did
You last write readable code?
I can open your eyes
Make you see your bad indent
Force you to respect the style
The core devs agreed upon
A whole new world
A new fantastic code format
A de facto standard
With some sugar
Enforced with clang-format
A whole new world
A dazzling style we all dreamed of
And when we read it through
It's crystal clear
That now we're in a whole new world of code
Diffstat (limited to 'modules/opus')
-rw-r--r-- | modules/opus/audio_stream_opus.cpp | 222 | ||||
-rw-r--r-- | modules/opus/audio_stream_opus.h | 36 | ||||
-rw-r--r-- | modules/opus/register_types.cpp | 4 |
3 files changed, 132 insertions, 130 deletions
diff --git a/modules/opus/audio_stream_opus.cpp b/modules/opus/audio_stream_opus.cpp index 4a6b2e224c..52b055ca82 100644 --- a/modules/opus/audio_stream_opus.cpp +++ b/modules/opus/audio_stream_opus.cpp @@ -30,32 +30,32 @@ /*************************************************************************/ #include "audio_stream_opus.h" -const float AudioStreamPlaybackOpus::osrate=48000.0f; +const float AudioStreamPlaybackOpus::osrate = 48000.0f; int AudioStreamPlaybackOpus::_op_read_func(void *_stream, unsigned char *_ptr, int _nbytes) { - FileAccess *fa=(FileAccess*)_stream; + FileAccess *fa = (FileAccess *)_stream; - if(fa->eof_reached()) + if (fa->eof_reached()) return 0; - uint8_t *dst = (uint8_t*)_ptr; + uint8_t *dst = (uint8_t *)_ptr; int read = fa->get_buffer(dst, _nbytes); return read; } -int AudioStreamPlaybackOpus::_op_seek_func(void *_stream, opus_int64 _offset, int _whence){ +int AudioStreamPlaybackOpus::_op_seek_func(void *_stream, opus_int64 _offset, int _whence) { #ifdef SEEK_SET - FileAccess *fa=(FileAccess*)_stream; + FileAccess *fa = (FileAccess *)_stream; switch (_whence) { case SEEK_SET: { fa->seek(_offset); } break; case SEEK_CUR: { - fa->seek(fa->get_pos()+_offset); + fa->seek(fa->get_pos() + _offset); } break; case SEEK_END: { fa->seek_end(_offset); @@ -64,7 +64,7 @@ int AudioStreamPlaybackOpus::_op_seek_func(void *_stream, opus_int64 _offset, in ERR_PRINT("BUG, wtf was whence set to?\n"); } } - int ret=fa->eof_reached()?-1:0; + int ret = fa->eof_reached() ? -1 : 0; return ret; #else return -1; // no seeking @@ -74,97 +74,100 @@ int AudioStreamPlaybackOpus::_op_seek_func(void *_stream, opus_int64 _offset, in int AudioStreamPlaybackOpus::_op_close_func(void *_stream) { if (!_stream) return 0; - FileAccess *fa=(FileAccess*)_stream; + FileAccess *fa = (FileAccess *)_stream; if (fa->is_open()) fa->close(); return 0; } opus_int64 AudioStreamPlaybackOpus::_op_tell_func(void *_stream) { - FileAccess *_fa = (FileAccess*)_stream; + FileAccess *_fa = (FileAccess *)_stream; return (opus_int64)_fa->get_pos(); } void AudioStreamPlaybackOpus::_clear_stream() { - if(!stream_loaded) + if (!stream_loaded) return; op_free(opus_file); _close_file(); - stream_loaded=false; - stream_channels=1; - playing=false; + stream_loaded = false; + stream_channels = 1; + playing = false; } void AudioStreamPlaybackOpus::_close_file() { if (f) { memdelete(f); - f=NULL; + f = NULL; } } Error AudioStreamPlaybackOpus::_load_stream() { - ERR_FAIL_COND_V(!stream_valid,ERR_UNCONFIGURED); + ERR_FAIL_COND_V(!stream_valid, ERR_UNCONFIGURED); _clear_stream(); - if (file=="") + if (file == "") return ERR_INVALID_DATA; Error err; - f=FileAccess::open(file,FileAccess::READ,&err); + f = FileAccess::open(file, FileAccess::READ, &err); if (err) { - ERR_FAIL_COND_V( err, err ); + ERR_FAIL_COND_V(err, err); } int _err = 0; - opus_file = op_open_callbacks(f,&_op_callbacks,NULL,0,&_err); + opus_file = op_open_callbacks(f, &_op_callbacks, NULL, 0, &_err); switch (_err) { case OP_EREAD: { // - Can't read the file. - memdelete(f); f=NULL; - ERR_FAIL_V( ERR_FILE_CANT_READ ); + memdelete(f); + f = NULL; + ERR_FAIL_V(ERR_FILE_CANT_READ); } break; case OP_EVERSION: // - Unrecognized version number. case OP_ENOTFORMAT: // - Stream is not Opus data. - case OP_EIMPL : { // - Stream used non-implemented feature. - memdelete(f); f=NULL; - ERR_FAIL_V( ERR_FILE_UNRECOGNIZED ); + case OP_EIMPL: { // - Stream used non-implemented feature. + memdelete(f); + f = NULL; + ERR_FAIL_V(ERR_FILE_UNRECOGNIZED); } break; case OP_EBADLINK: // - Failed to find old data after seeking. case OP_EBADTIMESTAMP: // - Timestamp failed the validity checks. case OP_EBADHEADER: { // - Invalid or mising Opus bitstream header. - memdelete(f); f=NULL; - ERR_FAIL_V( ERR_FILE_CORRUPT ); + memdelete(f); + f = NULL; + ERR_FAIL_V(ERR_FILE_CORRUPT); } break; case OP_EFAULT: { // - Internal logic fault; indicates a bug or heap/stack corruption. - memdelete(f); f=NULL; - ERR_FAIL_V( ERR_BUG ); + memdelete(f); + f = NULL; + ERR_FAIL_V(ERR_BUG); } break; } - repeats=0; - stream_loaded=true; - + repeats = 0; + stream_loaded = true; return OK; } AudioStreamPlaybackOpus::AudioStreamPlaybackOpus() { - loops=false; - playing=false; + loops = false; + playing = false; f = NULL; - stream_loaded=false; - stream_valid=false; - repeats=0; - paused=true; - stream_channels=0; - current_section=0; - length=0; - loop_restart_time=0; - pre_skip=0; + stream_loaded = false; + stream_valid = false; + repeats = 0; + paused = true; + stream_channels = 0; + current_section = 0; + length = 0; + loop_restart_time = 0; + pre_skip = 0; _op_callbacks.read = _op_read_func; _op_callbacks.seek = _op_seek_func; @@ -173,59 +176,62 @@ AudioStreamPlaybackOpus::AudioStreamPlaybackOpus() { } Error AudioStreamPlaybackOpus::set_file(const String &p_file) { - file=p_file; - stream_valid=false; + file = p_file; + stream_valid = false; Error err; - f=FileAccess::open(file,FileAccess::READ,&err); + f = FileAccess::open(file, FileAccess::READ, &err); if (err) { - ERR_FAIL_COND_V( err, err ); + ERR_FAIL_COND_V(err, err); } int _err; - opus_file = op_open_callbacks(f,&_op_callbacks,NULL,0,&_err); + opus_file = op_open_callbacks(f, &_op_callbacks, NULL, 0, &_err); switch (_err) { case OP_EREAD: { // - Can't read the file. - memdelete(f); f=NULL; - ERR_FAIL_V( ERR_FILE_CANT_READ ); + memdelete(f); + f = NULL; + ERR_FAIL_V(ERR_FILE_CANT_READ); } break; case OP_EVERSION: // - Unrecognized version number. case OP_ENOTFORMAT: // - Stream is not Opus data. - case OP_EIMPL : { // - Stream used non-implemented feature. - memdelete(f); f=NULL; - ERR_FAIL_V( ERR_FILE_UNRECOGNIZED ); + case OP_EIMPL: { // - Stream used non-implemented feature. + memdelete(f); + f = NULL; + ERR_FAIL_V(ERR_FILE_UNRECOGNIZED); } break; case OP_EBADLINK: // - Failed to find old data after seeking. case OP_EBADTIMESTAMP: // - Timestamp failed the validity checks. case OP_EBADHEADER: { // - Invalid or mising Opus bitstream header. - memdelete(f); f=NULL; - ERR_FAIL_V( ERR_FILE_CORRUPT ); + memdelete(f); + f = NULL; + ERR_FAIL_V(ERR_FILE_CORRUPT); } break; case OP_EFAULT: { // - Internal logic fault; indicates a bug or heap/stack corruption. - memdelete(f); f=NULL; - ERR_FAIL_V( ERR_BUG ); + memdelete(f); + f = NULL; + ERR_FAIL_V(ERR_BUG); } break; } - const OpusHead *oinfo = op_head(opus_file,-1); + const OpusHead *oinfo = op_head(opus_file, -1); - stream_channels=oinfo->channel_count; - pre_skip=oinfo->pre_skip; - frames_mixed=pre_skip; - ogg_int64_t len = op_pcm_total(opus_file,-1); - if(len < 0) { + stream_channels = oinfo->channel_count; + pre_skip = oinfo->pre_skip; + frames_mixed = pre_skip; + ogg_int64_t len = op_pcm_total(opus_file, -1); + if (len < 0) { length = 0; } else { - length=(len/osrate); + length = (len / osrate); } op_free(opus_file); memdelete(f); - f=NULL; - stream_valid=true; - + f = NULL; + stream_valid = true; return OK; } @@ -234,102 +240,100 @@ void AudioStreamPlaybackOpus::play(float p_from) { if (playing) stop(); - if (_load_stream()!=OK) + if (_load_stream() != OK) return; - frames_mixed=pre_skip; - playing=true; - if (p_from>0) { + frames_mixed = pre_skip; + playing = true; + if (p_from > 0) { seek_pos(p_from); } } void AudioStreamPlaybackOpus::stop() { _clear_stream(); - playing=false; + playing = false; } void AudioStreamPlaybackOpus::seek_pos(float p_time) { - if(!playing) return; + if (!playing) return; ogg_int64_t pcm_offset = (ogg_int64_t)(p_time * osrate); - bool ok = op_pcm_seek(opus_file,pcm_offset)==0; - if(!ok) { + bool ok = op_pcm_seek(opus_file, pcm_offset) == 0; + if (!ok) { ERR_PRINT("Seek time over stream size."); return; } - frames_mixed=osrate*p_time; + frames_mixed = osrate * p_time; } -int AudioStreamPlaybackOpus::mix(int16_t* p_bufer,int p_frames) { +int AudioStreamPlaybackOpus::mix(int16_t *p_bufer, int p_frames) { if (!playing) return 0; - int total=p_frames; + int total = p_frames; while (true) { int todo = p_frames; - if (todo==0 || todo<MIN_MIX) { + if (todo == 0 || todo < MIN_MIX) { break; } - int ret=op_read(opus_file,(opus_int16*)p_bufer,todo*stream_channels,¤t_section); - if (ret<0) { + int ret = op_read(opus_file, (opus_int16 *)p_bufer, todo * stream_channels, ¤t_section); + if (ret < 0) { playing = false; - ERR_EXPLAIN("Error reading Opus File: "+file); - ERR_BREAK(ret<0); - } else if (ret==0) { // end of song, reload? + ERR_EXPLAIN("Error reading Opus File: " + file); + ERR_BREAK(ret < 0); + } else if (ret == 0) { // end of song, reload? op_free(opus_file); _close_file(); - f=FileAccess::open(file,FileAccess::READ); + f = FileAccess::open(file, FileAccess::READ); int errv = 0; - opus_file = op_open_callbacks(f,&_op_callbacks,NULL,0,&errv); - if (errv!=0) { - playing=false; + opus_file = op_open_callbacks(f, &_op_callbacks, NULL, 0, &errv); + if (errv != 0) { + playing = false; break; // :( } if (!has_loop()) { - playing=false; - repeats=1; + playing = false; + repeats = 1; break; } if (loop_restart_time) { - bool ok = op_pcm_seek(opus_file, (loop_restart_time*osrate)+pre_skip)==0; + bool ok = op_pcm_seek(opus_file, (loop_restart_time * osrate) + pre_skip) == 0; if (!ok) { - playing=false; + playing = false; ERR_PRINT("loop restart time rejected") } - frames_mixed=(loop_restart_time*osrate)+pre_skip; + frames_mixed = (loop_restart_time * osrate) + pre_skip; } else { - frames_mixed=pre_skip; + frames_mixed = pre_skip; } repeats++; continue; - } - stream_channels=op_head(opus_file,current_section)->channel_count; - - frames_mixed+=ret; + stream_channels = op_head(opus_file, current_section)->channel_count; - p_bufer+=ret*stream_channels; - p_frames-=ret; + frames_mixed += ret; + p_bufer += ret * stream_channels; + p_frames -= ret; } - return total-p_frames; + return total - p_frames; } float AudioStreamPlaybackOpus::get_length() const { - if(!stream_loaded) { - if(const_cast<AudioStreamPlaybackOpus*>(this)->_load_stream() != OK) + if (!stream_loaded) { + if (const_cast<AudioStreamPlaybackOpus *>(this)->_load_stream() != OK) return 0; } return length; @@ -339,7 +343,7 @@ float AudioStreamPlaybackOpus::get_pos() const { int32_t frames = int32_t(frames_mixed); if (frames < 0) - frames=0; + frames = 0; return double(frames) / osrate; } @@ -351,9 +355,9 @@ AudioStreamPlaybackOpus::~AudioStreamPlaybackOpus() { _clear_stream(); } -RES ResourceFormatLoaderAudioStreamOpus::load(const String &p_path, const String& p_original_path, Error *r_error) { +RES ResourceFormatLoaderAudioStreamOpus::load(const String &p_path, const String &p_original_path, Error *r_error) { if (r_error) - *r_error=OK; + *r_error = OK; AudioStreamOpus *opus_stream = memnew(AudioStreamOpus); opus_stream->set_file(p_path); @@ -366,11 +370,11 @@ void ResourceFormatLoaderAudioStreamOpus::get_recognized_extensions(List<String> } String ResourceFormatLoaderAudioStreamOpus::get_resource_type(const String &p_path) const { - if (p_path.get_extension().to_lower()=="opus") + if (p_path.get_extension().to_lower() == "opus") return "AudioStreamOpus"; return ""; } -bool ResourceFormatLoaderAudioStreamOpus::handles_type(const String& p_type) const { - return (p_type=="AudioStream" || p_type=="AudioStreamOpus"); +bool ResourceFormatLoaderAudioStreamOpus::handles_type(const String &p_type) const { + return (p_type == "AudioStream" || p_type == "AudioStreamOpus"); } diff --git a/modules/opus/audio_stream_opus.h b/modules/opus/audio_stream_opus.h index 5093456ccd..f1bee121b2 100644 --- a/modules/opus/audio_stream_opus.h +++ b/modules/opus/audio_stream_opus.h @@ -40,10 +40,10 @@ class AudioStreamPlaybackOpus : public AudioStreamPlayback { - GDCLASS(AudioStreamPlaybackOpus,AudioStreamPlayback) + GDCLASS(AudioStreamPlaybackOpus, AudioStreamPlayback) enum { - MIN_MIX=1024 + MIN_MIX = 1024 }; FileAccess *f; @@ -78,19 +78,19 @@ class AudioStreamPlaybackOpus : public AudioStreamPlayback { float loop_restart_time; public: - Error set_file(const String& p_file); + Error set_file(const String &p_file); - virtual void play(float p_from=0); + virtual void play(float p_from = 0); virtual void stop(); virtual bool is_playing() const { return playing; } - virtual void set_loop_restart_time(float p_time) { loop_restart_time=p_time; } + virtual void set_loop_restart_time(float p_time) { loop_restart_time = p_time; } - virtual void set_paused(bool p_paused) { paused=p_paused; } + virtual void set_paused(bool p_paused) { paused = p_paused; } virtual bool is_paused() const { return paused; } - virtual void set_loop(bool p_enable) { loops=p_enable; } - virtual bool has_loop() const {return loops; } + virtual void set_loop(bool p_enable) { loops = p_enable; } + virtual bool has_loop() const { return loops; } virtual float get_length() const; @@ -106,35 +106,33 @@ public: virtual int get_minimum_buffer_size() const; - virtual int mix(int16_t* p_bufer,int p_frames); + virtual int mix(int16_t *p_bufer, int p_frames); AudioStreamPlaybackOpus(); ~AudioStreamPlaybackOpus(); }; +class AudioStreamOpus : public AudioStream { -class AudioStreamOpus: public AudioStream { - - GDCLASS(AudioStreamOpus,AudioStream) + GDCLASS(AudioStreamOpus, AudioStream) String file; -public: +public: Ref<AudioStreamPlayback> instance_playback() { - Ref<AudioStreamPlaybackOpus> pb = memnew( AudioStreamPlaybackOpus ); + Ref<AudioStreamPlaybackOpus> pb = memnew(AudioStreamPlaybackOpus); pb->set_file(file); return pb; } - void set_file(const String& p_file) { file=p_file; } - + void set_file(const String &p_file) { file = p_file; } }; -class ResourceFormatLoaderAudioStreamOpus: public ResourceFormatLoader { +class ResourceFormatLoaderAudioStreamOpus : public ResourceFormatLoader { public: - virtual RES load(const String &p_path,const String& p_original_path="",Error *r_error=NULL); + virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL); virtual void get_recognized_extensions(List<String> *p_extensions) const; - virtual bool handles_type(const String& p_type) const; + virtual bool handles_type(const String &p_type) const; virtual String get_resource_type(const String &p_path) const; }; diff --git a/modules/opus/register_types.cpp b/modules/opus/register_types.cpp index a177bc9463..8a48c5084e 100644 --- a/modules/opus/register_types.cpp +++ b/modules/opus/register_types.cpp @@ -34,12 +34,12 @@ static ResourceFormatLoaderAudioStreamOpus *opus_stream_loader = NULL; void register_opus_types() { - opus_stream_loader = memnew( ResourceFormatLoaderAudioStreamOpus ); + opus_stream_loader = memnew(ResourceFormatLoaderAudioStreamOpus); ResourceLoader::add_resource_format_loader(opus_stream_loader); ClassDB::register_class<AudioStreamOpus>(); } void unregister_opus_types() { - memdelete( opus_stream_loader ); + memdelete(opus_stream_loader); } |