Log in

View Full Version : How to do nearest-neighbor audio upsampling in FFmpeg?


SilSinn9801
30th May 2023, 04:55
Hello,

I have hundreds of one-minute-long MP4 files ripped from a WYZE security camera, & their monaural audio is encoded in A-Law ADPCM at 16000 Hz. I want to batch-decode the audio from all those 100s of MP4s to PCM (16-bit signed, little endian) & then batch-upsample from 16000 to 48000 Hz WITHOUT interpolation of any kind – that is, nearest-neighbor upsampling (instead of linear, cubic, or any other form of sample interpolation), & because 48000 is an integer multiple of 16000 (3× to be exact), the output should have no rounding errors. Effectively, I want each input sample value to repeat three times at the output before moving from one audio sample point to the next. The question is, how do I batch-do this in FFmpeg?

For context: trying FFmpeg’s -ar 48000 option always results in linear interpolation (or something close to it) between sample points, & trying instead -af aresample=isr=16000:linear_interp=0:osr=48000 also always results in linear upsampling (in spite of the linear_interp=0 part, unless that part means a different thing).

richardpl
30th May 2023, 13:12
Such upsampling for audio would sound worse than that original not-upsampled version.
Correct approach to do minimal upsample is to just put every 2nd and 3d sample to zero, which would reflect all original lower frequencies also into higher frequencies in upsampled version.
Than optionally just do lowpass filtering - which can be done in many ways - depending on speed/quality ratio.

Kisa_AG
30th May 2023, 16:41
Effectively, I want each input sample value to repeat three times at the output before moving from one audio sample point to the next. The question is, how do I batch-do this in FFmpeg?

ffmpeg.exe -i Input.wav -af aresample=resampler=swr:filter_size=1:phase_shift=0:linear_interp=0 -ar 48000 -c:a pcm_s16le Out.wav

It seems to me that it makes exactly what you've said.
But I don't think you'll like the result :)

Screenshot (https://4.downloader.disk.yandex.ru/preview/34e7c346de5f32e4805b875d454ea9bafe669b74bcb949de488077e8dc4f60b7/inf/aS52-mbFiDX-IOIwwTc7j7ZCXdeEw0uuURxdzptdRsfAlGhbj8kjZjOLpEsmM_K6nVWcEUj9RflipedKY1Oy2g%3D%3D?uid=109170062&filename=2023-05-30_183229.png&disposition=inline&hash=&limit=0&content_type=image%2Fpng&owner_uid=109170062&tknv=v2&size=1280x893)

SilSinn9801
30th May 2023, 19:17
Thankee.

If the result you’re talking about is a crunchy sound (rather than a smooth sound), yes I know that, but in the chiptune oscilloscope-view community, in many cases such crunchiness is a desired trait, especially for oscilloscope visualization.

To make an analogy or two:
Crunchy (nearest-neighbor-upsampled) sound
is to
Smooth (linear- or sinc-interpolation-upsampled) sound
what
Pixelated (nearest-neighbor-upscaled) image
is to
High-def (bilinear-/bicubic-/Lanczos-/etc.-upscaled) image
& also what
Repeated frames (in film-to-50/60fps upconversion)
are to
Motion-interpolated (or motion-compensated) frames.

Which one of them is desired depends on the target application as well as the user’s preferential tastes.

junh1024
1st June 2023, 07:30
Silsinn, your analogies aren't accurate. Video is a sampled discrete signal, but audio is meant to be a continuously changing signal. So, if you hold the sample value like NN, you'll actually change the signal in ways you probably don't want, as Kisa's picture shows.

If you want it to sound the same, most SRC defaults should do. If you want to add harmonics, try linear.

pandy
1st June 2023, 19:50
Both methods are fine, Zero Order Hold can be used for upsampling.