当前位置: 首页>>代码示例>>Python>>正文


Python audiolab.Sndfile类代码示例

本文整理汇总了Python中scikits.audiolab.Sndfile的典型用法代码示例。如果您正苦于以下问题:Python Sndfile类的具体用法?Python Sndfile怎么用?Python Sndfile使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Sndfile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: read_sound

def read_sound(fp):
    """
    create a normalized float array and datarate from any audo file
    """
    if fp.endswith('mp3'):
        try:
            oname = 'temp.wav'
            #cmd = 'lame --decode "{0}" {1}'.format( fp ,oname )
            result = subprocess.call(['lame', '--decode', fp, oname])
            assert(result is 0)
            samplerate, data = wav.read(oname)
        except:
            print "couldn't run lame"
            try:
                import moviepy.editor as mpy
                aud_clip = mpy.AudioFileClip(fp)
                samplerate = aud_clip.fps
                data = aud_clip.to_soundarray()
            except:
                print "moviepy not installed?"
    if fp.endswith('aif'):
        #sf = aifc.open(fp)
        oname = fp
        sf = Sndfile(fp, 'r')
        sf.seek(0)
        data = sf.read_frames(sf.nframes)
        samplerate = sf.samplerate
    if fp.endswith('wav'):
        samplerate, data = wav.read(fp)

    if len(data.shape)>1: data = data[:,0]
    data = data.astype('float64')
    data /= data.max()
    return data, samplerate
开发者ID:paul-bauer-rfcx,项目名称:sound-analysis,代码行数:34,代码来源:rfcx_sounds.py

示例2: read_wav

    def read_wav(self, sample_path):

        sample = Sndfile(cwd + sample_path, 'r')
        sampling_rate = sample.samplerate
        channels = sample.channels
        encoding = sample.encoding
        frames_count = sample.nframes

        frames = sample.read_frames(frames_count, dtype=np.float32)
        sample.close()
        del sample

        if channels == 1:
            text_type = 'mono'
            sample_type = 0
        elif channels == 2:
            text_type = 'stereo'
            sample_type = 0b01100100
        else:
            text_type = '{0}-channels'.format(channels)

        if OPTIONS['verbose'] > 1:
            print "*", encoding, text_type, 'sample "', sample_path, '"', 4 * frames_count, 'kB'

        if OPTIONS['play_sound']:
            play(frames.astype(np.float64).T, sampling_rate)

        self.update({
            'sample_data': frames,
            'sample_type': sample_type,
            'channels': 2,
            'sample_bittype': 4
        })
开发者ID:DINKIN,项目名称:Samplicity,代码行数:33,代码来源:samplicity.py

示例3: Format

    class AudioWriter:
        syllableIndex = 0
        baseFilename = "syllable"
        fileOpen = False
        format = Format('flac', 'pcm24')
        f = None
        filecount = 0

        def open(self):
            self.f = Sndfile(self.baseFilename + "." + str(self.syllableIndex) + '.flac', 'w', self.format, 1, 44100)
            self.fileOpen = True

        def close(self):
            if self.fileOpen:
                self.f.close()
                self.syllableIndex += 1
                self.fileOpen = False

        def write(self, data):
            if not self.fileOpen:
                self.open()
            self.f.write_frames(data)

        def parseData(self, data):
            buffer = []
            for i in range(len(data) - 1):
                if i == len(data) - 2 or (data[i] == zero_val and data[i + 1] == zero_val):
                    if len(buffer) > 0:
                        self.write(np.array(buffer))
                        self.filecount += 1
                        buffer = []
                    self.close()
                else:
                    buffer.append(data[i])
开发者ID:benoit-girard,项目名称:birdsong,代码行数:34,代码来源:parameter-finder.py

示例4: file_to_features

	def file_to_features(self, wavpath):
		"Reads through a mono WAV file, converting each frame to the required features. Returns a 2D array."
		if verbose: print("Reading %s" % wavpath)
		if not os.path.isfile(wavpath): raise ValueError("path %s not found" % wavpath)
		sf = Sndfile(wavpath, "r")
		#if (sf.channels != 1) and verbose: print(" Sound file has multiple channels (%i) - channels will be mixed to mono." % sf.channels)
		if sf.samplerate != fs:         raise ValueError("wanted sample rate %g - got %g." % (fs, sf.samplerate))
		window = np.hamming(framelen)
		features = []
		while(True):
			try:
				chunk = sf.read_frames(framelen, dtype=np.float32)
				if len(chunk) != framelen:
					print("Not read sufficient samples - returning")
					break
				if sf.channels != 1:
					chunk = np.mean(chunk, 1) # mixdown
				framespectrum = np.fft.fft(window * chunk)
				magspec = abs(framespectrum[:framelen/2])

				# do the frequency warping and MFCC computation
				melSpectrum = self.mfccMaker.warpSpectrum(magspec)
				melCepstrum = self.mfccMaker.getMFCCs(melSpectrum,cn=True)
				melCepstrum = melCepstrum[1:]   # exclude zeroth coefficient
				melCepstrum = melCepstrum[:13] # limit to lower MFCCs

				framefeatures = melCepstrum   # todo: include deltas? that can be your homework.

				features.append(framefeatures)
			except RuntimeError:
				break
		sf.close()
		return np.array(features)
开发者ID:PapRazzi,项目名称:cdam,代码行数:33,代码来源:cdam.py

示例5: get_fft_points

def get_fft_points(sound_filename, fps, fft_pixels, rate = 1, fourierwidth = 0.3):
	"""TODO
	will generate rate points per frame
	Based on the script from
	http://classicalconvert.com/2008/04/
	how-to-visualize-music-using-animated-spectrograms-with
	-open-source-everything/"""
	f = Sndfile(sound_filename, 'r')
	divisor = f.samplerate / (rate * fps) # should be integer
	points = []
	framepos = 0L
	while framepos < f.nframes:
		read_len = (
			divisor if (framepos + divisor < f.nframes)
			else f.nframes - framepos)
		frames = f.read_frames(read_len)
		buff = []
		for frame in frames:
			# is frame iterable or just one chan?
			if getattr(frame, '__iter__', False):
				fval = sum(frame) / len(frame)
			else:
				fval = frame
			buff.append(fval)
		# TODO: trim to 1024 or so?
		outfft = fft(buff)
		spectrum = [
			(outfft[y].real
				if y < len(outfft) else 0.0)
			for y in xrange(fft_pixels)]
		points.append(spectrum)
		framepos += len(frames)
	f.close()
	# maximise
	return points
开发者ID:BGCX262,项目名称:zvis-git,代码行数:35,代码来源:snd.py

示例6: file_to_specgram

def file_to_specgram(path, specgrammode=None):
	if specgrammode==None: # default is to do a "normal" spectrogram right here
		if fftsize != framelen: raise ValueError("this mode requires normal fftsize")
		if not os.path.isfile(path):
			raise ValueError("path %s not found" % path)
		sf = Sndfile(path, "r")
		if sf.channels != 1:
			raise Error("ERROR in spemptk: sound file has multiple channels (%i) - mono audio required." % sf.channels)
		if sf.samplerate != fs:
			raise Error("ERROR in spemptk: wanted srate %g - got %g." % (fs, sf.samplerate))
		chunksize = 4096
		pcm = np.array([])
		while(True):
			try:
				chunk = sf.read_frames(chunksize, dtype=np.float32)
				pcm = np.hstack((pcm, chunk))
			except RuntimeError:
				break
		spec = stft(pcm).T
	else:
		raise ValueError("specgrammode not recognised: %s" % specgrammode)
	spec = spec[specfreqbinrange[0]:specfreqbinrange[1],:]
	mags = abs(spec)
	phasifiers = spec / mags
	if specgrammode==None:
		mags = np.log(mags)
	return (mags, phasifiers)
开发者ID:danstowell,项目名称:mptk,代码行数:27,代码来源:specgram_anywave.py

示例7: file_to_features

    def file_to_features(self,wavpath):

        sf = Sndfile(wavpath, "r")
        window = np.hamming(framelen)
        features = []
        while(True):
                try:
                    chunk = sf.read_frames(framelen, dtype=np.float32)
                    if len(chunk) != framelen:
                        print("Not read sufficient samples - returning")
                        break
                    if sf.channels != 1:
                        chunk = np.mean(chunk, 1) # mixdown
                    framespectrum = np.fft.fft(window * chunk)
                    magspec = abs(framespectrum[:framelen/2])

                    # do the frequency warping and MFCC computation
                    melSpectrum = self.mfccMaker.warpSpectrum(magspec)
                    melCepstrum = self.mfccMaker.getMFCCs(melSpectrum,cn=True)
                    melCepstrum = melCepstrum[1:]   # exclude zeroth coefficient
                    melCepstrum = melCepstrum[:13] # limit to lower MFCCs
                    framefeatures = melCepstrum
                    features.append(framefeatures)

                except RuntimeError:
                    break

        sf.close()
        return np.array(features)
开发者ID:sunshinelala1991,项目名称:Msc-Project-672323-Yun-Wang,代码行数:29,代码来源:BirdSongClassification.py

示例8: test_read_wave

def test_read_wave():
    f = Sndfile("../fcjf0/sa1.wav", 'r')
    data = f.read_frames(46797)
    data_arr = np.array(data)
    #print data_arr
    pyplot.figure()
    pyplot.specgram(data_arr)
    pyplot.show()
开发者ID:omarelshenawy,项目名称:SpeechRecognitionCourse,代码行数:8,代码来源:read_wav.py

示例9: writeWAV

 def writeWAV(self, data, filename):
     format = Format('wav')
     if (len(data.shape) == 2):
         f = Sndfile(filename, 'w', format, 2, self.samplingRate)
         f.write_frames(data)
         f.close()
     else:
         f = Sndfile(filename, 'w', format, 1, self.samplingRate)
         f.write_frames(data)
         f.close()
