Skip to content

Instantly share code, notes, and snippets.

@ketanhdoshi
Last active March 7, 2021 09:40
Show Gist options
  • Save ketanhdoshi/1e288f71a77b01f8c37118c4b502aa93 to your computer and use it in GitHub Desktop.
Save ketanhdoshi/1e288f71a77b01f8c37118c4b502aa93 to your computer and use it in GitHub Desktop.
Transform Change Sampling Rate
# ----------------------------
# Since Resample applies to a single channel, we resample one channel at a time
# ----------------------------
@staticmethod
def resample(aud, newsr):
sig, sr = aud
if (sr == newsr):
# Nothing to do
return aud
num_channels = sig.shape[0]
# Resample first channel
resig = torchaudio.transforms.Resample(sr, newsr)(sig[:1,:])
if (num_channels > 1):
# Resample the second channel and merge both channels
retwo = torchaudio.transforms.Resample(sr, newsr)(sig[1:,:])
resig = torch.cat([resig, retwo])
return ((resig, newsr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment