我一直在試圖建立與樹莓派3的node.js運行最新raspbian和絃WAV播放:和絃音頻播放與樹莓派的node.js
- 炮擊了以aplay/mpg123的/其他程序 - 讓我只玩一次單聲
- 我試過的https://github.com/sebpiq/node-web-audio-api和https://github.com/TooTallNate/node-speaker(下面的示例代碼),但音頻質量相結合是非常低的,用了很多的扭曲
的有什麼我在這裏失蹤?我知道我可以用另一種編程語言輕鬆地完成它(我可以用SDL編寫C++代碼,用pygame編寫Python),但是問題是node.js是否可以使用:
這是我的當前web-音頻API +節點喇叭代碼:
var AudioContext = require('web-audio-api').AudioContext;
var Speaker = require('speaker');
var fs = require('fs');
var track1 = './tracks/1.wav';
var track2 = './tracks/1.wav';
var context = new AudioContext();
context.outStream = new Speaker({
channels: context.format.numberOfChannels,
bitDepth: context.format.bitDepth,
sampleRate: context.format.sampleRate
});
function play(audioBuffer) {
if (!audioBuffer) { return; }
var bufferSource = context.createBufferSource();
bufferSource.connect(context.destination);
bufferSource.buffer = audioBuffer;
bufferSource.loop = false;
bufferSource.start(0);
}
var audioData1 = fs.readFileSync(track1);
var audioData2 = fs.readFileSync(track2);
var audioBuffer1, audioBuffer2;
context.decodeAudioData(audioData1, function(audioBuffer) {
audioBuffer1 = audioBuffer;
if (audioBuffer1 && audioBuffer2) { playBoth(); }
});
context.decodeAudioData(audioData2, function(audioBuffer) {
audioBuffer2 = audioBuffer;
if (audioBuffer1 && audioBuffer2) { playBoth(); }
});
function playBoth() {
console.log('playing...');
play(audioBuffer1);
play(audioBuffer2);
}
音頻已經是44.1kHz和16bit深度;它在C++和Python中運行得非常好,所以我認爲我用JS代碼做錯了,或者node-speaker/node-web-audio-api實現有問題。我認爲RAM和CPU不應該是一個問題,因爲它在其他實現中起作用。 –