Rockbox Technical Forums

Rockbox Development => Feature Ideas => Topic started by: dizzie on August 11, 2008, 10:52:42 AM

Title: FS#10199 - Dynamic Range Compression
Post by: dizzie on August 11, 2008, 10:52:42 AM
Anyone willing to take a look at implementing this? It's been out there for a couple years now and sounds like a killer idea!!! Especially for those of us who use Rockbox in the car/ on the highway.

Thanks!  :)
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: delt on August 12, 2008, 11:24:31 PM
I second that. A compressor would be quite useful for listening to classical music or movie soundtracks.

But then again, i mostly use earphones, and they have a natural "compressor" property - really quiet passages that you barely hear through speakers, you hear them better through headphones, and vice versa.
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: soap on August 13, 2008, 07:29:15 AM
I second that. A compressor would be quite useful for listening to classical music or movie soundtracks.

But then again, i mostly use earphones, and they have a natural "compressor" property - really quiet passages that you barely hear through speakers, you hear them better through headphones, and vice versa.

Not to be pedantic, but headphones do not compress the dynamic range, they lower the ambient noise floor. 
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: goffa on August 15, 2008, 01:02:03 PM
I'd love to see this. Some albums blow you out of the vehicle after a quiet one.
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: saratoga on August 15, 2008, 01:46:27 PM
I'd love to see this. Some albums blow you out of the vehicle after a quiet one.

Compression won't help with that, but replaygain will, and that is already supported.   Compression just helps you with tracks that have loud and quiet portions in the same file.
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: goffa on August 15, 2008, 03:20:42 PM
Well if that's the case replaygain needs to be repaired. There are still loud and quiet albums with replaygain enabled.
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: MarcGuay on August 15, 2008, 03:30:26 PM
Simply enabling it in Rockbox isn't enough to have it take effect.  There are references in the sound files metadata tags that need to be set. 

http://en.wikipedia.org/wiki/Replaygain
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: goffa on August 15, 2008, 09:33:24 PM
ah.. learn something new every day :)

I was thinking that was something rockbox did on the fly once you enabled it.

replaygaining all my files as we speak... hopefully it works :)
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: Llorean on August 15, 2008, 09:36:17 PM
Out of curiosity, did you get this impression from any of our documentation or were you simply commenting on the feature without looking it up in the docs first?

If something in our manual suggests that it's done on the fly, please let me know so I can see about getting it fixed.
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: robin0800 on August 16, 2008, 04:15:28 AM
Quote
For replaygain to work, the songs must have been processed by a program that adds replaygain information to the ID3 tags (or Vorbis tags).

From the manual no change needed I think
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: Chronon on August 16, 2008, 01:50:28 PM
So, we already have ReplayGain for normalizing perceived loudness between tracks (or albums).  But as has been pointed out, there are other situations where compressing the dynamic range within a file can be desirable -- for example in listening to tracks with large dynamic range in noisy environments. 
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: ykevinw on September 11, 2008, 12:20:21 PM
For anyone wishing to implement this here is some info on an algorithm that works quite well:
http://vlevel.sourceforge.net/about/

There is a foobar 2000 plugin made from it:
http://stephan.kochen.nl/proj/foo_dsp_vlevel/

Hydrogenaudio thread:
http://www.hydrogenaudio.org/forums/index.php?showtopic=22057


I have used this as a dsp in foobar2k when making mp3s from lossless to play in my car or to play during dinner. It is more of a sophisticated micro volume adjuster rather than a true dynamic compressor. It doesn't seem to mung the sound at all and is very effective for classical music in high noise environments.

-kev
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: gevaerts on September 11, 2008, 12:43:46 PM
For anyone wishing to implement this here is some info on an algorithm that works quite well:
http://vlevel.sourceforge.net/about/

"It's different from other dynamic compressors because it looks ahead. "

Depending on how far it needs to look ahead this could be a problem

edit: forget that. Only a few seconds seem to be enough
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: ykevinw on September 11, 2008, 01:34:32 PM
It does look ahead, by default 2 sec. The algorithm works well for classical music which tends to ramp between loud and soft passages. It really does work just like an automatic intelligent "hand on the volume".

One odd effect is when a sudden loud sample comes up - you can notice the level ramp down just before. You can adjust this time window. This is not a big deal as most pieces tend to get quiet before a big trumpet blare to heighten the effect anyway.

Another effect is with the silence before and after a piece. The filter will ramp up to the noise level (tape hiss) if you let it. You can adjust the max gain to take care of this nicely. A gui would have to be made for these settings.

Check it out in foobar2k with a few pieces, I've been very happy with it and it would be great to be able to do this within Rockbox.
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: mborus on September 12, 2008, 03:46:16 PM
Quote
Anyone willing to take a look at implementing this? It's been out there for a couple years now and sounds like a killer idea!!! Especially for those of us who use Rockbox in the car/ on the highway.

On the Archos models there's the "Auto Volume" feature. If you set the AV Decay Time to 20ms, you get a very poor, but working compressor (which is great if you want to listen to speach in noisy environments like airplanes)

It sounds pretty awful with music, but is acceptable if it's the only way to get the volume high enough.

Regarding the look-ahead compressor: I prefer a one that lowers the volume at the last moment possible, because that's how normal sounding compressors work. For classical music, this might work fine. For dance and pop music, it sounds very strange to me.

BTW, for Windows Winamp-Users the best free compressor I know is Stereotool. (http://www.stereotool.com/) There's also a DOS command line version you can use to batch compress your music before putting it onto your Rockbox player...

Title: Re: FS#5455 - Dyanamic Range Compression
Post by: ykevinw on September 13, 2008, 02:43:09 PM
I'm not sure why you would really need a dynamic compressor for pop or dance music. It comes pretty much 100% compressed from the factory. Modern pop/rock/dance is pretty much just at one volume which is usually max. See:
http://en.wikipedia.org/wiki/Loudness_war

Replaygain works perfectly well in getting tracks of this type of music at the same average level without changing the waveform - that is if we are listening to our tracks and not running a competitive pop radio station.

A dynamic compressor is useful for dynamic music like classical when you can let it be dynamic - like in a car, or late at night, or if you have kids, or if the music is background. This music is dynamic by design because that's how it achieves emotional effect but in the preceding situations you just can't let it go at full range.

I don't think there has been much call for a dynamic compressor in Rockbox because it's a DAP OS and the high dynamic range of classical music is not a concern when you are wearing headphones - in fact you want music to be dynamic when you can.

However, I use my DAP as a source at home and in the car which is what I think the original poster was getting at. If there are many others with this situation that are into dynamic music like classical then adding a filter like vlevel would be really great. I prefer a volume leveling algorithm because it doesn't butcher the waveform. I don't code so I don't really know if this is easy or not.
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: pabouk on September 14, 2008, 10:25:17 AM
I don't think there has been much call for a dynamic compressor in Rockbox because it's a DAP OS and the high dynamic range of classical music is not a concern when you are wearing headphones - in fact you want music to be dynamic when you can.
It is a concern with headphones because in a noisy environment (which is nearly everywhere) you cannot hear quiet passages if the sound is fully dynamic and you would not like to be deafened by the loud passages. (It is very similar situation to listening to music in a car.)
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: ZincAlloy on September 14, 2008, 11:14:29 AM
another tool to mess up a perfectly well mastered recording? nothing against compressors per se, I just don't want to see them in the wrong hands.
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: Chronon on September 15, 2008, 01:25:52 PM
You can also "mess up" a perfectly well mastered recording with the EQ.
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: ZincAlloy on September 15, 2008, 03:15:35 PM
right. that's why I said "another" tool  ;D

on the othe hand: if all consumer music players had a compressor or limiter built in it would make the loudness wars redundant.
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: ykevinw on September 20, 2008, 12:37:29 AM
Don't worry ZincAlloy, it wont go into the wroing hands. 
If this compressor gets implemented I promise to use it for good and not evil  :)
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: CatBus on September 29, 2008, 05:44:49 PM
FWIW, I use a specially compressed (using the compand utility in sox) copy of my library for the car, and have discovered that per-track dynamic range compression can break gapless playback, because the sound levels at the juncture of two tracks will not necessarily match after each track has been separately processed.

So, in the event this gets implemented, it will need to be unaware of track boundaries and simply apply to the audio output stream, like the equalizer.  If it is aware of track boundaries, it will need to gently fade in and out the effect at the ends of tracks.
Title: Dynamic range compressor / limiter
Post by: Blue Dude on April 21, 2009, 07:54:47 PM
This feature is one I have been hoping for.  I play a lot of music with a large dynamic range (symphonic, instrumental, etc.) in the car and I find myself working the volume control a lot.  Since this feature doesn't seem to be immediately forthcoming, I think I'll just write it myself.  :)

The problem is that it's been a long time since I've done any programming, and I never learned C anyway.  I'm going to get myself up to speed on C just for the sake of doing it, but I'll likely need some assistance in getting around the Rockbox code.

Anyway, my plan is to implement a simple hard limiter with selectable pre-amplification.  It would be user selectable with a simple menu item: OFF, Min, Med, Max.  OFF will pass through audio with no changes.  Min, Med, and Max simply set the amount of pre-amplification of the audio, perhaps +3, +6 and +12 dB.  If DRC is active, the output buffer will be checked to make sure the audio will not be clipped.  If not, then no further changes are made.  If there will be clipping, then we can ramp down the volume output over a short period of time (a few milliseconds) leading up to the clipped sample(s) to keep it under the max signal limit (minus a bit of headroom).  The volume will then return to normal at a slower rate (call it 100ms) to prevent pumping.

Result: louder audio without digital clipping.  This does raise the noise floor, and it won't do very much for you if the audio is already reasonably loud to begin with.  It also robs the music of punch.  In fact, it'll sound much like commercial FM radio or TV ads.  This is really only intended for dynamic material in noisy environments, not critical listening.

Feedback, from users and developers, would be appreciated!
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: Chronon on April 21, 2009, 09:49:57 PM
Hi.  I merged your topic with the existing one.
Title: FS#10199 - Dynamic Range Compression / Hard Limiting
Post by: Blue Dude on May 11, 2009, 11:33:21 AM
A test patch is now available, as FS#10199.  This is an interface test only.  I'm just trying to hook into the system without compromising anything else.

I decided to go with a user selectable preamp setting vs. and OFF,MIN,MED,MAX scheme.  Amplification is selectable from 0 to +20dB, with 0 disabling the feature.  Comments are welcome, especially if you spot potential problems or bugs.
Title: Re: FS#10199 - Dynamic Range Compression / Hard Limiting
Post by: oayz on May 26, 2009, 05:44:25 PM
I decided to go with a user selectable preamp setting vs. and OFF,MIN,MED,MAX scheme.  Amplification is selectable from 0 to +20dB, with 0 disabling the feature.  Comments are welcome, especially if you spot potential problems or bugs.
I believe there are more parameters than preamp gain: attack/decay times, noise come to mind. Having a few presets may be a better option than giving an user full programmability.
Title: Re: FS#5455 - Dyanamic Range Compression
Post by: Blue Dude on May 31, 2009, 08:14:06 AM
I decided to hard code attack/delay times to a reasonable compromise.  I'm ignoring noise as a programmable component.  I wanted to leave the preamp level user selectable at least long enough to figure out if it's necessary.  For instance, I've got it set now for .5 dB increments.  That's likely too granular, maybe 1.5 or even 3 would be better?  That's getting close to the opaque Min, Med, Max scenario.

Title: Re: FS#5455 - Dynamic Range Compression
Post by: hoyo on July 06, 2009, 07:00:11 AM
i can give you an other reason why we need this feature !!!
i often hear radio dramas with H340@rockbox on boring car rides.
But, this radio dramas are recorded for quiet rooms.
There you have loud voices and quiet noises and susurrus.

If you want to understand what they whisper you pump up the Volume and if then someone screams in the drama your ears get busted.  :'(

On my home receiver is an feature called midnight mode which compressed the range hard.
On professional studio equipment you have also compressors on the microphone's from the singers.
It's an usual feature.

AFAIK at transformation from ac3 to mp3 DRC is used. Maybee you can find there some Information's how they work. --> besweet
Title: Re: FS#5455 - Dynamic Range Compression
Post by: CCCMikey on July 06, 2009, 08:36:42 PM
I too am hanging out for this.

The Palm TX did it well, but has died.

Now, rather than using the iPod I have to use a 10 year old Toshiba laptop with FooBar for playing the poddies in the car.

