예제 #1
0
def split_wav(wav_file, ref_file, is_save=False, dir_name="split"):
    from scikits.audiolab import formatinfo as format
    import scikits.audiolab as audiolab
    import shutil

    fr = audiolab.sndfile(wav_file, "read")
    n_channels = fr.get_channels()
    fmt = format("wav", fr.get_encoding())
    fs = fr.get_samplerate()

    if is_save:
        shutil.rmtree(dir_name, ignore_errors=True)
        shutil.os.mkdir(dir_name)

    slices = dict()
    with open(ref_file, "r") as ref_read:
        for i, line in enumerate(ref_read.readlines()):
            fields = line.strip().split(" ")
            assert len(fields) == 3
            begin = int(fields[0])
            end = int(fields[1])
            word = fields[2]

            fr.seek(begin)
            sli = fr.read_frames(end - begin + 1)
            if not slices.has_key(word):
                slices[word] = []
            # if(len(sli) == 1):
            # print word, line
            slices[word].append(sli)
            if is_save:
                path = shutil.os.path.join(dir_name, str(i) + "-" + word + ".wav")
                afile = audiolab.sndfile(path, "write", fmt, n_channels, fs)
                afile.write_frames(sli, len(sli))
                afile.close()
    return slices
예제 #2
0
from tempfile import mkstemp
from os import remove

import numpy as N
from scikits.audiolab import formatinfo as format
import scikits.audiolab as audiolab

# Create a temp file in the system temporary dir, and always remove
# it at the end
cd, filename = mkstemp('tmptest.wav')
try:
    fmt = format('wav', 'pcm24')
    nchannels = 2
    fs = 44100

    afile = audiolab.sndfile(filename, 'write', fmt, nchannels, fs)

    # Create a stereo white noise, with Gaussian distribution
    tmp = 0.1 * N.random.randn(1000, nchannels)

    # Write the first 500 frames of the signal
    # Note that the write_frames method uses tmp's numpy dtype to determine how
    # to write to the file; sndfile also converts the data on the fly if necessary
    afile.write_frames(tmp, 500)

    afile.close()

    # Let's check that the written file has the expected meta data
    afile = audiolab.sndfile(filename, 'read')
    assert (afile.get_samplerate() == fs)
    assert (afile.get_channels() == nchannels)
예제 #3
0
from scikits.audiolab import formatinfo as format

f = format('aiff', 'ulaw')
print f

f = format('ircam', 'float32')
print f
예제 #4
0
from tempfile import mkstemp
from os import remove

import numpy as N
from scikits.audiolab import formatinfo as format
import scikits.audiolab as audiolab

# Create a temp file in the system temporary dir, and always remove
# it at the end
cd, filename    = mkstemp('tmptest.wav')
try:
    fmt         = format('wav', 'pcm24')
    nchannels   = 2
    fs          = 44100

    afile =  audiolab.sndfile(filename, 'write', fmt, nchannels, fs)

    # Create a stereo white noise, with Gaussian distribution
    tmp = 0.1 * N.random.randn(1000, nchannels)

    # Write the first 500 frames of the signal
    # Note that the write_frames method uses tmp's numpy dtype to determine how
    # to write to the file; sndfile also converts the data on the fly if necessary
    afile.write_frames(tmp, 500)

    afile.close()

    # Let's check that the written file has the expected meta data
    afile = audiolab.sndfile(filename, 'read')
    assert(afile.get_samplerate() == fs)
    assert(afile.get_channels() == nchannels)