audio icon indicating copy to clipboard operation
audio copied to clipboard

Saving and loading the downsampled audio results in a tensor with zeros.

Open hiromis opened this issue 7 years ago • 7 comments

🐛 Bug

I was using this particular audio file that I downloaded from https://www.lynxstudio.com/ to experiment with 6 channel audio. When I downsample this from 44.1kHz 8kHz, everything seems fine and I am able to play the audio. However, if I were to save the downsampled file and load it again, the tensor I get back have all zeros in it.

import torchaudio

waveform, sample_rate = torchaudio.load('ChannelPlacement.wav')

downsample_rate=8000

downsample_resample = torchaudio.transforms.Resample(
    sample_rate, downsample_rate, resampling_method='sinc_interpolation')

down_sampled = downsample_resample(waveform)

print(down_sampled)

torchaudio.save('temp.wav', down_sampled, downsample_rate)

waveform2, sample_rate2 = torchaudio.load('temp.wav')

print(waveform2)
tensor([[-3.7585e-09, -3.3725e-09,  9.2130e-09,  ..., -4.0691e-08,
          7.2912e-09, -5.7485e-08],
        [-3.2915e-09,  7.5441e-09,  9.4772e-10,  ..., -7.3543e-09,
          3.1981e-08, -2.3025e-08],
        [ 1.5473e-08,  1.8003e-08,  2.5778e-09,  ..., -1.0129e-08,
         -2.0479e-08,  2.6770e-08],
        [-2.1108e-08, -3.9693e-08, -2.2911e-08,  ..., -2.4338e-08,
          3.7029e-08, -3.1360e-09],
        [-2.1277e-08, -1.9114e-09, -4.4245e-09,  ..., -2.3023e-08,
          6.9994e-09, -7.5472e-09],
        [ 5.2013e-09,  2.5186e-08,  2.1362e-08,  ..., -2.6036e-07,
         -1.5355e-07,  3.7919e-08]])
tensor([[0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.]])

To Reproduce

Steps to reproduce the behavior:

  1. Load ChannelPlacement.wav which is included in the attached zip file
  2. Downsample to 8000 (This 8000 seems to be a magic number as if I were to change it to another number, like 7999, it works fine)
  3. Save the downsampled version
  4. Load the downsampled file we just saved
  5. The tensor torchaudio.load returns have all zeros

Here is the gist https://gist.github.com/hiromis/3a0ce0e3b8a512465609c653364c02fe

The following zip file includes the jupyter notebook as well as the audio file: files.zip

Expected behavior

torchaudio.save('temp.wav', down_sampled, downsample_rate)
waveform, sample_rate = torchaudio.load('temp.wav')

Where waveform tensor to match the tensor I saved (down_sampled)

Environment

  • What commands did you used to install torchaudio (conda/pip/build from source)? pip
  • If you are building from source, which commit is it? N/A
  • What does torchaudio.__version__ print? (If applicable) '0.3.0'
PyTorch version: 1.2.0
Is debug build: No
CUDA used to build PyTorch: 10.0.130

OS: Ubuntu 18.04.3 LTS
GCC version: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
CMake version: version 3.10.2

Python version: 3.7
Is CUDA available: Yes
CUDA runtime version: 9.1.85
GPU models and configuration: GPU 0: GeForce GTX 1080 Ti
Nvidia driver version: 430.26
cuDNN version: /usr/local/cuda-10.0/lib64/libcudnn.so.7.5.0

Versions of relevant libraries:
[pip3] numpy==1.16.4
[pip3] torch==1.1.0
[conda] torch                     1.2.0                    pypi_0    pypi
[conda] torchaudio                0.3.0                    pypi_0    pypi
[conda] torchvision               0.4.0                    pypi_0    pypi

Additional context

I did try several other files, and it seems as though this file and sample rate of 8000 seem to hit the edge case of some sort.

hiromis avatar Aug 22 '19 02:08 hiromis

I don't think this is related to resampling. I think it's related to the save/load which has a wide variety of configurations (precision, normalization, and scaling). The signal starts off as a 24 bits per sample and after loaded it is a float32 (32 bits per sample) which is resampled and then saved to file.

down_sampled.abs().max() == 1.0006 which is greater than 1.0 so it is not scaled and converted to a int64/long https://github.com/pytorch/audio/blob/a424509dda5b57c932fa8b5b780de93e60ed7ee2/torchaudio/init.py#L191 This float32 tensor is then stored in a sox_sample_t/sox_int32_t buffer before being written to a file https://github.com/pytorch/audio/blob/a424509dda5b57c932fa8b5b780de93e60ed7ee2/torchaudio/torch_sox.cpp#L37. The copy of float32 to sox_int32_t will then convert all the values to zero as it is truncated.

Tangentially related concern in the past: https://github.com/pytorch/audio/pull/119#discussion_r293929024

Some resources for references, https://en.wikipedia.org/wiki/Single-precision_floating-point_format http://soundfile.sapp.org/doc/WaveFormat/ https://github.com/kaldi-asr/kaldi/blob/master/src/feat/wave-reader.cc

sox uses various sox_int32_t buffers for reading/writing where it does some conversion to various bits per sample. https://github.com/rbouqueau/SoX/blob/e29e9ceb7c25a2d83c09bc8a601de117fc65563c/src/wavpack.c#L129

I think the solution to the issue would be rewrite/fix torchaudio load and save so that all the bits of the waveform are saved in the file (e.g. somehow convert the tensor float32 to int32 bitwise for sox buffer or not use sox at all). The current torchaudio implementation of load/save seems to lose some bits and could be improved/more clear (e.g input/outputs dtype, scale of the input/output).

down_sampled2 = (down_sampled << 32).long()
torchaudio.save('temp.wav', down_sampled2, downsample_rate, precision=32)

waveform2, sample_rate2 = torchaudio.load('temp.wav', normalization=None)

print('down_sampled2\n', down_sampled2)
print('waveform2\n', waveform2)

# note not the same as information is lost in saving
# down_sampled2
# tensor([[  -16,   -14,    39,  ...,  -174,    31,  -246],
#     [  -14,    32,     4,  ...,   -31,   137,   -98],
#     [   66,    77,    11,  ...,   -43,   -87,   114],
#     [  -90,  -170,   -98,  ...,  -104,   159,   -13],
#     [  -91,    -8,   -19,  ...,   -98,    30,   -32],
#     [   22,   108,    91,  ..., -1118,  -659,   162]])
# waveform2
# tensor([[  -16.,   -14.,    39.,  ...,  -174.,    31.,  -246.],
#     [  -14.,    32.,     4.,  ...,   -31.,   137.,   -98.],
#     [   66.,    77.,    11.,  ...,   -43.,   -87.,   114.],
#     [  -90.,  -170.,   -98.,  ...,  -104.,   159.,   -13.],
#     [  -91.,    -8.,   -19.,  ...,   -98.,    30.,   -32.],
#     [   22.,   108.,    91.,  ..., -1118.,  -659.,   162.]])

jamarshon avatar Aug 22 '19 17:08 jamarshon

Thank you for very detailed explanation and resources, @jamarshon!

The key seems to be the if statement you pointed out: https://github.com/pytorch/audio/blob/a424509dda5b57c932fa8b5b780de93e60ed7ee2/torchaudio/init.py#L191

I tried different sample rates, and 8000 is the only one that goes over the threshold of 1.0 by 0.0005977153... (hence being considered "unnormalized" and taking a different path):

def downsample_to(waveform, sample_rate, downsample_rate):
    downsample_resample = torchaudio.transforms.Resample(
        sample_rate, downsample_rate, resampling_method='sinc_interpolation')
    down_sampled = downsample_resample(waveform)
    return down_sampled

down_sampled_8000 = downsample_to(waveform, sample_rate, 8000)
print(f'8000Hz: {down_sampled_8000.abs().max()}')

down_sampled_11025 = downsample_to(waveform, sample_rate, 11025)
print(f'11025Hz: {down_sampled_11025.abs().max()}')

