Skip to content

Commit e55e646

Browse files
committed
Add audio preview controls to all AudioStream subclasses
1 parent a8e37fc commit e55e646

2 files changed

Lines changed: 43 additions & 18 deletions

File tree

editor/audio/audio_stream_editor_plugin.cpp

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "editor/editor_string_names.h"
3636
#include "editor/settings/editor_settings.h"
3737
#include "editor/themes/editor_scale.h"
38-
#include "scene/resources/audio_stream_wav.h"
3938
#include "servers/rendering/rendering_server.h"
4039

4140
// AudioStreamEditor
@@ -75,17 +74,31 @@ void AudioStreamEditor::_notification(int p_what) {
7574
}
7675

7776
void AudioStreamEditor::_draw_preview() {
77+
if (stream.is_null()) {
78+
return;
79+
}
7880
Size2 size = get_size();
7981
int width = size.width;
8082
if (width <= 0) {
81-
return; // No points to draw.
83+
return;
84+
}
85+
if (stream->get_length() <= 0.0f) {
86+
const Color col = get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor));
87+
Ref<Font> font = get_theme_font(SNAME("status_source"), EditorStringName(EditorFonts));
88+
int font_size = get_theme_font_size(SNAME("status_source_size"), EditorStringName(EditorFonts));
89+
_preview->draw_string(font,
90+
Point2(8 * EDSCALE, size.height * 0.5f + font_size * 0.25f),
91+
TTR("Waveform preview not available"),
92+
HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, col);
93+
return;
8294
}
83-
84-
Rect2 rect = _preview->get_rect();
85-
8695
Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
8796
float preview_len = preview->get_length();
8897

98+
if (preview_len <= 0.0f) {
99+
return;
100+
}
101+
89102
Vector<Vector2> points;
90103
points.resize(width * 2);
91104

@@ -96,8 +109,8 @@ void AudioStreamEditor::_draw_preview() {
96109
float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
97110

98111
int idx = i;
99-
points.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y);
100-
points.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y);
112+
points.write[idx * 2 + 0] = Vector2(i + 1, min * size.height);
113+
points.write[idx * 2 + 1] = Vector2(i + 1, max * size.height);
101114
}
102115

103116
Vector<Color> colors = { get_theme_color(SNAME("contrast_color_2"), EditorStringName(Editor)) };
@@ -119,6 +132,9 @@ void AudioStreamEditor::_stream_changed() {
119132
}
120133

121134
void AudioStreamEditor::_play() {
135+
if (stream.is_null() || stream->get_length() <= 0.0f) {
136+
return;
137+
}
122138
if (_player->is_playing()) {
123139
_pausing = true;
124140
_player->stop();
@@ -152,13 +168,16 @@ void AudioStreamEditor::_on_finished() {
152168
}
153169

154170
void AudioStreamEditor::_draw_indicator() {
155-
if (stream.is_null()) {
156-
return;
157-
}
158-
159-
Rect2 rect = _preview->get_rect();
160-
float len = stream->get_length();
161-
float ofs_x = _current / len * rect.size.width;
171+
if (stream.is_null()) {
172+
return;
173+
}
174+
float len = stream->get_length();
175+
if (len <= 0.0f) {
176+
_current_label->set_text(String::num(_current, 2).pad_decimals(2) + " /");
177+
return;
178+
}
179+
Rect2 rect = _preview->get_rect();
180+
float ofs_x = _current / len * rect.size.width;
162181
const Color col = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
163182
Ref<Texture2D> icon = get_editor_theme_icon(SNAME("TimelineIndicator"));
164183
_indicator->draw_line(Point2(ofs_x, 0), Point2(ofs_x, rect.size.height), col, Math::round(2 * EDSCALE));
@@ -209,8 +228,12 @@ void AudioStreamEditor::set_stream(const Ref<AudioStream> &p_stream) {
209228
_player->set_stream(stream);
210229
_current = 0;
211230

212-
String text = String::num(stream->get_length(), 2).pad_decimals(2) + "s";
213-
_duration_label->set_text(text);
231+
float len = stream->get_length();
232+
if (len > 0.0f) {
233+
_duration_label->set_text(String::num(len, 2).pad_decimals(2) + "s");
234+
} else {
235+
_duration_label->set_text(TTR("dynamic"));
236+
}
214237

215238
queue_redraw();
216239
}
@@ -269,7 +292,7 @@ AudioStreamEditor::AudioStreamEditor() {
269292
// EditorInspectorPluginAudioStream
270293

271294
bool EditorInspectorPluginAudioStream::can_handle(Object *p_object) {
272-
return Object::cast_to<AudioStreamWAV>(p_object) != nullptr;
295+
return Object::cast_to<AudioStream>(p_object) != nullptr;
273296
}
274297

275298
void EditorInspectorPluginAudioStream::parse_begin(Object *p_object) {

modules/vorbis/audio_stream_ogg_vorbis.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,9 @@ double AudioStreamOggVorbis::get_loop_offset() const {
516516
}
517517

518518
double AudioStreamOggVorbis::get_length() const {
519-
ERR_FAIL_COND_V(packet_sequence.is_null(), 0);
519+
if (packet_sequence.is_null()) {
520+
return 0;
521+
}
520522
return packet_sequence->get_length();
521523
}
522524

0 commit comments

Comments
 (0)