Example #1
0
def load_wave(fname):
    tutor_wav = wav.wavfile(fname)
    aud_raw = np.array(tutor_wav.read())

    #figure(4);
    #clf
    #plot(aud_raw[0:samples_per_bin])
    #legend("Audio Input -- Audio Samples -- First Bin")

    #    aud_raw -= aud_raw.mean();
    #   aud_raw /= aud_raw.std();
    aud_sample = np.reshape(aud_raw,
                            (-1, samples_per_bin))  # 100 samples go into 1 bin
    aud_dct = fftp.dct(aud_sample.real.astype(float), type=2,
                       norm='ortho')  #.T # matrix right way round?

    # Normalise:
    aud_dct -= aud_dct.mean()
    aud_dct /= aud_dct.std()

    #figure(5);
    #clf;
    #plot(aud_dct[0])
    #legend("FFT of the first bin");

    #figure(6);
    #clf;
    #plot(aud_dct[:,0])
    #legend("Amplitude of first frequency of FFT across all bins");

    return aud_dct.T
Example #2
0
def save_wave(fname, song):

    output = fftp.idct(x=song.T, type=2, norm='ortho')
    #output = fftp.idct(x=song, type=2, norm='ortho');
    output2 = (output).ravel()

    maximum = (abs(output2)).max()

    #    output2 *= (30000) / maximum; # scale to about 80% of 16 bit range

    #figure(7)
    #clf
    #plot(output2[0:100]);
    #legend("Audio out -- first 100 sample points")
    out_wav = wav.wavfile(fname,
                          mode="w",
                          sampling_rate=rate,
                          dtype=u'f',
                          nchannels=1)
    out_wav.write(data=output2, scale=True)
    # do I need to reshape, or is the automatic flattening the right thing?
    #out_wav.write(data=output2); # do I need to reshape, or is the automatic flattening the right thing?
    out_wav.flush()

    return output2
Example #3
0
def load_data(basename, filterer=None, pitchdir=None):
    """
    Load data from wav and plg files. If filterer is not None, filters
    pitch trace.
    """
    from ewave import wavfile
    from chirp.common import plg
    fp = wavfile(basename + ".wav")
    signal, Fs = fp.read(), fp.sampling_rate

    if isinstance(pitchdir, basestring):
        plgfile = os.path.join(pitchdir, os.path.split(basename)[1] + '.plg')
    else:
        plgfile = basename + '.plg'
    if not os.path.exists(plgfile):
        return signal, Fs / 1000., None, None

    pitch = plg.read(plgfile)
    if filterer is not None:
        ind = postfilter.ind_endpoints(filterer(pitch))
        if ind is None:
            return signal, Fs / 1000., None, None
        pitch = pitch[ind[0]:ind[1] + 1]
    t = pitch['time']
    if 'p.map' in pitch.dtype.names:
        p = pitch['p.map']
    else:
        p = pitch['p.mmse']
    return signal, Fs / 1000., t, p
Example #4
0
    def splitfile(self, wavfile, lblfile, cout=None):
        """
        Split the signal in <wavfile> into the elements in <lblfile>.

        yields (extracted signal, sampling_rate)
        """
        elems = geom.elementlist.read(lblfile)
        if self.options['merge_elements']:
            if self.options['boxmask']:
                elems = [elems.range]
            else:
                raise NotImplementedError("merging polygons not implemented")

        with ewave.wavfile(wavfile, 'r') as fp:
            signal, Fs = fp.read(), fp.sampling_rate / 1000.
            for i, elem in enumerate(elems):
                if geom.elementlist.element_type(elem) == 'interval':
                    print >> cout, "** Element %d, interval bounds (%.2f, %.2f)" % (i, elem[0], elem[1])
                    fun = intervalsplit.split
                elif self.options['boxmask']:
                    print >> cout, "** Element %d, polygon interval (%.2f, %.2f)" % (i, elem.bounds[0],
                                                                                     elem.bounds[2])
                    fun = intervalsplit.split
                else:
                    raise NotImplementedError("polygon extraction not implemented")
                yield i, fun(signal, elem, Fs, **self.options), Fs
Example #5
0
def load_wave(fname): 
    tutor_wav = wav.wavfile(fname);
    aud_raw = np.array(tutor_wav.read());

    #figure(4);
    #clf
    #plot(aud_raw[0:samples_per_bin])
    #legend("Audio Input -- Audio Samples -- First Bin")

