Save the AudioBuffer used by AudioContext as a WAV file.

この記事は約4分で読めます。
スポンサーリンク

Things I want to do

This example demonstrates how to use the AudioBuffer, which is frequently used with AudioContext in JavaScript, by converting it to a WAV file and saving it locally.

スポンサーリンク

Environment

The following environment is being used:

NodeJS + Vite

audiobuffer-to-wav

スポンサーリンク

Environment setup

NodeJS is assumed to be installed.

You will be asked for the name of the project you want to create, so enter it.

(In this article, we’ve used the format wav.)

You will be asked which framework to use, so select Vanilla.

Next, select JavaScript as the language.

The project creation is now complete.

Library Installation

Once the project creation is complete, install the necessary libraries.

cd wav
npm install
npm install audiobuffer-to-wav

That completes the preparation.

スポンサーリンク

Code modification

import

Imports are written as follows:

import audioBufferToWav from "audiobuffer-to-wav"

Convert audio buffer to wav

Converting from audioBuffer to WAV is done in one line:

 const wav = audioBufferToWav(audioBuffer)

Regarding the arguments of audioBufferToWav

The argument for audioBufferToWav is audioBuffer.

In the code, this is the value set to the buffer of the AudioBufferSourceNode.

const source = audiocontext.createBufferSource();
source.buffer = audioBuffer;

Saving the converted WAV file

The following code allows you to save a WAV file as out.wav.

const audioBlob = new Blob([wav]);
const url = URL.createObjectURL(audioBlob);
const a = document.createElement("a")
a.href = url
a.download = "out.wav"
a.click()
TypeError: Failed to construct 'Blob': The object must have a callable @@iterator property.

If the above error occurs, new Blob([wav]);of[]Please check if it is available.

The above error occurs when using `new Blob(wav)`.

スポンサーリンク

Result

I was able to convert the AudioBuffer to a WAV file and save it locally.

スポンサーリンク

Websites I used as references

GitHub - Experience-Monks/audiobuffer-to-wav: convert an AudioBuffer to .wav format
convert an AudioBuffer to .wav format. Contribute to Experience-Monks/audiobuffer-to-wav development by creating an account on GitHub.

コメント

タイトルとURLをコピーしました