Skip to content

Instantly share code, notes, and snippets.

Embed
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):
# https://docs.python.org/3.6/library/wave.html
pass
def write(self, file_name, signal, nchannels=1, sampwidth=2, framerate=44100, duration_sec = 4, comptype="NONE", compname="noncompressed"):
file = wave.open(file_name, 'wb')
file.setparams( ( nchannels, sampwidth, framerate, framerate * duration_sec, comptype, compname) )
file.writeframes( signal )
file.close()
def read(self, file_name):
file = wave.open(file_name, 'rb')
nchannels, sampwidth, framerate, nframes, comptype, compname = file.getparams()
duration_sec = nframes / framerate
signal = file.readframes( nframes )
signal = np.frombuffer(signal)
file.close()
return signal, nchannels, sampwidth, framerate, duration_sec, nframes, comptype, compname
@imcomking

This comment has been minimized.

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