Exemple #1
0
 def removeFiles(self):
     if self.logs:
         self.logs.write('removing temp file')
     start_time = time.time()
     if '.flac' in self.filename: #if flac convert to wav
         if not self.removeFile:
             if self.logs:
                 self.logs.write('file was flac: creating wav copy')
             try:
                 format = Format('wav')
                 f = Sndfile(self.localfilename+".wav", 'w', format, self.channs, self.sample_rate)
                 f.write_frames(self.original)
                 f.close()
                 os.remove(self.localfilename)
                 self.localfilename = self.localfilename+".wav"
             except:
                 if self.logs :
                     self.logs.write("Rec.py : error creating wav copy : "+self.localfilename) 
                 return False
         
     if self.removeFile:
         if self.logs:
             self.logs.write('removing tmeporary file '+self.localfilename)
         if os.path.isfile(self.localfilename):
             os.remove(self.localfilename)
         if self.logs :
             self.logs.write("Rec.py : removed temporary file:" + str(time.time() - start_time))
     
     return True
Exemple #2
0
class WaveWriter(object):
    def __init__(
            self,
            filename,
            samplerate=44100,
            channels=1,
            format=Format('wav', 'float32'),
    ):

        self._info = {
            'filename': filename,
            'samplerate': samplerate,
            'channels': channels,
            'format': format,
            'frames': 0,
        }  # TODO: metadata not implemented

        self._sndfile = Sndfile(filename, 'w', format, channels, samplerate)
        if not self._sndfile:
            raise NameError('Sndfile error loading file %s' % filename)

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        self._sndfile.sync()
        self._sndfile.close()
        if value: raise  # ????

    def write(self, data):
        nframes, channels = data.shape
        assert channels == self._info['channels']
        self._sndfile.write_frames(data)
    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)
Exemple #4
0
    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
        })
Exemple #5
0
def savenoise(samps: np.ndarray, nhours: int, ofn: Path, fs: int, nsec: int,
              wavapi: str):
    if not ofn:
        return

    ofn = Path(ofn).expanduser()

    f: Any

    if wavapi == "raw":
        if ofn.is_file():  # delete because we're going to append
            ofn.unlink()

        with ofn.open("a+b") as f:
            for _ in range(int(nhours * 3600 / nsec)):
                f.write(samps)

    elif wavapi == "scipy":  # pragma: no cover
        from scipy.io import wavfile

        wavfile.write(ofn, fs, samps)
    elif wavapi == "skaudio":  # pragma: no cover
        from scikits.audiolab import Format, Sndfile

        fmt = Format("flac")
        f = Sndfile(ofn, "w", fmt, 1,
                    16000)  # scikit-audio does not have context manager
        f.write_frames(samps)
        f.close()
    else:
        raise ValueError(f"I do not understand write method {wavapi}")
    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)
Exemple #7
0
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
Exemple #8
0
	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)
    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])
Exemple #10
0
    def test_basic_io(self):
        """ Check open, close and basic read/write"""
        # dirty !
        ofilename = join(TEST_DATA_DIR, 'test.wav')
        rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav')
        try:
            nbuff = 22050

            # Open the test file for reading
            a = Sndfile(ofilename, 'r')
            nframes = a.nframes

            # Open the copy file for writing
            format = Format('wav', 'pcm16')
            b = Sndfile(fd, 'w', format, a.channels, a.samplerate)

            # Copy the data
            for i in range(nframes / nbuff):
                tmpa = a.read_frames(nbuff)
                assert tmpa.dtype == np.float
                b.write_frames(tmpa)
            nrem = nframes % nbuff
            tmpa = a.read_frames(nrem)
            assert tmpa.dtype == np.float
            b.write_frames(tmpa)

            a.close()
            b.close()
        finally:
            close_tmp_file(rfd, cfilename)
Exemple #11
0
def timeStretchAudio(inputAudio, outputAudio, outputDuration, writeOutput=1):

	originalWav = Sndfile(inputAudio, 'r')
	x = originalWav.read_frames(originalWav.nframes)
	fs = originalWav.samplerate
	nChannel = originalWav.channels
	print fs
	if nChannel >1:
		x = x[0]


	w = np.hamming(801)
	N = 2048
	t = -90
	minSineDur = .005
	maxnSines = 150
	freqDevOffset = 20
	freqDevSlope = 0.02
	Ns = 512
	H = Ns/4
	tfreq, tmag, tphase = SM.sineModelAnal(x, fs, w, N, H, t, maxnSines, minSineDur, freqDevOffset, freqDevSlope)
	inputDur = float(len(tfreq)*H/fs)
	#timeScale = np.array([0.1,0.1, inputDur, inputDur*2])
	timeScale = np.array([0,0, .4,outputDuration])

	ytfreq, ytmag = trans.sineTimeScaling(tfreq, tmag, timeScale)
	y = SM.sineModelSynth(ytfreq, ytmag, np.array([]), Ns, H, fs)
	
	if writeOutput ==1:
		outputWav = Sndfile(outputAudio, 'w', originalWav.format, originalWav.channels, originalWav.samplerate)
		outputWav.write_frames(y)
		outputWav.close()
	else:
		return y, fs, nChannel
Exemple #12
0
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
Exemple #13
0
    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)
    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)
Exemple #15
0
    def test_simple(self):
        ofilename = join(TEST_DATA_DIR, 'test.wav')
        # Open the test file for reading
        a = Sndfile(ofilename, 'r')
        nframes = a.nframes

        buffsize = 1024
        buffsize = min(nframes, buffsize)

        # First, read some frames, go back, and compare buffers
        buff = a.read_frames(buffsize)
        a.seek(0)
        buff2 = a.read_frames(buffsize)
        assert_array_equal(buff, buff2)

        a.close()

        # Now, read some frames, go back, and compare buffers
        # (check whence == 1 == SEEK_CUR)
        a = Sndfile(ofilename, 'r')
        a.read_frames(buffsize)
        buff = a.read_frames(buffsize)
        a.seek(-buffsize, 1)
        buff2 = a.read_frames(buffsize)
        assert_array_equal(buff, buff2)

        a.close()

        # Now, read some frames, go back, and compare buffers
        # (check whence == 2 == SEEK_END)
        a = Sndfile(ofilename, 'r')
        buff = a.read_frames(nframes)
        a.seek(-buffsize, 2)
        buff2 = a.read_frames(buffsize)
        assert_array_equal(buff[-buffsize:], buff2)
def load_pcm(path):
    wave = Sndfile(path, "r")
    pcm = wave.read_frames(wave.nframes)
    wave.close()
    if wave.channels is not 1:
        pcm = pcm[:, 0]
    return (pcm, wave.samplerate)
Exemple #17
0
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
    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)
Exemple #19
0
def steg(data1, S1, T1, data2, S2, T2, theta):
    d1count = len(data1)
    d2count = len(data2)
    expectedcount = d1count + d2count
    target_split = splitArray(data2, S2, theta)
    gap_in_s = int(numpy.floor(T1 * theta / T2))
    gap = gap_in_s * S1
    i = 0
    j = gap
    while len(target_split):
        data1[j:0] = target_split[0]
        #		print "####################################################################"
        #		print "New sample sequence: data1[", j-2, ":", j+len(target_split[0])+2, "] : ", data1[j-2:j+len(target_split[0])+2]
        j += gap + len(target_split[0])
        target_split.pop(0)

    data1[0] = T1 ^ len(data1)
    data1[1] = S2 ^ len(data1)
    data1[2] = T2 ^ len(data1)
    data1[3] = theta

    size = len(data1)
    #	print "\td1: ", d1count, "\n\td2: ", d2count, "\n\tec: ", expectedcount, "\n\tsize: ", size
    op = numpy.asarray(data1)
    format = Format('wav')
    f = Sndfile("stego_file.wav", 'w', format, 1, S1)
    f.write_frames(op)
    f.close()
    return size
    def test_basic_io(self):
        """ Check open, close and basic read/write"""
        # dirty !
        ofilename = join(TEST_DATA_DIR, 'test.wav')
        rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav')
        try:
            nbuff = 22050

            # Open the test file for reading
            a = Sndfile(ofilename, 'r')
            nframes = a.nframes

            # Open the copy file for writing
            format = Format('wav', 'pcm16')
            b = Sndfile(fd, 'w', format, a.channels, a.samplerate)

            # Copy the data
            for i in range(nframes / nbuff):
                tmpa    = a.read_frames(nbuff)
                assert tmpa.dtype == np.float
                b.write_frames(tmpa)
            nrem    = nframes % nbuff
            tmpa    = a.read_frames(nrem)
            assert tmpa.dtype == np.float
            b.write_frames(tmpa)

            a.close()
            b.close()
        finally:
            close_tmp_file(rfd, cfilename)
Exemple #21
0
	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)
    def test_simple(self):
        ofilename = join(TEST_DATA_DIR, 'test.wav')
        # Open the test file for reading
        a = Sndfile(ofilename, 'r')
        nframes = a.nframes

        buffsize = 1024
        buffsize = min(nframes, buffsize)

        # First, read some frames, go back, and compare buffers
        buff = a.read_frames(buffsize)
        a.seek(0)
        buff2 = a.read_frames(buffsize)
        assert_array_equal(buff, buff2)

        a.close()

        # Now, read some frames, go back, and compare buffers
        # (check whence == 1 == SEEK_CUR)
        a = Sndfile(ofilename, 'r')
        a.read_frames(buffsize)
        buff = a.read_frames(buffsize)
        a.seek(-buffsize, 1)
        buff2 = a.read_frames(buffsize)
        assert_array_equal(buff, buff2)

        a.close()

        # Now, read some frames, go back, and compare buffers
        # (check whence == 2 == SEEK_END)
        a = Sndfile(ofilename, 'r')
        buff = a.read_frames(nframes)
        a.seek(-buffsize, 2)
        buff2 = a.read_frames(buffsize)
        assert_array_equal(buff[-buffsize:], buff2)
Exemple #23
0
    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)
def extractData(file_names):
    data = []
    targets = []
    for k, v in file_names.items():
        for f_name in v:
            source_fname = k + "/" + f_name
            target_fname = k + "/" + f_name.split(".")[0] + ".TXT"
            source_fname = "./TIMIT" + source_fname[1:]
            target_fname = "./TIMIT" + target_fname[1:]

            audio_file = Sndfile(source_fname, "r")
            sr = audio_file.samplerate
            audio = audio_file.read_frames(audio_file.nframes)
            datum = mfcc(audio, samplerate=sr, nfilt=64, numcep=40)
            #datum = logfbank( audio, samplerate=sr, nfilt=64 )
            datum = preprocessing.scale(datum)
            data.append(datum)
            audio_file.close()

            with open(target_fname, "r") as text_file:
                target_txt = ' '.join(text_file.read().lower().strip().replace(
                    ".", "").split()[2:])
                target_txt = filter(lambda x: x not in special_chars,
                                    target_txt)
                target_txt = target_txt.replace(' ', '  ').split(' ')
                target = np.hstack(
                    ['<space>' if x == '' else list(x) for x in target_txt])
                target = np.asarray( [ 0 if x == '<space>' else ord(x) - ( ord('a') - 1 )\
                                        for x in target ] )
                targets.append(target)
    return data, targets
Exemple #25
0
class WaveWriter(object) :
	def __init__(self,
				filename,
				samplerate = 44100,
				channels = 1,
				format = Format('wav','float32'),
				) :

		self._info = {	'filename' : filename , 
				'samplerate' : samplerate,
				'channels' : channels,
				'format' : format,
				'frames' : 0,
				} # TODO: metadata not implemented

		self._sndfile = Sndfile(filename, 'w', format, channels, samplerate)
		if not self._sndfile :
			raise NameError('Sndfile error loading file %s' % filename)

	def __enter__(self) :
		return self
	def __exit__(self, type, value, traceback) :
		self._sndfile.sync()
		self._sndfile.close()
		if value: raise # ????

	def write(self, data) :
		nframes, channels = data.shape
		assert channels == self._info['channels']
		self._sndfile.write_frames(data)
Exemple #26
0
def save_file(device,
              sound,
              name_append=None,
              prefix=(BASE_PATH + "/../recordings/")):
    file = Sndfile(
        prefix + now_stamp() + ("__" + name_append if name_append else "") +
        ".wav", 'w', Format('wav', 'pcm16'), device.channels, device.rate)
    file.write_frames((1.0 / 32678) * sound)
    file.close()
Exemple #27
0
 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()
Exemple #28
0
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'
Exemple #29
0
def hodorifyIt(inputFile, outputFile, karaokeExt = '.txt'):

	#reading input wave file
	inputAudio = Sndfile(inputFile, 'r')
	audio = inputAudio.read_frames(inputAudio.nframes)
	nframes = inputAudio.nframes
	fs = inputAudio.samplerate
	nChannel = inputAudio.channels

	fname, ext = os.path.splitext(inputFile)
	karaokeFile  = fname + karaokeExt

	#parse the karaoke file
	karaokeData = KP.parseKaraokeFile(karaokeFile)


	#assign which syllable to use ho and which ones to use dor
	toogle = 0
	sylType = ['ho', 'dor']
	for ii,elem in enumerate(karaokeData['data']):
		if elem['syl'] == '-':
			toogle =0
			continue
		karaokeData['data'][ii]['sylType'] = sylType[toogle]
		toogle = (toogle +1)%2

	dumpSonicVisualizerAnnotFile("tryHODOR.txt", karaokeData['data'])

	#initialize all the hodor locations with not processed flag (later to be for exploiting repetitive hodors)
	for ii,elem in enumerate(karaokeData['data']):
		karaokeData['data'][ii]['processed']=0

	#creating mapping between file names and tones
	toneMapp = createToneMappFiles(toneMappFile)

	#processHere the logic for Hodor input file for each word
	karaokeData = hodorFileSelection(karaokeData, toneMapp)

	#do center channel cut
	audio = cutCenterChannel(audio, fs, karaokeData)

	#estimate the possible repetitions in the karaoke data, i.e. output with same note and duration
	print len(karaokeData['data'])
	repMTX = estimateRepetitiveHodors(karaokeData)

	emptyTrack  = np.zeros(len(audio))
	emptyTrack = generateHodorTrack(emptyTrack, fs, karaokeData, repMTX)

	audio[:,1] = audio[:,1] + emptyTrack
	audio[:,0] = audio[:,0] + emptyTrack

	outputWav = Sndfile(outputFile, 'w', inputAudio.format, inputAudio.channels, inputAudio.samplerate)
	outputWav.write_frames(audio)
	outputWav.close()
Exemple #30
0
def specgram_to_file(path, mags, phasifiers, normalise=True, specgrammode=None):
	if specgrammode==None:
		mags = np.exp(mags)
	if normalise:
		mags -= np.min(mags)
	cplx = mags * phasifiers
	pcm = istft(cplx.T)
	if normalise:
		pcm /= np.max(pcm)
	outsf = Sndfile(path, "w", Format('wav'), 1, fs)
	outsf.write_frames(pcm)
	outsf.close()
Exemple #31
0
def writeAudioOutput(output, fs, f, outputTitle):
    """Writes audio output"""

    outputTitle = "test.wav"
    # Define an output audio format
    formt = Format('wav', 'float64')
    outFile = Sndfile(outputTitle, 'w', formt, 1, fs)
    outFile.write_frames(output)
    #Clean Up
    f.close()
    outFile.close()
    return
