How to record and play sound with hermes javascript

Hey there,

I´m started using RaspberryPi and Snips some weeks ago.
I created some little Apps using Javascript like in the documentation stated.

Now I want to record a short voice clip and play it when desired. I already found this and this topic.

Since I know only the basics of Pi and JS, I don´t know were to start in the index.js file.
For a more complex work flow I work with dialog.on within the withHermes arrow function.
This is where I would put the recording and playing. But I´m missing the syntax for this.

Maybe someone can help me with this.
Many thanks in advance
Lars

Hi @Trancestar ,

Since I know only the basics of Pi and JS, I don´t know were to start in the index.js file.
For a more complex work flow I work with dialog.on within the withHermes arrow function.

I advice using dialog.flow instead. You can also use create-snip-action that will bootstrap an action for you: https://github.com/snipsco/create-snips-action

Now I want to record a short voice clip and play it when desired.

First, record you audio sample and save it as a 16-bit PCM wav.

Here is the documentation for registering an audio sample: https://github.com/snipsco/hermes-protocol/tree/develop/platforms/hermes-javascript#events-available-for-publishing-3

Example:

// Somewhere in the code when the action starts
const audioWav = fs.readFileSync("path to your audio file")

tts.publish('register_sound', {
    soundId: 'my.sound',
    wavSound: audioWav.toString('base64'),
    wavSoundLen: audioWav.length
})

// Later in the code when you use the TTS, for instance:
dialog.flow('anIntent', (msg, flow) => {
    /* code ... */

    return 'Playing the sound [[sound:my.sound]]'
})

Hey @elbywan,

thank you for the fast answer. Your code example works. Very nice!

Is there a possibility to record and save a wav file?
If I´m right I should be able to somehow invoke the comand for recording like it is done during sam test microphone. But I have no clue where to look for this.

In the first place I used the flow commands but I want to create a longer dialog with Snips where I can jump between several actions and paths on a “speech tree”. I also looked into the github page where it is explained how to implement this. But the dialog.on command seems to be more basic and I easely achieved, what I wanted to do. In addition I would get kind of “nested” intents using flow.

Lars

Great :).

There are various software that you could use to record a wav file (like audacity for instance).

This is exactly what the dialog.flow commands are made for. if you want to jump between actions just store the handlers in variables and call them again somewhere in your speech tree.

const handler = (msg, flow) => {
   /* ... */
}

/* ... */

dialog.flow('intent', handler);

// or inside a dialog session

dialog.continue('intent', handler)

But if you prefer using dialog.on then of course you can also achieve the same result with a lot more code.

Thank you again for your help.
The recording and playing works smoothly.

I tried the dialog.flow but still encountered some issuses. I think I need to sit down and try do understand the general concept of it. Then it might be more clear for me ;).

Anyway thank you for your time.

1 Like