Truly independent web browser
LibWeb: Report rendering progress in `OfflineAudioContext.currentTime`
Jelle Raaijmakersb8efa9412913
diff --git a/Libraries/LibWeb/WebAudio/BaseAudioContext.h b/Libraries/LibWeb/WebAudio/BaseAudioContext.hindex b94c48d500f..3c551219470 100644--- a/Libraries/LibWeb/WebAudio/BaseAudioContext.h+++ b/Libraries/LibWeb/WebAudio/BaseAudioContext.h@@ -67,7 +67,7 @@ public:GC::Ref<AudioDestinationNode> destination() const { return *m_destination; }float sample_rate() const { return m_sample_rate; }- virtual double current_time() const { return m_current_time; }+ virtual double current_time() const { return 0; }GC::Ref<AudioListener> listener() const{VERIFY(m_listener);@@ -132,8 +132,6 @@ public:void add_script_processor_node(ScriptProcessorNode&);void handle_ended_sources(ReadonlySpan<NodeID>);- void set_current_time(double current_time) { m_current_time = current_time; }-NodeID next_node_id(Badge<AudioNode>) { return ++m_next_node_id; }protected:@@ -159,7 +157,6 @@ private:u64 m_next_node_id { 0 };float m_sample_rate { 0 };- double m_current_time { 0 };GC::Ptr<AudioListener> m_listener;GC::Ref<DOM::EventTarget> m_global_object;diff --git a/Libraries/LibWeb/WebAudio/OfflineAudioContext.cpp b/Libraries/LibWeb/WebAudio/OfflineAudioContext.cppindex 988bb0ee98b..0c4a1b665de 100644--- a/Libraries/LibWeb/WebAudio/OfflineAudioContext.cpp+++ b/Libraries/LibWeb/WebAudio/OfflineAudioContext.cpp@@ -168,8 +168,6 @@ void OfflineAudioContext::finish_rendering(GC::Ref<WebIDL::Promise> promise)channel_data->viewed_array_buffer()->overwrite(channel_data->byte_offset(), samples.data(), samples.size() * sizeof(float));}- set_current_time(m_renderer->frames_rendered() / static_cast<double>(sample_rate()));-// AD-HOC: Other engines transition the context to "closed" once rendering completes.set_control_state(Bindings::AudioContextState::Closed);set_rendering_state(Bindings::AudioContextState::Closed);@@ -206,7 +204,6 @@ void OfflineAudioContext::handle_suspended(double suspend_time){set_control_state(Bindings::AudioContextState::Suspended);set_rendering_state(Bindings::AudioContextState::Suspended);- set_current_time(suspend_time);auto frame = static_cast<u64>(AK::round(suspend_time * sample_rate()));// Suspensions take effect at the beginning of the first render quantum at@@ -280,6 +277,16 @@ WebIDL::ExceptionOr<void> OfflineAudioContext::suspend(double suspend_time, GC::return {};}+// https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-currenttime+double OfflineAudioContext::current_time() const+{+ // This is the time in seconds of the sample frame immediately following the last sample-frame in the block of+ // audio most recently processed by the context's rendering graph.+ if (m_renderer)+ return m_renderer->frames_rendered() / static_cast<double>(sample_rate());+ return Base::current_time();+}+// https://webaudio.github.io/web-audio-api/#dom-offlineaudiocontext-lengthWebIDL::UnsignedLong OfflineAudioContext::length() const{diff --git a/Libraries/LibWeb/WebAudio/OfflineAudioContext.h b/Libraries/LibWeb/WebAudio/OfflineAudioContext.hindex 6944d913b94..27f12e74cf3 100644--- a/Libraries/LibWeb/WebAudio/OfflineAudioContext.h+++ b/Libraries/LibWeb/WebAudio/OfflineAudioContext.h@@ -42,6 +42,8 @@ public:WebIDL::UnsignedLong length() const;+ virtual double current_time() const override;+GC::Ptr<WebIDL::CallbackType> oncomplete();void set_oncomplete(GC::Ptr<WebIDL::CallbackType>);diff --git a/Tests/LibWeb/Text/expected/WebAudio/ScriptProcessorNode-currentTime.txt b/Tests/LibWeb/Text/expected/WebAudio/ScriptProcessorNode-currentTime.txtnew file mode 100644index 00000000000..f93c667feee--- /dev/null+++ b/Tests/LibWeb/Text/expected/WebAudio/ScriptProcessorNode-currentTime.txt@@ -0,0 +1,2 @@+currentTime in each event, in frames: 128, 384, 640, 896, 1152, 1408, 1664, 1920+source started relative to currentTime begins at the scheduled frame: truediff --git a/Tests/LibWeb/Text/input/WebAudio/ScriptProcessorNode-currentTime.html b/Tests/LibWeb/Text/input/WebAudio/ScriptProcessorNode-currentTime.htmlnew file mode 100644index 00000000000..7a1f390343c--- /dev/null+++ b/Tests/LibWeb/Text/input/WebAudio/ScriptProcessorNode-currentTime.html@@ -0,0 +1,27 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(async done => {+ const context = new OfflineAudioContext(1, 2048, 44100);+ const node = context.createScriptProcessor(256, 0, 1);+ node.connect(context.destination);++ const frames = [];+ let scheduledFrame;+ node.onaudioprocess = () => {+ const frame = Math.round(context.currentTime * context.sampleRate);+ frames.push(frame);+ if (frames.length === 3) {+ const source = new ConstantSourceNode(context);+ source.connect(context.destination);+ source.start(context.currentTime + 512 / context.sampleRate);+ scheduledFrame = frame + 512;+ }+ };++ const rendered = (await context.startRendering()).getChannelData(0);+ println(`currentTime in each event, in frames: ${frames.join(", ")}`);+ println(`source started relative to currentTime begins at the scheduled frame: ${rendered.findIndex(sample => sample !== 0) === scheduledFrame}`);+ done();+ });+</script>