Reputation: 2859
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.jsvar 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.
Upvotes: 2
Views: 1051
Reputation: 2243
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 https://www.audiostack.ai/
I'd say you'd have something like a parameter https://docs.audiostack.ai/docs/dynamic-creative-optimisation-dco 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.
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.
Upvotes: 1