down_sampled_16000 = downsample_to(waveform, sample_rate, 16000)
print(f'16000Hz: {down_sampled_16000.abs().max()}')

down_sampled_7999 = downsample_to(waveform, sample_rate, 7999)
print(f'7999Hz: {down_sampled_7999.abs().max()}')

down_sampled_8001 = downsample_to(waveform, sample_rate, 8001)
print(f'8001Hz: {down_sampled_8001.abs().max()}')
8000Hz: 1.0005977153778076
11025Hz: 0.9984769821166992
16000Hz: 0.9985944032669067
7999Hz: 0.9951688647270203
8001Hz: 0.9840729236602783

https://gist.github.com/hiromis/a7958a9bc72752534a0ea168b3b99462

I suppose we could add a small ε to 1.0 comparison to fix this, but to beginners like myself, if we saved and loaded values with all default arguments, the expectation would be for them to be the same.

hiromis avatar Aug 23 '19 01:08 hiromis

FYI: We are currently discussing standardizing the data loading in pytorch/pytorch#24915, and make any post-processing more transparent.

vincentqb avatar Aug 28 '19 21:08 vincentqb

I still have the same issue in version 0.4.0. Any update on this?

aconneau avatar Mar 25 '20 00:03 aconneau

The issue still exists on master.

import torchaudio

torchaudio.set_audio_backend("sox")  # sox is the default

waveform, sample_rate = torchaudio.load('ChannelPlacement.wav')

downsample_rate=8000

downsample_resample = torchaudio.transforms.Resample(
    sample_rate, downsample_rate, resampling_method='sinc_interpolation')

down_sampled = downsample_resample(waveform)

print(down_sampled)

torchaudio.save(
    'temp.wav', down_sampled, downsample_rate)

waveform2, sample_rate2 = torchaudio.load('temp.wav')

print(waveform2)

print((down_sampled - waveform2).abs().max())
print((down_sampled - waveform2).abs().median())
tensor([[-3.7585e-09, -3.3725e-09,  9.2130e-09,  ..., -4.0691e-08,
          7.2912e-09, -5.7485e-08],
        [-3.2915e-09,  7.5441e-09,  9.4772e-10,  ..., -7.3543e-09,
          3.1981e-08, -2.3025e-08],
        [ 1.5473e-08,  1.8003e-08,  2.5778e-09,  ..., -1.0129e-08,
         -2.0479e-08,  2.6770e-08],
        [-2.1108e-08, -3.9693e-08, -2.2911e-08,  ..., -2.4338e-08,
          3.7029e-08, -3.1360e-09],
        [-2.1277e-08, -1.9114e-09, -4.4245e-09,  ..., -2.3023e-08,
          6.9994e-09, -7.5472e-09],
        [ 5.2013e-09,  2.5186e-08,  2.1362e-08,  ..., -2.6036e-07,
         -1.5355e-07,  3.7919e-08]])
tensor([[0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.]])
tensor(1.0006)
tensor(1.9681e-08)

A workaround that does not reproduce exactly the original signal is to use torch.clamp and set precision when saving.

import torchaudio

torchaudio.set_audio_backend("sox")  # sox is the default

waveform, sample_rate = torchaudio.load('ChannelPlacement.wav')

downsample_rate=8000

downsample_resample = torchaudio.transforms.Resample(
    sample_rate, downsample_rate, resampling_method='sinc_interpolation')

down_sampled = downsample_resample(waveform)

print(down_sampled)

torchaudio.save(
    'temp.wav', torch.clamp(down_sampled, -1, 1), downsample_rate, precision=32)

waveform2, sample_rate2 = torchaudio.load('temp.wav')

print(waveform2)

