Hydrogenaudio Forums

Lossy Audio Compression => MP3 => MP3 - Tech => Topic started by: mypantsfelldown on 2020-08-14 07:51:15

Title: Downsample before encoding or just use lowpass?
Post by: mypantsfelldown on 2020-08-14 07:51:15
So I have a bunch of 48kHz files that I want to encode, and I'd like to know whether it's considered better practice to downsample to 44.1 before encoding or just use a lowpass. I lowpass all of my files at 20kHz regardless of the sample rate. I've read here before that LAME doesn't handle 48kHz that well, so I'm having trouble deciding what to do.
Title: Re: Downsample before encoding or just use lowpass?
Post by: magicgoose on 2020-08-14 13:48:03
Hard to tell but since 44.1kHz is most often used, I'd expect LAME is mostly tuned in this format.
However the difference is most likely too small to matter. Certainly smaller than the difference from using a more advanced format than MP3.
A bigger difference would be if the playback device favors only 1 of these sampling rates and has issues with the other (increased battery usage, or low quality resampling) -- but it's specific to the device and you'd have to test it.
If playback device is equally OK with both, I'd not bother with resampling.
Title: Re: Downsample before encoding or just use lowpass?
Post by: DVDdoug on 2020-08-14 18:26:18
When I have a 48kHz file I never downsample or filter before making an MP3.  

The LAME developers have put a LOT of time & effort into optimizing it and it's unlikely that you're going to get better performance by altering the files (or by changing the LAME defaults).
Title: Re: Downsample before encoding or just use lowpass?
Post by: [JAZ] on 2020-08-14 18:48:14
@DVDdoug : What you say is true, and what magicgoose has said is also true. Sampling rate plays a role on codecs that could have a small impact depending on the kind of signal and the trade-outs that the codec has chosen.

Specifically, on lossy codecs that work in blocks, changing the sampling rate also affects the amount of samples used for each analysis block. It's not the same 1151 samples at 44.1Khz than at 48Khz, and MP3 uses the same amount of samples for 32Khz, 44Khz and 48Khz.
That's where the sentence "LAME is mostly tuned in this format" gets its meaning. The tunings are samplerate-dependant, even if just a bit.

Of course, there is no reason to think that 48Khz will give bad results, but my suggestion is more in line with magicgoose response.
Title: Re: Downsample before encoding or just use lowpass?
Post by: mypantsfelldown on 2020-08-15 02:35:45
Thanks for the valuable info, everyone. I've decided that downsampling before encoding is probably the best option.
Title: Re: Downsample before encoding or just use lowpass?
Post by: Groove on 2020-08-15 07:35:40
I've decided that downsampling before encoding is probably the best option.
Sounds like a very sensible choice to me. 

The difference between resampling and not resampling may be small, whatever that means, but the time and cost of doing it properly by the book with batch processing in easily accessible free tools like fb2k is effectively nil.  Strikes me as crazy not to take advantage of that!

Title: Re: Downsample before encoding or just use lowpass?
Post by: magicgoose on 2020-08-15 11:37:20
I've decided that downsampling before encoding is probably the best option.
Sounds like a very sensible choice to me. 

The difference between resampling and not resampling may be small, whatever that means, but the time and cost of doing it properly by the book with batch processing in easily accessible free tools like fb2k is effectively nil.  Strikes me as crazy not to take advantage of that!
it's not as easy as it may sound if you want to be careful with continuous track transitions (a famous example is Pink Floyd - Dark Side of the Moon).
foobar2000 can "not reset DSP between tracks" but it's only on/off for the whole batch. if you turn it on while converting more than 1 album, you may get unwanted residuals at the first tracks of an album and/or timing errors. if you turn it off, you may get timing errors across tracks in one album and audible glitches in between tracks.

it's possible to do it correctly and without too much manual labor, but not in foobar2000 yet, unfortunately.
(so yeah, another argument for not resampling)
Title: Re: Downsample before encoding or just use lowpass?
Post by: Groove on 2020-08-16 09:43:12
it's not as easy as it may sound if you want to be careful with continuous track transitions (a famous example is Pink Floyd - Dark Side of the Moon).
foobar2000 can "not reset DSP between tracks" but it's only on/off for the whole batch. if you turn it on while converting more than 1 album, you may get unwanted residuals at the first tracks of an album and/or timing errors. if you turn it off, you may get timing errors across tracks in one album and audible glitches in between tracks.

it's possible to do it correctly and without too much manual labor, but not in foobar2000 yet, unfortunately.
(so yeah, another argument for not resampling)

Must admit I was not aware of any problems with fb2k, and actually had to resort to google to get a better grasp of the issue you described, and found an old thread on this forum with some discussion.

To try to test this out I ran fb2k SRC convert from 44.1kHz to 48 on a couple of albums with Dark side of the Moon at #2 (lossless to lossless), using both convert modes. 

Based on checking half the tracks there was no difference in the resulting track length down to the 1/1000 second level.

I then tried to listen for the artifacts you mention on transitions and really couldn't hear anything at all.

I don't want to sound obtuse (and generally use something else for 'serious' batch conversion) but I do use fb2k fairly often for quick and dirty conversion and wanted to try to get to the bottom of this.  Could the issue you mention have been fixed within SoX since the discussion in that old thread took place?

