Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support computing features for whisper #82

Merged
merged 3 commits into from
Nov 8, 2023
Merged

Conversation

csukuangfj
Copy link
Owner

@csukuangfj csukuangfj commented Nov 8, 2023

Usage

import torchaudio
import torch
import kaldifeat

def compute_features(filename: str) -> torch.Tensor:
    """
    Args:
      filename:
        Path to an audio file.
    Returns:
      Return a 3-D float32 tensor of shape (1, 80, 3000) containing the features.
    """
    wave, sample_rate = torchaudio.load(filename)
    audio = wave[0].contiguous()  # only use the first channel
    if sample_rate != 16000:
        audio = torchaudio.functional.resample(
            audio, orig_freq=sample_rate, new_freq=16000
        )

    opts = kaldifeat.WhisperFbankOptions(device="cpu")
    whisper_fbank = kaldifeat.WhisperFbank(opts)
    features = whisper_fbank(audio)  # [num_frames, 80]

    # we need to pad it to [3000, 80]
    pad = 3000 - features.shape[0]
    if pad > 0:
        features = torch.nn.functional.pad(features, (0, 0, 0, pad), "constant", 0)
    else:
        features = features[:3000]

    features = features.t()
    # now features is [80, 3000]
    return features.unsqueeze(0)  # [1, 80, 3000]

@csukuangfj csukuangfj merged commit 01aed93 into master Nov 8, 2023
5 of 35 checks passed
@csukuangfj csukuangfj deleted the support-whisper branch November 8, 2023 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant