Exemple #1
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)
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)
Exemple #3
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 #4
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)
Exemple #5
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 #6
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 #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
    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 #9
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)
    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])
    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 #12
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
    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 #14
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 #15
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 #17
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}")
Exemple #18
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 #19
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 #20
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 #21
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()
def save_record():
    global data
    n_channels, fmt = 1, Format('flac', 'pcm16')
    caller_id = agi.get_variable("CALLERID(num)")
    file_name = 'TmpSpeechFile_' + caller_id + '.flac'
    _, temp_sound_file = mkstemp(file_name)
    flac_file = Sndfile(temp_sound_file, 'w', fmt, n_channels, SAMPLE_RATE)

    flac_file.write_frames(data)
    flac_audio = AudioSegment.from_file(temp_sound_file, "flac")
    flac_audio.export(PATH + FILE_NAME, format="mp3")
Exemple #23
0
def create_flac_from(sound_samples):
    __console.log('prepare flac format for writing to file')
    n_channels, fmt     = 1, Format('flac', 'pcm16')
    caller_id           = get_stdn_var(stdin.CALLER_ID)
    __console.log('write to temp file')
    _, temp_sound_file  = mkstemp('TmpSpeechFile_' + caller_id + '.flac')
    __console.log('prepare sound file')
    flac_file           = Sndfile(temp_sound_file, 'w', fmt, n_channels, constants.RAW_RATE)

    flac_file.write_frames(np.array(sound_samples))
    __console.log('sound file saved')
    return temp_sound_file
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()
Exemple #25
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
Exemple #26
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 #27
0
def extractOnsets(audio):
        od1 = OnsetDetection(method = 'hfc')
        od2 = OnsetDetection(method = 'complex')

        # let's also get the other algorithms we will need, and a pool to store the results

        w = Windowing(type = 'hann')
        fft = FFT() # this gives us a complex FFT
        c2p = CartesianToPolar() # and this turns it into a pair (magnitude, phase)

        pool = essentia.Pool()

        # let's get down to business
        for frame in FrameGenerator(audio, frameSize = 1024, hopSize = 512):
                mag, phase, = c2p(fft(w(frame)))
                pool.add('features.hfc', od1(mag, phase))
                pool.add('features.complex', od2(mag, phase))


        # Phase 2: compute the actual onsets locations
        onsets = Onsets()

        onsets_hfc = onsets(# this algo expects a matrix, not a vector
                array([ pool['features.hfc'] ]),

                # you need to specify weights, but as there is only a single
                # function, it doesn't actually matter which weight you give it
                [ 1 ])
#        np.savetxt(outFile, onsets_hfc, fmt='%f')

        #Let's just take the complex as an example
        onsets_complex = onsets(array([ pool['features.complex'] ]), [ 1 ])

        startTimes = onsets_hfc
        endTimes = onsets_hfc[1:]
        duration = Duration()
        endTimes = np.append(endTimes, duration(audio))

        slicer = Slicer(startTimes = array(startTimes), endTimes = array(endTimes))
        
        frames = slicer(audio)        

        lengthInFrames = 0
        for i in range(len(frames)):
                lengthInFrames = lengthInFrames + len(frames[i])

        format = Format('wav')
        global counter
        f = Sndfile('out'+ str(counter) + '.wav' , 'w', format, 1, 44100)
        counter = counter + 1
        f.write_frames(np.asarray(frames[0]))

        return frames
Exemple #28
0
def wav_to_flac(wav_name):
  cd, tmp_name = mkstemp('tmp.flac')

  Signal, fs = wavread(wav_name)[:2]
  assert(fs == RATE)

  fmt = Format('flac', 'pcm16')
  nchannels = 1
  flac_file = Sndfile(tmp_name, 'w', fmt, nchannels, RATE)
  flac_file.write_frames(Signal)

  return tmp_name
