Skip to content

Instantly share code, notes, and snippets.

@hadware
Last active March 6, 2020 11:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadware/00c5eb0131f72a9379eeedd39ecffb50 to your computer and use it in GitHub Desktop.
Save hadware/00c5eb0131f72a9379eeedd39ecffb50 to your computer and use it in GitHub Desktop.
Resampling a wav ndarray with sox in python
import numpy as np
from subprocess import PIPE, run
from scipy.io.wavfile import read, write
## This is in case you have a numpy nd array of your sound, and the sampling rate
## isn't the same as another ndarray. This resamples the array by piping in and out of Sox
## This is a simple example with wav files encoded in float32, with only one channel (mono)
## These parameters can however be adjusted by tweaking -t and -c options in the sox command
with open("sample_48k.wav", "rb") as file_48k, open("sample_16k.wav", "rb") as file_16k:
rate_1, wave_data_1 = read(file_48k)
rate_2, wave_data_2 = read(file_16k)
cmd = "sox -N -V1 -t f32 -r %s -c 1 - -t f32 -r %s -c 1 -" % (rate_1, rate_2)
output = run(cmd, shell=True, stdout=PIPE, stderr=PIPE, input=wave_data_1.tobytes(order="f")).stdout
wave_data_1 = np.fromstring(output, dtype=np.float32)
# say that the sample_48k.wav is a bit longer (in seconds) than the sample_16k, let's cut a bit of its tail
# and 'mix' it to the other file, than save
mixed = wave_data_1[:len(wave_data_2) - 1] + wave_data_2
with open("mixed_16k.wav", "wb") as mixed_file:
write(mixed_file, rate_2, mixed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment