示例#1
0
def _wav_write(signal, wav_name, sample_rate):
    """
    ::

        Utility routine for writing wav files, use scikits.audiolab if available
    """
    if HAVE_AUDIOLAB:
        scikits.audiolab.wavwrite(signal, wav_name, sample_rate)
    else:
        signal = numpy.atleast_2d(signal)
        w = wave.Wave_write(wav_name)
        if not w:
            print("Error opening file named: ", wav_name)
            raise error.BregmanError()
        w.setparams(
            (signal.shape[0], 2, sample_rate, signal.shape[1], 'NONE', 'NONE'))
        b_signal = ''  # C-style binary string
        for i in range(signal.shape[1]):
            # transform to C-style binary string
            b_signal += wave.struct.pack('h', int(32767 * signal[0, i]))
            if signal.shape[0] > 1:
                # transform to C-style binary string
                b_signal += wave.struct.pack('h', int(32767 * signal[1, i]))
        w.writeframes(b_signal)
        w.close()
    return True
示例#2
0
    def insert_audio_files(fileList, dbName, chroma=True, mfcc=False, cqft=False, progress=None):
        """
        ::

            Simple insert features into an audioDB database named by dbBame.
            Features are either chroma [default], mfcc, or cqft. 
            Feature parameters are default.
        """
        db = adb.get(dbName, "w")
        if not db:
            print ("Could not open database: %s" %dbName)
            return False    
        del db # commit the changes by closing the header
        db = adb.get(dbName) # re-open for writing data
        # FIXME: need to test if KEY (%i) already exists in db
        # Support for removing keys via include/exclude keys
        for a, i in enumerate(fileList):
            if progress:
                progress((a+0.5)/float(len(fileList)),i) # frac, fname
            print ("Processing file: %s" %i)
            F = features.Features(i)            
            if chroma: F.feature_params['feature']='chroma'
            elif mfcc: F.feature_params['feature']='mfcc'
            elif cqft: F.feature_params['feature']='cqft'
            else:
                raise error.BregmanError("One of chroma, mfcc, or cqft must be specified for features")
            F.extract()
            # raw features and power in Bels
            if progress:
                progress((a+1.0)/float(len(fileList)),i) # frac, fname
            db.insert(featData=F.CHROMA.T, powerData=adb.feature_scale(F.POWER, bels=True), key=i) 
            # db.insert(featData=F.CHROMA.T, powerData=F.feature_scale(F.POWER, bels=True), key=i)
        return db
示例#3
0
    def _istftm(self,
                X_hat=None,
                Phi_hat=None,
                pvoc=False,
                usewin=True,
                resamp=None):
        """
        :: 
            Inverse short-time Fourier transform magnitude. Make a signal from a |STFT| transform.
            Uses phases from self.STFT if Phi_hat is None.

            Inputs:
            X_hat - N/2+1 magnitude STFT [None=abs(self.STFT)]
            Phi_hat - N/2+1 phase STFT   [None=exp(1j*angle(self.STFT))]
            pvoc - whether to use phase vocoder [False]      
            usewin - whether to use overlap-add [False]

            Returns:
             x_hat - estimated signal
        """
        if not self._have_stft:
            return None
        X_hat = self.X if X_hat is None else P.np.abs(X_hat)
        if pvoc:
            self._pvoc(X_hat, Phi_hat, pvoc)
        else:
            Phi_hat = P.angle(self.STFT) if Phi_hat is None else Phi_hat
            self.X_hat = X_hat * P.exp(1j * Phi_hat)
        if usewin:
            if self.win is None:
                self.win = P.ones(
                    self.wfft) if self.window == 'rect' else P.np.sqrt(
                        P.hanning(self.wfft))
            if len(self.win) != self.nfft:
                self.win = P.r_[self.win, P.np.zeros(self.nfft - self.wfft)]
            if len(self.win) != self.nfft:
                error.BregmanError(
                    "features_base.Features._istftm(): assertion failed len(self.win)==self.nfft"
                )
        else:
            self.win = P.ones(self.nfft)
        if resamp:
            self.win = sig.resample(self.win,
                                    int(P.np.round(self.nfft * resamp)))
        fp = self._check_feature_params()
        self.x_hat = self._overlap_add(P.real(P.irfft(self.X_hat.T)),
                                       usewin=usewin,
                                       resamp=resamp)
        if self.verbosity:
            print("Extracted iSTFTM->self.x_hat")
        return self.x_hat
示例#4
0
    def play_snd(data, sample_rate=44100):
        """
        ::

            Bregman Linux/OSX/Windows sound player function.
            data - a numpy array
            sample_rate - default 44100
        """
        m = abs(data).max() + 0.001
        if m > 1.0: data /= m
        _wav_write(data, AUDIO_TMP_FILE, sample_rate)
        command = [sound_options['soundplayer'], AUDIO_TMP_FILE]
        res = subprocess.call(command)
        if res:
            raise error.BregmanError("Error in " + command[0])
        return res
示例#5
0
 def _extract_error(self):
     error.BregmanError("unrecognized feature in Features.extract()")