次のメソッドを使用して、wav データを含むバイト配列を再生しています。関数は GWT プロジェクトから呼び出されています。
この関数は音を再生しますが、ある種の地獄のモンスターのように聞こえます。サンプル レートは間違いなく正しく (サウンドは neospeech によって生成されています)、numberOfSamples のあらゆる種類の値を試しましたが、これはオーディオ データの長さを表しているようです。
numberOfSamples の値が 30000 を超えると、オーディオ ファイルの全長が再生されますが、文字化けしてひどいものになります。
それで、私は何を間違っていますか?
function playByteArray(byteArray, numberOfSamples) {
sampleRate = 8000;
if (!window.AudioContext) {
if (!window.webkitAudioContext) {
alert("Your browser does not support any AudioContext and cannot play back this audio.");
return;
}
window.AudioContext = window.webkitAudioContext;
}
var audioContext = new AudioContext();
var buffer = audioContext.createBuffer(1, numberOfSamples, sampleRate);
var buf = buffer.getChannelData(0);
for (i = 0; i < byteArray.length; ++i) {
buf[i] = byteArray[i];
}
var source = audioContext.createBufferSource();
source.buffer = buffer;
source.connect(audioContext.destination);
source.start(0);
}