"""
Core : Functions useful for all the package.
--------------------------------------------
"""
import numpy,time,sys
from os import path, makedirs
from music22 import audio
[docs]class file():
"""A class to manage files.
Args:
path (str) : absolute path of a file.
Attributes:
path (str) : The path of the file.
basename (str) : The name of the file.
dirname (str) : The directory path of the file.
name (str) : The name of the file.
extension (str) : The extension of the file.
Example:
>>> audio = "/Users/anas/Barraq/AUDIO/P0.wav"
>>> A = file(audio)
>>> print A.path
/Users/anas/Barraq/AUDIO/P0.wav
>>> print A.basename
P0.wav
>>> print A.dirname
/Users/anas/Barraq/AUDIO
>>> print A.name
P0
>>> print A.extension
wav
"""
def __init__(self,path):
self.path = path
self._check_exist()
self._set_names()
def __repr__(self):
return '[File: %s]' % (self.path)
def _check_exist(self):
if ( not path.isfile(self.path)):
print("ERROR: File %s does not exist or is not readable !" % self.path)
def _set_names(self):
self.basename = path.basename(self.path)
self.dirname = path.dirname(self.path)
self.name = self.basename.split(".")[0]
self.extension = self.basename.split(".")[1]
[docs]class wavfile(file):
"""A class to manage audio files (.wav)."""
def __init__(self,path):
file.__init__(self,path)
self._check_extension()
def __repr__(self):
return '[WavFile: %s]' % (self.path)
def _check_extension(self):
if path.isfile(self.path) and self.extension == 'wav':
print "Instance created with the audio file :", self.basename
else :
print "ERROR: The file does not seem to be an audio .wav file !"
[docs]class f0file(file):
"""A class to manage data files (.txt)."""
def __init__(self,path):
file.__init__(self,path)
self._check_extension()
def __repr__(self):
return '[TxtFile: %s]' % (self.path)
def _check_extension(self):
if path.isfile(self.path) and self.extension == 'txt':
print "Instance created with the txt file :", self.basename
else :
print "ERROR: The file does not seem to be a text .txt file !"
[docs] def get_data(self):
self.data = numpy.loadtxt(self.path)
[docs] def clean_data(self):
"""Remove zeros or NaNs in the list.
Args:
L (numpy.ndarray) : A numpy list.
Return:
L (numpy.ndarray) : A `cleaned` numpy list."""
cleaned_data = self.data[numpy.nonzero(self.data)] # Remove zeros, if any
self.clean_data = cleaned_data[~numpy.isnan(cleaned_data)] # Remove NaNs, if any
if __name__ == "__main__":
import doctest
doctest.testmod()