Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
wave file read & write by Python Built-in function
import numpy as np
import wave
import matplotlib.pyplot as plt
import sounddevice as sd
class SoundFile:
def __init__(self, file_name=None):
def write(self, file_name, signal, nchannels=1, sampwidth=2, framerate=44100, duration_sec = 4, comptype="NONE", compname="noncompressed"):
file =, 'wb')
file.setparams( ( nchannels, sampwidth, framerate, framerate * duration_sec, comptype, compname) )
file.writeframes( signal )
def read(self, file_name):
file =, 'rb')
nchannels, sampwidth, framerate, nframes, comptype, compname = file.getparams()
duration_sec = nframes / framerate
signal = file.readframes( nframes )
signal = np.frombuffer(signal)
return signal, nchannels, sampwidth, framerate, duration_sec, nframes, comptype, compname

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment