Score:1

Why does mpg321 sometimes lose the start of audio?

vn flag

I am using Lubuntu 20.04 on a Raspberry Pi 3B.

I am trying to use gtts-cli (text to speech program) to generate audio directly by:

gtts-cli "Almost no modern computer function works properly" | mpg321 -
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layer 1, 2, and 3.
Version 0.3.2-1 (2012/03/25). Written and copyrights by Joe Drew,
now maintained by Nanakos Chrysostomos and others.
Uses code from various people. See 'README' for more!
THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!
tcgetattr(): Inappropriate ioctl for device

Playing MPEG stream from - ...
MPEG 2.0 layer III, 32 kbit/s, 24000 Hz mono
                                                                            
[0:03] Decoding of - finished.
Segmentation fault

Sometimes it works, and sometimes it doesn't. When it does not work, I hear either a click, a grating sound, or a grating sound followed by the last part of the audio. It makes me think that sometimes mpg321 misses the first part of the audio, or corrupt it in some way. Even when it reproduces all the audio correctly, there is a SEGMENTATION FAULT.

I did some investigation. Firstly, to eliminate gtts-cli as the source of the problem, I recorded mp3 output from it and sent that to mpg321. e.g

gtts-cli "It is enough to drive you mad!" -o test.mp3
cat test.mp3 | mpg321 -
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layer 1, 2, and 3.
Version 0.3.2-1 (2012/03/25). Written and copyrights by Joe Drew,
now maintained by Nanakos Chrysostomos and others.
Uses code from various people. See 'README' for more!
THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!
tcgetattr(): Inappropriate ioctl for device

Playing MPEG stream from - ...
MPEG 2.0 layer III, 32 kbit/s, 24000 Hz mono
                                                                            
[0:03] Decoding of - finished.
Segmentation fault

I get the same effect. Sometimes it works and sometimes it misses all, or the start of the audio. There is also always a segmentation fault.

I checked if the problem is due to feeding input to mpg321 from stdin, by using the file directly:

mpg321 test.mp3
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layer 1, 2, and 3.
Version 0.3.2-1 (2012/03/25). Written and copyrights by Joe Drew,
now maintained by Nanakos Chrysostomos and others.
Uses code from various people. See 'README' for more!
THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!

Playing MPEG stream from test.mp3 ...
MPEG 2.0 layer III, 32 kbit/s, 24000 Hz mono

That still misses some audio at the start, but does NOT give a segmentation fault.

My audio goes to Respeaker USB output. To eliminate the possibility of the problem being in the driver or audio production downstream of mpg321, I tried:

aplay temp.wav

That plays perfectly and consistently every time.

I then installed and tried mpg123 (instead of mpg321). The results were similar, but I think slightly worse than mpg321.

I tried re-installing mpg321 - no change.

What is wrong, and more importantly how can I fix it?

muru avatar
us flag
Your MP3 test hasn't really eliminated gtts-cli as the source of the error, AFAICT (unless this `temp.wav` is output by that command). It's still some output from gtts-cli that you then indirectly pass to mpg321. That output might be corrupt and causing mpg321 to behave in unpredictable ways.
vn flag
@muru the file was 'test.mp3' and it was output from gtts-cli. I subsequently used temp.wav with aplay, just to check that audio drivers were working. I have edited question to show I am using Raspberry Pi 3B, because I just tried gtts-cli/mpg321 on an intel based machine and it works perfectly.
muru avatar
us flag
Sorry, my comment tried to say two things at once and got jumbled up. You have one test apparently independent of `gtts-cli` - the one using `temp.wav`. It doesn't say anything about `mpg321` or `gtts-cli` because it doesn't test either thing. You have another test using an MP3 from `gtts-cli` with `mpg321`. However, that output is still from `gtts-cli`, so you cannot say for sure that `mpg321` is the problem and not `gtts-cli`.
Score:0
bz flag

I was having a similar issue with calling mpg321 from a python script that was launched from a bash script via crontab on reboot. I was getting a vague error: tcgetattr(): Inappropriate ioctl for device After digging thru numerous threads and trying everything I could, I tried changing to use omxplayer instead and it seems to have solved the issue. Best I can tell it was some kind of permissions issue with launching from crontab because I could run it without any issue from a terminal session.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.