js 中的随机声音

Random sound in js

本文关键字:声音 随机 js      更新时间:2023-09-26

如何在JavaScript中创建随机声音并将其保存到桌面?

使用像Riffwave这样的库:http://codebase.es/riffwave/

"riffwave.js"是一个很小的JavaScript库,它将音频数据编码为一种格式(RIFF容器内的PCM),可用于播放带有HTML5音频元素的合成声音。

例如,随机声音生成

var data = []; 
for (var i=0; i<1000; i++) {
    // fill data with random samples
    data[i] = Math.round(255 * Math.random()); 
}
var wave = new RIFFWAVE(data); // create the wave file
/*
     pass wave.dataURI property to a server page via POST and 
     then re-send the content along with the right headers 
     for a wav file so  to enforce download
     or just redirect your client using data:uri schema, like
     location.href =  wave.dataURI 
     (but this won't force the download in every browser)
*/