pytorch / audio

Data manipulation and transformation for audio signal processing, powered by PyTorch
https://pytorch.org/audio
BSD 2-Clause "Simplified" License
2.48k stars 640 forks source link

StreamWriter doesn't correctly write audio chunks #3823

Open arch-user-france1 opened 1 month ago

arch-user-france1 commented 1 month ago

🐛 Describe the bug

import torch
import torchaudio
from torchaudio.io import StreamWriter

### Minimal reproducible example

# Initialise StreamWriter
stream = StreamWriter("test.wav")
stream.add_audio_stream(44100, 2)

# Create random stereo waveform
noise = torch.rand(441000)
waveform = []
for wave in noise:
    waveform.append(torch.tensor((0.0, 0)))
    waveform.append(torch.tensor((wave, 1)))

# Write waveform in chunks, as you would do this in a real application
with stream.open() as f:
    print("Opened stream")
    print(len(waveform))
    for i in range(0, len(waveform), 4):
        print((waveform[i], waveform[i + 1], waveform[i + 2], waveform[i + 3]))
        f.write_audio_chunk(0, torch.stack((waveform[i], waveform[i + 1], waveform[i + 2], waveform[i + 3])))

print("Done")

Produces weird output files, such as:

image

The second channel always repeats the same pattern, creating a humming noise. The first channel seems to work fine if you replace 0.0 with wave. In the example shown above, though, it seems to contain the second channel's output. StreamWriter works well if the whole waveform is supplied, without writing it in chunks, as done in the documentation.

Versions

Collecting environment information...
PyTorch version: 2.4.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 15.0 (arm64)
GCC version: Could not collect
Clang version: 15.0.0 (clang-1500.3.9.4)
CMake version: Could not collect
Libc version: N/A

Python version: 3.12.1 | packaged by Anaconda, Inc. | (main, Jan 19 2024, 09:45:58) [Clang 14.0.6 ] (64-bit runtime)
Python platform: macOS-15.0-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Apple M2

Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] torch==2.4.0
[pip3] torch-tb-profiler==0.4.3
[pip3] torchaudio==2.4.0
[pip3] torchvision==0.19.0
[conda] libopenvino-pytorch-frontend 2023.3.0             hebf3989_2    conda-forge
[conda] numpy                     1.26.4                   pypi_0    pypi
[conda] torch                     2.4.0                    pypi_0    pypi
[conda] torch-tb-profiler         0.4.3                    pypi_0    pypi
[conda] torchaudio                2.4.0                    pypi_0    pypi
[conda] torchvision               0.19.0                   pypi_0    pypi
malfet commented 1 month ago

This sounds like a torchaudio issue, isn't it?