How to play a sound?

I am wanting to play a sound (MP3) on a ubuntu machine.

I have got 3 nodes but can't get any of them to work.

Say the file is called "sound.mp3". It can be where ever you want.

How would I got it to play when I send a signal to the node?

Basically this:

Screenshot%20from%202019-08-20%2009-27-28

I know that's a function node. But just to show the idea of what I am saying.

I went a bit rogue and tried this too:

[{"id":"54c7109a.bf10d8","type":"inject","z":"9c1e5490.5d70b","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":580,"wires":[["de798816.862268"]]},{"id":"ffe9a2de.ed2d5","type":"debug","z":"9c1e5490.5d70b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":560,"wires":[]},{"id":"de798816.862268","type":"exec","z":"9c1e5490.5d70b","command":"mpg123 /home/me/.node-red/public/Sounds/Bell-ding-loud.mp3","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Ding","x":300,"y":580,"wires":[[],["ffe9a2de.ed2d5"],["d782c383.3f657"]]},{"id":"d782c383.3f657","type":"debug","z":"9c1e5490.5d70b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":600,"wires":[]}]

SILENCE.

But the outputs are:

High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3	version 1.25.10; written and copyright by Michael Hipp and others	free software (LGPL) without any warranty but with best wishesDirectory: /home/me/.node-red/public/Sounds/Playing MPEG stream 1 of 1: Bell-ding-loud.mp3 ...MPEG 1.0 L III cbr192 44100 j-s[0:06] Decoding of Bell-ding-loud.mp3 finished.

And {"code":0} for the second debug node.

If I do it from the CLI it plays.

Where's the elephant?

Looking at your exec sample flow I believe you need the full path to the mpg123 command, something like /usr/bin/mpg123 depending where your system puts it. Also you might try exec mode instead of spawn mode, although I don't think it should really matter in this case.

1 Like

Appreciated.

I would think that mpg123 is being called. It is shown in the output from the exec node.
That is pretty well what I see when I type it in the CLI.

So I don't think the path needs to be specified. If it wasn't finding the command, I wouldn't be seeing the
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3 version 1.25.10; written and copyright by Michael Hipp and others etc

Where is the flow running ? Where are the speakers plugged in ? Rc of 0 looks like it should have worked.

It is running on THIS machine. Speakers - via HDMI.

mpg123 is installed.

If I use mpg123 to play the sound from the cli, it works.

Edited title, unless someone is searching for "dumb" it is not very useful.
Can we use descriptive titles ?

3 Likes

On my Ubuntu desktop I had to use:

mpg123 -a /dev/audio /absolute/path/sound.mp3

In order to get your sample flow to play.

Might help to change your Pi from auto (the normal default) to HDMI in raspi-config.

This isn't a RPI at this stage.

I'm on a UBUNTU NUC at this stage.

It may go to a RPI but for now, it can't/won't.

Thanks. Shall try and get back to you.

Well.... Alas other than a lot more happening in the debug window (right of the screen) nothing much happened.

Doing what you say form a TERMINAL (CLI) it really goes mental.
Lots of output with errors but the sound plays.

But just the vanilla:
mpg123 /path/to/sound/file.mp3 works.

Appreciated though.

I'm not sure there is a better node to play an MP3 file to the computer's speakers.

What errors when you run in terminal?

None from terminal.

me@me-desktop:~$ mpg123 /home/me/.node-red/public/Sounds/Bell-ding-loud.mp3 
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
	version 1.25.10; written and copyright by Michael Hipp and others
	free software (LGPL) without any warranty but with best wishes

Directory: /home/me/.node-red/public/Sounds/

Terminal control enabled, press 'h' for listing of keys and functions.

Playing MPEG stream 1 of 1: Bell-ding-loud.mp3 ...

MPEG 1.0 L III cbr192 44100 j-s

[0:06] Decoding of Bell-ding-loud.mp3 finished.
me@me-desktop:~$ 

Adding what was suggested I get this:

