Browse Source

Merge pull request #15980 from mrcdk/audio_stream_get_length

Expose audio streams get_length()
Rémi Verschelde 7 years ago
parent
commit
ed6bf28014

+ 6 - 6
modules/stb_vorbis/audio_stream_ogg_vorbis.cpp

@@ -115,7 +115,7 @@ void AudioStreamPlaybackOGGVorbis::seek(float p_time) {
 	if (!active)
 	if (!active)
 		return;
 		return;
 
 
-	if (p_time >= get_length()) {
+	if (p_time >= vorbis_stream->get_length()) {
 		p_time = 0;
 		p_time = 0;
 	}
 	}
 	frames_mixed = uint32_t(vorbis_stream->sample_rate * p_time);
 	frames_mixed = uint32_t(vorbis_stream->sample_rate * p_time);
@@ -123,11 +123,6 @@ void AudioStreamPlaybackOGGVorbis::seek(float p_time) {
 	stb_vorbis_seek(ogg_stream, frames_mixed);
 	stb_vorbis_seek(ogg_stream, frames_mixed);
 }
 }
 
 
-float AudioStreamPlaybackOGGVorbis::get_length() const {
-
-	return vorbis_stream->length;
-}
-
 AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() {
 AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() {
 	if (ogg_alloc.alloc_buffer) {
 	if (ogg_alloc.alloc_buffer) {
 		stb_vorbis_close(ogg_stream);
 		stb_vorbis_close(ogg_stream);
@@ -261,6 +256,11 @@ float AudioStreamOGGVorbis::get_loop_offset() const {
 	return loop_offset;
 	return loop_offset;
 }
 }
 
 
+float AudioStreamOGGVorbis::get_length() const {
+
+	return length;
+}
+
 void AudioStreamOGGVorbis::_bind_methods() {
 void AudioStreamOGGVorbis::_bind_methods() {
 
 
 	ClassDB::bind_method(D_METHOD("_set_data", "data"), &AudioStreamOGGVorbis::set_data);
 	ClassDB::bind_method(D_METHOD("_set_data", "data"), &AudioStreamOGGVorbis::set_data);

+ 2 - 2
modules/stb_vorbis/audio_stream_ogg_vorbis.h

@@ -71,8 +71,6 @@ public:
 	virtual float get_playback_position() const;
 	virtual float get_playback_position() const;
 	virtual void seek(float p_time);
 	virtual void seek(float p_time);
 
 
-	virtual float get_length() const; //if supported, otherwise return 0
-
 	AudioStreamPlaybackOGGVorbis() {}
 	AudioStreamPlaybackOGGVorbis() {}
 	~AudioStreamPlaybackOGGVorbis();
 	~AudioStreamPlaybackOGGVorbis();
 };
 };
@@ -112,6 +110,8 @@ public:
 	void set_data(const PoolVector<uint8_t> &p_data);
 	void set_data(const PoolVector<uint8_t> &p_data);
 	PoolVector<uint8_t> get_data() const;
 	PoolVector<uint8_t> get_data() const;
 
 
+	virtual float get_length() const; //if supported, otherwise return 0
+
 	AudioStreamOGGVorbis();
 	AudioStreamOGGVorbis();
 	virtual ~AudioStreamOGGVorbis();
 	virtual ~AudioStreamOGGVorbis();
 };
 };

+ 17 - 17
scene/resources/audio_stream_sample.cpp

@@ -77,7 +77,7 @@ void AudioStreamPlaybackSample::seek(float p_time) {
 	if (base->format == AudioStreamSample::FORMAT_IMA_ADPCM)
 	if (base->format == AudioStreamSample::FORMAT_IMA_ADPCM)
 		return; //no seeking in ima-adpcm
 		return; //no seeking in ima-adpcm
 
 
-	float max = get_length();
+	float max = base->get_length();
 	if (p_time < 0) {
 	if (p_time < 0) {
 		p_time = 0;
 		p_time = 0;
 	} else if (p_time >= max) {
 	} else if (p_time >= max) {
@@ -390,22 +390,6 @@ void AudioStreamPlaybackSample::mix(AudioFrame *p_buffer, float p_rate_scale, in
 	}
 	}
 }
 }
 
 
-float AudioStreamPlaybackSample::get_length() const {
-
-	int len = base->data_bytes;
-	switch (base->format) {
-		case AudioStreamSample::FORMAT_8_BITS: len /= 1; break;
-		case AudioStreamSample::FORMAT_16_BITS: len /= 2; break;
-		case AudioStreamSample::FORMAT_IMA_ADPCM: len *= 2; break;
-	}
-
-	if (base->stereo) {
-		len /= 2;
-	}
-
-	return float(len) / base->mix_rate;
-}
-
 AudioStreamPlaybackSample::AudioStreamPlaybackSample() {
 AudioStreamPlaybackSample::AudioStreamPlaybackSample() {
 
 
 	active = false;
 	active = false;
@@ -469,6 +453,22 @@ bool AudioStreamSample::is_stereo() const {
 	return stereo;
 	return stereo;
 }
 }
 
 
+float AudioStreamSample::get_length() const {
+
+	int len = data_bytes;
+	switch (format) {
+		case AudioStreamSample::FORMAT_8_BITS: len /= 1; break;
+		case AudioStreamSample::FORMAT_16_BITS: len /= 2; break;
+		case AudioStreamSample::FORMAT_IMA_ADPCM: len *= 2; break;
+	}
+
+	if (stereo) {
+		len /= 2;
+	}
+
+	return float(len) / mix_rate;
+}
+
 void AudioStreamSample::set_data(const PoolVector<uint8_t> &p_data) {
 void AudioStreamSample::set_data(const PoolVector<uint8_t> &p_data) {
 
 
 	AudioServer::get_singleton()->lock();
 	AudioServer::get_singleton()->lock();

+ 2 - 2
scene/resources/audio_stream_sample.h

@@ -77,8 +77,6 @@ public:
 
 
 	virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
 	virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
 
 
-	virtual float get_length() const; //if supported, otherwise return 0
-
 	AudioStreamPlaybackSample();
 	AudioStreamPlaybackSample();
 };
 };
 
 
@@ -137,6 +135,8 @@ public:
 	void set_stereo(bool p_enable);
 	void set_stereo(bool p_enable);
 	bool is_stereo() const;
 	bool is_stereo() const;
 
 
+	virtual float get_length() const; //if supported, otherwise return 0
+
 	void set_data(const PoolVector<uint8_t> &p_data);
 	void set_data(const PoolVector<uint8_t> &p_data);
 	PoolVector<uint8_t> get_data() const;
 	PoolVector<uint8_t> get_data() const;
 
 

+ 15 - 8
servers/audio/audio_stream.cpp

@@ -91,6 +91,13 @@ void AudioStreamPlaybackResampled::mix(AudioFrame *p_buffer, float p_rate_scale,
 }
 }
 ////////////////////////////////
 ////////////////////////////////
 
 
+void AudioStream::_bind_methods() {
+
+	ClassDB::bind_method(D_METHOD("get_length"), &AudioStream::get_length);
+}
+
+////////////////////////////////
+
 void AudioStreamRandomPitch::set_audio_stream(const Ref<AudioStream> &p_audio_stream) {
 void AudioStreamRandomPitch::set_audio_stream(const Ref<AudioStream> &p_audio_stream) {
 
 
 	audio_stream = p_audio_stream;
 	audio_stream = p_audio_stream;
@@ -136,6 +143,14 @@ String AudioStreamRandomPitch::get_stream_name() const {
 	return "RandomPitch";
 	return "RandomPitch";
 }
 }
 
 
+float AudioStreamRandomPitch::get_length() const {
+	if (audio_stream.is_valid()) {
+		return audio_stream->get_length();
+	}
+
+	return 0;
+}
+
 void AudioStreamRandomPitch::_bind_methods() {
 void AudioStreamRandomPitch::_bind_methods() {
 
 
 	ClassDB::bind_method(D_METHOD("set_audio_stream", "stream"), &AudioStreamRandomPitch::set_audio_stream);
 	ClassDB::bind_method(D_METHOD("set_audio_stream", "stream"), &AudioStreamRandomPitch::set_audio_stream);
@@ -209,14 +224,6 @@ void AudioStreamPlaybackRandomPitch::mix(AudioFrame *p_buffer, float p_rate_scal
 	}
 	}
 }
 }
 
 
-float AudioStreamPlaybackRandomPitch::get_length() const {
-	if (playing.is_valid()) {
-		return playing->get_length();
-	}
-
-	return 0;
-}
-
 AudioStreamPlaybackRandomPitch::~AudioStreamPlaybackRandomPitch() {
 AudioStreamPlaybackRandomPitch::~AudioStreamPlaybackRandomPitch() {
 	random_pitch->playbacks.erase(this);
 	random_pitch->playbacks.erase(this);
 }
 }

+ 7 - 4
servers/audio/audio_stream.h

@@ -50,8 +50,6 @@ public:
 	virtual void seek(float p_time) = 0;
 	virtual void seek(float p_time) = 0;
 
 
 	virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) = 0;
 	virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) = 0;
