/* * Copyright (c) 2024, Bar Yemini * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include namespace Web::WebAudio { GC_DEFINE_ALLOCATOR(AudioBufferSourceNode); AudioBufferSourceNode::AudioBufferSourceNode(JS::Realm& realm, GC::Ref context, AudioBufferSourceOptions const& options) : AudioScheduledSourceNode(realm, context) , m_buffer(options.buffer) , m_playback_rate(AudioParam::create(realm, options.playback_rate, NumericLimits::lowest(), NumericLimits::max(), Bindings::AutomationRate::ARate)) , m_detune(AudioParam::create(realm, options.detune, NumericLimits::lowest(), NumericLimits::max(), Bindings::AutomationRate::ARate)) , m_loop(options.loop) , m_loop_start(options.loop_start) , m_loop_end(options.loop_end) { } AudioBufferSourceNode::~AudioBufferSourceNode() = default; // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-buffer WebIDL::ExceptionOr AudioBufferSourceNode::set_buffer(GC::Ptr buffer) { // 1. Let new buffer be the AudioBuffer or null value to be assigned to buffer. auto new_buffer = buffer; // 2. If new buffer is not null and [[buffer set]] is true, throw an InvalidStateError and abort these steps. if (new_buffer && m_buffer_set) return WebIDL::InvalidStateError::create(realm(), "Buffer has already been set"_string); // 3. If new buffer is not null, set [[buffer set]] to true. if (new_buffer) m_buffer_set = true; // 4. Assign new buffer to the buffer attribute. m_buffer = new_buffer; // FIXME: 5. If start() has previously been called on this node, perform the operation acquire the content on buffer. return {}; } // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-buffer GC::Ptr AudioBufferSourceNode::buffer() const { return m_buffer; } // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-playbackrate GC::Ref AudioBufferSourceNode::playback_rate() const { return m_playback_rate; } // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-detune GC::Ref AudioBufferSourceNode::detune() const { return m_detune; } // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-loop WebIDL::ExceptionOr AudioBufferSourceNode::set_loop(bool loop) { m_loop = loop; return {}; } // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-loop bool AudioBufferSourceNode::loop() const { return m_loop; } // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-loopstart WebIDL::ExceptionOr AudioBufferSourceNode::set_loop_start(double loop_start) { m_loop_start = loop_start; return {}; } // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-loopstart double AudioBufferSourceNode::loop_start() const { return m_loop_start; } // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-loopend WebIDL::ExceptionOr AudioBufferSourceNode::set_loop_end(double loop_end) { m_loop_end = loop_end; return {}; } // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-loopend double AudioBufferSourceNode::loop_end() const { return m_loop_end; } // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-start` WebIDL::ExceptionOr AudioBufferSourceNode::start(Optional when, Optional offset, Optional duration) { (void)when; (void)offset; (void)duration; dbgln("FIXME: Implement AudioBufferSourceNode::start(when, offset, duration)"); return {}; } WebIDL::ExceptionOr> AudioBufferSourceNode::create(JS::Realm& realm, GC::Ref context, AudioBufferSourceOptions const& options) { return construct_impl(realm, context, options); } // https://webaudio.github.io/web-audio-api/#dom-audiobuffersourcenode-audiobuffersourcenode WebIDL::ExceptionOr> AudioBufferSourceNode::construct_impl(JS::Realm& realm, GC::Ref context, AudioBufferSourceOptions const& options) { // When the constructor is called with a BaseAudioContext c and an option object option, the user agent // MUST initialize the AudioNode this, with context and options as arguments. auto node = realm.create(realm, context, options); // Default options for channel count and interpretation // https://webaudio.github.io/web-audio-api/#AudioBufferSourceNode AudioNodeDefaultOptions default_options; default_options.channel_count = 2; default_options.channel_count_mode = Bindings::ChannelCountMode::Max; default_options.channel_interpretation = Bindings::ChannelInterpretation::Speakers; // FIXME: Set tail-time to no TRY(node->initialize_audio_node_options(options, default_options)); return node; } void AudioBufferSourceNode::initialize(JS::Realm& realm) { Base::initialize(realm); WEB_SET_PROTOTYPE_FOR_INTERFACE(AudioBufferSourceNode); } void AudioBufferSourceNode::visit_edges(Cell::Visitor& visitor) { Base::visit_edges(visitor); visitor.visit(m_buffer); visitor.visit(m_playback_rate); visitor.visit(m_detune); } }