简体   繁体   中英

Creating dynamic audio with Node.js

Problem

My application (morse code translator) takes user input and converts the text into sounds to be played on following the route. However, my current application will play only a single sound, and not more even if the method is called several times. And it plays it from the node process but not on the client. I feel like that requires creating some type of audio stream and then sending it to the user as part of a response, but I'll need some pointers on how to do that.

Edit

It appears that this isn't particularly easy, so I guess the cheating approach is just to have an audio file for each sound and then render an audio element for those given sounds. I'd love to see a non-trivial example of how making audio through Node.js dynamically would work.

Structure

In my server, I attempt to invoke the functions which are the short audio outbursts, but only a single sound is evoked.

Server.js
 var express = require('express'), jade = require('jade'), bodyParser = require('body-parser'), morseCode = require('./lib/translateMorseCode'), playSound = require('./lib/playSound'), sfx = require('sfx'), app = express(); app.use(bodyParser.urlencoded({ extended: true })); app.set('view engine', 'jade'); app.set('views', './views'); app.get('/', function(req, res) { res.render('index'); }); app.get('/translation', function(req, res) { var untranslated = req.query.input_text var translated = morseCode.translate(untranslated); var sounds = playSound.convert(translated); for (var i = 0; i < sounds.length; i++) { sounds[i](); } res.render('translation', { output: translated }); });
playSound.js
 var sfx = require('sfx'); module.exports = { convert: function(translation) { var audioOutput = []; translation.split("").forEach(function(sound) { if (sound === ".") { audioOutput.push(function() { sfx.ping() }); } else if (sound === '-') { audioOutput.push(function() { sfx.blow() }); } else { audioOutput.push(function() { sfx.blow() }); } }); return audioOutput; } }

I realize this solution isn't ideal, but I'm looking to just create a simple version of http://morsecode.scphillips.com/translator.html for learning nodejs.

Hey I know this is a few years too late. And this is largely for other users who might come across this. You might want to look at our API for this www.api.audio

I'd say you'd have something like a parameter https://docs.api.audio/docs/annotation#personalisation-parameters we call them personalisation parameters but you could have a list of these for morse code, and then when the morse code is sent you hear the audio associated for the morse code.

Here's a longish example in the javascript SDK - https://docs.api.audio/docs/fitness-example that might be of interest.

You'd still need to host a node server to serve this, but we built this to abstract away the challenges of serving audio dynamically - and as the writer of this post observed this can be quite tricky.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM