Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Change sample rate of an audio with best quality? (Read 17490 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Change sample rate of an audio with best quality?

When upsampling an audio from 24kHz to 48kHz SOX does not allow me to keep quality at 32 bit, it changes the Bit depth measured in Audition from 32 to 25.
```
sox 24kHz-32-bit-floating-point-input.wav 48kHz-32-bit-floating-point-out.wav rate -v 48k
sox 24kHz-32-bit-floating-point-input.wav -b 32 48kHz-32-bit-floating-point-out.wav rate -v 48k
```
I tried the same in ffmpeg: the result is even worse, 16 bits and some artifacts on the mel spectrogram.
```
ffmpeg -i 24kHz-32-bit-floating-point-input.wav -ar 48000 48kHz-32-bit-floating-point-out.wav
```
Can you suggest any python or command line tool on Windows for upsampling with the best possible quality?

Re: Change sample rate of an audio with best quality?

Reply #1
sox doesn't seem to support floating point values for output. it's a bit strange though that Audition shows 25 bits and not 24...

ffmpeg can be used for resampling too, but beware that by default it's not using a "high quality" resampler and it needs to be explicitly chosen.

docs:
https://ffmpeg.org/ffmpeg-filters.html#aresample-1
https://ffmpeg.org/ffmpeg-resampler.html#Resampler-Options

you probably want to choose "soxr" for resampling engine, and set "precision" to 28 (or higher, but I'm not sure what's the max value).
also the output bit depth has to be explicitly set too, I don't remember exactly how, but it's doable.
a fan of AutoEq + Meier Crossfeed

Re: Change sample rate of an audio with best quality?

Reply #2
How can I install it? I downloaded soxr-0.1.3-Source, cmake and got an error:
Code: [Select]
-- The C compiler identification is unknown
CMake Error at CMakeLists.txt:6 (project):
  The CMAKE_C_COMPILER:

    cl

  is not a full path and was not found in the PATH.

  To use the NMake generator with Visual C++, cmake must be run from a shell
  that can use the compiler cl from the command line.  This environment is
  unable to invoke the cl compiler.  To fix this problem, run cmake from the
  Visual Studio Command Prompt (vcvarsall.bat).

  Tell CMake where to find the compiler by setting either the environment
  variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
  the compiler, or to the compiler name if it is in the PATH.


-- Configuring incomplete, errors occurred!

Re: Change sample rate of an audio with best quality?

Reply #3
ffmpeg windows builds from Zeranoe already contain sox.
ffmpeg -i 24kHz-32-bit-floating-point-input.wav -ar 48000 48kHz-32-bit-floating-point-out.wav
ffmpeg -i 24kHz-32-bit-floating-point-input.wav -af aresample=48000:resampler=soxr:precision=28 -c:a pcm_f32le 48kHz-32-bit-floating-point-out.wav
max precision can be 33

Re: Change sample rate of an audio with best quality?

Reply #4
Thank you very much! It works!
After upsampling I'm trying to shift pitch +50. What is the right way to combine upsampling and pitch shifting? I did this in SOX and got some strange artifacts: it duplicates small pieces of the mell spectrogram.
Is it possible to make pitch shifting with good quality using ffmpeg?

Re: Change sample rate of an audio with best quality?

Reply #5
if you need to preserve time, idk, but if not, it's the same as resampling to a different sample rate and then assuming another sample rate without resampling.

for instance, if you resample from 44100 to 88200 and then assume it's 44100 (just by changing the sample rate metadata without changing the actual data), you've slowed it down x2.
a fan of AutoEq + Meier Crossfeed

Re: Change sample rate of an audio with best quality?

Reply #6
Of cause I need to preserve time.
I know that +50 cents in pitch is equivalent to about 1.0293 coefficient. So when the sample rate is changed to 48 kHz with @Rollin command I can do the following:
Code: [Select]
ffmpeg -i 48kHz-32-inp.wav -af asetrate=48000*1.0293,aresample=48000:resampler=soxr:precision=33,atempo=1/1.0293 -c:a pcm_f32le 48kHz-32-out.wav
Is it the best way I can do upsample and shift pitch +50? Can I get better quality with ffmpeg or any other tool?

Re: Change sample rate of an audio with best quality?

Reply #7
if you need to preserve time, this approach won't do what you want, as I mentioned.
the total duration will also change by this coefficient.

but if you choose to preserve the total duration, then there isn't any perfect way to do it, it's a rather complex task and it will (in general) destroy quality no matter what.

it would be helpful to know _why_ are you trying to do that... maybe you don't actually need to preserve the total duration.
a fan of AutoEq + Meier Crossfeed

Re: Change sample rate of an audio with best quality?

Reply #8
if you need to preserve time, this approach won't do what you want, as I mentioned.
the total duration will also change by this coefficient.
As you can see I correct the duration with reciprocal coefficient atempo=1/1.0293. Try it and you will see that duration is approximately the same.


Re: Change sample rate of an audio with best quality?

Reply #10
Using media-autobuild_suite you can compile ffmpeg with librubberband and use rubberband filter Or just use rubberband command line tool
I used Rubberband CLI and heard artifacts. For my ears it shifts pitch worse than SOX. Also I found an article which tells the same. Finally, I modified your code to one line:
Code: [Select]
ffmpeg -i 24kHz-32-inp.wav -af aresample=48000:resampler=soxr:precision=33,asetrate=48000*1.0293,aresample=48000:resampler=soxr:precision=33,atempo=1/1.0293 -c:a pcm_f32le 48kHz-32-out.wav
But some questions are still not clear. Can I choose quality mode for atempo? Some guy suggests to place atempo before asetrate, is it a good idea? Is there anything else I can improve in this ffmpeg line?

Or maybe this variant with only one resample would be better?
Code: [Select]
ffmpeg -i 24kHz-32-inp.wav -af aresample=48000/1.0293:resampler=soxr:precision=33,asetrate=48000,atempo=1/1.0293 -c:a pcm_f32le 48kHz-32-out.wav

Re: Change sample rate of an audio with best quality?

Reply #11
Quote
I used Rubberband CLI and heard artifacts.
I don't know what tool is "best" but changing pitch & tempo independently involves FFT and it's always mathematically imperfect.

(FFT it theoretically perfect reversible if you have a long-duration constant sound, but not with music which changes from moment-to-moment.) 

Re: Change sample rate of an audio with best quality?

Reply #12
Does it mean that if I change pitch and don't touch duration it is mathematically perfect? For example in this line:
Code: [Select]
ffmpeg -i 24kHz-32-inp.wav -af aresample=48000/1.0293:resampler=soxr:precision=33,asetrate=48000 -c:a pcm_f32le 48kHz-32-out.wav
With the exception of spline approximation on upsampling from 24 kHz to 48000/1.0293 Hz.

Re: Change sample rate of an audio with best quality?

Reply #13
Quote
I used Rubberband CLI and heard artifacts.
I don't know what tool is "best" but changing pitch & tempo independently involves FFT and it's always mathematically imperfect.

(FFT it theoretically perfect reversible if you have a long-duration constant sound, but not with music which changes from moment-to-moment.) 
one isn't the cause of another.
FFT per se is somewhat reversible (except the rounding errors, which can be made small), but not all operations made in frequency domain are reversible.
time stretching with preservation of pitch is... just not simple at all
I don't understand math enough to say it concisely why is that but I suppose this article might be helpful https://en.wikipedia.org/wiki/Audio_time_stretching_and_pitch_scaling
a fan of AutoEq + Meier Crossfeed

Re: Change sample rate of an audio with best quality?

Reply #14
Does it mean that if I change pitch and don't touch duration it is mathematically perfect? For example in this line:
Code: [Select]
ffmpeg -i 24kHz-32-inp.wav -af aresample=48000/1.0293:resampler=soxr:precision=33,asetrate=48000 -c:a pcm_f32le 48kHz-32-out.wav
With the exception of spline approximation on upsampling from 24 kHz to 48000/1.0293 Hz.
if I understand it right, this one should be "perfect" (in a sense that it just filters away a bit of frequencies close to the Nyquist frequency, and adds a little bit of noise, neither of which should be ever audible if you convert it back in a similar way and try to ABX) - because it doesn't involve independent pitch/speed adjustment.
a fan of AutoEq + Meier Crossfeed

Re: Change sample rate of an audio with best quality?

Reply #15
Which of these 2 lines gives better quality?
Code: [Select]
ffmpeg -i 24kHz-32-inp.wav -af aresample=48000/1.0293:resampler=soxr:precision=33,asetrate=48000 -c:a pcm_f32le 48kHz-32-out.wav
ffmpeg -i 24kHz-32-inp.wav -af asetrate=24000*1.0293,aresample=48000:resampler=soxr:precision=33 -c:a pcm_f32le 48kHz-32-out.wav

Re: Change sample rate of an audio with best quality?

Reply #16
there should be no difference
a fan of AutoEq + Meier Crossfeed

Re: Change sample rate of an audio with best quality?

Reply #17
Audacity has the highest quality for pitch corrections I heard so far. Also better than Adobe. However it sadly isn't possible to do a pitch correction with command line there and as I do most of my conversions with drag and drop cmd batchfiles I'm looking to replace audacity's pitch correct with a ffmpeg solution.
The best you could come up with in ffmpeg still isnt as precise as audacity is. For example a 4% (68 cent) downpitch of a 01:15:40:192 audio file comes out as 01:15:40:193 in audacity.
While in ffmpeg
Code: [Select]
-af asetrate=48000*0.96,aresample=48000:resampler=soxr:precision=33:osf=s16:dither_method=triangular,atempo=1/0.96 -ar 48000 -acodec pcm_s16le -f WAV %1%-downpitch.wav
comes out as  01:15:40:203 and
Code: [Select]
-af atempo=1/0.96,asetrate=48000*0.96,aresample=48000:resampler=soxr:precision=33:osf=s16:dither_method=triangular -ar 48000 -acodec pcm_s16le -f WAV %1%-downpitch-atempo-before-asetrate.wav 
comes out as  01:15:40:200

To my ears the first command sounds a bit better than the second "tempo-before-asetrate" command, however not shure if the quality is really equal to audacity. I would have to test higher quality tracks to evaluate on that.

Looking forward to read about the experiences of the experts here and wish everybody a nice weekend
regards

Re: Change sample rate of an audio with best quality?

Reply #18
Audacity has the highest quality for pitch corrections I heard so far. Also better than Adobe.
[...]
To my ears the first command sounds a bit better than the second
Blind tested it?
Forum terms of service item #8: https://hydrogenaud.io/index.php/topic,3974.html

(Messing up lengths is a different story.)

Re: Change sample rate of an audio with best quality?

Reply #19
I wrote a pair of command-line programs that can accomplish what you want. They are audio-stretch and autio-resampler. I used these commands to create a pair of samples of what you’re doing:

Code: [Select]
audio-stretch original.wav -r0.96 -sn downpitch.wav
art downpitch.wav -4 -r44100 downpitch-44k.wav

This suffers from the slight length change like what you see because the sampling rate is rounded to the nearest integer (the intermediate wav file has a sample rate of 42336 Hz), so when it’s resampled there’s a slight error. Of course, it should be less than 1 part in about 80000. I don’t know why FFmpeg has this same issue  (probably for a different reason, because if it’s all in one program it could be perfect).

Another community member has created another pitch/tempo algorithm that he claims is very good, especially with low-frequency content, and it’s built into another fork of FFmpeg, so it should be possible to try it with your existing commands. It’s described in this thread.

Re: Change sample rate of an audio with best quality?

Reply #20
where is original.wav?
I want to run same thing with ascale and see if there is ways for improvements.

Re: Change sample rate of an audio with best quality?

Reply #21
where is original.wav?
I want to run same thing with ascale and see if there is ways for improvements.
One thing that's interesting about this process is that when you change the pitch of, for example, a singer's voice, it doesn't sound like that singer singing a different note. It sounds like a different person singing.

I once tried to use this technique to create a full-range piano synth from a single sampled key (A-440), but at the frequency extremes it didn't sound like a piano at all. I understand why this is (it has to do with the distribution of the harmonics), but it's not obvious how to fix it. Of course, this is why the effect is also humorous when applied to people talking.

Re: Change sample rate of an audio with best quality?

Reply #22
pitchshifting does logarithmic shifting, and is non-trivial.
phaseshifting does linear shifting.

Re: Change sample rate of an audio with best quality?

Reply #23
This is a resurrected necro-thread (reply 17).  It's dubious whether the OP is still interested.
It's your privilege to disagree, but that doesn't make you right and me wrong.

Re: Change sample rate of an audio with best quality?

Reply #24
This is a resurrected necro-thread (reply 17).  It's dubious whether the OP is still interested.
I'm sure the OP is long gone. However, the new post is a logical continuation of the discussion.

But another issue is that the subject of the thread doesn't mention anything about pitch changes, which is a completely different animal than just sample rate conversion (which is much easier to get right).