#    aud_raw -= aud_raw.mean();
    #   aud_raw /= aud_raw.std();
    aud_sample = np.reshape(aud_raw, (-1, samples_per_bin)) # 100 samples go into 1 bin
    aud_dct = fftp.dct(aud_sample.real.astype(float), type=2, norm='ortho')#.T # matrix right way round?

    # Normalise:
    aud_dct -= aud_dct.mean(); 
    aud_dct /= aud_dct.std();

    #figure(5);
    #clf;
    #plot(aud_dct[0])
    #legend("FFT of the first bin");

    #figure(6);
    #clf;
    #plot(aud_dct[:,0])
    #legend("Amplitude of first frequency of FFT across all bins");
         
    return aud_dct.T; # we need matrix in format that we have the 100 frequency as rows.
Example #6
0
def main(argv=None, cout=None, cerr=None, **kwargs):
    import os
    import sys
    from chirp.version import version
    if argv is None:
        argv = sys.argv[1:]
    if cout is None:
        cout = sys.stdout
    if cerr is None:
        cerr = sys.stderr

    import getopt
    from chirp.common.config import configoptions
    config = configoptions()

    opts, args = getopt.getopt(argv, 'hvc:')

    for o, a in opts:
        if o == '-h':
            print _scriptdoc
            return -1
        elif o == '-v':
            print " %s version %s" % (_scriptname, version)
            return -1
        elif o == '-c':
            config.read(a)
    if len(args) < 1:
        print _scriptdoc
        return -1

    wavfile = args[0]
    basename = os.path.splitext(wavfile)[0]
    if len(args) > 1:
        maskfile = args[1]
    else:
        maskfile = basename + ".ebl"

    print >> cout, "* Program: %s" % _scriptname
    print >> cout, "** Version: %s" % version
    print >> cout, "* Sound file: %s" % wavfile
    print >> cout, "* Mask file: %s" % maskfile

    splt = splitter(config)
    print >> cout, splt.options_str()

    for i, signal, Fs in splt.splitfile(wavfile, maskfile, cout=cout):
        outfile = "%s_e%03d.wav" % (os.path.split(basename)[1], i)
        print "** Writing extracted signal to %s" % outfile
        fp = ewave.wavfile(outfile, 'w', sampling_rate=Fs * 1000)
        fp.write(signal)
