Skip to main content
Topic: Change sample rate of an audio with best quality? (Read 559 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.
some ANC'd headphones + AutoEq-based impulse + Meier Crossfeed (30%)

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.
some ANC'd headphones + AutoEq-based impulse + Meier Crossfeed (30%)

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.
some ANC'd headphones + AutoEq-based impulse + Meier Crossfeed (30%)

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
some ANC'd headphones + AutoEq-based impulse + Meier Crossfeed (30%)

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.
some ANC'd headphones + AutoEq-based impulse + Meier Crossfeed (30%)

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
some ANC'd headphones + AutoEq-based impulse + Meier Crossfeed (30%)

 
SimplePortal 1.0.0 RC1 © 2008-2020