As an example, Merrick and Rosso podcasts have 'wipes' (sound effects between show segments) that can be twice as loud as their voices. Radio station transmitters automatically level this out but podcasts tend to have the full dynamic range - hence it's deafening.

ABC's Thank God It's Friday is a live presentation and has full dynamic range so audience cheering is much louder than the presenter telling his 'the news from nowhere.'

I guess the only other solution is to remaster the poddies before loading them into the iPod.
Title: Re: FS#5455 - Dynamic Range Compression
Post by: Chronon on July 06, 2009, 11:59:52 PM
Those of you interested in this feature, why not try out the patch and offer some feedback?  http://www.rockbox.org/tracker/task/10199
Title: Re: FS#5455 - Dynamic Range Compression
Post by: saratoga on July 07, 2009, 12:08:55 AM
That patch doesn't actually work.  I'm not sure why he posted it before actually implementing compression. 
Title: Re: FS#5455 - Dynamic Range Compression
Post by: Chronon on July 07, 2009, 12:14:58 AM
Oops.  I missed this: "This is a cleaned up version of the interface test. Functionality is still forthcoming."


====

It looks like some functionality has now been added.  Also, I updated the topic title to refer to the current patch rather than the obsolete feature request.

(clarify my edit)
Title: Re: FS#10199 - Dynamic Range Compression
Post by: Blue Dude on July 11, 2009, 02:25:34 PM
I just posted a first attempt at FS#10199.  It does work, but it's not ready for prime time yet.  I've loaded it on my Sansa E280 and it behaves itself so far.
Title: Re: FS#10199 - Dynamic Range Compression
Post by: CCCMikey on July 11, 2009, 10:13:40 PM
Excellent :) Would you like any of us to trial it and provide feedback yet?

Just converted to RockBox last week and loving it :)
Title: Re: FS#10199 - Dynamic Range Compression
Post by: Blue Dude on July 11, 2009, 11:11:47 PM
I'm working on bugfixes right now.  I'll post something tomorrow that should work better.

I've got it running on my Sansa E280 at the moment, but I've found several issues that I'm addressing.  I'll post here when a better version is out.

Update: an improved version is posted.  This one has smoother output and most bugs worked out.  Give it a try and leave feedback here please.
Title: Re: FS#10199 - Dynamic Range Compression
Post by: CCCMikey on July 15, 2009, 06:29:27 AM
Just saw this post - since I read it before it was edited, I didn't get the reply notification - will go see if I can figure out how to install it now :)

Edit: n00b alert: This exceeds my knowledge. I assume the file is at http://www.rockbox.org/tracker/task/10199?getfile=20037 - which yields a text file starting with
Code: [Select]
Index: apps/pcmbuf.c
===================================================================
--- apps/pcmbuf.c (revision 21841)
+++ apps/pcmbuf.c (working copy)

I'm guessing this file doesn't go into RockBox utility, but is either copied to the device or is 'built' somehow? Feel free to ignore this post if I'm too far below a reasonable knowledge to proceed.

