User avatar
TerryC65
Posts: 143
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK

Audio Distortion using mpg321/123 or aplay

Sat May 25, 2019 9:39 am

Hi,

My latest project is to provide a simple MP3 player to play sound effects at our local Model Town. It is running Raspbian Lite on a Pi Zero with a USB Audio Adaptor to hear the sound, but I've also tested this problem with the full Raspbian installation through the analogue jack socket. Both installations use the latest, fully updated version of Stretch.

My code invokes mpg321 from Python to play two MP3 files. However, the sound is awful! There is a persistent background clicking noise which is almost a loud as the sound we want. The annoying thing is that I used almost identical code in an earlier project (c2017) to play bells, chimes and church music and that did not have this problem and is working still.

To try to get to the bottom of this, I've copied an audio file to an SD Card containing full Raspbian and invoked the playback from the command line. Here is what it looks like:

Code: Select all

pi@raspberrypi:~ $ mpg321 01-Giacomo_Puccini-Madama_Butterfly-Un_bel_dì_vedremo.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!
Title	: Madama Butterfly: Un bel d� ve Artist : Giacomo Puccini               
Album	: The Best of � Romantic Callas  Year	 : 2001
Comment :                                Genre : Unknown                       

Playing MPEG stream from 01-Giacomo_Puccini-Madama_Butterfly-Un_bel_dì_vedremo.mp3 ...
MPEG 1.0 layer III, 48 kbit/s, 44100 Hz mono
I can see no errors there but the problem is exhibited, so it isn't the Pi or the particular installation of OS.

I had read somewhere that someone had solved an audio problem using mplayer instead of mpg321, so I installed it. This works, here is what I get:

Code: Select all

pi@raspberrypi:~ $ mplayer 01-Giacomo_Puccini-Madama_Butterfly-Un_bel_dì_vedremo.mp3 
MPlayer 1.3.0 (Debian), built with gcc-6.2.1 (C) 2000-2016 MPlayer Team
do_connect: could not connect to socket
connect: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing 01-Giacomo_Puccini-Madama_Butterfly-Un_bel_dì_vedremo.mp3.
libavformat version 57.56.101 (external)
Mismatching header version 57.56.100
Audio only file format detected.
Load subtitles in ./
==========================================================================
Opening audio decoder: [mpg123] MPEG 1.0/2.0/2.5 layers I, II, III
AUDIO: 44100 Hz, 2 ch, s16le, 160.0 kbit/11.34% (ratio: 20000->176400)
Selected audio codec: [mpg123] afm: mpg123 (MPEG 1.0/2.0/2.5 layers I, II, III)
==========================================================================
AO: [pulse] Init failed: Connection refused
Failed to initialize audio driver 'pulse'
AO: [alsa] 44100Hz 2ch s16le (2 bytes per sample)
Video: no video
Starting playback...
A: 284.2 (04:44.1) of 283.0 (04:43.0)  1.6% 


Exiting... (End of file)
I notice some interesting things. There are some errors but the music plays fine. Also mplayer invokes mpg123, so I tried using that from the command line and still got the distortion.

Can anyone shed any light on what's going on?

BTW. I posted this here instead of in the Audio Topics area because the only difference I can see between the system that I completed in 2017 and this one is the version of Raspbian.
Last edited by TerryC65 on Sat May 25, 2019 9:50 am, edited 1 time in total.

User avatar
rpiMike
Posts: 813
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Audio Distortion using mpg321/123 or aplay

Sat May 25, 2019 9:49 am

Try using omxplayer.

User avatar
TerryC65
Posts: 143
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK

Re: Audio Distortion using mpg321/123 or aplay

Sat May 25, 2019 10:00 am

rpiMike wrote:
Sat May 25, 2019 9:49 am
Try using omxplayer.
Thanks. That works, although the volume is a bit low. I tried using 'omxplayer --vol' and 'omxplayer --amp' to change it, but it seems to have no effect.

I'd still like to know why mpg123 worked in 2017 (and when invoked through mplayer) but not now. (Enquiring minds want to know :) )

User avatar
DougieLawson
Posts: 35347
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Audio Distortion using mpg321/123 or aplay

Sat May 25, 2019 10:04 am

TerryC65 wrote:
Sat May 25, 2019 10:00 am
... I tried using 'omxplayer --vol' and 'omxplayer --amp' to change it, but it seems to have no effect.
Try using alsamixer to set the master volume and enjoy the crude terminal graphics.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
TerryC65
Posts: 143
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK

Re: Audio Distortion using mpg321/123 or aplay

Sat May 25, 2019 10:27 am

DougieLawson wrote:
Sat May 25, 2019 10:04 am
Try using alsamixer to set the master volume and enjoy the crude terminal graphics.
This is weird! I had used alsamixer earlier (I'm fairly sure I was the Raspbian Lite instance) and it worked. I just tried to invoke it now and I get 'cannot open mixer: no such file or directory'. :!:

I tried installing alsa-utils and it was already there so I removed it and re-installed. Still no alsamixer. Before I start over with a clean copy of Raspbian Lite, can anyone shed any light on this?

User avatar
TerryC65
Posts: 143
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK

Re: Audio Distortion using mpg321/123 or aplay

Sat May 25, 2019 11:33 am

TerryC65 wrote:
Sat May 25, 2019 10:27 am
I had used alsamixer earlier (I'm fairly sure I was the Raspbian Lite instance) and it worked. I just tried to invoke it now and I get 'cannot open mixer: no such file or directory'. :!:
I worked it out :D alsamixer refuses to load if the default sound device is missing. I suppose it was telling me that in its own inimical UNIX way by saying ' no such file or directory', but in this case, the missing file was the audio device and not the utility itself. (I had previously set the USB Audio Adaptor as the default.)

Having done that, I had to work out how to use alsactl to store the new volume level (I'm all for utilities only doing one thing and doing that thing well, but letting alsamixer save the setting would be very useful. After all, you aren't likely to want to store a level unless you've just changed it).

My next problem was to get omxplayer to play to the USB Audio Adaptor.... Even though that was the default device, it still seemed to want to play to the audio jack on the Pi 3 (which of course isn't there on the Zero :o ).

So for anyone who ends up in this situation, this is what worked:

Code: Select all

omxplayer -o alsa:hw:1,0  01-Giacomo_Puccini-Madama_Butterfly-Un_bel_dì_vedremo.mp3
I'd still like to know what is wrong with mpg321 / 123 all of a sudden. :?

User avatar
DougieLawson
Posts: 35347
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Audio Distortion using mpg321/123 or aplay

Sat May 25, 2019 1:10 pm

Have you updated to the 4.19.42 kernel (which is now the stock (apt-get) kernel - there's no need for rpi-update)?
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
TerryC65
Posts: 143
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK

Re: Audio Distortion using mpg321/123 or aplay

Sat May 25, 2019 1:18 pm

DougieLawson wrote:
Sat May 25, 2019 1:10 pm
Have you updated to the 4.19.42 kernel (which is now the stock (apt-get) kernel - there's no need for rpi-update)?
Yes. uname -a gives 4.19.42+

User avatar
TerryC65
Posts: 143
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK

Re: Audio Distortion using mpg321/123 or aplay

Sat May 25, 2019 4:08 pm

TerryC65 wrote:
Sat May 25, 2019 11:33 am
So for anyone who ends up in this situation, this is what worked:

Code: Select all

omxplayer -o alsa:hw:1,0  01-Giacomo_Puccini-Madama_Butterfly-Un_bel_dì_vedremo.mp3
Actually it didn't. It worked when I ran it on my Pi 3, but when I transferred the SD Card to the target Pi Zero, the problem recurred.

This is strange, because back in 2017, I had originally intended to use a Pi Zero with two USB Audio Adaptors, but eventually had to use a Pi 3 because I couldn't guarantee which adaptor would be allocated to which channel. (There's a thread all about it on these forums somewhere. ) Anyway, I played around with the Zero for some weeks and don't recall having this problem.
TerryC65 wrote:
Sat May 25, 2019 11:33 am
I'd still like to know what is wrong with mpg321 / 123 all of a sudden. :?
So back to the same problem. I'm still not sure if this is a Raspbian problem, an audio driver problem or what. Since I experienced it on the Pi 3 with mpg321 but was able to play the same file OK with omxplayer, I'm leaning towards the audio driver, perhaps being masked with a more powerful processor/more memory.

Ideas anyone :?:

User avatar
TerryC65
Posts: 143
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK

Re: Audio Distortion using mpg321/123 or aplay

Sun May 26, 2019 12:54 pm

TerryC65 wrote:
Sat May 25, 2019 4:08 pm
So back to the same problem. I'm still not sure if this is a Raspbian problem, an audio driver problem or what. Since I experienced it on the Pi 3 with mpg321 but was able to play the same file OK with omxplayer, I'm leaning towards the audio driver, perhaps being masked with a more powerful processor/more memory.
I've just spent most of the intervening period since my last post trying to get to the bottom of this.

What I have now discovered is that the problem seems to be the Pi Zero not having enough processing power, memory or both. I'm really surprised at this because, as mentioned earlier, I had initially used a Zero for my bells project and I didn't recall any real problems. However, to finally put that to rest, I burnt an image of the 2017 Bells system onto an SD Card and ran that. The results were exactly the same as those obtained using the latest Raspbian Stretch image. :(

I can only conclude that the sound files that I was using back in 2017 were less 'energetic' than the ones I'm trying to use now. Back then, the files were of organ music and church bells and chimes; all fairly low frequency. Now I'm tying to play sounds that have a much higher pitch.

Anyway the lesson learnt is don't expect too much from the Zero, it is quite a low performance device.

I've now purchased a PHAT DAC which should so the job, but of course is three to four times the price of the Zero (which is still needed).

Chimneyfactory
Posts: 23
Joined: Sun May 19, 2019 1:30 pm

Re: Audio Distortion using mpg321/123 or aplay

Sun May 26, 2019 6:25 pm

I've been having problems with distorted audio since the kernel update.

The solution/workaround for me was to remove pulseaudio. So if you don't think you need it, try

Code: Select all

sudo apt-get remove pulseaudio
reboot and see if the sound problem goes away.

User avatar
TerryC65
Posts: 143
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK

Re: Audio Distortion using mpg321/123 or aplay

Sun May 26, 2019 6:31 pm

Chimneyfactory wrote:
Sun May 26, 2019 6:25 pm
The solution/workaround for me was to remove pulseaudio.
Pulseaudio wasn't installed on Raspbian Lite so I tried installing it!

No improvement and no worse either.

Chimneyfactory
Posts: 23
Joined: Sun May 19, 2019 1:30 pm

Re: Audio Distortion using mpg321/123 or aplay

Sun May 26, 2019 9:48 pm

TerryC65 wrote:
Sun May 26, 2019 6:31 pm
Chimneyfactory wrote:
Sun May 26, 2019 6:25 pm
The solution/workaround for me was to remove pulseaudio.
Pulseaudio wasn't installed on Raspbian Lite so I tried installing it!

No improvement and no worse either.
It seems Pulseaudio isn't installed by default on the full fat Raspbian either, but some other software install pulls it in as I found it on my system when the impaired audio started after the Kernel upgrade. Removing Pulseaudio sorted the sound issue out for me without having any other obvious downsides.

Hence I wondered if that's what had happened to you.

User avatar
TerryC65
Posts: 143
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK

Re: Audio Distortion using mpg321/123 or aplay

Mon May 27, 2019 6:06 am

Chimneyfactory wrote:
Sun May 26, 2019 9:48 pm
It seems Pulseaudio isn't installed by default on the full fat Raspbian either, but some other software install pulls it in as I found it on my system when the impaired audio started after the Kernel upgrade. Removing Pulseaudio sorted the sound issue out for me without having any other obvious downsides.
Are you using a Pi Zero? I used a Pi 3 for my Bells project and the audio certainly isn't HiFi, but it's adequate for the job. If I put my Sound Effects SD Card into a Pi 3, the audio is generally OK (although I did hear some crackles and pops at one point). When I moved the SD Card back to the Zero, the audio was awful.

When you are listening to a 1950s phone ringing or an old 'Crapper' type loo flushing and think that the audio is awful, then you know it is. When listening to the music that I used as a test piece then the term 'Total Harmonic Distortion' was very apt since the harmonics were totally distorted. :!:

energyi
Posts: 139
Joined: Tue Mar 24, 2015 9:39 pm

Re: Audio Distortion using mpg321/123 or aplay

Sun Jun 30, 2019 2:47 am

What files have to be removed or purged to totally remove pulseaudio?

Code: Select all

pi@raspberrypi:~ $ dpkg -l | grep pulseaudio
ii  pulseaudio-dlna                       0.5.3+git20170406-1                   all          Stream audio to DLNA devices and Chromecasts
ii  pulseaudio-utils                      12.2-4                                armhf        Command line tools for the PulseAudio sound server
ii  xfce4-pulseaudio-plugin:armhf         0.4.1-1                               armhf        Xfce4 panel plugin to control pulseaudio
Thanks
energyi

Return to “Raspbian”