Javascript Firefox音频数据API-如何平稳地停止/暂停原始音频(没有弹出)?
我正在使用Firefox音频数据API生成原始音频,我需要停止/暂停此音频,而不会出现弹出。以下是音频数据API中的音调生成器示例:Javascript Firefox音频数据API-如何平稳地停止/暂停原始音频(没有弹出)?,javascript,api,audio,Javascript,Api,Audio,我正在使用Firefox音频数据API生成原始音频,我需要停止/暂停此音频,而不会出现弹出。以下是音频数据API中的音调生成器示例: <!DOCTYPE html> <html> <head> <title>JavaScript Audio Write Example</title> </head> <body> <input type="text" size="4" id="f
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Audio Write Example</title>
</head>
<body>
<input type="text" size="4" id="freq" value="440"><label for="hz">Hz</label>
<button onclick="start()">play</button>
<button onclick="stop()">stop</button>
<script type="text/javascript">
var timerID, audio;
function AudioDataDestination(sampleRate, readFn) {
// Initialize the audio output.
audio = new Audio();
audio.mozSetup(1, sampleRate);
var currentWritePosition = 0;
var prebufferSize = sampleRate / 2; // buffer 500ms
var tail = null;
// The function called with regular interval to populate
// the audio output buffer.
timerID = setInterval(function() {
var written;
// Check if some data was not written in previous attempts.
if(tail) {
written = audio.mozWriteAudio(tail);
currentWritePosition += written;
if(written < tail.length) {
// Not all the data was written, saving the tail...
tail = tail.slice(written);
return; // ... and exit the function.
}
tail = null;
}
// Check if we need add some data to the audio output.
var currentPosition = audio.mozCurrentSampleOffset();
var available = currentPosition + prebufferSize - currentWritePosition;
if(available > 0) {
// Request some sound data from the callback function.
var soundData = new Float32Array(available);
readFn(soundData);
// Writting the data.
written = audio.mozWriteAudio(soundData);
if(written < soundData.length) {
// Not all the data was written, saving the tail.
tail = soundData.slice(written);
}
currentWritePosition += written;
}
}, 100);
}
// Control and generate the sound.
var frequency = 0, currentSoundSample;
var sampleRate = 44100;
function requestSoundData(soundData) {
if (!frequency) {
return; // no sound selected
}
var k = 2* Math.PI * frequency / sampleRate;
for (var i=0, size=soundData.length; i<size; i++) {
soundData[i] = Math.sin(k * currentSoundSample++);
}
}
var audioDestination = new AudioDataDestination(sampleRate, requestSoundData);
function start() {
currentSoundSample = 0;
frequency = parseFloat(document.getElementById("freq").value);
}
function stop() {
frequency = 0;
//audio.volume = 0;
//clearInterval(timerID);
}
</script>
</body>
</html>
JavaScript音频编写示例
赫兹
玩
停止
var-timerID,音频;
函数AudioDataDestination(sampleRate,readFn){
//初始化音频输出。
音频=新音频();
音频设置(1,采样器);
var currentWritePosition=0;
var prebufferSize=sampleRate/2;//缓冲区500ms
var-tail=null;
//以常规间隔调用以填充的函数
//音频输出缓冲区。
timerID=setInterval(函数(){
var编写;
//检查以前的尝试中是否未写入某些数据。
如果(尾){
writed=audio.mozWriteAudio(tail);
currentWritePosition+=已写入;
如果(写入<尾部长度){
//并不是所有的数据都被写入,正在保存尾部。。。
tail=tail.slice(已写入);
返回;/…并退出该函数。
}
tail=null;
}
//检查是否需要向音频输出添加一些数据。
var currentPosition=audio.mozCurrentSampleOffset();
var available=currentPosition+prebufferSize-currentWritePosition;
如果可用(大于0){
//从回调函数请求一些声音数据。
var soundData=新的Float32Array(可用);
readFn(声音数据);
//正在写入数据。
writed=audio.mozWriteAudio(soundData);
如果(写入 对于(var i=0,size=soundData.length;它需要淡入/淡出。你知道为什么会发生弹出吗?只是猜测一下:与其设置频率=0来注入平坦的声波,不如在正弦波停止之前平滑正弦波的最末端。是的,我同意我需要平滑缓冲区中的剩余音频,并以clearInterval停止计时器(timerID)来摆脱流行音乐。但是当“尾巴”中有时还有音频时,我该怎么做呢变量?我需要“停止”按钮在每次单击音频时快速淡出音频,以获得一致的行为。这需要淡入/淡出。你知道为什么会发生弹出吗?只是猜测:与其设置频率=0以注入平坦的声波,不如在停止前平滑正弦波的最末端?是的,我同意我需要这样做平滑缓冲区中的剩余音频,并使用clearInterval(timerID)停止计时器以消除弹出。但是,当“tail”变量中有时还剩下音频时,我该怎么做?我需要停止按钮快速淡出音频,每次单击时都是一样的,以确保行为一致。