Exemple #29
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 #30
0
def main(RAW_DATA_DIR, chunk_length, slide_length):
	OUTPUT_DIR=os.path.join(RAW_DATA_DIR, "parts")
	
	if os.path.isdir(OUTPUT_DIR):
		answer = raw_input("Parts already exist.\nType y to rebuild from scratch : ")
		if answer != 'y':
			print("NOT REBUILT !")
			sys.exit()
		else:
			shutil.rmtree(OUTPUT_DIR)
	os.makedirs(OUTPUT_DIR)
	
	print RAW_DATA_DIR
	print OUTPUT_DIR

	chunk_counter = 0
	format = Format('wav')

	wav_files = glob.glob(RAW_DATA_DIR + '/*.wav')

	for filename in wav_files:
		wav_file_path = filename
		path_no_extension = re.sub(ur'\.wav$', '', filename)
		# Read wav and csv files
		wave_info = scikits.audiolab.wavread(wav_file_path)
		data_wave = wave_info[0]
		samplerate = wave_info[1]
		len_wave = len(data_wave)

		# Cut them in chunk_length seconds chunks.
		# Sliding of slide_length seconds
		# Zero padding at the end
		time_counter = 0
		start_index = 0
		while(start_index < len_wave):
			end_index = (time_counter + chunk_length) * samplerate
			if end_index > len_wave:
				chunk_wave = np.concatenate((data_wave[start_index:], np.zeros((end_index-len_wave))))
			else:
				chunk_wave = data_wave[start_index:end_index]

			time_counter += slide_length
			start_index = time_counter * samplerate

			# Write the chunks
			outwave_name = OUTPUT_DIR + '/p' + str(chunk_counter) + '.wav'
			f = Sndfile(outwave_name, 'w', format, 1, samplerate)  # 1 stands for the number of channels
			f.write_frames(chunk_wave)

			chunk_counter += 1
	return
   def _createDiff(self, expected, result):
       diff = Sndfile(self.diffPath,  mode='w',  format=result.format,  channels=result.channels,  samplerate=result.samplerate)
 
       index = 0
       frameSize = 1024
       while index < result.nframes:
               frameSize = min(result.nframes - index ,frameSize)
               resultFrame = result.read_frames(frameSize)
               expectedFrame = expected.read_frames(frameSize)
               diffFrame = expectedFrame - resultFrame
               diff.write_frames(diffFrame)
               if not allclose(resultFrame, expectedFrame):
                       return
               index += frameSize
    def test_rw(self):
        """Test read/write pointers for seek."""
        ofilename = join(TEST_DATA_DIR, 'test.wav')
        rfd, fd, cfilename   = open_tmp_file('rwseektest.wav')
        try:
            ref = Sndfile(ofilename, 'r')
            test = Sndfile(fd, 'rw', format=ref.format,
                           channels=ref.channels, samplerate=ref.samplerate)
            n = 1024

            rbuff = ref.read_frames(n, dtype = np.int16)
            test.write_frames(rbuff)
            tbuff = test.read_frames(n, dtype = np.int16)

            assert_array_equal(rbuff, tbuff)

            # Test seeking both read and write pointers
            test.seek(0, 0)
            test.write_frames(rbuff)
            tbuff = test.read_frames(n, dtype = np.int16)
            assert_array_equal(rbuff, tbuff)

            # Test seeking only read pointer
            rbuff1 = rbuff.copy()
            rbuff2 = rbuff1 * 2 + 1
            rbuff2.clip(-30000, 30000)
            test.seek(0, 0, 'r')
            test.write_frames(rbuff2)
            tbuff1 = test.read_frames(n, dtype = np.int16)
            try:
                tbuff2 = test.read_frames(n, dtype = np.int16)
            except IOError, e:
                msg = "write pointer was updated in read seek !"
                msg += "\n(msg is %s)" % e
                raise AssertionError(msg)

            assert_array_equal(rbuff1, tbuff1)
            assert_array_equal(rbuff2, tbuff2)
            if np.all(rbuff2 == tbuff1):
                raise AssertionError("write pointer was updated"\
                        " in read seek !")

            # Test seeking only write pointer
            rbuff3 = rbuff1 * 2 - 1
            rbuff3.clip(-30000, 30000)
            test.seek(0, 0, 'rw')
            test.seek(n, 0, 'w')
            test.write_frames(rbuff3)
            tbuff1 = test.read_frames(n, np.int16)
            try:
                assert_array_equal(tbuff1, rbuff1)
            except AssertionError:
                raise AssertionError("read pointer was updated in write seek !")

            try:
                tbuff3 = test.read_frames(n, np.int16)
            except IOError, e:
                msg = "read pointer was updated in write seek !"
                msg += "\n(msg is %s)" % e
                raise AssertionError(msg)
Exemple #33
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 #34
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 #35
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 #36
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 #38
0
def export_audio_vidId(fn, samplerate, vid, time, cols):
    f = [interp1d(time, cols[:, i]) for i in range(cols.shape[1])]
    fmt = Format(type=fn.split('.')[-1])

    newfn = '.'.join(
        fn.split('.')[:-2] + [fn.split('.')[-2] + ',%d' % vid] +
        [fn.split('.')[-1]])

    print 'Writing', newfn
    wav = Sndfile(newfn, 'w', fmt, cols.shape[1], samplerate)
    a = arange(samplerate) / samplerate + time[0]
    for i in xrange(int((time[-1] - time[0]))):
        y = array([g(a + i) for g in f]).T
        wav.write_frames(y)
        print '%0.2f%%     \r' % (i / (time[-1] - time[0]) * 100),
        sys.stdout.flush()
    print '100%     '
Exemple #39
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)
Exemple #40
0
def export_audio_vidId(fn, samplerate, vid, time, cols):
    f = [interp1d(time, cols[:,i]) for i in range(cols.shape[1])]
    fmt = Format(type=fn.split('.')[-1])

    newfn = '.'.join(fn.split('.')[:-2]
                     + [fn.split('.')[-2]+',%d'%vid]
                     + [fn.split('.')[-1]])

    print 'Writing',newfn
    wav = Sndfile(newfn, 'w', fmt, cols.shape[1], samplerate)
    a = arange(samplerate)/samplerate + time[0]
    for i in xrange(int((time[-1]-time[0]))):
        y = array([g(a+i) for g in f]).T
        wav.write_frames(y)
        print '%0.2f%%     \r'%(i / (time[-1]-time[0]) * 100),
        sys.stdout.flush()
    print '100%     '
Exemple #41
0
class TelegraphWriter(Telegraph):
    """Write Morse Code to an Audio File. TelegraphWriter uses SndFile from the scikits.audiolab package to write out
    audio data,  and as such TelegraphWriter can output to whatever formats are available. See the
    :func:`available_file_formats` and :func:`available_encodings` to determine what audio outputs are available.

    :param filename: (str) Filename to output to
    :param audio_format: (str) Audio format
    :param audio_encoding: (str) Encoding of audio_format
    :param alphabet: (str) Morse Code alphabet to be used
    :raise InvalidFormatEncoding:

    The default format and encoding is ogg vorbis (http://www.vorbis.com). This produces good quality compressed
    files, but is slower that (say) WAV 16pcm
    """

    def __init__(self, filename, audio_format="ogg", audio_encoding="vorbis", alphabet="international"):
        super(TelegraphWriter, self).__init__(alphabet)
        self.__filename = filename        
        self.__audio_format = audio_format
        self.__audio_encoding = audio_encoding

        self.__output_formats = available_file_formats()

        from scikits.audiolab import Format, Sndfile          
        if self.__audio_format not in available_file_formats() or self.__audio_encoding not in available_encodings(self.__audio_format):
            raise InvalidFormatEncoding(self.__audio_format, self.__audio_encoding)
        output_format = Format(self.__audio_format, self.__audio_encoding)
        self.__output_file = Sndfile(self.__filename, 'w', output_format, 1, 44100)

    def __write_character(self, character):
        """Write a character to the output file. 

        :param character: (str) Character to be written
        :raise TypeError: If a single character is not passed
        :raise CharacterNotFound: if ignore_unknown is set to False and a character is not found
        """
        self.__output_file.write_frames(character)

    def encode(self, message):
        """Write a message to the output file. 

        :param message: (str) Message to be written
        :raise CharacterNotFound: if ignore_unknown is set to False and a character is not found
        """
        super(TelegraphWriter, self)._encode_message(self._clean_message(message), self.__write_character)
        self.__output_file.sync()
Exemple #42
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
    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)
Exemple #44
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)
Exemple #45
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 #46
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 #47
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 #48
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)
    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 #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
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}
    tone[i][1] = np.sin(2*np.pi*(freq*2)*phaseVal)*amp


#################################################
########## WRITING TONES TO AUDIO FILE ##########
#################################################

# create a name for the new file
new_filename = 'tone.wav'

# Create a Sndfile instance for writing wav files @ 44100 Hz
format = Format('wav')
f = Sndfile(new_filename, 'w', format, 2, fs)

# Write out the samples to the file
f.write_frames(tone)

# close the audio file
f.close()


#################################################
############### PLOT USING PYLAB ################
#################################################   

toneL = tone[:,0]
toneR = tone[:,1]

plt.subplot(211)
plt.plot(toneL[0:200])
plt.subplot(212)
Exemple #53
0
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 #54
0
 def SaveSoundObject(waveFileName, signals, numChannels, sampleRate):
     outputFile = Sndfile(waveFileName, 'w', AudioEngine_IO.outFormat, numChannels, sampleRate)
     outputFile.write_frames(signals)
     outputFile.close()
Exemple #55
0
    def export(self, **kwargs):
        """
        Generate audio file from composition.

        :param str. filename: Output filename (no extension)
        :param str. filetype: Output file type (only .wav supported for now)
        :param integer samplerate: Sample rate of output audio
        :param integer channels: Channels in output audio, if different than originally specified
        :param bool. separate_tracks: Also generate audio file for each track in composition
        :param int min_length: Minimum length of output array (in frames). Will zero pad extra length.
        :param bool. adjust_dynamics: Automatically adjust dynamics (will document later)

        """
        # get optional args
        filename = kwargs.pop('filename', 'out')
        filetype = kwargs.pop('filetype', 'wav')
        adjust_dynamics = kwargs.pop('adjust_dynamics', False)
        samplerate = kwargs.pop('samplerate', None)
        channels = kwargs.pop('channels', self.channels)
        separate_tracks = kwargs.pop('separate_tracks', False)
        min_length = kwargs.pop('min_length', None)
        
        if samplerate is None:
            samplerate = N.min([track.samplerate for track in self.tracks])

        encoding = 'pcm16'
        if filetype == 'ogg':
            encoding = 'vorbis'
        
        if separate_tracks:
            # build the separate parts of the composition if desired
            for track in self.tracks:
                out = self.build(track=[track],
                                 adjust_dynamics=adjust_dynamics,
                                 min_length=min_length,
                                 channels=channels)
                out_file = Sndfile("%s-%s.%s" %
                                   (filename, track.name, filetype),
                                   'w',
                                   Format(filetype, encoding=encoding),
                                   channels, samplerate)
                out_file.write_frames(out)
                out_file.close()

        # always build the complete composition
        out = self.build(adjust_dynamics=adjust_dynamics,
                         min_length=min_length,
                         channels=channels)

        out_filename = "%s.%s" % (filename, filetype)
        out_file = Sndfile(out_filename, 'w',
                           Format(filetype, encoding=encoding), 
                           channels, samplerate)
        out_file.write_frames(out)
        out_file.close()

        if LIBXMP and filetype == "wav":
            xmp = libxmp.XMPMeta()
            ns = libxmp.consts.XMP_NS_DM
            p = xmp.get_prefix_for_namespace(ns)
            xpath = p + 'Tracks'
            xmp.append_array_item(ns, xpath, None, 
                array_options={"prop_value_is_array": True},
                prop_value_is_struct=True)

            xpath += '[1]/' + p
            xmp.set_property(ns, xpath + "trackName", "CuePoint Markers")
            xmp.set_property(ns, xpath + "trackType", "Cue")
            xmp.set_property(ns, xpath + "frameRate", "f%d" % samplerate)

            for i, lab in enumerate(self.labels):
                xmp.append_array_item(ns, xpath + "markers", None,
                    array_options={"prop_value_is_array": True},
                    prop_value_is_struct=True)
                xmp.set_property(ns,
                    xpath + "markers[%d]/%sname" % (i + 1, p), lab.name)
                xmp.set_property(ns,
                    xpath + "markers[%d]/%sstartTime" % (i + 1, p),
                    str(lab.sample(samplerate))) 

            xmpfile = libxmp.XMPFiles(file_path=out_filename, open_forupdate=True)
            if xmpfile.can_put_xmp(xmp):
                xmpfile.put_xmp(xmp)
            xmpfile.close_file()


        return out
Exemple #56
0
def writeresynthaudio(ch, frames, outpath, hopsize=0.5):
    "Actually resynthesises the audio and writes that out"
    audiodata = ch.resynth([frame['peaks'] for frame in frames], hopsize)
    sf = Sndfile(outpath, "w", Format(), 1, ch.sr)
    sf.write_frames(audiodata)
    sf.close()
