Created
February 21, 2022 18:48
-
-
Save bobbicodes/445109bf1b757d9fc8803f4c22dbcdd9 to your computer and use it in GitHub Desktop.
Pipe audio from server side to Web Audio API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://web.archive.org/web/20170715041035/http://www.willvillanueva.com/the-web-audio-api-from-nodeexpress-to-your-browser/ | |
var express = require('express'); | |
var fs = require('fs'); | |
var ejs = require('ejs'); | |
var path = require('path'); | |
var app = express(); | |
app.use(express.static(__dirname + '/public')); | |
var filepath = path.join(__dirname, 'monty.mp3'); | |
app.get('/music', function(req, res){ | |
res.set({'Content-Type': 'audio/mpeg'}); | |
var readStream = fs.createReadStream(filepath); | |
readStream.pipe(res); | |
}) | |
app.listen(8000); | |
function loadSound() { | |
var request = new XMLHttpRequest(); | |
request.open("GET", "http://localhost:8000/music", true); | |
request.responseType = "arraybuffer"; | |
request.onload = function() { | |
var Data = request.response; | |
process(Data); | |
}; | |
request.send(); | |
} | |
function process(Data) { | |
source = context.createBufferSource(); | |
context.decodeAudioData(Data, function(buffer){ | |
source.buffer = buffer; | |
source.connect(context.destination); | |
source.start(context.currentTime); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment