You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-09 12:50:35 +00:00
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
5dbf1809c6.
This commit is contained in:
@@ -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;
|
||||
stream_channels = op_head(opus_file, current_section)->channel_count;
|
||||
|
||||
frames_mixed+=ret;
|
||||
|
||||
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.extension().to_lower()=="opus")
|
||||
if (p_path.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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user