me@me-desktop:~$ mpg123 -a /dev/audio /home/me/.node-red/public/Sounds/Bell-ding-loud.mp3 
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
	version 1.25.10; written and copyright by Michael Hipp and others
	free software (LGPL) without any warranty but with best wishes
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock

Directory: /home/me/.node-red/public/Sounds/

Terminal control enabled, press 'h' for listing of keys and functions.

Playing MPEG stream 1 of 1: Bell-ding-loud.mp3 ...

MPEG 1.0 L III cbr192 44100 j-s
ALSA lib pcm.c:8306:(snd_pcm_recover) underrun occurred
ALSA lib pcm.c:8306:(snd_pcm_recover) underrun occurred
ALSA lib pcm.c:8306:(snd_pcm_recover) underrun occurred
ALSA lib pcm.c:8306:(snd_pcm_recover) underrun occurred

[0:06] Decoding of Bell-ding-loud.mp3 finished.
me@me-desktop:~$ 

Both cases the sound plays.

So /dev/audio doesn't exist on your system. Try /dev/dsp

Yeah, ok, but if I just do the command:
mpg123 (sound file) it works.
I showed that.

Can you explain why the /dev/dsp is needed?

Not really, for some reason when you run it from the exec node it does not have a default device enabled (or it points to the wrong device), so you have to specify it explicitly.
What does
ls -l /dev/dsp* show?

me@me-desktop:~$ ls -l /dev/dsp*
ls: cannot access '/dev/dsp*': No such file or directory
me@me-desktop:~$ 

Well that is just ridiculous. I can't explain that at all.

Could be an device located in /dev/snd/

Try:
aplay -l

In my case that shows up as:

**** List of PLAYBACK Hardware Devices ****
card 0: HDMI [HDA Intel HDMI], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: HDMI [HDA Intel HDMI], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: HDMI [HDA Intel HDMI], device 8: HDMI 2 [HDMI 2]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: PCH [HDA Intel PCH], device 0: ALC3239 Analog [ALC3239 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

The device is card 1, subdevice 0 in my case.

mpg123 -a hw:1,0 <file>

But /dev/dsp works when @Trying_to_learn runs mpg123 from an exec node. In which case I don't understand why ls /dev/dsp doesn't find it.

1 Like

In reply:
me@me-desktop:~$ aplay -l **** List of PLAYBACK Hardware Devices **** card 0: PCH [HDA Intel PCH], device 0: ALC283 Analog [ALC283 Analog] Subdevices: 1/1 Subdevice #0: subdevice #0 card 0: PCH [HDA Intel PCH], device 3: HDMI 0 [HDMI 0] Subdevices: 0/1 Subdevice #0: subdevice #0 card 0: PCH [HDA Intel PCH], device 7: HDMI 1 [HDMI 1] Subdevices: 1/1 Subdevice #0: subdevice #0 card 0: PCH [HDA Intel PCH], device 8: HDMI 2 [HDMI 2] Subdevices: 1/1 Subdevice #0: subdevice #0 card 0: PCH [HDA Intel PCH], device 9: HDMI 3 [HDMI 3] Subdevices: 1/1 Subdevice #0: subdevice #0 card 0: PCH [HDA Intel PCH], device 10: HDMI 4 [HDMI 4] Subdevices: 1/1 Subdevice #0: subdevice #0 me@me-desktop:~$

Now, the /dev/snd/

me@me-desktop:~$ ls /dev/snd by-path hwC0D0 pcmC0D0c pcmC0D10p pcmC0D7p pcmC0D9p timer controlC0 hwC0D2 pcmC0D0p pcmC0D3p pcmC0D8p seq me@me-desktop:~$

Does this work in node-red ?

mpg123 -a hw:0,0 /home/me/.node-red/public/Sounds/Bell-ding-loud.mp3

:frowning: No.

If I do that from the CLI it also does not play a sound.

mpg123 -a hw:0,0 /home/me/.node-red/public/Sounds/Bell-ding-loud.mp3

But if I negate the -a hw:0,0 bit, it works from the CLI.