Javascript生成方波声音
javascript generate square wave sound
我想使用信号"1 0 0 0"生成一个方波声音。每个码(0,1)有一个图案,如图所示。
例如,代码1将产生500µs的声音,然后在1000µs时停止。信号应该从零到最大的正振幅,不会有任何负振幅。这个声音的频率是10KHz。
基本上我需要从移动设备(iPhone, Android和Windows phone 8)产生这种声音。我使用Cordova框架。有什么建议吗?
检查
var frequency = 10000;
var data = {
1: {duration:500, sleep:1000},
0: {duration:500, sleep:500}
}
var audio = new window.webkitAudioContext();
//function creates an Oscillator. In this code we are creating an Oscillator for every tune, which help you control the gain.
//If you want, you can try creating the Oscillator once and stopping/starting it as you wish.
function createOscillator(freq, duration) {
var attack = 10, //duration it will take to increase volume full sound volume, makes it more natural
gain = audio.createGain(),
osc = audio.createOscillator();
gain.connect(audio.destination);
gain.gain.setValueAtTime(0, audio.currentTime); //change to "1" if you're not fadding in/out
gain.gain.linearRampToValueAtTime(1, audio.currentTime + attack / 1000); //remove if you don't want to fade in
gain.gain.linearRampToValueAtTime(0, audio.currentTime + duration / 1000); //remove if you don't want to fade out
osc.frequency.value = freq;
osc.type = "square";
osc.connect(gain);
osc.start(0);
setTimeout(function() {
osc.stop(0);
osc.disconnect(gain);
gain.disconnect(audio.destination);
}, duration)
}
function play() {
//your pattern
var song = [1,0,1,1];
timeForNext = 0;
for (i=0;i<song.length;i++){
duration = data[song[i]].duration;
//use timeout to delay next tune sound
window.setTimeout(function(){
createOscillator(frequency, duration);
},timeForNext);
timeForNext+=data[song[i]].sleep;
}
}
//play the music
play();
这个链接有一些很好的信息http://www.bit-101.com/blog/?p=3896我用它来创建一个钢琴应用程序与Cordova前一段时间。
相关文章:
- 有可能过滤来自嵌入式YouTube的声音吗
- 如何访问声音管理器2创建的声音对象
- 如何减少在移动网络应用程序上播放声音的延迟
- 使用JS和JQuery播放和停止声音
- p5.js声音库:如何添加/删除p5.第5页中的短语().零件()
- 公共第一方客户端的适当OAuth2流
- 添加一个setTimeout方法会使jQuery悬停方法忽略setTimeout方法中的函数
- 创建一个倒计时计时器脚本,该脚本计算声音文件的持续时间,而不是特定的日期
- HTML5音频如何防止浏览器麦克风拾取所有声音
- 如何将声音添加到jjump代码中
- 整形三角形和锯齿波
- HTML/JS github页面项目没有't在使用firefox运行时加载图像或声音
- 如何使用Audio API对同一声音文件进行分层
- 斐波那契序列Javascript做while循环
- 游戏声音的无头jplayer
- 你能用sound.js在播放声音文件后5秒执行一个动作吗
- 无声声音数据uri
- 方波输出使用java脚本代码
- Javascript生成方波声音
- 如何在javascript中从输入声音生成负音频波