Get precise notes with riffwave.js

蓝咒 提交于 2019-12-14 01:13:39

问题


Does anyone know if its possible to get a precise note (C, C#, D, Eb, etc) from a javascript library like riffwave.js?

The demo makes me think is possible but I'm not sure how to transpose the piano frequency for a given note into the data array required for the generated wave file.


回答1:


Sure! You'd want to create some mapping function from key to frequency (could just be a dictionary).

To synthesize a given frequency with riffwave.js, you would do something like this

function simHertz(hz) {
    var audio = new Audio();
    var wave = new RIFFWAVE();
    var data = [];

    wave.header.sampleRate = 44100;

    var seconds = 1;

    for (var i = 0; i < wave.header.sampleRate * seconds; i ++) {
        data[i] = Math.round(128 + 127 * Math.sin(i * 2 * Math.PI * hz / wave.header.sampleRate));
    }

    wave.Make(data);
    audio.src = wave.dataURI;
    return audio;
}

var audio = simHertz(1000);
audio.play();


来源:https://stackoverflow.com/questions/15326396/get-precise-notes-with-riffwave-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!