Exemple #57
0
    def test_rw(self):
        """Test read/write pointers for seek."""
        ofilename = join(TEST_DATA_DIR, 'test.wav')
        rfd, fd, cfilename = open_tmp_file('rwseektest.wav')
        try:
            ref = Sndfile(ofilename, 'r')
            test = Sndfile(fd,
                           'rw',
                           format=ref.format,
                           channels=ref.channels,
                           samplerate=ref.samplerate)
            n = 1024

            rbuff = ref.read_frames(n, dtype=np.int16)
            test.write_frames(rbuff)
            tbuff = test.read_frames(n, dtype=np.int16)

            assert_array_equal(rbuff, tbuff)

            # Test seeking both read and write pointers
            test.seek(0, 0)
            test.write_frames(rbuff)
            tbuff = test.read_frames(n, dtype=np.int16)
            assert_array_equal(rbuff, tbuff)

            # Test seeking only read pointer
            rbuff1 = rbuff.copy()
            rbuff2 = rbuff1 * 2 + 1
            rbuff2.clip(-30000, 30000)
            test.seek(0, 0, 'r')
            test.write_frames(rbuff2)
            tbuff1 = test.read_frames(n, dtype=np.int16)
            try:
                tbuff2 = test.read_frames(n, dtype=np.int16)
            except IOError, e:
                msg = "write pointer was updated in read seek !"
                msg += "\n(msg is %s)" % e
                raise AssertionError(msg)

            assert_array_equal(rbuff1, tbuff1)
            assert_array_equal(rbuff2, tbuff2)
            if np.all(rbuff2 == tbuff1):
                raise AssertionError("write pointer was updated"\
                        " in read seek !")

            # Test seeking only write pointer
            rbuff3 = rbuff1 * 2 - 1
            rbuff3.clip(-30000, 30000)
            test.seek(0, 0, 'rw')
            test.seek(n, 0, 'w')
            test.write_frames(rbuff3)
            tbuff1 = test.read_frames(n, np.int16)
            try:
                assert_array_equal(tbuff1, rbuff1)
            except AssertionError:
                raise AssertionError(
                    "read pointer was updated in write seek !")

            try:
                tbuff3 = test.read_frames(n, np.int16)
            except IOError, e:
                msg = "read pointer was updated in write seek !"
                msg += "\n(msg is %s)" % e
                raise AssertionError(msg)
Exemple #58
0
    tone[i][0] = np.sin(2 * np.pi * freq * phaseVal) * amp
    tone[i][1] = np.sin(2 * np.pi * (freq * 2) * phaseVal) * amp

#################################################
########## WRITING TONES TO AUDIO FILE ##########
#################################################

# create a name for the new file
new_filename = 'tone.wav'

# Create a Sndfile instance for writing wav files @ 44100 Hz
format = Format('wav')
f = Sndfile(new_filename, 'w', format, 2, fs)

# Write out the samples to the file
f.write_frames(tone)

# close the audio file
f.close()

#################################################
############### PLOT USING PYLAB ################
#################################################

toneL = tone[:, 0]
toneR = tone[:, 1]

plt.subplot(211)
plt.plot(toneL[0:200])
plt.subplot(212)
plt.plot(toneR[0:200])
Exemple #59
0
class Stream(object):
    """
    Interface for streaming audio files through libsndfile
    """
    def __init__(self,
                 filename,
                 write=False,
                 format='wav',
                 rate=None,
                 channels=None):
        """
        Open audiofile for writing or reading

        Parameters
        ----------
        filename : mixed
            Input wav file. String if a real file, `sys.stdin` for
            standard in.
        write: boolean
            Set true for writing to a file
        rate : int
            Sample rate. Only required for writing
        channels : int
            Number of Channels. Only required for writing

        Notes
        -----

        * The data is assumed to be a numpy array of
          floats, normalized between -1 and 1.

        """
        try:
            from scikits.audiolab import Format, Sndfile
        except:
            raise RuntimeError('You must have scikits.audiolab installed')

        if filename is sys.stdin:
            filename = '-'

        if write is True and (rate is None or channels is None):
            raise ValueError('You must provide sampling rate and '
                             'number of channels for writing file.')

        if write is False:
            self.f = Sndfile(filename, 'r')

            self.channels = self.f.channels
            self.rate = self.f.samplerate

        else:
            format = Format(format)
            self.f = Sndfile(filename, 'w', format, channels, rate)

            self.channels = channels
            self.rate = rate

    def write(self, data):
        """
        Write data to file

        Parameters
        ----------
        data : mixed
            Input data. Numpy array for single data chunks.
            Generator for automated writing.

        """
        import types

        if isinstance(data, types.GeneratorType):
            for i in data:
                self.f.write_frames(i)

            self.close()

        else:
            self.f.write_frames(data)

    def read(self, framesize=1024):
        """
        Write data to file

        Parameters
        ----------
        framesize : int
            Number of samples to be read per frame.

        Returns
        -------
        data : Generator
            Generator of numpy arrays that can be iterated over.

        """
        while True:
            try:
                yield self.f.read_frames(framesize)
            except RuntimeError:
                self.close()
                raise StopIteration

    def close(self):
        self.f.close()
Exemple #60
0
def save_wav(fname, signal, rate):
    fp = Sndfile(fname, 'w', Format('wav'), signal.shape[1], rate)
    fp.write_frames(signal)
    fp.close()