サウンドを録音し、再生し、.wavファイルとしてクライアントのコンピュータに保存します。3つのオブジェクトが必要です:p5.AudioIn(マイク/音源)、p5.SoundRecorder(サウンドを録音)、および p5.SoundFile(再生/保存)。
recorder save wav
Sound Utility
View Source Code
/**
* @name Record Save Audio
* @arialabel The user clicks the grey screen to begin recording audio input through their mic. When recording the screen turns red. The user clicks their mouse again to end recording and the screen turns green. If the user clicks their mouse again the audio recording is saved to their downloads as a wav file
* @description Record a sound, play it back and save
* it as a .wav file to the client's computer.
* We need three objects: a p5.AudioIn (mic / sound source),
* p5.SoundRecorder (records the sound), and a
* p5.SoundFile (play back / save).
* <p><em><span class="small"> To run this example locally, you will need the
* <a href="http://p5js.org/reference/#/libraries/p5.sound">p5.sound library</a>
* a sound file, and a running <a href="https://github.com/processing/p5.js/wiki/Local-server">local server</a>.</span></em></p>
*/
let mic, recorder, soundFile;
let state = 0; // mousePress will increment from Record, to Stop, to Play
function setup() {
// createCanvas(400, 400);
createCanvas(windowWidth, windowHeight);
background(200);
fill(0);
text('Enable mic and click the mouse to begin recording', 20, 20);
// create an audio in
mic = new p5.AudioIn();
// users must manually enable their browser microphone for recording to work properly!
mic.start();
// create a sound recorder
recorder = new p5.SoundRecorder();
// connect the mic to the recorder
recorder.setInput(mic);
// create an empty sound file that we will use to playback the recording
soundFile = new p5.SoundFile();
}
function mousePressed() {
// use the '.enabled' boolean to make sure user enabled the mic (otherwise we'd record silence)
if (state === 0 && mic.enabled) {
// Tell recorder to record to a p5.SoundFile which we will use for playback
recorder.record(soundFile);
background(255, 0, 0);
text('Recording now! Click to stop.', 20, 20);
state++;
} else if (state === 1) {
recorder.stop(); // stop recorder, and send the result to soundFile
background(0, 255, 0);
text('Recording stopped. Click to play & save', 20, 20);
state++;
} else if (state === 2) {
soundFile.play(); // play the result!
saveSound(soundFile, 'mySound.wav'); // save file
state++;
}
}
License
Source code is available on GitHub p5.js website legacy.
All examples are licensed under CC BY-NC-SA 4.0.