If you'd like an example of a loud 'n' quiet file that would test this, you could try http://mpegmedia.abc.net.au/local/sydney/200907/r397504_1862526.mp3
Title: Re: FS#10199 - Dynamic Range Compression
Post by: soap on July 15, 2009, 08:36:22 AM
CCCMikey:
Flyspray contains (for the most part) Patches (http://www.rockbox.org/twiki/bin/view/Main/WorkingWithPatches)
Title: Re: FS#10199 - Dynamic Range Compression
Post by: nerdrunner on July 22, 2009, 09:32:44 PM
As I said in another post, a really good way to implement this would be to utilize the _microphone_ to sample the background sound & adjust the volume & dynamic range compression to match this noise floor.  This is an analogy to a program that you used to be able to get for the Palm Treo that utilized the Treo's builtin camera to sample the amount of ambient light & use this info to set the brightness & contrast on the Treo's display.  Similar ideas are used in some TV's to adjust brightness & contrast.
Title: Re: FS#10199 - Dynamic Range Compression
Post by: Llorean on July 22, 2009, 10:27:55 PM
Adjust volume based on background noise is just a recipe for hearing damage. Compression might make sense though.
Title: Re: FS#10199 - Dynamic Range Compression
Post by: nerdrunner on July 24, 2009, 01:34:36 AM
Here's a high level description of how a microphone closed loop dynamic range compression system could work.

1.  Implement a simple system to sample the microphone & compute a crude dB number for the ambient background noise.

2.  Implement a system to compute the current dB number from the playing track; this should already be available from the VU meter.

3.  Provide an exponential decay with a time constant sigma1 to filter the background noise.

4.  Provide an exponential decay with a time constant sigma2 to filter the playing track.

5.  Instead of using the volume control to set the absolute dB, utilize the volume control to set the _difference_ between the filtered playback dB and the filtered background noise dB.

6.  Yes, different players, different earphones & different microphones will all affect these numbers, but you can utilize the "volume" control to provide enough headroom above the background noise to hear the podcast and/or music.

7.  If your ears are getting blasted by the earphones with such an algorithm, then you should probably leave the environment, because the earphones are only a few dB ahead of the background dB.  When I'm riding on the subway and/or riding my bike in traffic, the earphones may well have to kick up pretty loud to still be understandable.

8.  The settings of sigma1 & sigma2 should be user controllable.
Title: Re: FS#10199 - Dynamic Range Compression
Post by: soap on July 24, 2009, 06:25:25 AM
7.  If your ears are getting blasted by the earphones with such an algorithm, then you should probably leave the environment, because the earphones are only a few dB ahead of the background dB.  When I'm riding on the subway and/or riding my bike in traffic, the earphones may well have to kick up pretty loud to still be understandable.

The appropriate response from a hearing protection point of view is to always be listening to the same, safe, volume level and to control the background noise be it though passive or active noise reduction.  A few (3) dB over 80ish traffic noise is twice as damaging!

Title: Re: FS#10199 - Dynamic Range Compression
Post by: CCCMikey on July 24, 2009, 08:22:00 AM
Dynamic Range Compression and Ambient noise-based volume adjusting are two rather different things.

Compression is (would be) most useful for those podcasts which are recorded raw and thus have the laughter much louder than the sick joke that caused it :) While this would be particularly useful for cars, etc, where there's a high amount of background noise so you end up getting deafened when someone laughs!

Ambient noise-based volume would also be useful in cars, etc but for a different purpose. Also, I wonder how many devices actually have micrphones out of the box.

Testing patches is currently out of my league at this stage I think, being a Windows guy.



Title: Re: FS#10199 - Dynamic Range Compression
Post by: Chronon on July 24, 2009, 04:58:18 PM
CCCMikey, you can build just fine on Windows.  You just need to set up a build environment as described in the docs index.

nerdrunner: I also don't like the idea of changing volume level according to sound pressure at a microphone.  I sometimes use IEMs and the volume difference between sound out there and sound "in here" can be quite dramatic (20 to 35 dB).  How is the algorithm supposed to know how loud to drive the phones when it doesn't know the actual sound pressure in my ear canal? 
Title: Re: FS#10199 - Dynamic Range Compression
Post by: Blue Dude on August 13, 2009, 11:48:51 PM
A stable patch is posted at FS#10199.  I'm interested in feedback from users of all kinds of targets.  Sansa E200 works great but I'd like to hear from everyone.
Title: Re: FS#10199 - Dynamic Range Compression
Post by: fredwbauer on October 05, 2009, 01:03:10 PM
I just updated my Sansa Fuze to version 22895 and I saw the compressor option. It's GREAT. Thank you! Thank you! (Now I can hear the other half of my music over the road noise.) This really makes my Fuze more useful.
Title: Re: FS#10199 - Dynamic Range Compression
Post by: Blue Dude on October 05, 2009, 02:32:17 PM
You're welcome!  That's why I wrote it.  Of course, I primarily wrote it so I could use it, but I don't mind if you can use it too.  ;D
Title: Re: FS#10199 - Dynamic Range Compression
Post by: dewbop on April 19, 2010, 11:44:45 PM
Well for some reason or another I hadn't been using Rockbox on my iPod over the last 6 months or so. This weekend on a long car ride I was thinking "I wonder if anybody has come up with a compressor for Rockbox?"

Lo and behold I update my Rockbox install, do a little search and what do I find! I'm not the only one who listens to dynamic music in the car!

Thanks Blue Dude for taking on the challenge and making it happen.