Jelajahi Sumber

LibWeb: Implement AudioBuffer.copyFromChannel

Shannon Booth 1 tahun lalu
induk
melakukan
5e7678d1c6

+ 9 - 0
Tests/LibWeb/Text/expected/WebAudio/AudioBuffer-copyFromChannel.txt

@@ -0,0 +1,9 @@
+Error calling copyFromChannel: IndexSizeError: Channel index is out of range
+Error calling copyFromChannel: TypeError: Not an object of type Float32Array
+0,0,0,0,0,0,0
+5,5,5,5,5,5,5
+0,0,0,0,0,0,0,0,0,0
+5,5,5,5,5,5,5,0,0,0
+2,2,2,2,2,5,5,0,0,0
+2,2,2,2,2,5,5,0,0,0
+Done.

+ 65 - 0
Tests/LibWeb/Text/input/WebAudio/AudioBuffer-copyFromChannel.html

@@ -0,0 +1,65 @@
+<script src="../include.js"></script>
+<script>
+    test(() => {
+        // Create an empty AudioBuffer
+        let audioBuffer = new AudioBuffer({
+            numberOfChannels: 2,
+            length: 7,
+            sampleRate: 8000,
+        });
+
+        // Fill channel 0 with 5
+        let channel0Data = audioBuffer.getChannelData(0);
+        for (let i = 0; i < channel0Data.length; i++) {
+            channel0Data[i] = 5;
+        }
+
+        // Fill channel 1 with 2
+        let channel1Data = audioBuffer.getChannelData(1);
+        for (let i = 0; i < channel1Data.length; i++) {
+            channel1Data[i] = 2;
+        }
+
+        // Copy into out of range channel
+        try {
+            let errorBuffer = new Float32Array(channel0Data.length);
+            audioBuffer.copyFromChannel(errorBuffer, 2);
+        } catch (e) {
+            println(`Error calling copyFromChannel: ${e}`);
+        }
+
+        // Copy into a non-Float32Array
+        try {
+            let notFloatArray = new Uint8Array(channel0Data.length);
+            audioBuffer.copyFromChannel(notFloatArray, 1, 2);
+        } catch (e) {
+            println(`Error calling copyFromChannel: ${e}`);
+        }
+
+        // Copy full channel
+        let fullBuffer = new Float32Array(channel0Data.length);
+        println(fullBuffer);
+        audioBuffer.copyFromChannel(fullBuffer, 0);
+        println(fullBuffer);
+
+        // Copy channel 0 into buffer with bigger size
+        let biggerBuffer = new Float32Array(channel0Data.length + 3);
+        println(biggerBuffer);
+        audioBuffer.copyFromChannel(biggerBuffer, 0);
+        println(biggerBuffer);
+
+        // Copy channel into buffer with offset
+        audioBuffer.copyFromChannel(biggerBuffer, 1, 2);
+        println(biggerBuffer);
+
+        // Copy channel into buffer with offset bigger than channel size.
+        audioBuffer.copyFromChannel(biggerBuffer, 1, channel1Data.length + 1);
+        println(biggerBuffer);
+
+        // Copy channel into detached buffer (no crash)
+        let detachedBuffer = new Float32Array(channel0Data.length);
+        const transferred = detachedBuffer.buffer.transfer();
+        audioBuffer.copyFromChannel(detachedBuffer, 0);
+        println("Done.");
+    });
+</script>

+ 22 - 4
Userland/Libraries/LibWeb/WebAudio/AudioBuffer.cpp

@@ -79,11 +79,29 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Float32Array>> AudioBuffer::get_channel
 }
 
 // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-copyfromchannel
-WebIDL::ExceptionOr<void> AudioBuffer::copy_from_channel(JS::Handle<WebIDL::BufferSource> const&, WebIDL::UnsignedLong channel_number, WebIDL::UnsignedLong buffer_offset) const
+WebIDL::ExceptionOr<void> AudioBuffer::copy_from_channel(JS::Handle<WebIDL::BufferSource> const& destination, WebIDL::UnsignedLong channel_number, WebIDL::UnsignedLong buffer_offset) const
 {
-    (void)channel_number;
-    (void)buffer_offset;
-    return WebIDL::NotSupportedError::create(realm(), "FIXME: Implement AudioBuffer:copy_from_channel:"_fly_string);
+    // The copyFromChannel() method copies the samples from the specified channel of the AudioBuffer to the destination array.
+    //
+    // Let buffer be the AudioBuffer with Nb frames, let Nf be the number of elements in the destination array, and k be the value
+    // of bufferOffset. Then the number of frames copied from buffer to destination is max(0,min(Nb−k,Nf)). If this is less than Nf,
+    // then the remaining elements of destination are not modified.
+    auto& vm = this->vm();
+
+    if (!is<JS::Float32Array>(*destination->raw_object()))
+        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "Float32Array");
+    auto& float32_array = static_cast<JS::Float32Array&>(*destination->raw_object());
+
+    auto const channel = TRY(get_channel_data(channel_number));
+
+    auto channel_length = channel->data().size();
+    if (buffer_offset >= channel_length)
+        return {};
+
+    u32 count = min(float32_array.data().size(), channel_length - buffer_offset);
+    channel->data().slice(buffer_offset, count).copy_to(float32_array.data());
+
+    return {};
 }
 
 // https://webaudio.github.io/web-audio-api/#dom-audiobuffer-copytochannel