def writeAudioOutput(output, fs, f, f2, outputTitle):
  """Writes audio output"""

  # Define an output audio format
  formt = Format('wav', 'float64')
  outFile = Sndfile(outputTitle, 'w', formt, 1, fs)
  outFile.write_frames(output)

  #Clean Up
  f.close()
  f2.close()
  outFile.close()
def analysefile(path, hopsize=0.5, mode='ch', numtop=1, framesize = 1024, chrm_kwargs=None, maxdursecs=None):
	"""Analyses an audio file from disk, dividing into lapped frames and returning an array holding [raw, peaks, slopecent] for each frame.
	Can also do plain FFT-type analysis as an alternative."""
	if (mode != 'ch') and (mode != 'fft'):
		raise ValueError('Mode %s not recognised' % mode)
	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 chirpletringmod: sound file has multiple channels (%i) - mono audio required." % sf.channels)
	#print sf.format
	if maxdursecs!=None:
		maxdurspls = maxdursecs * sf.samplerate
	else:
		maxdurspls = sf.nframes

	if chrm_kwargs != None:
		chrm_kwargs = deepcopy(chrm_kwargs)
		chrm_kwargs['samplerate'] = sf.samplerate
		chrm_kwargs['framesize']  = framesize
	else:
		chrm_kwargs = {'samplerate':sf.samplerate, 'framesize':framesize}

	ch = chirpletringmod.Chirpletringmod(**chrm_kwargs)

	ihop = int(hopsize * ch.framesize)
	unhop = ch.framesize - ihop
	numspecframes = sf.nframes / ihop
	print "File contains %i spectral frames" % numspecframes
	storeraw = numspecframes < 500
	frames = []
	moretocome = True
	data = zeros(ch.framesize, float32)
	while(moretocome):
		try:
			nextdata = sf.read_frames(ihop, dtype=float32)
		except RuntimeError:
			#print "sf.read_frames runtime error, assuming EOF"
			moretocome = False
		if len(nextdata) != ihop:
			print "data truncated, detected EOF"
			moretocome = False
			nextdata = hstack((nextdata, zeros(ihop - len(nextdata))))
		data = hstack(( data[ihop:],  nextdata ))

		frames.append(ch.analyseframeplusfeatures(data, hopsize, mode, numtop, storeraw))

		if len(data) >= maxdurspls:
			break

	sf.close()
	return {'ch':ch, 'frames':frames, 'srate':sf.samplerate, 'hopsize':hopsize, 'framesize':ch.framesize}   # the ch knows srate and framesize, why are we duplicating?
 def test_bigframes(self):
     """ Try to seek really far."""
     rawname = join(TEST_DATA_DIR, 'test.wav')
     a = Sndfile(rawname, 'r')
     try:
         try:
             a.seek(2 ** 60)
             raise Exception, \
                   "Seek really succeded ! This should not happen"
         except IOError, e:
             pass
     finally:
         a.close()
Exemple #35
0
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'
Exemple #36
0
 def test_bigframes(self):
     """ Try to seek really far."""
     rawname = join(TEST_DATA_DIR, 'test.wav')
     a = Sndfile(rawname, 'r')
     try:
         try:
             a.seek(2**60)
             raise Exception, \
                   "Seek really succeded ! This should not happen"
         except IOError, e:
             pass
     finally:
         a.close()
Exemple #37
0
def load_soundfile(inwavpath, startpossecs, maxdursecs=None):
    """Loads audio data, optionally limiting to a specified start position and duration.
    Must be SINGLE-CHANNEL and matching our desired sample-rate."""
    framelen = 4096
    hopspls = framelen
    unhopspls = framelen - hopspls
    if (framelen % wavdownsample) != 0:
        raise ValueError("framelen needs to be a multiple of wavdownsample: %i, %i" % (
            framelen, wavdownsample))
    if (hopspls % wavdownsample) != 0:
        raise ValueError("hopspls  needs to be a multiple of wavdownsample: %i, %i" % (
            hopspls, wavdownsample))
    if maxdursecs == None:
        maxdursecs = 9999
    sf = Sndfile(inwavpath, "r")
    splsread = 0
    framesread = 0
    if sf.channels != 1:
        raise ValueError(
            "Sound file %s has multiple channels (%i) - mono required." % (inwavpath, sf.channels))
    timemax_spls = int(maxdursecs * sf.samplerate)
    if sf.samplerate != (srate * wavdownsample):
        raise ValueError(
            "Sample rate mismatch: we expect %g, file has %g" % (srate, sf.samplerate))
    if startpossecs > 0:
        # note: returns IOError if beyond the end
        sf.seek(startpossecs * sf.samplerate)
    audiodata = np.array([], dtype=np.float32)
    while(True):
        try:
            if splsread == 0:
                chunk = sf.read_frames(framelen)[::wavdownsample]
                splsread += framelen
            else:
                chunk = np.hstack(
                    (chunk[:unhopspls], sf.read_frames(hopspls)[::wavdownsample]))
                splsread += hopspls
            framesread += 1
            if framesread % 25000 == 0:
                print("Read %i frames" % framesread)
            if len(chunk) != (framelen / wavdownsample):
                print("Not read sufficient samples - returning")
                break
            chunk = np.array(chunk, dtype=np.float32)
            audiodata = np.hstack((audiodata, chunk))
            if splsread >= timemax_spls:
                break
        except RuntimeError:
            break
    sf.close()
    return audiodata