Title: Re: Downsample before encoding or just use lowpass?
Post by: Groove on 2020-08-16 10:43:13
. . .Just to add, in case there is a difference in resampling up and down, I went back and did the same test with 96 kHz --> 44.1 files (don't have any original 48kHz to hand).

Again there was no difference in track times down to 1/1000th of a second
Title: Re: Downsample before encoding or just use lowpass?
Post by: magicgoose on 2020-08-16 22:56:05
I then tried to listen for the artifacts you mention on transitions and really couldn't hear anything at all.

I don't want to sound obtuse (and generally use something else for 'serious' batch conversion) but I do use fb2k fairly often for quick and dirty conversion and wanted to try to get to the bottom of this.  Could the issue you mention have been fixed within SoX since the discussion in that old thread took place?
I didn't read the source code to say for sure (and I couldn't even if I tried, it's not open), but from what I've noticed some time ago, and read on the Web, if you reset the DSP state between tracks, it can't completely avoid this problem because it sees each track in isolation. I don't know if it actually implements it, but in theory it can try to be smarter-than-usual and instead of assuming that the undefined samples are zero, extrapolate the data before the track and after, and then these errors on track boundaries can be hard or impossible to notice in most places.
IIRC I tested this long time ago and could hear clicks but to be 100% certain I'd have to try again because foobar2000 and resampler DSP plugins could have had a lot of improvement since then.
However, if you don't reset DSP state between tracks, then this issue simply cannot happen, it's safer in this regard. (But then it may cause different issues if you convert a lot of albums at once, because it concatenates them all; also may be a performance penalty, because everything is done sequentially so unless the encoder is multi-threaded, it'll use only 1 thread for encoding, because it sees just 1 huge track.)
Quote
Again there was no difference in track times down to 1/1000th of a second
Individual track time changes won't be significant, and even the full album length. But the issue is that each time you go to next track, the relative delay of all subsequent samples will change because 0th sample of the next track is going to correspond to 0th sample in the output; and if there was no change of track here, it's almost never going to be an integer number. So it's a sudden sharp jitter, limited in length to 1/2 samples.
Title: Re: Downsample before encoding or just use lowpass?
Post by: Max9000 on 2020-10-15 23:13:59
If you want to make much smaller VBR MP3 files, then downsampling is a way to achieve it. 

Fun chart!
sampletrebleused-for
2205011khzaudiobooks
2400012khzinternet radio
3200016khzdigital master tape
4410022khzCD
4800024khzmovies

Considering car audio, it seems logical to use sample rate 32000 to fit more files on the VBR MP3 data CD or chip. 
If file size is a concern, downsampling could be better than lowering the VBR MP3 quality setting.

Title: Re: Downsample before encoding or just use lowpass?
Post by: rutra80 on 2020-10-16 07:34:06
Sample rate doesn't change MP3 file size.
Title: Re: Downsample before encoding or just use lowpass?
Post by: Sunhillow on 2020-10-16 09:59:30
Fun chart!
sampletrebleused-for
2205011khzaudiobooks
2400012khzinternet radio
3200016khzdigital master tape
4410022khzCD
4800024khzmovies
Digital master tape has very soon in digital era been 48 kHz, and was never 32 kHz. There have been variants at 44.1 kHz (which were recorded on analog video tape) and 56 kHz. Some DAT recorders had a longplay mode with 32 kHz/12 bit nonlinear resolution. Internet radio is mostly 44.1 kHz like CD, but using data reduction.
Title: Re: Downsample before encoding or just use lowpass?
Post by: Max9000 on 2020-10-18 01:41:19
...Internet radio is mostly 44.1 kHz like CD, but using data reduction.
Sadly, that really does happen a lot.  It is like the mistake I've made for years--setting the quality level lower and lower, trying to squeeze more tracks onto data mp3 cd or 512mb mp3 player.  The results could have been subjectively more favorable if I had downsampled before making vbr mp3's.  This is easy for sample rate 32000, with today's good resampler software.

But, with sample rate 24000, there's a trick to it--then it could help to use a processor (sa stereo tool, tdr nova, etc) to maintain a similar treble signal density (to prevent audible treble-droop).  That's straightforward if the processor is already in the chain. 
You can check it out with Foobar2000 Converter:
Downmix channels to stereo
Resampler PPHS, 24000 ultra
Noise sharpening (add Foo Delta) set to 5%
MP3 variable bitrate (may need to experiment the vbr setting)

In this example, using Foo Delta cost ~+5kbits to include the treble effect within the very same file may need it.  So, this may read like nonsense, except for radio stations that were going to use their combo processor anyway.   The efficient 70-ish kbit files could make the audible donations/bandwidth-cost complaints less frequent.

This isn't too relevant for home replay of local FLAC files.  But, you could try it for MP3 Car Audio. 

P.S.
Here is a sample rate 24000, 51kbit stereo MP3 file with 0.94 megabyte file size. 
In my opinion, this quality level, or maybe slightly higher, fits internet radio and the car too. Hear and decide for yourself. 
The reduced file is presented for educational purpose.  Mind the trumpets...
Title: Re: Downsample before encoding or just use lowpass?
Post by: Sunhillow on 2020-10-18 11:11:30
Here is a sample rate 24000, 51kbit stereo MP3 file with 0.94 megabyte file size. 
Thanks for this sample! I admit that it sounds really good considering bitrate and the fact it is MP3! Maybe enough for car audio unless the car is not really silent, but I will prefer saying in the 192 to 256 range. SD cards are quite cheap nowadays and for music playback a slow one is good enough.
I think the recording is from 1956, so you won't lose much treble with this method. Still, it does not at all sound as bad as I expected for this bitrate!
Title: Re: Downsample before encoding or just use lowpass?
Post by: rutra80 on 2020-10-18 13:16:43
44.1 kHz is MPEG-1 Layer III
24 kHz is MPEG-2(.5) Layer III
LAME uses slightly different psychoacoustics then. Not so well tuned I would say.
Still if you lowpass instead of resampling, you shall get almost the same bitrate/quality.
Title: Re: Downsample before encoding or just use lowpass?
Post by: AndyH-ha on 2020-10-18 17:00:48
Quote
Sample rate doesn't change MP3 file size.
I’m not sure what you are communicating. Reducing the sample rate makes quite a difference in file size. As an example, I took a short track (2:40) recorded from analogue and made two copies, on at 44.1kHz, the other at 22.05kHz. Obviously the wav file size is quite different.

Encoding each to mp3 using LAME V0, the lower sample rate file size is 62% of the 44.1kHz file size.

I never use CBR encoding so I don’t know if I’m doing something wrong. The higher sample rate file encodes readily, the lower sample rate file fails. Since I have no expectation of needing CBR anytime soon, as in ever, I gave up after four variations.
Title: Re: Downsample before encoding or just use lowpass?
Post by: rutra80 on 2020-10-18 17:53:17
It's not sample rate but frequency content.

44.1 kHz WAV will be big, 24 kHz one will be smaller, 44.1 kHz lowpassed at 12 kHz will be big.

44.1 VBR MP3 will be big, 24 kHz one will be smaller, but 44.1 kHz lowpassed at 12 kHz will be as small.

And CBR ones will be same size no matter the sampling rate.
Title: Re: Downsample before encoding or just use lowpass?
Post by: AndyH-ha on 2020-10-19 07:14:39
Sample rate is well defined. It is the number of samples per second, thus it is one determinate of file size (the other is bit depth, which determines the size of a sample). Sample rate determines the frequency bandwidth, the maximum frequency that may be encoded by that sample rate.

44,1 kHz, 48 kHz, 24 kHz, 32 kHz, 96 kHz. etc. are sample rates. The frequency bandwidth of any particular sample rate is 1/2 the sample rate. The actual audio signal in any particular recording may have the maximum frequency that can be encoded by that sample rate or it may have a much lower maximum frequency (or may not contain any lower frequency content, or may be complete silence. That all depends on the audio digitized,) The sample size is independent of the frequencies contained in the sample.

How may bits might be used in a lossy compression is a different matter altogether.
Title: Re: Downsample before encoding or just use lowpass?
Post by: rutra80 on 2020-10-19 09:20:53
Sample rate is well defined. It is the number of samples per second, thus it is one determinate of file size (the other is bit depth, which determines the size of a sample).
In lossy codecs a sample is something else and bit depth is ambiguous.
Sample rate determines the frequency bandwidth, the maximum frequency that may be encoded by that sample rate.
After lossy encoding there's no sample rate in that meaning. There is only frequency content which may but doesn't have to use whole frequency bandwidth. On decoding you may decode it to any sample rate (preferably not lower than twice the frequency content).
Title: Re: Downsample before encoding or just use lowpass?
Post by: Max9000 on 2020-10-20 11:52:10
Thanks for this sample! I admit that it sounds really good considering bitrate and the fact it is MP3!...
For that 51kbit MP3 file, it is gxlame5.2 (https://hydrogenaud.io/index.php?action=dlattach;topic=80509.0;attach=6500) with settings:
-S -mj -b16 -F --highpass 40 --noreplaygain -V36 -h - %d

Since I had PPHS resample to 24000 ultra, that did at least 2 more things:
treble cutoff 12khz  (just a tiny percentage different than a good FM car radio)
mpeg 2 layer 3, which is an mp3 with time-strict compression (neither pre-echo nor warbles)

Also, at post#13, I had added a very mild treble effect to sort of 'refill' the average treble signal density, so, it would sound as if there was just as much treble volume as a CD (other option, just eq up 1 or 2 db at the top).  That's ever so easy at sample rate 24000, but could get laborious at sample rates lower than that.

I think that the track may have compressed unexpectedly small because the artists were taking turns instead of sounding simultaneously.  I had expected 70-ish kbits or higher average, for good quality stereo MP3. 

It surely isn't impossible that either a lowpass or downsample could make a similar file size mp3, but there could be some large difference in subjective quality due to codec tuning.  Well, the tuning thing may get exhausting for developers; but, you could stack the deck with some downsampling prior to encoding.   
Only after downsample, could I opinion that a lowpass might be useful; so I tried the combo (of 24000 rate with 10khz lowpass, for the attached file).

And, now, way down at 35kbits, here's a mono MP3, with 0.65 megabyte file size.
It is aimed at AM radio station capacity. Notice that fore-aft positioning cues have survived 35kbit MP3.
The reduced file is presented for educational purpose.  Mind the trumpets...
Title: Re: Downsample before encoding or just use lowpass?
Post by: rutra80 on 2020-10-21 21:09:06
mpeg 2 layer 3, which is an mp3 with time-strict compression (neither pre-echo nor warbles)
Citation needed :)
Title: Re: Downsample before encoding or just use lowpass?
Post by: lvqcl on 2020-10-21 21:46:07
Quote
neither pre-echo nor warbles

IIRC it's MPEG-1 Layer 2. Which, of course, is not Layer 3.
Title: Re: Downsample before encoding or just use lowpass?
Post by: Max9000 on 2020-10-22 19:41:14
Citation needed :)
I searched but couldn't find that documentation again. However,
MPEG-1 Layer III (ordinary MP3) was made for storing
MPEG-2 Layer III (efficient MP3) was made for streaming

MPEG-1 Layer III (ordinary MP3) has sample rates of 32k/44k/48k and from 32kbits to 320kbits
MPEG-2 Layer III (efficient MP3) has sample rates of 16k/22k/24k and from 8kbits to 160kbits

Some interesting quality thresholds:
160kbit, 48k sample rate
128kbit, 44k sample rate
96kbit, 32k sample rate
80kbit, 24k sample rate

IIRC it's MPEG-1 Layer 2. Which, of course, is not Layer 3.
MP3 literally means MPEG Layer 3
Title: Re: Downsample before encoding or just use lowpass?
Post by: rutra80 on 2020-10-22 20:10:58
MPEG-2 supports lower bitrates and mostly that's why it was said that it's suitable for streaming. I don't think there's anything that could limit pre-echo or warbles - there still are long and short blocks (unless they consist of different number of samples?).
24kHz is half of 48kHz so no wonder that one could come up with 80kbit and 160kbit thresholds respectively.

That lower limit of 8kbps might be worthwhile if we don't care about anything above 12kHz, but otherwise I'd stick with lowpassed MPEG-1 in case of LAME, as it was extensively tuned for CDDA (44.1kHz) audio.
Title: Re: Downsample before encoding or just use lowpass?
Post by: Max9000 on 2020-10-23 16:16:27
... might be worthwhile if we don't care about anything above 12kHz...
I was thinking of the number of cases where that is done accidentally, such as whenever the speaker isn't pointed directly at you (aka off-beam), and listening to an ordinary radio (tends to filter lower than the 15khz spec).  The typical place for both of these, is in the car.  So, that seems like a good place to test drive some downsampled MP3's. 
With 24k sample rate, there's a cool trick of changing the average signal treble density (slightly) back up to what you'd expect; but, with 32k sample rate there's no extra steps, except for how nice the resampler can do. 

If goodly smaller files was the goal, then downsampling is effective.  For smaller MP3 files, the resampling cost is more than paid-off.   However, if large files is the goal, then resampling cost might not be appropriate.
Title: Re: Downsample before encoding or just use lowpass?
Post by: [JAZ] on 2020-10-24 00:31:28
At Max9000:
More than 20 years ago, we were listening to MP3 radios at 24kbps, or, those lucky enough to use ISDN instead of modem, could listen to some 56kbps radios.  (You would need to use a pair of ISDN channels at the same time if you would want to listen to 64kbps or more)

For some reason, you are trying to make it like we are still living at that time, and for some inconceivable reason, saying that we can go on with low quality music on a car.
I have a 64GB SDcard on my mobile phone which is enough space for all my mp3, mp4 and opus files and listening via bluetooth to the integrated car radio I can clearly hear those that are of bad quality ( very few 22Khz ones and some other low bitrate/bad quality encoding with artifacts and lowpasses between 12-14Khz).

There is literally no reason to want a 32kbps or 48kbps MP3 file sounding "not too bad" nowadays.

And obviously, there is no reason to expect to win a "smallest file" contest with MP3.
Back in 2003, with Ogg Vorbis, some tests were done with files as small as 3,2kbps.  Here you can still listen to a 46-seconds file encoded at 5.2kbps. https://sjeng.org/floggy.html
Info threads:
https://hydrogenaud.io/index.php?topic=2726.0
https://hydrogenaud.io/index.php?topic=6023.150

Official Vorbis cannot do as low, so  -q -1 at 8Khz does 12kbps "stereoish". (10 in mono), although it does sound better.

And yet, that's just history. Why would we care about them with xHE-AAC and Opus?
Title: Re: Downsample before encoding or just use lowpass?
Post by: Max9000 on 2020-10-25 05:07:11
If a little MP3 sounds bad, then it was done wrong.  But, that really proves your point. 
Doing a small+good MP3 takes some extra steps and care; however, a more modern codec is easier to use in one step.
Even with extra steps and care, a similar MP3 will be ~20% larger than an AAC file.
Well, you'd only use an MP3 if you needed it.  That is still possible. 

The question was about downsampling vs lowpass, and the answer was:
downsample if the goal is smaller files.
lowpass if the goal is not smaller files.
Highpoint/max-file-size would obscure the difference, but lowpoint reveals it (lowpoint is tiny MP3).
In fact, CD-spec cripples MP3, maybe because MP3's aren't circular?  :)  

