diff options
Diffstat (limited to 'modules/speex')
| -rw-r--r-- | modules/speex/audio_stream_speex.cpp | 273 | ||||
| -rw-r--r-- | modules/speex/audio_stream_speex.h | 43 | ||||
| -rw-r--r-- | modules/speex/register_types.cpp | 4 |
3 files changed, 125 insertions, 195 deletions
diff --git a/modules/speex/audio_stream_speex.cpp b/modules/speex/audio_stream_speex.cpp index 3aa80fd0b..a3c883cd7 100644 --- a/modules/speex/audio_stream_speex.cpp +++ b/modules/speex/audio_stream_speex.cpp @@ -28,24 +28,20 @@ /*************************************************************************/ #include "audio_stream_speex.h" -#include "os_support.h" #include "os/os.h" +#include "os_support.h" #define READ_CHUNK 1024 -static _FORCE_INLINE_ uint16_t le_short(uint16_t s) -{ - uint16_t ret=s; +static _FORCE_INLINE_ uint16_t le_short(uint16_t s) { + uint16_t ret = s; #if 0 //def BIG_ENDIAN_ENABLED ret = s>>8; ret += s<<8; #endif - return ret; + return ret; } - -int AudioStreamPlaybackSpeex::mix(int16_t* p_buffer,int p_frames) { - - +int AudioStreamPlaybackSpeex::mix(int16_t *p_buffer, int p_frames) { //printf("update, loops %i, read ofs %i\n", (int)loops, read_ofs); //printf("playing %i, paused %i\n", (int)playing, (int)paused); @@ -73,54 +69,48 @@ int AudioStreamPlaybackSpeex::mix(int16_t* p_buffer,int p_frames) { while (todo > page_size) { - int ret=0; - while ((todo>page_size && packets_available && !eos) || (ret = ogg_sync_pageout(&oy, &og))==1) { + int ret = 0; + while ((todo > page_size && packets_available && !eos) || (ret = ogg_sync_pageout(&oy, &og)) == 1) { if (!packets_available) { - /*Add page to the bitstream*/ + /*Add page to the bitstream*/ ogg_stream_pagein(&os, &og); page_granule = ogg_page_granulepos(&og); page_nb_packets = ogg_page_packets(&og); - packet_no=0; - if (page_granule>0 && frame_size) - { - skip_samples = page_nb_packets*frame_size*nframes - (page_granule-last_granule); + packet_no = 0; + if (page_granule > 0 && frame_size) { + skip_samples = page_nb_packets * frame_size * nframes - (page_granule - last_granule); if (ogg_page_eos(&og)) skip_samples = -skip_samples; /*else if (!ogg_page_bos(&og)) skip_samples = 0;*/ - } else - { + } else { skip_samples = 0; } - last_granule = page_granule; - packets_available=true; + packets_available = true; } /*Extract all available packets*/ while (todo > page_size && !eos) { - if (ogg_stream_packetout(&os, &op)!=1) { - packets_available=false; + if (ogg_stream_packetout(&os, &op) != 1) { + packets_available = false; break; } packet_no++; - /*End of stream condition*/ if (op.e_o_s) - eos=1; + eos = 1; /*Copy Ogg packet to Speex bitstream*/ - speex_bits_read_from(&bits, (char*)op.packet, op.bytes); - + speex_bits_read_from(&bits, (char *)op.packet, op.bytes); - for (int j=0;j!=nframes;j++) - { + for (int j = 0; j != nframes; j++) { - int16_t* out = p_buffer; + int16_t *out = p_buffer; int ret; /*Decode frame*/ @@ -129,57 +119,49 @@ int AudioStreamPlaybackSpeex::mix(int16_t* p_buffer,int p_frames) { /*for (i=0;i<frame_size*channels;i++) printf ("%d\n", (int)output[i]);*/ - if (ret==-1) { + if (ret == -1) { printf("decode returned -1\n"); break; }; - if (ret==-2) - { - OS::get_singleton()->printerr( "Decoding error: corrupted stream?\n"); + if (ret == -2) { + OS::get_singleton()->printerr("Decoding error: corrupted stream?\n"); break; } - if (speex_bits_remaining(&bits)<0) - { - OS::get_singleton()->printerr( "Decoding overflow: corrupted stream?\n"); + if (speex_bits_remaining(&bits) < 0) { + OS::get_singleton()->printerr("Decoding overflow: corrupted stream?\n"); break; } //if (channels==2) // speex_decode_stereo_int(output, frame_size, &stereo); - /*Convert to short and save to output file*/ - for (int i=0;i<frame_size*stream_channels;i++) { - out[i]=le_short(out[i]); + for (int i = 0; i < frame_size * stream_channels; i++) { + out[i] = le_short(out[i]); } - { int new_frame_size = frame_size; /*printf ("packet %d %d\n", packet_no, skip_samples);*/ - if (packet_no == 1 && j==0 && skip_samples > 0) - { + if (packet_no == 1 && j == 0 && skip_samples > 0) { /*printf ("chopping first packet\n");*/ new_frame_size -= skip_samples; } - if (packet_no == page_nb_packets && skip_samples < 0) - { - int packet_length = nframes*frame_size+skip_samples; - new_frame_size = packet_length - j*frame_size; - if (new_frame_size<0) + if (packet_no == page_nb_packets && skip_samples < 0) { + int packet_length = nframes * frame_size + skip_samples; + new_frame_size = packet_length - j * frame_size; + if (new_frame_size < 0) new_frame_size = 0; - if (new_frame_size>frame_size) + if (new_frame_size > frame_size) new_frame_size = frame_size; /*printf ("chopping end: %d %d %d\n", new_frame_size, packet_length, packet_no);*/ } - - p_buffer+=new_frame_size*stream_channels; - todo-=new_frame_size; + p_buffer += new_frame_size * stream_channels; + todo -= new_frame_size; } } - }; }; //todo = get_todo(); @@ -192,18 +174,18 @@ int AudioStreamPlaybackSpeex::mix(int16_t* p_buffer,int p_frames) { int nb_read = MIN(data.size() - read_ofs, READ_CHUNK); /*Get the ogg buffer for writing*/ - char* ogg_dst = ogg_sync_buffer(&oy, nb_read); + char *ogg_dst = ogg_sync_buffer(&oy, nb_read); /*Read bitstream from input file*/ copymem(ogg_dst, &data[read_ofs], nb_read); read_ofs += nb_read; ogg_sync_wrote(&oy, nb_read); } else { - if (loops) { + if (loops) { reload(); ++loop_count; //break; } else { - playing=false; + playing = false; unload(); break; }; @@ -211,14 +193,11 @@ int AudioStreamPlaybackSpeex::mix(int16_t* p_buffer,int p_frames) { }; }; - return p_frames-todo; + return p_frames - todo; }; - void AudioStreamPlaybackSpeex::unload() { - - if (!active) return; speex_bits_destroy(&bits); @@ -240,44 +219,38 @@ void *AudioStreamPlaybackSpeex::process_header(ogg_packet *op, int *frame_size, SpeexHeader *header; int modeID; - header = speex_packet_to_header((char*)op->packet, op->bytes); - if (!header) - { - OS::get_singleton()->printerr( "Cannot read header\n"); + header = speex_packet_to_header((char *)op->packet, op->bytes); + if (!header) { + OS::get_singleton()->printerr("Cannot read header\n"); return NULL; } - if (header->mode >= SPEEX_NB_MODES) - { - OS::get_singleton()->printerr( "Mode number %d does not (yet/any longer) exist in this version\n", - header->mode); + if (header->mode >= SPEEX_NB_MODES) { + OS::get_singleton()->printerr("Mode number %d does not (yet/any longer) exist in this version\n", + header->mode); return NULL; } modeID = header->mode; - const SpeexMode *mode = speex_lib_get_mode (modeID); + const SpeexMode *mode = speex_lib_get_mode(modeID); - if (header->speex_version_id > 1) - { - OS::get_singleton()->printerr( "This file was encoded with Speex bit-stream version %d, which I don't know how to decode\n", header->speex_version_id); + if (header->speex_version_id > 1) { + OS::get_singleton()->printerr("This file was encoded with Speex bit-stream version %d, which I don't know how to decode\n", header->speex_version_id); return NULL; } - if (mode->bitstream_version < header->mode_bitstream_version) - { - OS::get_singleton()->printerr( "The file was encoded with a newer version of Speex. You need to upgrade in order to play it.\n"); + if (mode->bitstream_version < header->mode_bitstream_version) { + OS::get_singleton()->printerr("The file was encoded with a newer version of Speex. You need to upgrade in order to play it.\n"); return NULL; } - if (mode->bitstream_version > header->mode_bitstream_version) - { - OS::get_singleton()->printerr( "The file was encoded with an older version of Speex. You would need to downgrade the version in order to play it.\n"); + if (mode->bitstream_version > header->mode_bitstream_version) { + OS::get_singleton()->printerr("The file was encoded with an older version of Speex. You would need to downgrade the version in order to play it.\n"); return NULL; } - void* state = speex_decoder_init(mode); - if (!state) - { - OS::get_singleton()->printerr( "Decoder initialization failed.\n"); + void *state = speex_decoder_init(mode); + if (!state) { + OS::get_singleton()->printerr("Decoder initialization failed.\n"); return NULL; } //speex_decoder_ctl(state, SPEEX_SET_ENH, &enh_enabled); @@ -292,7 +265,7 @@ void *AudioStreamPlaybackSpeex::process_header(ogg_packet *op, int *frame_size, *channels = header->nb_channels; - if (*channels!=1) { + if (*channels != 1) { OS::get_singleton()->printerr("Only MONO speex streams supported\n"); return NULL; } @@ -303,12 +276,8 @@ void *AudioStreamPlaybackSpeex::process_header(ogg_packet *op, int *frame_size, return state; } - - void AudioStreamPlaybackSpeex::reload() { - - if (active) unload(); @@ -319,18 +288,18 @@ void AudioStreamPlaybackSpeex::reload() { speex_bits_init(&bits); read_ofs = 0; -// char *buf; + // char *buf; int packet_count = 0; int extra_headers = 0; int stream_init = 0; - page_granule=0; - last_granule=0; - skip_samples=0; - page_nb_packets=0; - packets_available=false; - packet_no=0; + page_granule = 0; + last_granule = 0; + skip_samples = 0; + page_nb_packets = 0; + packets_available = false; + packet_no = 0; int eos = 0; @@ -338,14 +307,14 @@ void AudioStreamPlaybackSpeex::reload() { /*Get the ogg buffer for writing*/ int nb_read = MIN(data.size() - read_ofs, READ_CHUNK); - char* ogg_dst = ogg_sync_buffer(&oy, nb_read); + char *ogg_dst = ogg_sync_buffer(&oy, nb_read); /*Read bitstream from input file*/ copymem(ogg_dst, &data[read_ofs], nb_read); read_ofs += nb_read; ogg_sync_wrote(&oy, nb_read); /*Loop for all complete pages we got (most likely only one)*/ - while (ogg_sync_pageout(&oy, &og)==1) { + while (ogg_sync_pageout(&oy, &og) == 1) { if (stream_init == 0) { ogg_stream_init(&os, ogg_page_serialno(&og)); @@ -355,46 +324,38 @@ void AudioStreamPlaybackSpeex::reload() { ogg_stream_pagein(&os, &og); page_granule = ogg_page_granulepos(&og); page_nb_packets = ogg_page_packets(&og); - if (page_granule>0 && frame_size) - { - skip_samples = page_nb_packets*frame_size*nframes - (page_granule-last_granule); + if (page_granule > 0 && frame_size) { + skip_samples = page_nb_packets * frame_size * nframes - (page_granule - last_granule); if (ogg_page_eos(&og)) skip_samples = -skip_samples; /*else if (!ogg_page_bos(&og)) skip_samples = 0;*/ - } else - { + } else { skip_samples = 0; } - last_granule = page_granule; /*Extract all available packets*/ - while (!eos && ogg_stream_packetout(&os, &op)==1) - { + while (!eos && ogg_stream_packetout(&os, &op) == 1) { /*If first packet, process as Speex header*/ - if (packet_count==0) - { + if (packet_count == 0) { int rate = 0; int channels; st = process_header(&op, &frame_size, &rate, &nframes, &channels, &extra_headers); if (!nframes) - nframes=1; + nframes = 1; if (!st) { unload(); return; }; page_size = nframes * frame_size; - stream_srate=rate; - stream_channels=channels; - stream_minbuff_size=page_size; - + stream_srate = rate; + stream_channels = channels; + stream_minbuff_size = page_size; - } else if (packet_count==1) - { - } else if (packet_count<=1+extra_headers) - { + } else if (packet_count == 1) { + } else if (packet_count <= 1 + extra_headers) { /* Ignore extra headers */ }; }; @@ -403,26 +364,25 @@ void AudioStreamPlaybackSpeex::reload() { } while (packet_count <= extra_headers); - active=true; - + active = true; } void AudioStreamPlaybackSpeex::_bind_methods() { //ObjectTypeDB::bind_method(_MD("set_file","file"),&AudioStreamPlaybackSpeex::set_file); -// ObjectTypeDB::bind_method(_MD("get_file"),&AudioStreamPlaybackSpeex::get_file); + // ObjectTypeDB::bind_method(_MD("get_file"),&AudioStreamPlaybackSpeex::get_file); - ObjectTypeDB::bind_method(_MD("_set_bundled"),&AudioStreamPlaybackSpeex::_set_bundled); - ObjectTypeDB::bind_method(_MD("_get_bundled"),&AudioStreamPlaybackSpeex::_get_bundled); + ObjectTypeDB::bind_method(_MD("_set_bundled"), &AudioStreamPlaybackSpeex::_set_bundled); + ObjectTypeDB::bind_method(_MD("_get_bundled"), &AudioStreamPlaybackSpeex::_get_bundled); - ADD_PROPERTY( PropertyInfo(Variant::DICTIONARY,"_bundled",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_BUNDLE),_SCS("_set_bundled"),_SCS("_get_bundled")); + ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_bundled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_BUNDLE), _SCS("_set_bundled"), _SCS("_get_bundled")); //ADD_PROPERTY( PropertyInfo(Variant::STRING,"file",PROPERTY_HINT_FILE,"*.spx"),_SCS("set_file"),_SCS("get_file")); }; -void AudioStreamPlaybackSpeex::_set_bundled(const Dictionary& dict) { +void AudioStreamPlaybackSpeex::_set_bundled(const Dictionary &dict) { - ERR_FAIL_COND( !dict.has("filename")); - ERR_FAIL_COND( !dict.has("data")); + ERR_FAIL_COND(!dict.has("filename")); + ERR_FAIL_COND(!dict.has("data")); filename = dict["filename"]; data = dict["data"]; @@ -436,83 +396,69 @@ Dictionary AudioStreamPlaybackSpeex::_get_bundled() const { return d; }; +void AudioStreamPlaybackSpeex::set_data(const Vector<uint8_t> &p_data) { - -void AudioStreamPlaybackSpeex::set_data(const Vector<uint8_t>& p_data) { - - data=p_data; + data = p_data; reload(); } - void AudioStreamPlaybackSpeex::play(float p_from_pos) { - - reload(); if (!active) return; playing = true; - } -void AudioStreamPlaybackSpeex::stop(){ - +void AudioStreamPlaybackSpeex::stop() { unload(); playing = false; - } -bool AudioStreamPlaybackSpeex::is_playing() const{ +bool AudioStreamPlaybackSpeex::is_playing() const { return playing; } - -void AudioStreamPlaybackSpeex::set_loop(bool p_enable){ +void AudioStreamPlaybackSpeex::set_loop(bool p_enable) { loops = p_enable; } -bool AudioStreamPlaybackSpeex::has_loop() const{ +bool AudioStreamPlaybackSpeex::has_loop() const { return loops; } -float AudioStreamPlaybackSpeex::get_length() const{ +float AudioStreamPlaybackSpeex::get_length() const { return 0; } -String AudioStreamPlaybackSpeex::get_stream_name() const{ +String AudioStreamPlaybackSpeex::get_stream_name() const { return ""; } -int AudioStreamPlaybackSpeex::get_loop_count() const{ +int AudioStreamPlaybackSpeex::get_loop_count() const { return 0; } -float AudioStreamPlaybackSpeex::get_pos() const{ +float AudioStreamPlaybackSpeex::get_pos() const { return 0; } void AudioStreamPlaybackSpeex::seek_pos(float p_time){ - }; - - AudioStreamPlaybackSpeex::AudioStreamPlaybackSpeex() { - active=false; + active = false; st = NULL; - stream_channels=1; - stream_srate=1; - stream_minbuff_size=1; - playing=false; - - + stream_channels = 1; + stream_srate = 1; + stream_minbuff_size = 1; + playing = false; } AudioStreamPlaybackSpeex::~AudioStreamPlaybackSpeex() { @@ -520,20 +466,14 @@ AudioStreamPlaybackSpeex::~AudioStreamPlaybackSpeex() { unload(); } - - - - //////////////////////////////////////// - - -void AudioStreamSpeex::set_file(const String& p_file) { +void AudioStreamSpeex::set_file(const String &p_file) { if (this->file == p_file) return; - this->file=p_file; + this->file = p_file; if (p_file == "") { data.resize(0); @@ -541,7 +481,7 @@ void AudioStreamSpeex::set_file(const String& p_file) { }; Error err; - FileAccess* file = FileAccess::open(p_file, FileAccess::READ,&err); + FileAccess *file = FileAccess::open(p_file, FileAccess::READ, &err); if (err != OK) { data.resize(0); }; @@ -551,13 +491,12 @@ void AudioStreamSpeex::set_file(const String& p_file) { data.resize(file->get_len()); int read = file->get_buffer(&data[0], data.size()); memdelete(file); - } -RES ResourceFormatLoaderAudioStreamSpeex::load(const String &p_path, const String& p_original_path, Error *r_error) { +RES ResourceFormatLoaderAudioStreamSpeex::load(const String &p_path, const String &p_original_path, Error *r_error) { if (r_error) - *r_error=OK; + *r_error = OK; AudioStreamSpeex *stream = memnew(AudioStreamSpeex); stream->set_file(p_path); @@ -568,14 +507,14 @@ void ResourceFormatLoaderAudioStreamSpeex::get_recognized_extensions(List<String p_extensions->push_back("spx"); } -bool ResourceFormatLoaderAudioStreamSpeex::handles_type(const String& p_type) const { +bool ResourceFormatLoaderAudioStreamSpeex::handles_type(const String &p_type) const { - return (p_type=="AudioStream" || p_type=="AudioStreamSpeex"); + return (p_type == "AudioStream" || p_type == "AudioStreamSpeex"); } String ResourceFormatLoaderAudioStreamSpeex::get_resource_type(const String &p_path) const { - if (p_path.extension().to_lower()=="spx") + if (p_path.extension().to_lower() == "spx") return "AudioStreamSpeex"; return ""; } diff --git a/modules/speex/audio_stream_speex.h b/modules/speex/audio_stream_speex.h index e321d7705..70d05b8b4 100644 --- a/modules/speex/audio_stream_speex.h +++ b/modules/speex/audio_stream_speex.h @@ -29,15 +29,15 @@ #ifndef AUDIO_STREAM_SPEEX_H #define AUDIO_STREAM_SPEEX_H -#include "scene/resources/audio_stream.h" -#include "os/file_access.h" #include "io/resource_loader.h" +#include "os/file_access.h" #include "os/thread_safe.h" +#include "scene/resources/audio_stream.h" #include <speex/speex.h> +#include <speex/speex_callbacks.h> #include <speex/speex_header.h> #include <speex/speex_stereo.h> -#include <speex/speex_callbacks.h> // (akien) Prevents unbundling properly, but it's only for 2.1.x as speex // is dropped in 3.0+, so don't want to lose too much time on this. // Needed for speex_free (internal). @@ -49,7 +49,6 @@ class AudioStreamPlaybackSpeex : public AudioStreamPlayback { OBJ_TYPE(AudioStreamPlaybackSpeex, AudioStreamPlayback); - void *st; SpeexBits bits; Vector<uint8_t> data; @@ -66,8 +65,8 @@ class AudioStreamPlaybackSpeex : public AudioStreamPlayback { void reload(); ogg_sync_state oy; - ogg_page og; - ogg_packet op; + ogg_page og; + ogg_packet op; ogg_stream_state os; int nframes; int frame_size; @@ -79,23 +78,20 @@ class AudioStreamPlaybackSpeex : public AudioStreamPlayback { int stream_srate; int stream_minbuff_size; - void* process_header(ogg_packet *op, int *frame_size, int *rate, int *nframes, int *channels, int *extra_headers); + void *process_header(ogg_packet *op, int *frame_size, int *rate, int *nframes, int *channels, int *extra_headers); static void _bind_methods(); protected: - //virtual bool _can_mix() const; Dictionary _get_bundled() const; - void _set_bundled(const Dictionary& dict); + void _set_bundled(const Dictionary &dict); public: + void set_data(const Vector<uint8_t> &p_data); - - void set_data(const Vector<uint8_t>& p_data); - - virtual void play(float p_from_pos=0); + virtual void play(float p_from_pos = 0); virtual void stop(); virtual bool is_playing() const; @@ -115,42 +111,37 @@ public: virtual int get_mix_rate() const { return stream_srate; } virtual int get_minimum_buffer_size() const { return stream_minbuff_size; } - virtual int mix(int16_t* p_bufer,int p_frames); + virtual int mix(int16_t *p_bufer, int p_frames); - virtual void set_loop_restart_time(float p_time) { } //no loop restart, ignore + virtual void set_loop_restart_time(float p_time) {} //no loop restart, ignore AudioStreamPlaybackSpeex(); ~AudioStreamPlaybackSpeex(); }; - - class AudioStreamSpeex : public AudioStream { - OBJ_TYPE(AudioStreamSpeex,AudioStream); + OBJ_TYPE(AudioStreamSpeex, AudioStream); Vector<uint8_t> data; String file; -public: +public: Ref<AudioStreamPlayback> instance_playback() { - Ref<AudioStreamPlaybackSpeex> pb = memnew( AudioStreamPlaybackSpeex ); + Ref<AudioStreamPlaybackSpeex> pb = memnew(AudioStreamPlaybackSpeex); pb->set_data(data); return pb; } - void set_file(const String& p_file); - + void set_file(const String &p_file); }; - class ResourceFormatLoaderAudioStreamSpeex : 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; - }; #endif // AUDIO_STREAM_SPEEX_H diff --git a/modules/speex/register_types.cpp b/modules/speex/register_types.cpp index f5b60cf6f..19f17be40 100644 --- a/modules/speex/register_types.cpp +++ b/modules/speex/register_types.cpp @@ -34,12 +34,12 @@ static ResourceFormatLoaderAudioStreamSpeex *speex_stream_loader = NULL; void register_speex_types() { - speex_stream_loader = memnew( ResourceFormatLoaderAudioStreamSpeex ); + speex_stream_loader = memnew(ResourceFormatLoaderAudioStreamSpeex); ResourceLoader::add_resource_format_loader(speex_stream_loader); ObjectTypeDB::register_type<AudioStreamSpeex>(); } void unregister_speex_types() { - memdelete( speex_stream_loader ); + memdelete(speex_stream_loader); } |