开发者ID:nlintz,项目名称:SigsysFinal,代码行数:10,代码来源:Analysis.py

示例10: test_bad_wavread

    def test_bad_wavread(self):
        """ Check wavread on bad file"""
        # Create a tmp audio file with non wav format, write some random data into it,
        # and check it can not be opened by wavread
        rfd, fd, cfilename   = open_tmp_file('pysndfiletest.wav')
        try:
            nbuff = 22050
            noise = 0.1 * N.random.randn(nbuff)

            # Open the copy file for writing
            format = audio_format('aiff', 'pcm16')
            b = Sndfile(cfilename, 'w', format, 1, nbuff)

            b.write_frames(noise)

            b.close()

            b = Sndfile(cfilename, 'r')
            rcnoise = b.read_frames(nbuff)
            b.close()

            try:
                rnoise  = wavread(cfilename)[0]
                raise Exception("wavread on non wav file succeded, expected to fail")
            except ValueError, e:
                pass
                #print str(e) + ", as expected"

        finally:
            close_tmp_file(rfd, cfilename)
开发者ID:LiberationFrequency,项目名称:BazzArch,代码行数:30,代码来源:test_matapi.py

示例11: _test_int_io

    def _test_int_io(self, dt):
        # TODO: check if neg or pos value is the highest in abs
        rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav')
        try:
            # Use almost full possible range possible for the given data-type
            nb = 2 ** (8 * np.dtype(dt).itemsize - 3)
            fs = 22050
            nbuff = fs
            a = np.random.random_integers(-nb, nb, nbuff)
            a = a.astype(dt)

            # Open the file for writing
            format = Format('wav', _DTYPE_TO_ENC[dt])
            b = Sndfile(fd, 'w', format, 1, fs)

            b.write_frames(a)
            b.close()

            b = Sndfile(cfilename, 'r')

            read_a  = b.read_frames(nbuff, dtype=dt)
            b.close()

            assert_array_equal(a, read_a)

        finally:
            close_tmp_file(rfd, cfilename)
开发者ID:LiberationFrequency,项目名称:BazzArch,代码行数:27,代码来源:test_sndfile.py

示例12: downsample

def downsample(fs, sig):
    in_file = random_string() + ".wav"
    out_file = random_string() + ".wav"

    frame_len = fs * WINDOW_SIZE
    pad = len(sig)%frame_len
    if pad > 0:
        sig = np.append(sig, np.zeros(frame_len - pad))

    f = Sndfile(in_file, 'w', Format(type="wav", encoding='pcm16', endianness="file"), 1, fs)
    f.write_frames(sig) 
    f.close()

    sox_in = pysox.CSoxStream(in_file)
    sox_out = pysox.CSoxStream(out_file, 'w', pysox.CSignalInfo(SAMPLE_RATE, 1, 8), fileType='wav')
    sox_chain = pysox.CEffectsChain(sox_in, sox_out)
    sox_chain.add_effect(pysox.CEffect("rate", [str(SAMPLE_RATE)]))
    sox_chain.flow_effects()
    sox_out.close()

    f = Sndfile(out_file, 'r')
    sig = f.read_frames(f.nframes)
    f.close()

    os.unlink(in_file)
    os.unlink(out_file)

    return sig
开发者ID:braindead,项目名称:nnvad,代码行数:28,代码来源:mlp_vad.py

示例13: load_sound

def load_sound(filename):
    """
    load a sound file and return a numpy array

    INFO: The values are normalized between -1 and 1
    :param filename:
    :return: numpy array with (sound_lenght, channels) shape
    """
    f = Sndfile(filename, 'r')
    data = f.read_frames(f.nframes, dtype=np.float64)
    return data, f.samplerate
开发者ID:arventwei,项目名称:protolab_sound_recognition,代码行数:11,代码来源:io_sound.py

示例14: load

def load(filename):
    """Load an audio file and average over channels. Returns the data as a
    numpy array and the sampling rate.

    """
    fh = Sndfile(filename, "r")
    data = fh.read_frames(fh.nframes)
    if data.ndim == 2:
        data = np.mean(data, axis=-1)
    rate = fh.samplerate
    return data, rate
开发者ID:jhamrick,项目名称:fall2013-python-seminar,代码行数:11,代码来源:sound.py

示例15: save_wav

def save_wav(sound, action_label, object_label):
    wav_path = '/tmp/new_wav'
    filename = os.path.join(wav_path, action_label + '-' + object_label + '-' + str(time.time()) + '.wav')
    format = Format('wav')

    print 'writing', filename, '...',

    f = Sndfile(filename, 'w', format, 1, 44100)
    f.write_frames(sound)
    f.close()
    print 'DONE'
开发者ID:Kenkoko,项目名称:ua-ros-pkg,代码行数:11,代码来源:audio_read.py


注:本文中的scikits.audiolab.Sndfile类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。