-
-	virtual float get_length() const = 0; //if supported, otherwise return 0
 };
 };
 
 
 class AudioStreamPlaybackResampled : public AudioStreamPlayback {
 class AudioStreamPlaybackResampled : public AudioStreamPlayback {
@@ -85,9 +83,14 @@ class AudioStream : public Resource {
 	GDCLASS(AudioStream, Resource)
 	GDCLASS(AudioStream, Resource)
 	OBJ_SAVE_TYPE(AudioStream) //children are all saved as AudioStream, so they can be exchanged
 	OBJ_SAVE_TYPE(AudioStream) //children are all saved as AudioStream, so they can be exchanged
 
 
+protected:
+	static void _bind_methods();
+
 public:
 public:
 	virtual Ref<AudioStreamPlayback> instance_playback() = 0;
 	virtual Ref<AudioStreamPlayback> instance_playback() = 0;
 	virtual String get_stream_name() const = 0;
 	virtual String get_stream_name() const = 0;
+
+	virtual float get_length() const = 0; //if supported, otherwise return 0
 };
 };
 
 
 class AudioStreamPlaybackRandomPitch;
 class AudioStreamPlaybackRandomPitch;
@@ -114,6 +117,8 @@ public:
 	virtual Ref<AudioStreamPlayback> instance_playback();
 	virtual Ref<AudioStreamPlayback> instance_playback();
 	virtual String get_stream_name() const;
 	virtual String get_stream_name() const;
 
 
+	virtual float get_length() const; //if supported, otherwise return 0
+
 	AudioStreamRandomPitch();
 	AudioStreamRandomPitch();
 };
 };
 
 
@@ -139,8 +144,6 @@ public:
 
 
 	virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
 	virtual void mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames);
 
 
-	virtual float get_length() const; //if supported, otherwise return 0
-
 	~AudioStreamPlaybackRandomPitch();
 	~AudioStreamPlaybackRandomPitch();
 };
 };