Permalink
Browse files

Removed deprecated OO code from libsndfile-ctypes

  • Loading branch information...
vokimon committed Jul 2, 2014
1 parent 2faf999 commit 327c5bbe8d6f25b9541043cbe2247daaef1a575d
Showing with 4 additions and 174 deletions.
  1. +3 −0 README.md
  2. +1 −1 setup.py
  3. +0 −173 wavefile/libsndfile.py
View
@@ -234,6 +234,9 @@ A pythonic layer was added on the top of it.
Version history
---------------
+### 1.4 (devel)
+
+
### 1.3
- Fix: Whole-file interface works again, regression tests added
View
@@ -26,7 +26,7 @@
setup(
name = "wavefile",
- version = "1.3",
+ version = "1.4~git",
description = "Pythonic wave file reader and writer",
author = "David Garcia Garzon",
author_email = "voki@canvoki.net",
View
@@ -255,176 +255,3 @@ class SEEK_MODES():
SEEK_CUR = 1
SEEK_END = 2
-# deprecated (Code kept as reference of some functionalities not yet implemented in python-wavefile)
-class SndFile(object):
- """ Main Class of the wrapper, provides easy access to audio file contents """
-
- def __init__(self, filename, open_mode=OPEN_MODES.SFM_READ,
- writeSamplerate = 48000,
- writeFormat = FILE_FORMATS.SF_FORMAT_WAV^FILE_FORMATS.SF_FORMAT_PCM_16,
- writeNbChannels = 2):
- """ initializes a SndFile object from the file 'filename'
- using the open mode specified in OPEN_MODES """
- self._lib = _lib
- self._filename = filename
- self._sf_info = SF_INFO()
- if open_mode == OPEN_MODES.SFM_WRITE:
- self._sf_info.samplerate = writeSamplerate
- self._sf_info.format = writeFormat
- self._sf_info.channels = writeNbChannels
- self.currentPosition = 0
- self._SNDFILE = self._lib.sf_open(filename, open_mode, self._sf_info)
- if self._lib.sf_error(self._SNDFILE)!=0:
- raise Exception(self._lib.sf_strerror(self._SNDFILE))
-
- @property
- def nbFrames(self):
- """ the total number of frames for each channels """
- return self._sf_info.frames
- @property
- def samplerate(self):
- """ the samplerate in Hz"""
- return self._sf_info.samplerate
- @property
- def format(self):
- """ the audio file format as defined in FILE_FORMATS """
- return self._sf_info.format
- @property
- def isSeekable(self):
- """ True if seekable, False otherwise """
- return self._sf_info.seekable
- @property
- def nbChannels(self):
- """ the number of channels """
- return self._sf_info.channels
-
- def __enter__(self):
- return self
-
- def __exit__(self, type, value, traceback):
- if type!=None:
- print("an exception occured while in the with statement")
- print(value)
- if self._SNDFILE != None:
- if self._lib.sf_close(self._SNDFILE)!=0:
- print("Could not close the file on exit")
-
- def __str__(self):
- s = """
- audio file : %s
- frames: : %d
- samplerate : %d
- channels : %d
- format : %d
- seekable : %d
- """%(self._filename, self.nbFrames, self.samplerate,
- self.nbChannels, self.format, self.isSeekable)
- return s
-
- def write(self, data):
- """ write all the provided data to the file with the parameters
- specified when opening the file """
- if data.dtype==np.float64:
- return data, self._lib.sf_write_double(self._SNDFILE, data.ctypes.data_as(ct.POINTER(ct.c_double)), data.size)
- elif data.dtype==np.float32:
- return data, self._lib.sf_read_float(self._SNDFILE, data.ctypes.data_as(ct.POINTER(ct.c_float)), data.size)
- elif data.dtype==np.int16:
- return data, self._lib.sf_read_short(self._SNDFILE, data.ctypes.data_as(ct.POINTER(ct.c_short)), data.size)
- elif data.dtype==np.int32:
- return data, self._lib.sf_read_int(self._SNDFILE, data.ctypes.data_as(ct.POINTER(ct.c_int)), data.size)
- else:
- raise TypeError("Please choose a correct dtype")
-
- def read(self, nbFrames=None, dtype=np.float32):
- """ returns a numpy array of dimension (nbFrames, nbChannels) and of type dtype
- reads nbFrames for each channels if provided, else reads the entire file
- the data is read from the current position in the file, this can be set
- using the seek method.
- Reading beyond the limits of the file fills the output array with 0s.
- Accepted dtypes are numpy's int16, int32, float32, float64."""
- if not nbFrames: nbFrames=self.nbFrames
- try:
- data, nbFramesRead = self._read(nbFrames, dtype)
- except TypeError as te:
- raise te
- return data, nbFramesRead
-
- def seek(self, frame_position, whence=SEEK_MODES.SEEK_SET):
- """ seek to a position in the audio file, seek modes are those
- specified in SEEK_MODES.
- Raises an exception if tried to seek beyond the borders of the file.
- Else returns the current offset."""
- offset = self._lib.sf_seek(self._SNDFILE, frame_position, whence)
- if offset==-1:
- raise Exception(self._lib.sf_strerror(self._SNDFILE))
- return offset
-
- def _read(self, nbFrames, dtype):
- """ class internal common part of read and readFromTo """
- data = np.empty((nbFrames, self.nbChannels), dtype)
- if dtype==np.float64:
- return data, self._lib.sf_read_double(self._SNDFILE, data.ctypes.data_as(ct.POINTER(ct.c_double)), nbFrames*self.nbChannels)/self.nbChannels
- elif dtype==np.float32:
- return data, self._lib.sf_read_float(self._SNDFILE, data.ctypes.data_as(ct.POINTER(ct.c_float)), nbFrames*self.nbChannels)/self.nbChannels
- elif dtype==np.int16:
- return data, self._lib.sf_read_short(self._SNDFILE, data.ctypes.data_as(ct.POINTER(ct.c_short)), nbFrames*self.nbChannels)/self.nbChannels
- elif dtype==np.int32:
- return data, self._lib.sf_read_int(self._SNDFILE, data.ctypes.data_as(ct.POINTER(ct.c_int)), nbFrames*self.nbChannels)/self.nbChannels
- else:
- raise TypeError("Please choose a correct dtype")
-
- def readFromTo(self, startFrame, stopFrame, dtype=np.float32):
- """ helper read method to specify the start and ending frame of reading.
- Raises an exception if startFrame<0 or stopFrame<startFrame.
- Accepted dtypes are numpy's int16, int32, float32, float64.
- returns a numpy array of dimension (stopFrame-startFrame, nbChannels)
- and the number of frames actually read.
- Reading beyond the limits of the file fills the output array with 0s."""
- if stopFrame<=startFrame and startFrame>=0:
- raise Exception("Please choose stopFrame > startFrame >= 0")
- nbFrames = stopFrame-startFrame
- self.seek(startFrame)
- try:
- data, nbFramesRead = self._read(nbFrames, dtype)
- except TypeError as te:
- raise te
- return data, nbFramesRead
-
- def get_string(self, what_string):
- """ returns the queried string value if the file format supports it.
- the available string queries are specified in FILE_STRINGS """
- return self._lib.sf_get_string(self._SNDFILE, what_string)
- def set_string(self, what_string, value):
- """ sets the string of the audio file as specified in FILE_STRINGS """
- return self._lib.sf_set_string(self._SNDFILE, what_string, value)
-
-if __name__=="__main__":
- with SndFile("test/LS100673.WAV") as f:
- #print various information
- print(f)
- #read from 1 to 3 seconds
- data, nbFramesRead = f.readFromTo(1*f.samplerate, 3*f.samplerate, dtype=np.float64)
- print("nb frames read:",nbFramesRead)
-
- #get the left channel
- lChannel = data[:,0]
-
- from scipy.signal import butter, lfilter
- cutoffL=200.
- # Low-pass filter on the left channel at 200 Hz
- b,a = butter(3, cutoffL/(f.samplerate/2), btype="low")
- lChannelFiltered = lfilter(b, a, lChannel)
-
- #write the 2 seconds read as an ogg file
- with SndFile("output.ogg", OPEN_MODES.SFM_WRITE,
- writeFormat=FILE_FORMATS.SF_FORMAT_OGG^FILE_FORMATS.SF_FORMAT_VORBIS) as fo:
- fo.write(data)
-
- import matplotlib.pyplot as plt
- plt.plot(np.arange(len(data), dtype=np.float)*1000./f.samplerate, lChannel, label="left channel")
- plt.plot(np.arange(len(data), dtype=np.float)*1000./f.samplerate, lChannelFiltered, label="filtered left channel")
- plt.xlabel("time (ms)")
- plt.ylabel("amplitude (arbitrary unit)")
- plt.title("waveform of filtered and original left channel")
- plt.legend()
- plt.show()

0 comments on commit 327c5bb

Please sign in to comment.