print((down_sampled - waveform2).abs().max())
print((down_sampled - waveform2).abs().median())
tensor([[-3.7585e-09, -3.3725e-09,  9.2130e-09,  ..., -4.0691e-08,
          7.2912e-09, -5.7485e-08],
        [-3.2915e-09,  7.5441e-09,  9.4772e-10,  ..., -7.3543e-09,
          3.1981e-08, -2.3025e-08],
        [ 1.5473e-08,  1.8003e-08,  2.5778e-09,  ..., -1.0129e-08,
         -2.0479e-08,  2.6770e-08],
        [-2.1108e-08, -3.9693e-08, -2.2911e-08,  ..., -2.4338e-08,
          3.7029e-08, -3.1360e-09],
        [-2.1277e-08, -1.9114e-09, -4.4245e-09,  ..., -2.3023e-08,
          6.9994e-09, -7.5472e-09],
        [ 5.2013e-09,  2.5186e-08,  2.1362e-08,  ..., -2.6036e-07,
         -1.5355e-07,  3.7919e-08]])
tensor([[-3.7253e-09, -3.2596e-09,  8.8476e-09,  ..., -4.0513e-08,
          6.9849e-09, -5.7276e-08],
        [-3.2596e-09,  7.4506e-09,  9.3132e-10,  ..., -6.9849e-09,
          3.1665e-08, -2.2817e-08],
        [ 1.5367e-08,  1.7695e-08,  2.3283e-09,  ..., -9.7789e-09,
         -2.0023e-08,  2.6543e-08],
        [-2.0955e-08, -3.9581e-08, -2.2817e-08,  ..., -2.4214e-08,
          3.6787e-08, -2.7940e-09],
        [-2.0955e-08, -1.8626e-09, -4.1910e-09,  ..., -2.2817e-08,
          6.9849e-09, -7.4506e-09],
        [ 5.1223e-09,  2.5146e-08,  2.0955e-08,  ..., -2.6030e-07,
         -1.5320e-07,  3.7719e-08]])
tensor(0.0006)
tensor(2.0791e-10)

Figure_1 Figure_2

Note that both torch.clamp and precision are needed, since the following two variants still lead to the original error:

torchaudio.save(
    'temp.wav', down_sampled, downsample_rate, precision=32)
torchaudio.save(
    'temp.wav', torch.clamp(down_sampled, -1, 1), downsample_rate)

Both backends (sox and soundfile) are affected by this.

import torchaudio

torchaudio.set_audio_backend("soundfile")

waveform, sample_rate = torchaudio.load('ChannelPlacement.wav')

downsample_rate=8000

downsample_resample = torchaudio.transforms.Resample(
    sample_rate, downsample_rate, resampling_method='sinc_interpolation')

down_sampled = downsample_resample(waveform)

print(down_sampled)

torchaudio.save(
    'temp.wav', torch.clamp(down_sampled, -1, 1), downsample_rate, precision=32)

waveform2, sample_rate2 = torchaudio.load('temp.wav')

print(waveform2)

print((down_sampled - waveform2).abs().max())
print((down_sampled - waveform2).abs().median())
tensor([[-3.7585e-09, -3.3725e-09,  9.2130e-09,  ..., -4.0691e-08,
          7.2912e-09, -5.7485e-08],
        [-3.2915e-09,  7.5441e-09,  9.4772e-10,  ..., -7.3543e-09,
          3.1981e-08, -2.3025e-08],
        [ 1.5473e-08,  1.8003e-08,  2.5778e-09,  ..., -1.0129e-08,
         -2.0479e-08,  2.6770e-08],
        [-2.1108e-08, -3.9693e-08, -2.2911e-08,  ..., -2.4338e-08,
          3.7029e-08, -3.1360e-09],
        [-2.1277e-08, -1.9114e-09, -4.4245e-09,  ..., -2.3023e-08,
          6.9994e-09, -7.5472e-09],
        [ 5.2013e-09,  2.5186e-08,  2.1362e-08,  ..., -2.6036e-07,
         -1.5355e-07,  3.7919e-08]])
tensor([[0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.],
        [0., 0., 0.,  ..., 0., 0., 0.]])
tensor(1.0006)
tensor(1.9681e-08)

What workaround do you do?

vincentqb avatar Mar 25 '20 22:03 vincentqb

The new backends should not exhibit this behavior. Closing the issue.

mthrok avatar Apr 01 '21 03:04 mthrok

How do I do the same directly in C++/Libtorch? Thanks

ghost avatar Jun 08 '21 16:06 ghost