Esempio n. 1
0
    def __init__(self, filename, audio_format="ogg", audio_encoding="vorbis", alphabet="international"):
        super(TelegraphWriter, self).__init__(alphabet)
        self.__filename = filename        
        self.__audio_format = audio_format
        self.__audio_encoding = audio_encoding

        self.__output_formats = available_file_formats()

        from scikits.audiolab import Format, Sndfile          
        if self.__audio_format not in available_file_formats() or self.__audio_encoding not in available_encodings(self.__audio_format):
            raise InvalidFormatEncoding(self.__audio_format, self.__audio_encoding)
        output_format = Format(self.__audio_format, self.__audio_encoding)
        self.__output_file = Sndfile(self.__filename, 'w', output_format, 1, 44100)
Esempio n. 2
0
from tempfile import mkstemp

from numpy.testing import *
import numpy as N

from scikits.audiolab import wavread, auread, aiffread, sdifread, flacread, \
                             oggread
from scikits.audiolab import wavwrite, auwrite, aiffwrite, sdifwrite, flacwrite, \
                             oggwrite
from scikits.audiolab import PyaudioException
from scikits.audiolab import Sndfile, Format as audio_format
from scikits.audiolab import available_file_formats

from testcommon import open_tmp_file, close_tmp_file

AVAILABLE_FORMATS = available_file_formats()


class test_audiolab(TestCase):
    def _test_read(self, func, format, filext):
        # Create a tmp audio file, write some random data into it, and check it
        # is the expected data when read from a function from the matapi.
        rfd, fd, cfilename = open_tmp_file('pySndfiletest.' + filext)
        try:
            nbuff = 22050
            noise = 0.1 * N.random.randn(nbuff)

            # Open the copy file for writing
            b = Sndfile(cfilename, 'w', format, 1, nbuff)
            b.write_frames(noise)
            b.close()
Esempio n. 3
0
def check_audio_file_specs(sndfile, samplerate, encoding, channels):
    if sndfile.samplerate != samplerate:
        raise StandardError("%s\nSample rate of above file doesn't match required sample rate of %i", f, samplerate)
    if sndfile.encoding != encoding:
        raise StandardError("%s\nEncoding of above file doesn't match required encoding %s", f, encoding)
    if sndfile.channels != channels:
        raise StandardError("%s\nNumber of channels of above file doesn't match required number of channels (%i)", f, channels)

if __name__ == '__main__':
    init.init_logger()
    conf = config.get_config()
    audio_folder = os.path.expanduser(conf.get('Input', 'AudioFolder'))
    ground_truth_path = os.path.expanduser(conf.get('Preprocessing', 'GroundTruthPath'))
    features_path = os.path.expanduser(conf.get('Preprocessing', 'RawFeaturesPath'))
    extensions = available_file_formats()
    lengthinseconds = int(conf.get('Tracks', 'LengthInSeconds'))
    samplerate = int(conf.get('Tracks', 'SampleRate'))
    encoding = conf.get('Tracks', 'Encoding')
    channels = int(conf.get('Tracks', 'Channels'))
    windowsize = int(conf.get('Spectrogram', 'WindowSize'))
    stepsize = int(conf.get('Spectrogram', 'StepSize'))
    windowtype = conf.get('Spectrogram', 'WindowType')
    fftres = int(conf.get('Spectrogram', 'FFTResolution'))
    if os.path.isdir(audio_folder):
        # ground truth
        audiofiles = list_audio_files_and_genres(audio_folder, extensions)
        gt = GroundTruth(audiofiles)
        gt.save_to_pickle_file(ground_truth_path)
        logging.info("Ground Truth: saved in %s" % ground_truth_path)