Your bluetooth solution is brilliant for using the up-to-date decoders in the phone, not the wonky-sounding early-adopter AAC decoders typically built in a car radio (old wonky AAC also available in internet radio appliance and Sony HD radio too).   My phone is an Apple with many hours spent for quickly-lost iTunes playlists.  So, either replace the phone or the car?  Yes, eventually.  But, not immediately. 

Likewise, I guess that MP3 internet radio stations are waiting for wonky-sounding, headache-inspiring AAC early-adopter internet radio appliances to die out (same problem stalled-out HD-radio--spend extra for a headache or other poorly review, didn't get popular).  If I remember right, it was but 2 years ago that I got to listen to nice sounding AAC's via Foobar2000.  The difficult-if-not-impossible to update appliances should mostly die out in 3 or 4 years more.  But, at this time, the drop in listenership might not be worth the bandwidth savings of a newer codec.  I'd like to suppose that everyone-plays-simultaneously styles like classical have already changed to a newer codec.  But, for others, there's post#13, this same thread, with the tiny+excellent 51kbit mp3 (https://hydrogenaud.io/index.php?action=dlattach;topic=119708.0;attach=18069) example. 

Thanks for your advice.  It is good!  Using the up-to-date (preferably convenient-to-load android phone) to get up-to-date decoder signals sent to car or home radio, is very good advice.  However, this is not easily done with my iPhone and Ford (Apple vs Microsoft).  Also feel free to post an example (for educational purpose) that competes with 51kbit mp3 (https://hydrogenaud.io/index.php?action=dlattach;topic=119708.0;attach=18069).  AAC can't touch that (because the example is a fluke which should have been up at almost 80kbits for clear MP3).  Probably, Vorbis can compete with that, and more easily done too. 
Anyhow, my reason for using MP3 was to illustrate that in downsampling vs lowpass, then downsampling is the clear, exceptionally clear, winner (unless you wanted maximized file size to wipe-out the difference). 

This commentary because it isn't feasible to mention all of the details in one post; and, also because [JAZ]'s counterpoise post is both good and helpful too. 
Title: Re: Downsample before encoding or just use lowpass?
Post by: Max9000 on 2020-10-29 11:40:25
Perhaps another example is good. 
Here is Ricky Martin ft Rosie Odonnell on their CD 'Another Rosie Christmas' available for purchase:
US:  https://www.amazon.com/Another-Christmas-ODonnell-Various-Artists/dp/B00PUU81H4
UK:  https://www.amazon.co.uk/Another-Christmas-ODonnell-Various-Artists/dp/B00PUU81H4
That annotation is required in my region.  The reduced mp3 example, of one track, is presented for educational purpose. 

Perhaps consider purchasing the CD for comparison and also because it has more fun tracks.
The attached, educational purpose, stereo file is 24000 sample rate, 79kbit MP3. 
Not long ago, I was unaware of how small-size MP3's should sound and hadn't tried downsampling for MP3. 
Gosh, I had a shock.
Did the CD or the downsampled 79kbit MP3 perform better?  Hear and decide for yourself. 
SimplePortal 1.0.0 RC1 © 2008-2020