import torch


def mu_law_decode_torch(output, quantization_channels=256):
    """
    Recovers waveform from quantized values.
    Reference: https://github.com/vincentherrmann/pytorch-wavenet/blob/master/audio_data.py
    """
    mu = torch.tensor(quantization_channels - 1, device=output.device, dtype=torch.float)

    expanded = (output / quantization_channels) * 2. - 1
    waveform = torch.sign(expanded) * (
                   torch.exp(torch.abs(expanded) * torch.log(mu + 1)) - 1
               ) / mu

    return waveform