def readwavefile(inputwav):
	f = Sndfile(inputwav, 'r')
	fs = f.samplerate
	if fs != 44100 :
		print 'only 44.1kHz filess are supported at present'
		exit(1)
	nc = f.channels
	if nc != 1 :
		print 'only 1 channel supported at present'
		exit(1)
	nframes =  f.nframes
	wav = f.read_frames(nframes, dtype=np.float32)
	f.close()
	return wav
Exemple #39
0
 def read_audio_file(self, file_name):
     #print "read_audio_file: reading file [",file_name,"]"
     if file_name=='': return
     f = Sndfile(unicode(file_name), 'r')
     wav_data = np.array(f.read_frames(f.nframes), dtype=np.float64)
     samplerate = f.samplerate
     f.close()
     nsamples = len(wav_data)
     if (len(wav_data.shape) > 1):    # take left channel of stereo track
         wav_data = wav_data[:,0]
     y = 1.0*wav_data
     x = np.arange(nsamples)*1.0/samplerate   # time values
     #print "read_audio_file: finished with file [",file_name,"]"
     return y, x, samplerate
Exemple #40
0
def convert(filename):
    f = Sndfile(filename, 'r')
    fs = f.samplerate
    nc = f.channels
    enc = f.encoding

    data = f.read_frames(f.nframes)

    new_name = 'newfile.wav'
    format = Format('wav')
    f = Sndfile(new_name, 'w', format, 1, fs)
    f.write_frames(data)
    f.close()

    return new_name
Exemple #41
0
def specgram_to_file(path,
                     mags,
                     phasifiers,
                     normalise=True,
                     specgrammode=None):
    if specgrammode == None:
        mags = np.exp(mags)
    if normalise:
        mags -= np.min(mags)
    cplx = mags * phasifiers
    pcm = istft(cplx.T)
    if normalise:
        pcm /= np.max(pcm)
    outsf = Sndfile(path, "w", Format('wav'), 1, fs)
    outsf.write_frames(pcm)
    outsf.close()
Exemple #42
0
def convert(filename, name):
    f = Sndfile(filename, 'r')
    fs = f.samplerate
    nc = f.channels
    enc = f.encoding

    data = f.read_frames(f.nframes)

    new_name = '/home/bitnami/apps/django/django_projects/Project/sonic_bar_code/static/newfile.wav'
    format = Format('wav')
    # f = Sndfile(new_name, 'w', format, 1, fs)
    f = Sndfile(new_name, 'w', format, nc, fs)
    f.write_frames(data)
    f.close()

    return new_name
Exemple #43
0
def convert(filename, name):
    f = Sndfile(filename, 'r')
    fs = f.samplerate
    nc = f.channels
    enc = f.encoding

    data = f.read_frames(f.nframes)

    new_name ='/home/bitnami/apps/django/django_projects/Project/sonic_bar_code/static/newfile.wav'
    format = Format('wav')
    # f = Sndfile(new_name, 'w', format, 1, fs)
    f = Sndfile(new_name, 'w', format, nc, fs)
    f.write_frames(data)
    f.close()

    return new_name
    def test_float_frames(self):
        """ Check nframes can be a float"""
        rfd, fd, cfilename   = open_tmp_file('pysndfiletest.wav')
        try:
            # Open the file for writing
            format = Format('wav', 'pcm16')
            a = Sndfile(fd, 'rw', format, channels=1, samplerate=22050)
            tmp = np.random.random_integers(-100, 100, 1000)
            tmp = tmp.astype(np.short)
            a.write_frames(tmp)
            a.seek(0)
            a.sync()
            ctmp = a.read_frames(1e2, dtype=np.short)
            a.close()

        finally:
            close_tmp_file(rfd, cfilename)
Exemple #45
0
    def test_float_frames(self):
        """ Check nframes can be a float"""
        rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav')
        try:
            # Open the file for writing
            format = Format('wav', 'pcm16')
            a = Sndfile(fd, 'rw', format, channels=1, samplerate=22050)
            tmp = np.random.random_integers(-100, 100, 1000)
            tmp = tmp.astype(np.short)
            a.write_frames(tmp)
            a.seek(0)
            a.sync()
            ctmp = a.read_frames(1e2, dtype=np.short)
            a.close()

        finally:
            close_tmp_file(rfd, cfilename)
    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:
            self.count = self.count + 1
            print("Reading %s :" % wavpath)
#print self.count
        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 / 2)  #check here
        features = []
        while (True):
            try:
                chunk = sf.read_frames(
                    framelen / 2, dtype=np.float32
                )  #read each window sized value from the audio
                sf.seek(-framelen / 4,
                        1)  #take the current pointer backward for overlap
                if len(chunk) != framelen / 2:
                    print("Not read sufficient samples - returning")
                    break
                if sf.channels != 1:
                    chunk = np.mean(chunk, 1)  # mixdown
                framespectrum = np.fft.fft(
                    window * chunk, framelen / 2
                )  # first the window type is implemented then the padding is done here
                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)
