LibWeb: Implement AudioBuffer.copyFromChannel

This commit is contained in:
Shannon Booth 2024-05-12 14:20:40 +12:00 committed by Andreas Kling
parent 8bcaf68023
commit 5e7678d1c6
Notes: sideshowbarker 2024-07-16 23:38:54 +09:00
3 changed files with 96 additions and 4 deletions

View file

@ -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.

View file

@ -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>

View file

@ -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(Nbk,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