Example #7
0
def test(soundfile):

    from ewave import wavfile
    fp = wavfile(soundfile)
    signal, Fs = fp.read(), fp.sampling_rate

    class SpecViewFrame(wx.Frame):
        def __init__(self, parent=None):
            super(SpecViewFrame, self).__init__(parent, title="TSViewer Test App", size=(1000, 300),
                                                style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
            self.figpanel = SpecViewer(self, -1)
            self.figpanel.plot_data(signal, Fs)

    app = wx.PySimpleApp()
    app.frame = SpecViewFrame()
    app.frame.Show()
    app.MainLoop()
Example #8
0
    def load_signal(self, locator, dtype='d'):
        """
        Loads the signal and computes the spectrogram.

        dtype: the data type to store the output in. Use
               single-precision floats if needed to reduce storage
               requirements.
        """
        from ewave import wavfile
        from libtfr import fgrid, dynamic_range
        from chirp.common.signal import spectrogram
        from chirp.common.geom import elementlist, masker
        from numpy import linspace, log10

        fp = wavfile(locator)
        signal = fp.read()
        Fs = fp.sampling_rate

        speccr = spectrogram(**self.options)
        # adjust window size to get correct number of frequency bands
        df = 1. * (self.options['freq_range'][1] - self.options['freq_range'][0]) / self.options['nfreq']
        nfft = int(Fs / df)

        spec, extent = speccr.linspect(signal, Fs / 1000, nfft=nfft)
        F, ind = fgrid(Fs, nfft, self.options['freq_range'])   # in Hz
        spec = spec[ind, :]
        T = linspace(extent[0], extent[1], spec.shape[1])      # in ms

        # first convert the spectrogram to its final scale
        if self.options['powscale'].startswith('log'):
            # TODO calculate dynamic range of the signal for non 16 bit PCM?
            spec = log10(dynamic_range(spec, 96))
            # recenter spectrogram
            if self.options['subtract_mean']:
                spec -= spec.mean()

        if self.options['mask'] != 'none':
            eblfile = os.path.splitext(locator)[0] + elementlist.default_extension
            if os.path.exists(eblfile):
                mask = elementlist.read(eblfile)
                spec = masker(boxmask=self.options['mask'] == 'box').cut(spec, mask, T, F / 1000.)

        return spec.astype(dtype)
Example #9
0
def save_wave(fname, song):

    output = fftp.idct(x=song.T, type=2, norm='ortho');
    #output = fftp.idct(x=song, type=2, norm='ortho');
    output2 = (output).ravel();

    maximum = (abs(output2)).max()

#    output2 *= (30000) / maximum; # scale to about 80% of 16 bit range

    #figure(7)
    #clf
    #plot(output2[0:100]);
    #legend("Audio out -- first 100 sample points")
    out_wav = wav.wavfile(fname, mode="w", sampling_rate = rate, dtype = u'f', nchannels = 1);
    out_wav.write(data=output2, scale=True); # do I need to reshape, or is the automatic flattening the right thing?    
    #out_wav.write(data=output2); # do I need to reshape, or is the automatic flattening the right thing?
    out_wav.flush()

    return output2
Example #10
0
def cpitch(argv=None, cout=None, cerr=None, **kwargs):
    """ The script front-end """
    import sys
    from chirp.version import version
    if argv is None:
        argv = sys.argv[1:]
    if cout is None:
        cout = sys.stdout
    if cerr is None:
        cerr = sys.stderr

    import getopt
    from chirp.common.config import configoptions
    config = configoptions()

    maskfile = None

    opts, args = getopt.getopt(argv, 'hvc:m:')

    for o, a in opts:
        if o == '-h':
            print _scriptdoc
            return -1
        elif o == '-v':
            print "cpitch version %s" % version
            return -1
        elif o == '-c':
            if not os.path.exists(a):
                print >> cout, "ERROR: config file %s doesn't exist" % a
                return -1
            config.read(a)
        elif o == '-m':
            maskfile = a
    if len(args) < 1:
        print _scriptdoc
        return -1

    print >> cout, "* Program: cpitch"
    print >> cout, "** Version: %s" % version
    print >> cout, "* Input: %s" % args[0]

    from ewave import wavfile
    try:
        fp = wavfile(args[0])
    except IOError:
        print >> cerr, "No such file %s" % args[0]
        return -2
    except:
        print >> cerr, "Input file %s must be in WAV format" % args[0]
        return -2

    pcm = fp.read()
    samplerate = fp.sampling_rate / 1000.
    print >> cout, "** Samples:", pcm.size
    print >> cout, "** Samplerate: %.2f (kHz)" % samplerate

    pt = tracker(configfile=config, samplerate=samplerate * 1000, **kwargs)
    print >> cout, pt.spectrogram_options_str()
    print >> cout, pt.template_options_str()
    print >> cout, "* DLFT spectrogram:"
    spec, tgrid, fgrid = pt.matched_spectrogram(pcm, samplerate)
    print >> cout, "** Dimensions:", spec.shape

    print >> cout, pt.particle_options_str()

    if maskfile is not None and os.path.exists(maskfile):
        from chirp.common.geom import elementlist, masker
        print >> cout, "* Mask file:", maskfile
        elems = elementlist.read(maskfile)
        mask = masker(configfile=config, **kwargs)
        for startcol, mspec, imask in mask.split(spec, elems, tgrid, fgrid, cout=cout):
            try:
                startframe, pitch_mmse, pitch_var, pitch_map, stats = pt.track(mspec, cout=cout, mask=imask)
                stats['p.map'] = None if pitch_map is None else pitch_map * samplerate
                T = tgrid[startframe + startcol:startframe + startcol + pitch_mmse.shape[0]]
                # + tracker.options['winsize'] / (samplerate * 1000) ??
                ptrace = pitchtrace(T, pitch_mmse * samplerate, pitch_var * samplerate * samplerate,
                                    **stats)
                print >> cout, "*** Pitch calculations:"
                ptrace.write(cout)
            except ValueError, e:
                print >> cout, "*** Pitch calculation error: %s" % e
                continue
Example #11
0
    def load_file(self, fname):
        file_type = os.path.splitext(fname)[1]
        if file_type == _el_ext:
            el = self.load_elements(fname)
            if el: self.status.SetStatusText("Loaded %d elements from %s" % (len(el), fname))
        elif file_type == _pitch_ext:
            try:
                self.spec.plot_plg(fname)
                self.status.SetStatusText("Loaded pitch data from %s" % fname)
            except Exception, e:
                self.status.SetStatusText("Error reading pitch data from {}: {}".format(fname, e))
        elif file_type == _config_ext:
            self.load_params(fname)
        else:
            fp = ewave.wavfile(fname)
            sig, Fs = fp.read(), fp.sampling_rate / 1000
            self.filename = fname
            self.SetTitle(fname)
            self.spec.clear()
            self.list.DeleteAllItems()
            self.spec.plot_data(sig, Fs)
            el = self.load_elements(fname)
            if self.configfile.getboolean('gui', 'auto_load_plg', True):
                pitchfile = os.path.splitext(fname)[0] + _pitch_ext
                try:
                    self.spec.plot_plg(pitchfile)
                except:
                    pass
            if el: self.status.SetStatusText("Opened file %s; loaded %d elements" % (fname, len(el)))
            else: self.status.SetStatusText("Opened file %s" % fname)