Exemple #47
0
    def __init__(self, filename_or_matrix, samplerate=None):
        if isinstance(filename_or_matrix, basestring):
            filename = filename_or_matrix
            if filename.endswith("mp3"):
                filename = "%s.wav" % filename[:-4]
                pass  # TODO convert to wav and then fall through
            assert filename.endswith("wav")

            f = Sndfile(filename, "r")
            self.samplerate = f.samplerate
            self.matrix = f.read_frames(f.nframes)
            f.close()

        elif isinstance(filename_or_matrix, (np.matrix, np.ndarray)):
            assert samplerate is not None
            self.samplerate = samplerate
            self.matrix = filename_or_matrix

        self.length = self.matrix.shape[0] / float(self.samplerate)
def analyze(filename):
    wave_file = Sndfile(filename, 'r')
    signal = wave_file.read_frames(wave_file.nframes)
    channels = wave_file.channels
    sample_rate = wave_file.samplerate
    header = 'dBFS values are relative to a full-scale square wave'
    
    results = [
    'Properties for "' + filename + '"',
    str(wave_file.format),
    'Channels:\t%d' % channels,
    'Sampling rate:\t%d Hz' % sample_rate,
    'Samples:\t%d' % wave_file.nframes,
    'Length: \t' + str(wave_file.nframes/sample_rate) + ' seconds',
    '-----------------',
    ]
    
    wave_file.close()
    
    if channels == 1:
        # Monaural
        results += properties(signal, sample_rate)
    elif channels == 2:
        # Stereo
        if array_equal(signal[:,0],signal[:,1]):
            results += ['Left and Right channels are identical:']
            results += properties(signal[:,0], sample_rate)
        else:
            results += ['Left channel:']
            results += properties(signal[:,0], sample_rate)
            results += ['Right channel:']
            results += properties(signal[:,1], sample_rate)
    else:
        # Multi-channel
        for ch_no, channel in enumerate(signal.transpose()):
            results += ['Channel %d:' % (ch_no + 1)]
            results += properties(channel, sample_rate)
    
    display(header, results)
    
    plot_histogram = False
    if plot_histogram:
        histogram(signal)
Exemple #49
0
def analyze(filename):
    wave_file = Sndfile(filename, 'r')
    signal = wave_file.read_frames(wave_file.nframes)
    channels = wave_file.channels
    sample_rate = wave_file.samplerate
    header = 'dBFS values are relative to a full-scale square wave'

    results = [
        'Properties for "' + filename + '"',
        str(wave_file.format),
        'Channels:\t%d' % channels,
        'Sampling rate:\t%d Hz' % sample_rate,
        'Samples:\t%d' % wave_file.nframes,
        'Length: \t' + str(wave_file.nframes / sample_rate) + ' seconds',
        '-----------------',
    ]

    wave_file.close()

    if channels == 1:
        # Monaural
        results += properties(signal, sample_rate)
    elif channels == 2:
        # Stereo
        if array_equal(signal[:, 0], signal[:, 1]):
            results += ['Left and Right channels are identical:']
            results += properties(signal[:, 0], sample_rate)
        else:
            results += ['Left channel:']
            results += properties(signal[:, 0], sample_rate)
            results += ['Right channel:']
            results += properties(signal[:, 1], sample_rate)
    else:
        # Multi-channel
        for ch_no, channel in enumerate(signal.transpose()):
            results += ['Channel %d:' % (ch_no + 1)]
            results += properties(channel, sample_rate)

    display(header, results)

    plot_histogram = False
    if plot_histogram:
        histogram(signal)
Exemple #50
0
def convertAndRemoveVoice(inputfile):
    #print strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
    song_folder = os.getcwd() + '/'

    print inputfile[-3:]
    mp3_file = song_folder + inputfile
    wav_file = song_folder + inputfile[:-4] + '.wav'  #'song.wav'
    command = "ffmpeg " + "-i " + '"' + mp3_file + '"' + " -y " + " -ac 2 " + " -ar 44100 " + '"' + wav_file + '"'
    #print '\n'
    print command
    try:
        p = subprocess.Popen(command,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             close_fds=True)
        output = p.communicate()[0]
        #lyricfileess = song_folder + inputfile.replace('.mp3','_beatSynced.json')
        #origfileess = song_folder + inputfile.replace('.mp3','_original.json')
    except:
        print 'wav conversion problem'
        return 0
    original_wav = Sndfile(wav_file, 'r')
    audio = original_wav.read_frames(original_wav.nframes)
    #return audio
    #audio /= float(np.max(abs(audio)))  # normalize audio
    #outputAudio = np.zeros(original_wav.nframes)
    #print type(outputAudio)
    #for idx,frame in enumerate(audio):
    #print idx
    #print frame
    outputAudio = (audio[:, 0] - audio[:, 1]) / 2
    #print len(audio)
    print 2
    new_filename = wav_file.replace('.wav', '_VocalRemoved.wav')
    print new_filename
    output_wav = Sndfile(new_filename, 'w', original_wav.format, 1,
                         original_wav.samplerate)
    output_wav.write_frames(outputAudio)
    output_wav.close()
    #original_wav.close()
    return 1
