facebookresearch / encodec

State-of-the-art deep learning based audio codec supporting both mono 24 kHz audio and stereo 48 kHz audio.
MIT License
3.51k stars 304 forks source link

EOFError encounted for some special audio lengths #35

Closed chenjiasheng closed 1 year ago

chenjiasheng commented 1 year ago

🐛 Bug Report

EOFError encounted for some special audio lengths. The reason is that when calculating the number of decompressed frames, the wrong order of operations resulted in floating point errors. E.g.:

math.ceil(53760 / 24000 * 75) evals to 169
math.ceil(53760 * 75 / 24000) evals to 168

https://github.com/facebookresearch/encodec/blob/c79ba28c9199494d106d2c7f56006260528d7b16/encodec/compress.py#L120 This line of code should be changed to frame_length = int(math.ceil(this_segment_length * model.frame_rate / model.sample_rate ))

To Reproduce

Create a .wav file with duration 2.24s and content all zeros.

import numpy as np
from scipy.io import wavfile
wavfile.write('bug.wav', 24000, data=np.zeros(53760, dtype=np.int16))

Run encodec compress and decompress

import subprocess
subprocess.run('encodec bug.wav bug.encodec.wav'.split())

gives error message:

Traceback (most recent call last):
  File "/home/chenjiasheng/.local/bin/encodec", line 33, in <module>
    sys.exit(load_entry_point('encodec', 'console_scripts', 'encodec')())
  File "/mnt/d/code/encodec/encodec/__main__.py", line 117, in main
    out, out_sample_rate = decompress(compressed)
  File "/mnt/d/code/encodec/encodec/compress.py", line 185, in decompress
    return decompress_from_file(fo, device=device)
  File "/mnt/d/code/encodec/encodec/compress.py", line 147, in decompress_from_file
    raise EOFError("The stream ended sooner than expected.")
EOFError: The stream ended sooner than expected.

Your Environment

repo version:

commit c79ba28c9199494d106d2c7f56006260528d7b16 (HEAD -> main, origin/main, origin/HEAD)
Author: Alexandre Défossez <alexandre.defossez@gmail.com>
Date:   Tue Jan 24 14:07:56 2023 +0100
adefossez commented 1 year ago

thanks for the detailed report, this should now be fixed in main. can you confirm it is fixed for you ?

chenjiasheng commented 1 year ago

Thank you. Fix confirmed.