import torch


def stft(x, n_fft, hop_length, win_length, window):
    """Perform STFT and convert to magnitude spectrogram.
    Args:
      x: Input signal tensor (B, T).
    Returns:
      Tensor: Magnitude spectrogram (B, T, n_fft // 2 + 1).
    """
    x_stft = torch.stft(x, n_fft, hop_length, win_length, window, center=False, return_complex=True).abs()

    return x_stft