MattOwnby
Posts: 58
Joined: Thu Aug 16, 2012 7:22 pm

When playing at 44100 Hz, audio frequency is closer to 44275

Wed Sep 12, 2012 6:46 am

So I am working on this project to make the Raspberry Pi play NTSC video with accompanying analog audio and as such, the video timing is fixed to NTSC standards which means the audio needs to be in sync too. I have been trying to play 44100 Hz audio and noticed that the PCM audio was being consumed by my little ALSA test program too quickly. So I concluded that whatever internal clock the Pi is using for audio is a bit off, which is not uncommon for consumer-grade sound cards, but it seems to be a bit more off than I would expect.

So I wrote a little test program that does nothing but feeds samples to the ALSA driver as fast as possible and times how fast it consumes.

I've had the program running now for 8137 seconds (long enough to get a pretty accurate average) and the audio frequency appears to be settling toward 44275 Hz (when configured to output at 44100 Hz) which means I won't be able to easily reach my goal at the moment without some further work.

Is this something that can/will be tweaked in software to make it more accurate or should I start investigating whether the openmax resampler can solve this problem for me in real-time?

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5091
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: When playing at 44100 Hz, audio frequency is closer to 4

Wed Sep 12, 2012 11:05 am

What's 48kHz like?
The board runs off a 19.2MHz oscillator. 48kHz is a integer divisor of this. 44.1KHz is not.
I would expect the PLLs that produce the audio clock will work better with an integer divisor.

However your design won't work. Even if the clocks are 100 or 1000 times more accurate, they will still drift too far apart *eventually*.
You need a feedback path for a project like this.

I don't really understand if you are planning on using openMAX for the video playback? If so, you won't be using ALSA.

MattOwnby
Posts: 58
Joined: Thu Aug 16, 2012 7:22 pm

Re: When playing at 44100 Hz, audio frequency is closer to 4

Wed Sep 12, 2012 4:32 pm

dom wrote:What's 48kHz like?
The board runs off a 19.2MHz oscillator. 48kHz is a integer divisor of this. 44.1KHz is not.
I would expect the PLLs that produce the audio clock will work better with an integer divisor.
I will try it. I just changed 44100 to 48000 in my ALSA code and now snd_pcm_hw_params is failing with "Invalid argument" so maybe I will have to switch over to OMX for this test.
However your design won't work. Even if the clocks are 100 or 1000 times more accurate, they will still drift too far apart *eventually*.
You need a feedback path for a project like this.
It doesn't need to be perfect. The video/audio will usually only be playing for less than 1 minute which means as long as the clock is reasonably close to the target, it will be good enough with some buffering. Unfortunately, 44275 seems to be too different from 44100 to stay in sync for that long.
I don't really understand if you are planning on using openMAX for the video playback? If so, you won't be using ALSA.
I currently am using openmax for jpeg decoding (which each jpeg represents one NTSC field), and gles2 for the actual rendering (it is working great, BTW). So I can still use ALSA if it makes sense to do so; I have both "working" ALSA and OMX audio code in my project and they both seem to have the same problem (which I suppose isn't surprising).

Could you give me some hints on how to find this resampler? Is it part of "audio_mixer" ? If 48khz mode is a lot closer, I could resample my source 44.1khz data to 48khz before playing it.

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5091
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: When playing at 44100 Hz, audio frequency is closer to 4

Wed Sep 12, 2012 4:48 pm

MattOwnby wrote: I will try it. I just changed 44100 to 48000 in my ALSA code and now snd_pcm_hw_params is failing with "Invalid argument" so maybe I will have to switch over to OMX for this test.
I don't see why that should fail. ALSA driver reports it support 8000-48000 as sampling rates.
Do you have a asound.conf file?
MattOwnby wrote:Could you give me some hints on how to find this resampler? Is it part of "audio_mixer" ? If 48khz mode is a lot closer, I could resample my source 44.1khz data to 48khz before playing it.
The resampler is used for HDMI audio, where the sink typically only supports a few discrete sample rates.
As the analogue audio can vary the PWM clock arbitrarily, that resampling isn't required.
There may be some way of opening a silence channel at 48kHz and then sending your 44.1kHz to force the 44.1kHz resampling, but you need to find why 48kHz is not working first.

/opt/vc/src/hello_pi/hello_audio does output 48kHz over openMAX, so you could try timing that to see if the drift is better.

MattOwnby
Posts: 58
Joined: Thu Aug 16, 2012 7:22 pm

Re: When playing at 44100 Hz, audio frequency is closer to 4

Wed Sep 12, 2012 8:39 pm

dom wrote: As the analogue audio can vary the PWM clock arbitrarily, that resampling isn't required.
Could this arbitrary variation of the PWM clock be the cause for the inaccuracies? Or is it the hardware itself?
/opt/vc/src/hello_pi/hello_audio does output 48kHz over openMAX, so you could try timing that to see if the drift is better.
Ok, I modified hello_audio and here is what I got after letting it run for a while:

Code: Select all

Total elapsed time: 4126.477000
Total frames processed: 198854656
Effective audio frequency: 48189.934416
In other words, the rate seems to be about 48190 Hz.

My changes were pretty minimal and the resulting time is close enough to 48000 that I am fairly confident that my changes don't have any defects, but if anyone wants to doublecheck, I uploaded my code to: http://www.rulecity.com/browse/rpi/audio.c.txt. Search for "// MPO" for all my changes.

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5091
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: When playing at 44100 Hz, audio frequency is closer to 4

Sun Sep 16, 2012 11:55 am

There some evidence we lose two samples every 512:
http://www.raspberrypi.org/phpBB3/viewt ... 12#p174295

which does tie up quite closely with your 0.4% speed increase.

It's unconfirmed, but it's probably worth you keeping an eye on the other thread.

MattOwnby
Posts: 58
Joined: Thu Aug 16, 2012 7:22 pm

Re: When playing at 44100 Hz, audio frequency is closer to 4

Mon Sep 24, 2012 2:02 pm

Just wanted to post here in case anyone was using this thread for help.

The firmware updated mentioned in the previous post's URL resolves this issue.

A new test shows that when configured for 44100 Hz, the audio frequency ends up being about 44101 Hz (I don't have the exact number in front of me but I did run a long test) which is close enough for most applications (including mine).

Return to “Graphics, sound and multimedia”

Who is online

Users browsing this forum: No registered users and 8 guests