Exemple #51
0
    def _extract_waveform_data(self,
                               storage_backend,
                               archive_stream,
                               size=1800):
        """Extract waveform data from .wav archive stream.

        Extracts waveform data from stream as normalized numpy array
        suitable for rendering. Values in the array indicate the 
        maximum magnitude of the waveform in a time window and will
        be between 0 and 1.
        
        Args:
            storage_backend: Storage object, accessible on local filesystem,
                where archive_stream can be found.
            archive_stream: ArchiveStream object containing a .wav stream for
                which to extract the waveform data.
            size: size of the array to return
        Returns:
            numpy array of normalized max amplitude waveform data
        Raises:
            StorageException
        """
        self.log.info("Extracting waveform data from %s" % archive_stream)

        sound_file = Sndfile(storage_backend.path(archive_stream.filename),
                             'r')
        frames = sound_file.read_frames(sound_file.nframes, dtype=np.float64)
        if sound_file.channels == 2:
            frames = frames[::2]

        frames_per_pixel = len(frames) / size

        data = []
        for x in range(0, size):
            f = frames[x * frames_per_pixel:(x + 1) * frames_per_pixel]
            value = np.abs(f).max()
            data.append(value)

        sound_file.close()

        return np.array(data)
def decompose(inpath, outdir='output', niters=3, framesize=1024, hopsize=0.5, writefiles=True, wintype='hann'):
	"""Given a path to an input file, runs a pursuit iteration to decompose the signal into atoms.
	Writes out quite a lot of files - for each iteration, partial resynth, total resynth, residual.
	Also returns the aggregated peaks and the residual."""
	if not os.path.isfile(inpath):
		raise ValueError("path %s not found" % inpath)
	sf = Sndfile(inpath, "r")
	if sf.channels != 1:
		raise Error("ERROR in chirpletringmod: sound file has multiple channels (%i) - mono audio required." % sf.channels)

	ch = chirpletringmod.Chirpletringmod(samplerate=sf.samplerate, framesize=framesize, wintype=wintype)
	signal = sf.read_frames(sf.nframes, dtype=float32)
	sf.close()

	outnamestem = "%s/%s" % (outdir, os.path.splitext(os.path.basename(inpath))[0])

	resynthtot = zeros(len(signal))
	aggpeaks = []
	residual = signal
	print("chf.decompose: original signal energy %g" % sum(signal ** 2))
	for whichiter in range(niters):
		print("----------------------------------------")
		print("iteration %i" % whichiter)

		iterdata = ch.decompose_oneiter(residual, hopsize=hopsize)
		"""Given an input signal, decomposes it a bit like one round of matching-pursuit or suchlike, with the added constraint of
		one detection per frame. Returns the peaks found, the resynthesised version, and the residual."""
		#return {'peaks':framespeaks, 'resynth':resynth, 'residual':residual}

		resynthtot += iterdata['resynth']
		aggpeaks.extend(iterdata['peaks'])

		if writefiles:
			sf = Sndfile("%s_%i_resynth.wav" % (outnamestem, whichiter), "w", Format(), 1, ch.sr)
			sf.write_frames(iterdata['resynth'])
			sf.close()

			sf = Sndfile("%s_%i_resynthtot.wav" % (outnamestem, whichiter), "w", Format(), 1, ch.sr)
			sf.write_frames(resynthtot)
			sf.close()

			sf = Sndfile("%s_%i_residual.wav" % (outnamestem, whichiter), "w", Format(), 1, ch.sr)
			sf.write_frames(iterdata['residual'])
			sf.close()
		residual = iterdata['residual'] # fodder for next iter

		print("resynth    signal energy %g" % sum(iterdata['resynth'] ** 2))
		print("resynthtot signal energy %g" % sum(resynthtot ** 2))
		print("residual   signal energy %g" % sum(residual   ** 2))

	return {'ch':ch, 'peaks':aggpeaks, 'residual':residual}
Exemple #53
0
def transform_wav(wav_file, output):
    filename = os.path.basename(wav_file)
    pathname = os.path.dirname(wav_file)
    speaker = os.path.basename(pathname)
    dr = os.path.basename(os.path.dirname(pathname))
    output_dir = os.path.join(output, dr, speaker)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    output_file = os.path.join(output_dir, filename)

    f = Sndfile(wav_file, 'r')
    data = f.read_frames(f.nframes)
    fs = f.samplerate
    nc = f.channels
    enc = f.encoding
    
    wav_format = Format('wav')
    f_out = Sndfile(output_file, 'w', wav_format, nc, fs)
    f_out.write_frames(data)
    f.close()
    f_out.close()
Exemple #54
0
    def _test_write(self, func, format, filext):
        """ Check *write functions from matpi """
        rfd1, fd1, cfilename1 = open_tmp_file('matapi_test.' + filext)
        rfd2, fd2, cfilename2 = open_tmp_file('matapi_test.' + filext)
        try:
            nbuff = 22050
            fs = nbuff
            noise = 0.1 * N.random.randn(nbuff)

            # Open the first file for writing with Sndfile
            b = Sndfile(cfilename1, 'w', format, 1, fs)

            b.write_frames(noise)

            b.close()

            # Write same data with wavwrite
            func(noise, cfilename2, fs)

            # Compare if both files have both same audio data and same
            # meta-data
            f1 = Sndfile(cfilename1)
            f2 = Sndfile(cfilename2)

            assert_array_equal(f1.read_frames(f1.nframes),
                               f2.read_frames(f2.nframes))
            assert_equal(f1.format, f2.format)
            assert_equal(f1.samplerate, f2.samplerate)
            assert_equal(f1.channels, f2.channels)
            f1.close()
            f2.close()
        finally:
            close_tmp_file(rfd1, cfilename1)
            close_tmp_file(rfd2, cfilename2)
Exemple #55
0
def get_volume_points(sound_filename, fps, inertia = 0.03):
	"""Reads an audio file and generates a list of float values corresponding
	to the volume assosciated with each keyframe."""
	f = Sndfile(sound_filename, 'r')
	k = 1.0 - 1.0 / (inertia * f.samplerate)
	vol = 0.0
	framepos = 0L
	divisor = f.samplerate / fps # should be integer
	points = []
	while framepos < f.nframes:
		read_len = (
			VOL_BUFFSIZE if (framepos + VOL_BUFFSIZE < f.nframes)
			else f.nframes - framepos)
		frames = f.read_frames(read_len)
		for frame in frames:
			vol *= k
			# is frame iterable or just one chan?
			if getattr(frame, '__iter__', False):
				loudest = max(abs(chan) for chan in frame)
			else:
				loudest = abs(frame)
			if loudest > vol:
				vol = loudest
			if framepos % divisor == 0:
				points.append(vol)
			framepos += 1
	f.close()
	# maximise
	min_val = min(points)
	max_val = max(points)
	if min_val < max_val:
		points = [(val - min_val) / (max_val - min_val) for val in points]
	else:
		points = [0.0 for _ in points]
	# get log values (dB)
	#C1 = 0.0
	#C2 = 20.0 / math.log(10.0)
	#small = 0.4
	#points = [C1 + C2 * math.log(val + small) for val in points]
	return points
    def _test_write(self, func, format, filext):
        """ Check *write functions from matpi """
        rfd1, fd1, cfilename1  = open_tmp_file('matapi_test.' + filext)
        rfd2, fd2, cfilename2  = open_tmp_file('matapi_test.' + filext)
        try:
            nbuff = 22050
            fs = nbuff
            noise = 0.1 * N.random.randn(nbuff)

            # Open the first file for writing with Sndfile
            b = Sndfile(cfilename1, 'w', format, 1, fs)

            b.write_frames(noise)

            b.close()

            # Write same data with wavwrite
            func(noise, cfilename2, fs)

            # Compare if both files have both same audio data and same
            # meta-data
            f1  = Sndfile(cfilename1)
            f2  = Sndfile(cfilename2)

            assert_array_equal(f1.read_frames(f1.nframes), f2.read_frames(f2.nframes))
            assert_equal(f1.format, f2.format)
            assert_equal(f1.samplerate, f2.samplerate)
            assert_equal(f1.channels, f2.channels)
            f1.close()
            f2.close()
        finally:
            close_tmp_file(rfd1, cfilename1)
            close_tmp_file(rfd2, cfilename2)
Exemple #57
0
    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()

            # Reread the data
            b = Sndfile(cfilename, 'r')
            rcnoise = b.read_frames(nbuff)
            b.close()

            rnoise = func(cfilename)[0]

            assert_array_equal(rnoise, rcnoise)
        finally:
            close_tmp_file(rfd, cfilename)
Exemple #58
0
def audiowrite(data, sr, filename):
    """
    Write audio data to a file.  Infer type from name.
    """
    stem, ext = os.path.splitext(filename)
    fmt = ext[1:]
    if fmt == "sph":
        fmt = "nist"
    format = Format(fmt)
    if len(np.shape(data)) > 1:
        nchans = np.size(data, axis=0)
    else:
        nchans = 1
    f = Sndfile(filename, 'w', format, nchans, sr)
    if np.max(np.abs(data)) >= 0.999:
        clippos = data >= 0.999
        data[clippos] = 0.999
        clipneg = data <= -0.999
        data[clipneg] = -0.999
        print "audiowrite: WARNING: %d samples clipped" % np.sum(
            np.r_[clippos, clipneg])
    f.write_frames(data)
    f.close()
Exemple #59
0
    def _test_read_write(self, dtype):
        # dirty !
        ofilename = join(TEST_DATA_DIR, 'test.wav')
        rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav')
        try:
            nbuff = 22050

            # Open the test file for reading
            a = Sndfile(ofilename, 'r')
            nframes = a.nframes

            # Open the copy file for writing
            format = Format('wav', _DTYPE_TO_ENC[dtype])
            b = Sndfile(fd, 'w', format, a.channels, a.samplerate)

            # Copy the data in the wav file
            for i in range(nframes / nbuff):
                tmpa = a.read_frames(nbuff, dtype=dtype)
                assert tmpa.dtype == dtype
                b.write_frames(tmpa)
            nrem = nframes % nbuff
            tmpa = a.read_frames(nrem)
            b.write_frames(tmpa)

            a.close()
            b.close()

            # Now, reopen both files in for reading, and check data are
            # the same
            a = Sndfile(ofilename, 'r')
            b = Sndfile(cfilename, 'r')
            for i in range(nframes / nbuff):
                tmpa = a.read_frames(nbuff, dtype=dtype)
                tmpb = b.read_frames(nbuff, dtype=dtype)
                assert_array_equal(tmpa, tmpb)

            a.close()
            b.close()

        finally:
            close_tmp_file(rfd, cfilename)