Esempio n. 4
0
class AudioTrack(object):
    extensions = available_file_formats()

    def __init__(self, path, genre=None, seconds=None, offset_seconds=None):
        super(AudioTrack, self).__init__()

        filename = os.path.basename(path)
        _seconds = seconds

        self.__dict__.update(locals())
        del self.self
        del seconds

        if self.offset_seconds is not None:
            assert (self.offset_seconds + self.seconds) <= self.seconds_total

    def __str__(self):
        return str(self.__dict__)

    def __repr__(self):
        return "".join([
            self.__module__, ".", self.__class__.__name__, "(**",
            self.__str__(), ")"
        ])

    @property
    def samplerate(self):
        return Sndfile(self.path, mode='r').samplerate

    @property
    def channels(self):
        return Sndfile(self.path, mode='r').channels

    @property
    def nframes_total(self):
        return Sndfile(self.path, mode='r').nframes

    @property
    def nframes(self):
        return int(self.seconds * self.samplerate)

    @property
    def nframes_extended(self):
        return int((self.seconds + self.offset_seconds) * self.samplerate)

    @property
    def format(self):
        return Sndfile(self.path, mode='r').format

    @property
    def encoding(self):
        return Sndfile(self.path, mode='r').encoding

    @property
    def seconds_total(self):
        return float(self.nframes_total) / float(self.samplerate)

    @property
    def seconds(self):
        if self._seconds is None:
            self._seconds = self.seconds_total
        return self._seconds

    @property
    def signal(self):
        if not hasattr(self, '_signal'):
            if self.offset_seconds is None:
                self._signal = Sndfile(self.path, mode='r').read_frames(
                    self.nframes, dtype=numpy.dtype(theanoconfig.floatX).type)
            else:
                self._signal = Sndfile(self.path, mode='r').read_frames(
                    self.nframes_extended,
                    dtype=numpy.dtype(theanoconfig.floatX).type)
                self._signal = \
                    self._signal[self.nframes_extended - self.nframes:]
            self.normalize()
        return self._signal

    @signal.setter
    def signal(self, value):
        self._signal = value

    def rm_signal(self):
        if hasattr(self, '_signal'):
            del self._signal

    def normalize(self):
        self.signal /= numpy.max(numpy.abs(self.signal), axis=0)

    @property
    def spectrogram(self):
        if not hasattr(self, '_spectrogram'):
            return self.calc_spectrogram()
        else:
            return self._spectrogram

    def calc_spectrogram(self,
                         step_size=None,
                         fft_resolution=None,
                         scale_factors=None):
        self._spectrogram = Spectrogram.from_waveform(
            self.signal,
            step_size=step_size,
            fft_resolution=fft_resolution,
        )
        if scale_factors is not None:
            self._spectrogram.data = self._spectrogram.scale(scale_factors)
        return self._spectrogram

    def rm_spectrogram(self):
        if hasattr(self, '_spectrogram'):
            del self._spectrogram

    def play(self):
        play(self.signal, fs=self.samplerate)

    def plot_signal(self, title=None, out=None):
        import matplotlib.pyplot as plt
        if title is None:
            title = self.filename
        plt.title(title)
        waveplot(self.signal, sr=self.samplerate)
        if out is None:
            plt.show()
        else:
            plt.savefig(out, dpi=300)
        plt.close()

    def plot_spectrogram(self, title=None, out=None):
        if title is None:
            title = self.filename
        self.spectrogram.plot(sample_rate=self.samplerate,
                              title=title,
                              with_colorbar=True,
                              out=out)
from tempfile import mkstemp

from numpy.testing import *
import numpy as N

from scikits.audiolab import wavread, auread, aiffread, sdifread, flacread, \
                             oggread
from scikits.audiolab import wavwrite, auwrite, aiffwrite, sdifwrite, flacwrite, \
                             oggwrite
from scikits.audiolab import PyaudioException
from scikits.audiolab import Sndfile, Format as audio_format
from scikits.audiolab import available_file_formats

from testcommon import open_tmp_file, close_tmp_file

AVAILABLE_FORMATS = available_file_formats()

class test_audiolab(TestCase):
    def _test_read(self, func, format, filext):
        # Create a tmp audio file, write some random data into it, and check it
        # is the expected data when read from a function from the matapi.
        rfd, fd, cfilename   = open_tmp_file('pySndfiletest.' + filext)
        try:
            nbuff = 22050
            noise = 0.1 * N.random.randn(nbuff)

            # Open the copy file for writing
            b = Sndfile(cfilename, 'w', format, 1, nbuff)
            b.write_frames(noise)
            b.close()
Esempio n. 6
0
from scikits.audiolab import available_file_formats, available_encodings

for format in available_file_formats():
    print "File format %s is supported; available encodings are:" % format
    for enc in available_encodings(format):
        print "\t%s" % enc
    print ""