Example #1
0
    def pack1(self):
	if self.width1==1: 
		fmt="%iB" % self.frames1*self.channel1 
	else: 
		fmt="%ih" % self.frames1*self.channel1

	out=struct.pack(fmt,*(self.new))
	
	if self.pl1==0 or self.pl1==3:
		out_file=wave.open("ampli.wav",'w')
	elif self.pl1==1:
		out_file=wave.open("wave_mix1.wav","w")
	elif self.pl1==2:
		out_file=wave.open("wave_mod1.wav",'w')
	out_file.setframerate(self.rate1) 
	out_file.setnframes(self.frames1) 
	out_file.setsampwidth(self.width1) 
	out_file.setnchannels(self.channel1) 
	out_file.writeframes(out) 

	out_file.close()
	if self.pl1==0:
		self.read_new("ampli.wav",0)
	elif self.pl1==1:	
		self.read_new("wave_mix1.wav",4)
		self.pl1=0
	elif self.pl1==2:
		self.read_new("wave_mod1.wav",3)
		self.pl1=0
	else:
		self.pl1=0
Example #2
0
def mix_files(a, b, c, chann = 2, phase = -1.):
	f1 = wave.open(a,'r')
	f2 = wave.open(b,'r')
	f3 = wave.open(c,'w')
	f3.setnchannels(chann)
	f3.setsampwidth(2)
	f3.setframerate(44100)
	f3.setcomptype('NONE','Not Compressed')
	frames = min(f1.getnframes(), f2.getnframes())

	print "Mixing files, total length %.2f s..." % (frames / 44100.)
	d1 = f1.readframes(frames)
	d2 = f2.readframes(frames)
	for n in range(frames):
		if not n%(5*44100): print n // 44100, 's'
		if chann < 2:
			d3 = struct.pack('h',
				.5 * (struct.unpack('h', d1[2*n:2*n+2])[0] +
				struct.unpack('h', d2[2*n:2*n+2])[0]))
		else:
			d3 = ( struct.pack('h',
				phase * .3 * struct.unpack('h', d1[2*n:2*n+2])[0] +
				.7 * struct.unpack('h', d2[2*n:2*n+2])[0]) +
				struct.pack('h',
				.7 * struct.unpack('h', d1[2*n:2*n+2])[0] +
				phase * .3 * struct.unpack('h', d2[2*n:2*n+2])[0]) )
		f3.writeframesraw(d3)
	f3.close()
Example #3
0
def open_wave(path=None, rw="r"):
    global _wavefd
    if _wavefd is None:
        try:
            if path is None:
                path = "~/radar" + time.strftime("%Y%m%d_%H%M%S") + ".wav"
            path = os.path.expanduser(path)
            if rw == "w":
                f = open(path, "w")  # make the file
                f.close()
                _wavefd = wave.open(path, rw)
                _wavefd.setnchannels(2)
                _wavefd.setsampwidth(2)
                _wavefd.setframerate(SAMPLING_RATE)
            elif rw == "r":
                _wavefd = wave.open(path, rw)
                try:
                    assert _wavefd.getnchannels() == 2
                    assert _wavefd.getsampwidth() == 2
                except AssertionError:  # SAMPLING_RATE = 48000
                    print "wave file format is not valid"
                    print "    number channels:%d, sample width:%d" % (_wavefd.getnchannels(), _wavefd.getsampwidth())
                    raise
                if int(_wavefd.getframerate()) != int(SAMPLING_RATE):
                    print "warning, the sampling rate of the file diagrees with SAMPLING_RATE"
                    print "    file: %d, SAMPLING_RATE: %d" % (int(_wavefd.getframerate()), int(SAMPLING_RATE))
            else:
                raise ValueError
        except:
            print "could not open file"
            raise  # rethrow the exception

    else:
        raise AssertionError, "a record file is already opened"
Example #4
0
    def pack2(self):
	if self.width2==1: 
		fmt="%iB" % self.frames2*self.channel2 
	else: 
		fmt="%ih" % self.frames2*self.channel2

	out=struct.pack(fmt,*(self.new2))
	if self.pl2==0 or self.pl2==3:
		out_file=wave.open("ampli2.wav",'w')
	elif self.pl2==1:
		out_file=wave.open("wave_mix2.wav",'w')
	elif self.pl2==2:
		out_file=wave.open("wave_mod2.wav",'w')

	out_file.setframerate(self.rate2) 
	out_file.setnframes(self.frames2) 
	out_file.setsampwidth(self.width2) 
	out_file.setnchannels(self.channel2) 
	out_file.writeframes(out) 

	out_file.close()
	if self.pl2==0:
		self.read_new("ampli2.wav",1)
	elif self.pl2==1:
		self.read_new("wave_mix2.wav",4)
		self.pl2=0
	elif self.pl2==2:
		self.read_new("wave_mod2.wav",3)
		self.pl2=0
	else:
		self.pl2=0
Example #5
0
def noisered(fname):
	wr = wave.open(fname, 'r')
	par = list(wr.getparams()) 
	par[3] = 0
	ww = wave.open('filtered-talk.wav', 'w')
	ww.setparams(tuple(par)) 
	
	lowpass = 200 
	highpass = 6000 
	
	sz = wr.getframerate() 
	c = int(wr.getnframes()/sz) 
	for num in range(c):
	    da = np.fromstring(wr.readframes(sz), dtype=np.int16)
	    left, right = da[0::2], da[1::2] 
	    lf, rf = np.fft.rfft(left), np.fft.rfft(right)
	    lf[:lowpass], rf[:lowpass] = 0, 0  
	    lf[55:66], rf[55:66] = 0, 0  
	    lf[highpass:], rf[highpass:] = 0,0 
	    nl, nr = np.fft.irfft(lf), np.fft.irfft(rf)
	    ns = np.column_stack((nl,nr)).ravel().astype(np.int16)
	    ww.writeframes(ns.tostring())
	 
	wr.close()
	ww.close()
	fname='filtered-talk.wav'
	sptotex(fname)
Example #6
0
def clip_wav(wav_file, clip_file, start, stop):
    """Clip from start to stop in wav_file file, save to clip_file.

    Args:
        wav_file: full path to input wav
        clip_file: full path to output wav clip
        start: start time in wav_file in milliseconds
        stop: stop time in wav_file in milliseconds
    """

    win = wave.open(wav_file, 'r')
    framerate = win.getframerate()
    length = stop - start
    frames = int((length / 1000.0) * framerate)
    start_frame = int((start / 1000.0) * framerate)
    wout = wave.open(clip_file, 'w')
    wout.setparams(win.getparams())
    print wav_file, clip_file, start, stop
    try:
        win.setpos(start_frame)
    except wave.Error:
        print "Bad position"
        print
        return False
    wout.setparams(win.getparams())
    wout.writeframes(win.readframes(frames))
    win.close()
    wout.close()

    return True
def splitSong(inputSongName, inputSongFolder, outputSongFolder):
    inputSongFileNameNoExt = os.path.splitext(inputSongName)[0]

    waveExtension = ".wav"
    inputSongFileName = inputSongFolder+'//'+inputSongName
    segmentLengthInSeconds = 10;
    try:
        waveInput = wave.open(inputSongFileName, 'rb')

        totalNumberOfFrames = waveInput.getnframes()
        frameRate = waveInput.getframerate()
        segmentLengthInFrames = (frameRate * segmentLengthInSeconds)-((frameRate * segmentLengthInSeconds)%1024)
        numberOfSegments = int(float(totalNumberOfFrames)/segmentLengthInFrames)

        #print segmentLengthInFrames

        for i in xrange(numberOfSegments):
            outputSegmentFileName = outputSongFolder+'//'+inputSongFileNameNoExt + "_part" + str(i) + waveExtension
            waveOutput = wave.open(outputSegmentFileName, 'wb')
            waveOutput.setparams(waveInput.getparams())
            frames = waveInput.readframes(segmentLengthInFrames)  # read 10s of input
            waveOutput.writeframes(frames)  # write 10 s to output segment
            waveOutput.close

        waveInput.close()
    except EOFError as e:
        print e

#splitSong("testSong.wav", "//home//christophe//IdeaProjects//GenreClassificationScripts//truncateSong//TestFolderOutput")
Example #8
0
def mix_files(a, b, c, chann = 2, phase = -1.):
	f1 = wave.open(a, 'r')
	f2 = wave.open(b, 'r')

	r1, r2 = f1.getframerate(), f2.getframerate()
	if r1 != r2:
		print("Error: frame rates must be the same!")
		sys.exit(1)

	f3 = wave.open(c, 'w')
	f3.setnchannels(chann)
	f3.setsampwidth(2)
	f3.setframerate(r1)
	f3.setcomptype('NONE', 'Not Compressed')
	frames = min(f1.getnframes(), f2.getnframes())

	print("Mixing files, total length %.2f s..." % (frames / float(r1)))
	d1 = f1.readframes(frames)
	d2 = f2.readframes(frames)
	for n in range(frames):
		if not n % (5 * r1): print(n // r1, 's')
		if chann < 2:
			d3 = struct.pack('h', int(
				.5 * (struct.unpack('h', d1[2*n:2*n+2])[0] +
				struct.unpack('h', d2[2*n:2*n+2])[0])))
		else:
			d3 = ( struct.pack('h', int(
				phase * .3 * struct.unpack('h', d1[2*n:2*n+2])[0] +
				.7 * struct.unpack('h', d2[2*n:2*n+2])[0])) +
				struct.pack('h', int(
				.7 * struct.unpack('h', d1[2*n:2*n+2])[0] +
				phase * .3 * struct.unpack('h', d2[2*n:2*n+2])[0])) )
		f3.writeframesraw(d3)
	f3.close()
Example #9
0
def main():
    HOST = 'localhost'
    PORT = 50007
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr = s.accept()
    print 'Connected by', addr

    lengthbuf = recvall(conn, 4)
    length, = struct.unpack('!I', lengthbuf)
    data = recvall(conn, length)
    output_framelist = pickle.loads(data)
    #conn.close()

    ifile = wave.open("../files/9.wav","r")
    ofile = wave.open("../files/noise.wav", "w")
    ofile.setparams(ifile.getparams())

    sampwidth = ifile.getsampwidth()
    fmts = (None, "=B", "=h", None, "=l")
    fmt = fmts[sampwidth]
    dcs  = (None, 128, 0, None, 0)
    dc = dcs[sampwidth]

    for iframe in output_framelist:
	    oframe = iframe / 2;
	    oframe += dc
	    oframe = struct.pack(fmt, oframe)
	    ofile.writeframes(oframe)

    ifile.close()
    ofile.close()
Example #10
0
def som(self, tipo):
    # define stream chunk
    chunk = 1024

    # open a wav format music
    if tipo == 1:
        f = wave.open(BEEP, "rb")
    elif tipo == 2:
        f = wave.open(FIM, "rb")
    else:
        return
    # instantiate PyAudio
    p = pyaudio.PyAudio()
    # open stream
    stream = p.open(format=p.get_format_from_width(f.getsampwidth()),
                    channels=f.getnchannels(),
                    rate=f.getframerate(),
                    output=True)
    # read data
    data = f.readframes(chunk)

    # play stream
    while data != "":
        stream.write(data)
        data = f.readframes(chunk)

    # stop stream
    stream.stop_stream()
    stream.close()

    # close PyAudio
    p.terminate()
Example #11
0
def split_wav_file(filename, part_length, dest_dir=None, basename=None):
	"""
	@brief: splits a wav file into smaller 
	@param filename: the name of the file
	@param part_length: the length in seconds of each part
	@param dest_dir: the directory in which all parts should be. default is current directory
	@param basename: the name of the output files. they will be called <basename>_00000.wav
	@note: the maxium original file length is 833 hours
	"""

	if dest_dir is None:
		dest_dir = '.'
	if basename is None:
		basename = os.path.basename(filename)
	original_file = wave.open(filename, 'r')
	file_params = original_file.getparams()
	number_of_frames_per_part = int(original_file.getframerate() * part_length)
	total_number_of_frames = file_params[3]
	file_counter = 0
	data = 'init'

	while len(data) != 0:
		new_filename = basename + '_' + '0' * (DIGITS_IN_NAME-len(str(file_counter))) + str(file_counter) + '.wav'
		current_file = wave.open(os.path.join(dest_dir, new_filename), 'w')
		current_file.setparams(file_params)
		data = original_file.readframes(number_of_frames_per_part)
		total_number_of_frames -= number_of_frames_per_part
		current_file.writeframes(data)
		current_file.close()
		file_counter += 1

	original_file.close()
Example #12
0
    def save_file(frames):
        p = pyaudio.PyAudio()  # Create a PyAudio session
        if not os.path.isfile(
                WAVE_OUTPUT_FILENAME):  # If there is not such a file
            wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')  # Create the file
            wf.setnchannels(CHANNELS)  # Set number of channels
            wf.setsampwidth(p.get_sample_size(FORMAT))  # Set sampling format
            wf.setframerate(RATE)  # Set Bit Rate / Frame Rate
            wf.writeframes("")  # Write nothing
            wf.close()  # Close the session

        wf = wave.open(WAVE_OUTPUT_FILENAME,
                       'rb')  # Open the file with only read permission
        n_frames = wf.getnframes()  # Get all frames in it
        previous_wav = wf.readframes(
            n_frames)  # Assign all frames to a variable
        wf.close()  # Close the session

        wf = wave.open(WAVE_OUTPUT_FILENAME,
                       'wb')  # Open the file with write permission
        wf.setnchannels(CHANNELS)  # Set number of channels
        wf.setsampwidth(p.get_sample_size(FORMAT))  # Set sampling format
        wf.setframerate(RATE)  # Set Bit Rate / Frame Rate
        wf.writeframes(
            previous_wav +
            b''.join(frames))  # Write the all frames including previous ones
        wf.close()  # Close the session
def 切割音檔(被切音檔路徑, 完成音檔指標, 開頭時間, 結尾時間):
	# 切割一份字格中的一個漢字產生一個音檔
	
	origAudio = wave.open(被切音檔路徑,'r')
	frameRate = origAudio.getframerate()
	nChannels = origAudio.getnchannels()
	sampWidth = origAudio.getsampwidth()
	nFrames = origAudio.getnframes()
	
	# 	0.2和0.1是隨便給的
	start = int((float(開頭時間) - 0.05)*frameRate)
	end = int((float(結尾時間) + 0.01)*frameRate)
	
	# 確認切割位置:若是輸入的切割位置超出了音檔的長度,就切齊。
	if(start < 0):
		start = 0
	if(end > nFrames):
		end = nFrames		
	anchor = origAudio.tell()
	origAudio.setpos(anchor + start)
	chunkData = origAudio.readframes(end-start)
		
	# 輸出:存出音檔
	chunkAudio = wave.open(完成音檔指標,'w')
	chunkAudio.setnchannels(nChannels)
	chunkAudio.setsampwidth(sampWidth)
	chunkAudio.setframerate(frameRate)
	chunkAudio.writeframes(chunkData)
	
	chunkAudio.close()
	origAudio.close()
	
Example #14
0
        def trs():

            card = 'default'

            opts, args = getopt.getopt(sys.argv[1:], 'c:')
            for o, a in opts:
                if o == '-c':
                    card = a
            n1=e1.get()
            n2=e2.get()
            n3=e3.get()
            n4=e3.get()
            f = wave.open(n1, 'rb')
            A = wave.open(n2, 'rb')
            p = wave.open(n3, 'rb')
            x = wave.open(n4, 'rb')
            sumar(f, A, p,x)



            Archivo5 = Reproducir('sumaa.wav')
            Archivo5.ruta = "sumaa.wav"
            Argumentos = Archivo5.abrir()
            Archivo5.inicio(Argumentos[0],Argumentos[1],Argumentos[2])


            Archivo5.rep()
Example #15
0
def buildFileWav(infiles, files_dir):
  import wave
  outfile = os.path.join(files_dir,OUTPUT_FILENAME_WAV)

  data= []
  wav_frames_total = 0

  for infile in infiles:
    log(infile)
    w = wave.open(infile, 'rb')
    wav_frames_total += w.getnframes()
    data.append( [w.getparams(), w.readframes(w.getnframes())] )
    w.close()

  output = wave.open(outfile, 'wb')
  log(data[0][0])
  output.setparams(data[0][0])

  # On older (buggy?) Python versions like XBMC's built-in 2.4 .writeframes() seems not to update the "nframes" field of the WAV header 
  # and the resulting output file is a truncated mess. Therefore, force the nframes for the header and only write raw data. 
  #
  # To give developers even more fun, trying to manually build the full header on python 2.4 is an epic fail:
  # - when you read the docs for wave module (python 2.4 and all next versions) it says .setcomptype() takes 2 parameters;
  # - when you call .readcomptype() you get a list of two elements;
  # - when you feed this list to .setcomptype(), it raises an error that it takes "exactly 3 parameters"!
  #
  # On a modern Python version, just skip the .setnframes() and inside the loop, call .writeframes() instead of .writeframesraw()
  output.setnframes(wav_frames_total)

  for i in range(0, 32):
    output.writeframesraw(data[i][1])
  output.close()

  return outfile
Example #16
0
    def _extract_tokens(self, tokens, output_dir):
        if not self.has_audio():
            return
        filenames = []
        with wave.open(self.wav_path,'r') as w_in:
            sr = w_in.getframerate()
            bitdepth = w_in.getsampwidth()
            for t in tokens:
                wt = self[t]
                name = '{}_{}.wav'.format(self.name,wt.begin)
                wt.wav_path = os.path.join(output_dir,name)
                filenames.append(wt.wav_path)
                if os.path.exists(wt.wav_path):
                    continue

                begpos = int(wt.begin * sr)
                endpos = int(wt.end * sr)
                duration = endpos - begpos
                w_in.setpos(begpos)
                data = w_in.readframes(duration)
                with wave.open(wt.wav_path,'w') as w_out:
                    w_out.setnchannels(1)
                    w_out.setframerate(sr)
                    w_out.setsampwidth(bitdepth)
                    w_out.writeframes(data)
        return filenames
def mix_files(a, b, c, chann=2, phase=-1.0):
    f1 = wave.open(a, "r")
    f2 = wave.open(b, "r")
    f3 = wave.open(c, "w")
    f3.setnchannels(chann)
    f3.setsampwidth(2)
    f3.setframerate(44100)
    f3.setcomptype("NONE", "Not Compressed")
    frames = min(f1.getnframes(), f2.getnframes())

    print "Mixing files, total length %.2f s..." % (frames / 44100.0)
    d1 = f1.readframes(frames)
    d2 = f2.readframes(frames)
    for n in range(frames):
        if not n % (5 * 44100):
            print n // 44100, "s"
        if chann < 2:
            d3 = struct.pack(
                "h", 0.5 * (struct.unpack("h", d1[2 * n : 2 * n + 2])[0] + struct.unpack("h", d2[2 * n : 2 * n + 2])[0])
            )
        else:
            d3 = struct.pack(
                "h",
                phase * 0.3 * struct.unpack("h", d1[2 * n : 2 * n + 2])[0]
                + 0.7 * struct.unpack("h", d2[2 * n : 2 * n + 2])[0],
            ) + struct.pack(
                "h",
                0.7 * struct.unpack("h", d1[2 * n : 2 * n + 2])[0]
                + phase * 0.3 * struct.unpack("h", d2[2 * n : 2 * n + 2])[0],
            )
        f3.writeframesraw(d3)
    f3.close()
Example #18
0
def chop_input(wav_queue):
    while True:
        if not input_queue.empty():
            handle = wave.open(input_queue.get(), 'rb')
            frame_rate = handle.getframerate()
            n_frames = handle.getnframes()
            window_size = 2 * frame_rate
            num_secs = int(math.ceil(n_frames/frame_rate))

            snippet_list = []

            print "Slicing Audio file..."
            #for i in xrange(num_secs):
            for i in xrange(38):
                '''TODO: USE STRINGIO'''
                #wav_buffer = StringIO.StringIO(buffer)
                handle2 = '../audio/sample/'+str(i)+'snippet.wav'
                snippet = wave.open(handle2 ,'wb')
                snippet.setnchannels(2)
                snippet.setsampwidth(handle.getsampwidth())
                snippet.setframerate(frame_rate)
                snippet.writeframes(handle.readframes(window_size))
                handle.setpos(handle.tell() - int(1.8 * frame_rate))
                snippet.close()
                wav_queue.put(handle2)
            handle.close()
        else:
            print "Chop Worker waiting...\n"
            sleep(1)
Example #19
0
def cchanges(request,a):
	slist = audio_story.objects.filter(title=a)
	fname_a = slist[0].file_name
	stream = wave.open('/home/mayank/Desktop/bep/bep/bep_users/static/story_audio/temp.wav',"rb")
	num_channels = stream.getnchannels()
	sample_rate = stream.getframerate()
	sample_width = stream.getsampwidth()
	num_frames = stream.getnframes()
	t = stream.readframes(num_frames)
	stream.close()
	
	p = wave.open('/home/mayank/Desktop/bep/bep/bep_users/static/story_audio/'+fname_a,"wb")
	p.setnchannels(2)
	p.setsampwidth(sample_width)
	p.setframerate(44100)
	p.writeframes(t)
	p.close()
	
	story_list = story.objects.filter(title=a)
	fname = story_list[0].file_name
	fid = open('/home/mayank/Desktop/bep/bep/bep_users/static/story_text/'+fname, 'rb+')
	temp = fid.read()
	fid.close()
	frnd_list = audio_story.objects.filter(title=a)
	c = {'temp_title':a,'temp':temp,'frnd_list':frnd_list,'status':'false'}
	c.update(csrf(request))
	return render_to_response('add_effects.html',c)	
Example #20
0
    def audioFromDrama(dramaName):
        # Go to dir containing audio from subs
        for path, subdirs, files in os.walk(subAudioDir):
            for filename in files:
                if filename == str(dramaName + '.wav'):
                    dramaAudioFileName = os.path.join(
                        path, filename)

        with contextlib.closing(wave.open(
                dramaAudioFileName, 'r')) as dramaAudioFile:
            # Get wave information from drama wave
            rate = dramaAudioFile.getframerate()
            channels = dramaAudioFile.getnchannels()
            sampleWidth = dramaAudioFile.getsampwidth()

            # Make dirs for saving word audio to
            wordDir = os.path.join(subIndvAudioDir, word)

            if not os.path.exists(wordDir):
                os.makedirs(wordDir)

            # Prepare a container for wave data
            dataContainer = []

            # Loop over each instance where the word appears in the drama
            for i in range(0, len(db[word][key])):
                # Set up new file for writing
                # The [i][0] section writes the index number
                # taken from the SRT file.
                # This will allow future lookup of the sentence
                # by index number
                fileName = (word + '_' + dramaName + '_' + str(
                    db[word][key][i][0]) + '.wav')
                filePath = os.path.join(wordDir, fileName)
                extractAudio = wave.open(filePath, 'w')
                extractAudio.setsampwidth(sampleWidth)
                extractAudio.setframerate(rate)
                extractAudio.setnchannels(channels)

                # Set starting point by converting second data from dict
                # to frame position for wave
                dramaAudioFile.setpos(round((db[word][key][i][1]) * rate))
                # Do the same conversion to set the duration of the clip
                # add an extra second on just in case
                # Note: this could result in an error if the final word
                # occurs at the end of a clip. But, with dramas this
                # doesn't happen because of music/credits at the end.
                second = 1 * rate
                duration = round(db[word][key][i][2] * rate + second)
                # Collect data from drama audio file
                for indvFrames in range(0, duration):
                    frame = dramaAudioFile.readframes(1)
                    dataContainer.append(frame)
                # Write data to new word audio file
                for data in range(0, len(dataContainer)):
                    extractAudio.writeframesraw(dataContainer[data])

                extractAudio.close()
                print('Wrote audio file ', fileName)
                dataContainer = []
Example #21
0
File: asr.py Project: C2Tao/zrst
 def slice(self, corpus=(), target=(), label=()):
     if not corpus: corpus = self.X['corpus_dir']
     if not label:  label = self.X['result_mlf']
     if not target: target = self.X['acoust_dir']
     SYS().cldir(target)
     A = HTK().readMLF(label)
     waveData = dict({})
     params = []
     for wavefile in A.wav2word:
         W = wave.open(corpus + wavefile + '.wav')
         scale = float(W.getnframes()) / A.wav2word[wavefile][-1][1]
         params = W.getparams()
         for word in A.wav2word[wavefile]:
             framechunk = W.readframes(int(scale * (word[2] - word[1])))
             if word[0] in waveData:
                 try:
                     waveData[word[0]] += framechunk
                 except:
                     print word[0], 'out of memory'
                     pass
             else:
                 waveData[word[0]] = framechunk
     for word in waveData:
         if "<" in word:
             continue
             # this should only happen for words with illegal characters
         S = wave.open(target + word + '.wav', 'w')
         S.setparams(params)
         S.writeframes(waveData[word])
Example #22
0
def slice_wav(in_file, out_file, start_time, stop_time):
    # Open file
    spf = wave.open(in_file, 'r')
    waveform = spf.readframes(-1)

    # Adjust for stereo vs. mono encoding
    cpf = len(waveform) / spf.getnframes()
    adjusted_sample_rate = spf.getframerate() * cpf

    start_index = adjusted_sample_rate * start_time
    stop_index = adjusted_sample_rate * stop_time

    truncated_wave = waveform[start_index:stop_index]

    # Create and to new file, preserving most params
    f = open(out_file,'a')
    f.close()

    spf2 = wave.open(out_file,'w')

    spf2.setparams(spf.getparams())
    spf2.setnframes(len(truncated_wave) / adjusted_sample_rate)
    spf2.writeframes(truncated_wave)

    spf.close()
    spf2.close()
Example #23
0
    def _locate_and_modify_sounds(self, n_sounds):
        """Find wum samples and preload them as streams"""
        sounds = []
        for speed in range(1, n_sounds+1): # speed 0 = no sound
            filename = os.path.join(self.config['samples'], 'wum' + str(speed) + '.wav')
            print(filename)
            original_wave = wave.open(filename, 'rb')
            original_stream = original_wave.readframes(self.FRAME_RATE * self.FRAME_DURATION)


            volumes = []
            for volume in range(1, self.MAX_VOLUME + 1):
                print("building volume", volume)

                fmt = 'h' * (self.FRAME_RATE * self.FRAME_DURATION)
                values = struct.unpack(fmt, original_stream)
                values = map(lambda sample: int(float(sample) * (float(volume) / self.MAX_VOLUME)) , values)
                data_chunk_modified = struct.pack(fmt, *values)

                modified_stream = io.BytesIO()
                modified_wave = wave.open(modified_stream, 'w')
                modified_wave.setparams((self.CHANNELS, self.BPS, self.FRAME_RATE, self.FRAME_RATE * self.FRAME_DURATION, "NONE", "Uncompressed"))
                modified_wave.writeframes(data_chunk_modified)
                modified_wave.close()
                modified_stream.seek(0)
               
                volumes.append(wave.open(modified_stream, 'rb'))
            sounds.append(volumes)    
            #TODO: fail gracefully if a sample is missing
        
            original_wave.close()
        return sounds
Example #24
0
	def add_wav(self,files,read=False) : 
		"files : filename, filename , ...]"
		# read all files in memory (not too big) and append to chip
		assert files,'nothing to write'
		for f in files : # check all BEFORE actual writing
			fp_in = wave.open(f)
			print >>sys.stderr,fp_in.getnchannels(), "channels"
			assert fp_in.getnchannels()!='1',"mono sound file only !"
			print >>sys.stderr,fp_in.getsampwidth(), "byte width"
			assert fp_in.getsampwidth()==1,'only 8 bits input !'
			print >>sys.stderr,fp_in.getframerate(), "samples per second"
			assert fp_in.getframerate()==8000,'only 8khz samplerate !'

		self.read_table()
		for f in files : 
			print >>sys.stderr,'Adding ',f,'...'
			# read input entirely into memory
			fp_in = wave.open(f, "r")
			frameraw = fp_in.readframes(fp_in.getnframes())

			# append / prepend ramping sound to avoid clicks
			pre  = ''.join([chr(i) for i in range(0,ord(frameraw[0]),16)])
			post = ''.join([chr(i) for i in range(ord(frameraw[-1]),0,-16)])

			self.write(f,pre+frameraw+post,read)

		self.write_table()
def wavSamplesNextFrame(wavFile=None, chunk=None, overlap=None):
    #read a frame from wave file back as float numpy array, each index is a channel
    #can give chunk/overlap/wavfile name on first call and all is stored in function
    if wavSamplesNextFrame.w is None:
        if wavFile is None:
            sys.exit( "ERROR: must specify WAV FILE!!" )
            return
        wavSamplesNextFrame.w = wave.open(wavFile, 'r')
        wavSamplesNextFrame.name = wavFile
    if wavFile is not None:
        if (wavFile != wavSamplesNextFrame.name):
            wavSamplesNextFrame.w.close()
            wavSamplesNextFrame.w = wave.open(wavFile, 'r')
            wavSamplesNextFrame.name = wavFile
    if chunk is not None:
        wavSamplesNextFrame.chunk = chunk
    if overlap is not None:
        wavSamplesNextFrame.overlap = overlap
    #set pointer to wav based on overlap
    currentPos = wavSamplesNextFrame.w.tell()
    if (currentPos > wavSamplesNextFrame.overlap):
        wavSamplesNextFrame.w.setpos(currentPos - wavSamplesNextFrame.overlap)
    #read chunk as string
    astr = wavSamplesNextFrame.w.readframes(wavSamplesNextFrame.chunk)
    # convert binary chunks to short
    a = struct.unpack("%ih" % (wavSamplesNextFrame.chunk* wavSamplesNextFrame.w.getnchannels()), astr)
    a = [float(val) / pow(2, 15) for val in a]
    #make into numpy array by channel
    anew = []
    for ind in range(wavSamplesNextFrame.w.getnchannels()):
        anew.append(a[ind::wavSamplesNextFrame.w.getnchannels()])

    return np.array(anew)
Example #26
0
def crop_wav(split_start, split_end, input_file_name, output_file_name):
	print("splitting")
	mydir = os.getcwd()

	input_file_path = mydir + "/" + input_file_name
	output_file_path = mydir + "/" + output_file_name

	input_file = wave.open(input_file_path, 'r')
	width = input_file.getsampwidth()
	rate = input_file.getframerate()
	fpms = rate / 1000 # frames per ms
	#programmatically figure out start and end split
	length = (split_end - split_start) * fpms
	start_index = split_start * fpms
	


	# os.mkdir(path)
	output_file = wave.open(output_file_path, "w")
	output_file.setparams((input_file.getnchannels(), width, rate, length, input_file.getcomptype(), input_file.getcompname()))
	
	input_file.rewind()
	anchor = input_file.tell()
	input_file.setpos(anchor + start_index)
	output_file.writeframes(input_file.readframes(length))
	input_file.close()
	output_file.close()
	print("finished split")
Example #27
0
def slice_wave(in_file, out_file, offset=0, duration=0, max_framerate=None):
    """Write a section of a wavefile to a new file"""
    with closing(wave.open(in_file)) as win:
        params = win.getparams()
    wav = wave.open(out_file, 'wb')
    try:
        if not max_framerate:
            frames, params = _get_frames(in_file, offset, duration)
        else:
            audio, framerate = get_audio(in_file, offset=offset, duration=duration, max_framerate=max_framerate)
            params = list(params)
            if len(audio.shape) == 1:
                params[0] = 1
            else:
                params[0] = audio.shape[1]
            params[2] = framerate
            audio = audio.flatten() + _zeroline[params[1]]
            frames = struct.pack(_formats[params[1]] % (len(audio),), *audio)
        wav.setparams(params)
        wav.writeframes(frames)
    finally:
        try:
            wav.close()
        except:
            pass
Example #28
0
def decrypt(key, cipherfile, decryptfile):
	if cipherfile.endswith('.wav'):
		# Wave file open
		waveRead = wave.open(cipherfile, 'rb')
		waveWrite = wave.open(decryptfile, 'wb')

		# Reads the parameters
		header = waveRead.getparams()
		frames = waveRead.getnframes()
		sampleWidth = waveRead.getsampwidth()
		assert(waveRead.getnchannels() == 1)

		ciphertext = [byte for byte in waveRead.readframes(frames)]
		plaintext = bytearray([x for x in crypt(key, ciphertext)])

		# Writes the parameters and data to wave
		waveWrite.setparams(header)
		waveWrite.setnchannels(1)
		waveWrite.setsampwidth(sampleWidth)
		waveWrite.writeframes(plaintext)

		waveRead.close()
		waveWrite.close()

	else:
		# Regular file
		with open(cipherfile) as cipherTextFile:
			with open(decryptfile, mode='w') as plainTextFile:
				ciphertext=cipherTextFile.read()
				plaintext = ''.join(crypt(key, ciphertext))
				plainTextFile.write(plaintext)
Example #29
0
    def pack3(self):
	if self.width3==1: 
		fmt="%iB" % self.frames3*self.channel3 
	else: 
		fmt="%ih" % self.frames3*self.channel3

	out=struct.pack(fmt,*(self.new3))
	if self.pl3==0 or self.pl3==3:
		out_file=wave.open("ampli3.wav",'w')
	elif self.pl3==1:
		out_file=wave.open("wave_mix3.wav",'w')
	elif self.pl3==2:
		out_file=wave.open("wave_mod3.wav",'w')
	print self.pl3

	out_file.setframerate(self.rate3) 
	out_file.setnframes(self.frames3) 
	out_file.setsampwidth(self.width3) 
	out_file.setnchannels(self.channel3) 
	out_file.writeframes(out) 

	out_file.close()
	if self.pl3==0 :
		self.read_new("ampli3.wav",2)
	elif self.pl3==1:
		self.read_new("wave_mix3.wav",4)
		self.pl3=0
	elif self.pl3==2:
		self.read_new("wave_mod3.wav",3)
		self.pl3=0
	else:
		self.pl3=0
Example #30
0
def recognize(data):
    s = StringIO.StringIO(data)
    s.seek(0)
    r = wave.open(s, "rb")
    frames = r.readframes(11026)
    result = []
    while frames != '':
        md5 = hashlib.md5(frames).hexdigest()
        o = StringIO.StringIO()
        w = wave.open(o, "wb")
        w.setnchannels(r.getnchannels())
        w.setsampwidth(r.getsampwidth())
        w.setframerate(r.getframerate())
        w.writeframes(frames)
        w.close()
        o.seek(0)
        md5 = hashlib.md5(o.read()).hexdigest()
        if md5 in AUDIO_MAP:
            result.append(AUDIO_MAP[md5])
        else:
            (fd, fname) = tempfile.mkstemp(prefix=md5 + "-",
                                           suffix=".wav")
            f = os.fdopen(fd, "wb")
            o.seek(0)
            f.write(o.read())
            f.close()
            log.warning("Unknown individual character, please add %s" % (
                    (fname,)))
            return None
        frames = r.readframes(11026)
    return "".join(result)
Example #31
0
@author: Harry Ahlas
"""


import os
import wave
import struct
import random
import numpy as np
from numpy import array

import matplotlib.pyplot as plt

os.chdir("C:\\Development\\github\\GAN-tests")

wrd=wave.open("C:\Development\Python\encoder_decoder\encoder_decoder_sounds\Audio\drum_train-06.wav","r")

# Need to improve speed of import.  See http://www.cameronmacleod.com/blog/reading-wave-python
def read_whole(filename):
    wav_r = wave.open(filename, 'r')
    ret = []
    while wav_r.tell() < wav_r.getnframes():
        decoded = struct.unpack("<h", wav_r.readframes(1))
        ret.append(decoded)
    return ret

# Import training samples
drum_train_01 = read_whole("Audio\drum_train-01.wav")
drum_train_02 = read_whole("Audio\drum_train-02.wav")
drum_train_03 = read_whole("Audio\drum_train-03.wav")
drum_train_04 = read_whole("Audio\drum_train-04.wav")
Example #32
0
        data = f.readframes(periodsize)


def usage():
    print('usage: playwav.py [-d <device>] <file>', file=sys.stderr)
    sys.exit(2)


#Ctrl+C
def signal_handler(sig, frame):
    print('Stop: Ctrl+C!')
    sys.exit(0)


#Main
if __name__ == '__main__':
    signal.signal(signal.SIGINT, signal_handler)
    device = 'default'
    opts, args = getopt.getopt(sys.argv[1:], 'd:')
    #Cerco un se è specificato un altro device
    for o, a in opts:
        if o == '-d':
            device = a
    #Come usare
    if not args:
        usage()

    #Read only mode
    with wave.open(args[0], 'rb') as f:
        play(device, f)
Example #33
0
def main():
    import sys
    import os
    import wave
    from optparse import OptionParser
    import crcmod

    from hexdump import hexdump
    import binascii
    import zlib

    usage = "usage: %prog [options] FILENAME"
    parser = OptionParser(usage)
    parser.add_option("-d",
                      "--dump",
                      help="dump configuration to text",
                      action="store_true",
                      dest="dump")
    parser.add_option("-s",
                      "--summary",
                      help="summarize DFU as human readable text",
                      action="store_true",
                      dest="summary")
    parser.add_option("-o",
                      "--output",
                      dest="outfile",
                      help="write data to OUTFILE")

    parser.add_option("-u",
                      "--unpack",
                      help="unpack Samples to UNPACK directory",
                      dest="unpack")
    parser.add_option("-p",
                      "--pack",
                      help="pack Samples to PACK directory",
                      dest="pack")
    parser.add_option("-r", "--replace",
        help="pack REPLACE directory of samples to DFU " + \
            "(overwrites contents, either padded or clipped to original size)",
        dest="replace")
    parser.add_option("-R",
                      "--raw",
                      help="use '.raw' sample files (rather than '.wav')",
                      action="store_true",
                      dest="raw")

    parser.add_option("-t",
                      "--test",
                      help="scripted test, dev use only",
                      action="store_true",
                      dest="test")

    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.error("FILE not specified")

    print("Opening:", args[0])
    infile = open(args[0], "rb")
    if not infile:
        sys.exit("Unable to open FILE for reading")
    else:
        data = infile.read()
    infile.close()

    if data:
        config = DFU.parse(data)

        if options.summary:
            print("Number of samples:", config['block']['total'])

            total = 0
            btotal = 0
            a_count = 1
            b_count = 1
            for sample in config['block']['samples']:
                print("Sample %d-%d: %s (%s bytes, %f sec)" % \
                    (a_count, b_count, sample['length'], sample['bytes'], int(sample['length'])/32000))
                total += int(sample['length'])
                btotal += int(sample['bytes'])
                if b_count == config['block']['elements'][a_count - 1]:
                    a_count += 1
                    b_count = 1

                    while (a_count <= len(config['block']['elements']) and \
                            config['block']['elements'][a_count-1] == 0):
                        a_count += 1
                else:
                    b_count += 1
            print("Total length: %d bytes" % btotal)
            print("Total length: %f sec" % (total / 32000))

        if options.unpack:
            path = os.path.join(os.getcwd(), options.unpack)
            if os.path.exists(path):
                sys.exit("Directory %s already exists" % path)

            os.mkdir(path)

            a_count = 1
            b_count = 1
            for data in config['block']['data']:
                unpacked = unpack_samples(data.data)

                if options.raw:
                    name = os.path.join(
                        path, "sample-{0:0=2d}-{1:0=1d}.raw".format(
                            a_count, b_count))
                    outfile = open(name, "wb")
                    for value in unpacked:
                        outfile.write(value.to_bytes(2, byteorder='little'))
                    outfile.close()
                else:
                    name = os.path.join(
                        path, "sample-{0:0=2d}-{1:0=1d}.wav".format(
                            a_count, b_count))
                    outfile = wave.open(name, "wb")
                    outfile.setsampwidth(2)
                    outfile.setnchannels(1)
                    outfile.setframerate(32000)

                    for value in unpacked:
                        outfile.writeframesraw(
                            value.to_bytes(2, byteorder='big'))
                    outfile.close()

                if b_count == config['block']['elements'][a_count - 1]:
                    a_count += 1
                    b_count = 1

                    while (a_count <= len(config['block']['elements']) and \
                            config['block']['elements'][a_count-1] == 0):
                        a_count += 1
                else:
                    b_count += 1

        if options.pack or options.replace:
            if options.replace:
                path = os.path.join(os.getcwd(), options.replace)
            else:
                path = os.path.join(os.getcwd(), options.pack)
            if not os.path.exists(path):
                sys.exit("Directory %s does not exist" % path)

            count = 1
            a_count = 1
            b_count = 1

            if options.pack:
                # create more than enough empty slots
                Empty = Array(
                    200, Struct(
                        "length" / Computed(0),
                        "bytes" / Computed(0),
                    ))
                empty = Empty.parse(b"")
                config['block']['samples'] = empty

                Empty = Array(200, Struct("data" / Bytes(0), ))
                empty = Empty.parse(b"")
                config['block']['data'] = empty

            for sample in config['block']['samples']:
                unpacked = []
                infile = None
                if options.raw:
                    name = os.path.join(
                        path, "sample-{0:0=2d}-{1:0=1d}.raw".format(
                            a_count, b_count))
                    if os.path.isfile(name):
                        infile = open(name, "rb")
                        if infile:
                            if options.pack:
                                file_stats = os.stat(name)
                                length = file_stats.st_size / 2
                            else:
                                length = sample['length']

                            if length > 0xffff:
                                length = 0xffff

                            for temp in range(length):
                                value = infile.read(2)
                                unpacked.append(
                                    int.from_bytes(value, byteorder='little'))
                            infile.close()
                else:
                    name = os.path.join(
                        path, "sample-{0:0=2d}-{1:0=1d}.wav".format(
                            a_count, b_count))
                    if os.path.isfile(name):
                        infile = wave.open(name, "rb")
                        if infile:
                            # checks
                            if infile.getnchannels() != 1:
                                sys.exit("Samples should be 1 channel: %s" %
                                         name)
                            if infile.getsampwidth() != 2:
                                sys.exit("Samples should be 16 bit: %s" % name)
                            if infile.getframerate() != 32000:
                                sys.exit("Samples should be 3200KHz: %s" %
                                         name)

                            if options.pack:
                                length = infile.getnframes()
                            else:
                                length = sample['length']

                            if length > 0xffff:
                                length = 0xffff

                            for temp in range(length):
                                value = infile.readframes(1)
                                unpacked.append(
                                    int.from_bytes(value, byteorder='big'))
                            infile.close()

                if len(unpacked):
                    config['block']['data'][count - 1].data = bytes(
                        pack_samples(unpacked))

                count += 1
                if options.pack:
                    if not infile:
                        # file not found, advanced to next element
                        count -= 1

                        config['block']['elements'][a_count - 1] = b_count - 1
                        config['block']['total'] = count - 1
                        a_count += 1
                        b_count = 1

                        if a_count == 13:
                            # all elements done, truncate data arrays
                            config['block']['samples'] = config['block'][
                                'samples'][:count - 1]
                            config['block']['data'] = config['block'][
                                'data'][:count - 1]
                            break
                    else:
                        config['block']['samples'][count - 2].length = length
                        config['block']['samples'][count-2].bytes = \
                                len(config['block']['data'][count-2].data)
                        b_count += 1
                elif b_count == config['block']['elements'][a_count - 1]:
                    a_count += 1
                    b_count = 1

                    while (a_count <= len(config['block']['elements']) and \
                            config['block']['elements'][a_count-1] == 0):
                        a_count += 1
                else:
                    b_count += 1

        if options.dump:
            print(config)

        if options.outfile:
            # re-calc inner checksum
            data = Block.build(config['block'])
            crc32 = crcmod.Crc(0x104c11db7,
                               rev=False,
                               initCrc=0,
                               xorOut=0xFFFFFFFF)
            crc32.update(data)
            config['checksum'] = crc32.crcValue ^ 0xFFFFFFFF

            # re-calc outer checksum
            data = DFU.build(config)
            crc32 = crcmod.Crc(0x104c11db7,
                               rev=True,
                               initCrc=0,
                               xorOut=0xFFFFFFFF)
            crc32.update(data)

            outfile = open(options.outfile, "wb")
            if outfile:
                outfile.write(data)
                outfile.write(
                    (crc32.crcValue ^ 0xFFFFFFFF).to_bytes(4,
                                                           byteorder='little'))
                outfile.close
Example #34
0
def getFrequenciesFromPartialAudioFile(waveFilenameOrHandle='temp',
                                       length=10.0,
                                       startSample=0):
    '''
    It calculates the fundamental frequency at every instant of time of an audio signal
    extracted either from the microphone or from an already recorded song.
    It uses a period of time defined by the variable "length" in seconds.

    It returns a list with the frequencies, a variable with the file descriptor, 
    and the end sample position.

    >>> #_DOCS_SHOW readFile = 'pachelbel.wav'
    >>> import os #_DOCS_HIDE
    >>> sp = common.getSourceFilePath() #_DOCS_HIDE
    >>> readFile = os.path.join(sp, 'audioSearch', 'test_audio.wav') #_DOCS_HIDE
    >>> fTup  = audioSearch.getFrequenciesFromPartialAudioFile(readFile, length=1.0)
    >>> frequencyList, pachelbelFileHandle, currentSample = fTup
    >>> for i in range(5):
    ...     print(frequencyList[i])
    143.627689055
    99.0835452019
    211.004784689
    4700.31347962
    767.827403482
    >>> print(currentSample)  # should be near 44100, but probably not exact
    44032

    Now read the next 1 second...

    >>> fTup = audioSearch.getFrequenciesFromPartialAudioFile(pachelbelFileHandle, length=1.0, 
    ...                                                       startSample=currentSample)
    >>> frequencyList, pachelbelFileHandle, currentSample = fTup
    >>> for i in range(5):
    ...     print(frequencyList[i])
    187.798213268
    238.263483185
    409.700397349
    149.958733396
    101.989786226
    >>> print(currentSample)  # should be exactly double the previous
    88064
    '''
    if "numpy" in base._missingImport:
        raise AudioSearchException(
            "Cannot run getFrequenciesFromPartialAudioFile without numpy installed"
        )
    import numpy

    if waveFilenameOrHandle == 'temp':
        waveFilenameOrHandle = environLocal.getRootTempDir(
        ) + os.path.sep + 'temp.wav'

    if common.isStr(waveFilenameOrHandle):
        # waveFilenameOrHandle is a filename
        waveFilename = waveFilenameOrHandle
        try:
            waveHandle = wave.open(waveFilename, 'r')
        except IOError:
            raise AudioSearchException(
                "Cannot open %s for reading, does not exist" % waveFilename)
    else:
        # waveFilenameOrHandle is a filehandle
        waveHandle = waveFilenameOrHandle

    storedWaveSampleList = []

    environLocal.printDebug("* reading file from disk a part of the song")
    for i in range(
            int(math.floor(length * recordSampleRate / audioChunkLength))):
        startSample = startSample + audioChunkLength
        if startSample < waveHandle.getnframes():
            data = waveHandle.readframes(audioChunkLength)
            storedWaveSampleList.append(data)
    freqFromAQList = []

    for data in storedWaveSampleList:
        samps = numpy.fromstring(data, dtype=numpy.int16)
        freqFromAQList.append(autocorrelationFunction(samps, recordSampleRate))

    endSample = startSample
    return (freqFromAQList, waveHandle, endSample)
import os
import numpy as np

_format = pyaudio.paInt16
_channels = 2
_rate = 44100
_chunk = 2048

maxValue = 2**16
bars = 50

if len(sys.argv) < 2:
    print("Syntax: %s entrada.wav" % sys.argv[0])
    sys.exit(-1)

wf = wave.open(sys.argv[1], 'rb')

audio = pyaudio.PyAudio()


np.seterr(all='ignore')

stream = audio.open(format=_format,
                channels=_channels,
                rate=_rate,
                output=True)

os.system("clear")

data = wf.readframes(_chunk)
Example #36
0
    def render(self, cap, output=None, with_audio=False):
        """
        Iterate each video frame to print a set of ascii chars

        This method reads each video frame from a opencv video capture
        resizing the frame and truncate the width if necessary to
        print correctly the final string built with the method
        convert_frame_pixels_to_ascii.
        Finally each final string is printed correctly, if the process
        was done too fast will sleep the necessary time to comply
        with the fps expected (30 fps by default).

        Args:
            cap: An OpenCV video capture
            output: If the render should be exported to a bash file
        """

        v_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        v_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
        length = cap.get(cv2.CAP_PROP_FRAME_COUNT)
        fps = cap.get(cv2.CAP_PROP_FPS)
        fps = fps or 30

        if with_audio:
            import pyaudio
            import wave

            temp_dir = tempfile.gettempdir()
            temp_file_path = temp_dir + "/temp-audiofile-for-vta.wav"
            wave_file = wave.open(temp_file_path, 'rb')
            chunk = int(wave_file.getframerate() / fps)
            p = pyaudio.PyAudio()

            stream = p.open(format=p.get_format_from_width(
                wave_file.getsampwidth()),
                            channels=wave_file.getnchannels(),
                            rate=wave_file.getframerate(),
                            output=True)

            data = wave_file.readframes(chunk)

        if output is not None:
            file = open(output, 'w+')
            file.write("#!/bin/bash \n")
            file.write("echo -en '\033[2J' \n")
            file.write("echo -en '\u001b[0;0H' \n")

        time_delta = 1. / fps
        counter = 0
        if PLATFORM:
            sys.stdout.write("echo -en '\033[2J' \n")
        else:
            sys.stdout.write('\033[2J')
        # read each frame
        while cap.isOpened():
            t0 = time.process_time()
            if PLATFORM:
                rows, cols = os.popen('stty size', 'r').read().split()
            else:
                cols, rows = os.get_terminal_size()
            _ret, frame = cap.read()
            if frame is None:
                break
            if with_audio:
                data = wave_file.readframes(chunk)
                stream.write(data)
            # sleep if the process was too fast
            if output is None:
                if PLATFORM:
                    sys.stdout.write('\u001b[0;0H')
                else:
                    sys.stdout.write("\x1b[0;0H")
                # scale each frame according to terminal dimensions
                resized_frame = self.resize_frame(frame, (cols, rows))
                # convert frame pixels to colored string
                msg = self.convert_frame_pixels_to_ascii(
                    resized_frame, (cols, rows))
                t1 = time.process_time()
                delta = time_delta - (t1 - t0)
                if delta > 0:
                    time.sleep(delta)
                sys.stdout.write(msg)  # Print the final string
            else:
                print(self.build_progress(counter, length))
                if PLATFORM:
                    print("\u001b[2A")
                else:
                    print("\x1b[2A")
                resized_frame = self.resize_frame(frame)
                msg = self.convert_frame_pixels_to_ascii(resized_frame,
                                                         new_line_chars=True)
                file.write("sleep 0.033 \n")
                file.write("echo -en '" + msg + "'" + "\n")
                file.write("echo -en '\u001b[0;0H' \n")
            counter += 1
        if with_audio:
            stream.close()
            p.terminate()
        if PLATFORM:
            sys.stdout.write("echo -en '\033[2J' \n")
        else:
            os.system('cls') or None
import pyaudio
import wave
import sys

filename = sys.argv[1]

# set the chunk size of 1024 samples
chunk = 1024

# open the audio file
wf = wave.open(filename, "rb")

# initialize PyAudio object
p = pyaudio.PyAudio()

# open stream object
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                channels=wf.getnchannels(),
                rate=wf.getframerate(),
                output=True)

# read data in chunks
data = wf.readframes(chunk)

# writing to the stream (playing audio)
while data:
    stream.write(data)
    data = wf.readframes(chunk)

# close stream
stream.close()
Example #38
0
import numpy as np
from scipy.io.wavfile import write
import wave
import binascii
import bitarray
import TA.pesan

ba = bitarray.bitarray()

spf = wave.open('testgrde2.wav', 'r')

# Extract Raw Audio from Wav File
signal = spf.readframes(-1)
signal = np.fromstring(signal, 'uint8')

size = signal.size

mod = divmod(size, 4)
headlen = size - mod[1]
# print(size)
# print(mod)

head = signal[:headlen]
tail = signal[headlen:]
print(signal[10467])
pair = head.reshape((-1, 4))

with open("lmgrde2", mode='rb') as file:  # b is important -> binary
    fileContent = file.read()

fileContent = list(fileContent)
Example #39
0
def write_wav(path, params, raw_audio):
    with wave.open(path, 'wb') as out:
        out.setparams(params)
        out.writeframes(raw_audio)
amplitude = []

for i in range(len(amp0)):
    amp_per_sec += ((amp0[i])**2)
    amp_per_sec += ((amp1[i])**2)
    if (i != 0 and i % 500 == 0):
        amp_per_sec = (amp_per_sec / 1000)**(1 / 2)
        amplitude.extend([amp_per_sec])
        amp_per_sec = 0
    if (i == len(amp0)):
        amp_per_sec = (amp_per_sec / (i % 1000))**(1 / 2)
        amplitude.extend([amp_per_sec])

# length (time) of wav file
fname = song
with contextlib.closing(wave.open(fname, 'r')) as f:
    frames = f.getnframes()
    rate = f.getframerate()
    duration = frames / float(rate)

ANALYSISRATE = 88

#raw data from taps.py
Final_Times_Array = [
    0.8732759475708008, 1.9280156294504804, 2.88275531133016, 3.88749499320984
]

ind = 0

count = 0
success = True
Example #41
0
class VAD(object):
    """docstring for VAD"""
    def __init__(self, frame_size = 256, over_lap = 128):
        super(VAD, self).__init__()
        self.frame_size = frame_size
        self.over_lap = over_lap
        
    # method 1: absSum
    def get_volume(self,wave_data):
        wave_data = np.fromstring(wave_data, dtype=np.int16)
        wave_data = wave_data*1.0/max(abs(wave_data))  # normalization
        wlen = len(wave_data)
        step = self.frame_size - self.over_lap
        frame_num = int(math.ceil(wlen*1.0/step))
        volume = np.zeros(frame_num)
        for i in range(frame_num):
            cur_frame = wave_data[np.arange(i*step,min(i*step+self.frame_size,wlen))]
            cur_frame = cur_frame - np.median(cur_frame) # zero-justified
            volume[i] = np.sum(np.abs(cur_frame))
        return volume

    # method 2: 10 times log10 of square sum
    def get_volumeDB(self,wave_data):
        wave_data = np.fromstring(wave_data, dtype=np.int16)
        wave_data = wave_data*1.0/max(abs(wave_data))  # normalization
        wlen = len(wave_data)
        step = self.frame_size - self.over_lap
        frame_num = int(math.ceil(wlen*1.0/step))
        volume = np.zeros(frame_num)
        for i in range(frame_num):
            cur_frame = wave_data[np.arange(i*step,min(i*step+self.frame_size,wlen))]
            cur_frame = cur_frame - np.mean(cur_frame) # zero-justified
            volume[i] = 10*np.log10(np.sum(cur_frame*cur_frame))
        return volume

    @staticmethod
    def string_split(string,width):
        return [string[x:x+width] for x in range(0,len(string),width)]

    def remove_mute(self,input_file,output_file,case = 1):
        wf = wave.open(input_file,'r')
        params = wf.getparams()
        nchannels, sampwidth, framerate, nframes = params[:4]
        wave_data = wf.readframes(nframes)
        wf.close()

        volume = self.get_volume(wave_data)

        if case == 1:
            threshhold = max(volume)*0.10
        if case == 2:
            threshhold = min(volume)*10.0
        if case == 3: 
            threshhold = max(volume)*0.05+min(volume)*5.0

        wave_data = self.string_split(wave_data,self.frame_size)
        length = len(wave_data)

        # print len(volume)
        # print len(wave_data)

        frames = []
        start = 0
        end = length-1
        for i in range(len(volume)):
            if(volume[i]-threshhold)>0:
                start = i
                break
        while(end > start):
            if (volume[end]-threshhold)<0:
                break
            else:
                end = end - 1

        print start
        print end

        for i in xrange(start,end):
            frames.append(wave_data[i])

        wf = wave.open(output_file,"wb")
        wf.setnchannels(nchannels)
        wf.setsampwidth(sampwidth)
        wf.setframerate(framerate)
        wf.writeframes(b''.join(frames))
Example #42
0
import wave

import pyaudio

import numpy

import pylab

 

#打开WAV文档,文件路径根据需要做修改

wf = wave.open("D:\\Python\\wavs\\Do-piano-2.20s.wav", "rb")

#创建PyAudio对象

p = pyaudio.PyAudio()

stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),

channels=wf.getnchannels(),

rate=wf.getframerate(),

output=True)

nframes = wf.getnframes()

framerate = wf.getframerate()
Example #43
0
def read_duration(fname):
    with contextlib.closing(wave.open(fname, 'r')) as f:
        frames = f.getnframes()
        rate = f.getframerate()
        duration = frames / float(rate)
    return duration
Example #44
0
print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data) # 2 bytes(16 bits) per channel

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'rb')


p = pyaudio.PyAudio()


stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
				channels=wf.getnchannels(),
				rate=wf.getframerate(),
Example #45
0
    def record_audio(self, audio_file_name, detect_silence=True):
        """
        Records audio from the model to the given audio file.
            :param audio_file_name:
                the wav file to be created with the recorded audio;
                recorded with 8-bit linear compression at 8.0 kHz sampling rate
            :return:
                True if a message was saved.
        """
        if self.config["DEBUG"]:
            print("> Recording {}...".format(audio_file_name))

        self._serial.cancel_read()
        with self._lock:
            try:
                if not self._send(ENTER_VOICE_MODE):
                    raise RuntimeError("Failed to put modem into voice mode.")

                if not self._send(SET_VOICE_COMPRESSION):
                    raise RuntimeError("Failed to set compression method and sampling rate specifications.")

                if not self._send(DISABLE_SILENCE_DETECTION):
                    raise RuntimeError("Failed to disable silence detection.")

                if not self._send(TELEPHONE_ANSWERING_DEVICE_OFF_HOOK):
                    raise RuntimeError("Unable put modem (TAD) off hook.")

                if not self._send(SEND_VOICE_TONE_BEEP):
                    raise RuntimeError("Failed to play 1.2 second beep.")

                if not self._send(ENTER_VOICE_RECIEVE_DATA_STATE, "CONNECT"):
                    raise RuntimeError("Error: Unable put modem into voice receive mode.")

            except RuntimeError as error:
                print("Modem initialization error: ", error)
                return False

            # Record Audio File
            start_time = datetime.now()
            CHUNK = 1024
            audio_frames = []
            # Define the range of amplitude values that are to be considered silence.
            # In the 8-bit audio data, silence is \0x7f or \0x80 (127.5 rounded up or down)
            threshold = 1
            min_silence = 127 - threshold
            max_silence = 128 + threshold
            silent_frame_count = 0
            success = True
            while 1:
                # Read audio data from the Modem
                audio_data = self._serial.read(CHUNK)

                # Scan the audio data for DLE codes from modem
                if (DCE_END_VOICE_DATA_TX in audio_data):
                    # <DLE><ETX> is in the stream
                    print(">> <DLE><ETX> Char Recieved... Stop recording.")
                    break
                if (DCE_PHONE_OFF_HOOK in audio_data):
                    # <DLE>H is in the stream
                    print(">> Local phone off hook... Stop recording")
                    break
                if (DCE_BUSY_TONE in audio_data):
                    print(">> Busy Tone... Stop recording.")
                    break

                # Test for silence
                if detect_silence:
                    if len(audio_data) == sum(1 for x in audio_data if min_silence <= x <= max_silence):
                        # Increment number of contiguous silent frames
                        silent_frame_count += 1
                    else:
                        silent_frame_count = 0
                    # At 8KHz sample rate, 5 secs is ~40K bytes
                    if silent_frame_count > 40:  # 40 frames is ~5 secs
                        # TODO: Consider trimming silent tail from audio data.
                        print(">> Silent frames detected... Stop recording.")
                        break

                # Timeout
                if ((datetime.now() - start_time).seconds) > REC_VM_MAX_DURATION:
                    print(">> Stop recording: max time limit reached.")
                    break

                # Add Audio Data to Audio Buffer
                audio_frames.append(audio_data)

            # Save the file if there is audio
            if len(audio_frames) > silent_frame_count:
                print(">> Saving audio file.")
                with wave.open(audio_file_name, 'wb') as wf:
                    wf.setnchannels(1)
                    wf.setsampwidth(1)
                    wf.setframerate(8000)
                    wf.writeframes(b''.join(audio_frames))
            else:
                print(">> Skipped saving silent audio.")
                success = False

            print(">> Recording stopped after {} seconds.".format((datetime.now() - start_time).seconds))

            # Clear input buffer before sending commands else its
            # contents may interpreted as the cmd's return code
            self._serial.reset_input_buffer()

            # Send End of Recieve Data state by passing "<DLE>!"
            # USR-5637 note: The command returns <DLE><ETX>, but the DLE is stripped
            # from the response during the test, so we only test for the ETX.
            response = "OK" if self.model == "CONEXANT" else ETX_CODE
            if not self._send(DTE_END_VOICE_DATA_RX, response):
                print("* Error: Unable to signal end of data receive state")

        return success
import wave

import numpy as np
import scipy.signal as sp

from sound_source_separation import wave_writer, wave_loader, specgram
from sound_source_separation.stft import short_term_fourier_transform

with wave.open("sample_noise_mixed.wav") as wav:
    f, t, stft_data = short_term_fourier_transform(wav)
    amp = np.abs(stft_data)
    phase = stft_data / np.maximum(amp, 1.e-20)

    n_noise_only = 40000
    n_noise_only_frame = np.sum(t < n_noise_only / wav.getframerate())
    noise_part_amp = amp[:, :n_noise_only_frame]
    noise_amp_mean = np.sqrt(np.mean(np.power(noise_part_amp, 2), axis=1, keepdims=True))

    p = 1.0
    alpha = 3.3
    eps = 0.01 * np.power(amp, p)

    processed_amp = np.power(np.maximum(np.power(amp, p) - alpha * np.power(noise_amp_mean, p), eps), 1./p)
    processed_amp = processed_amp * phase

    _, istft_data = sp.istft(processed_amp, fs=wav.getframerate(), nperseg=512, noverlap=256)
    istft_data = istft_data.astype(np.int16)
    wave_writer.write("sample_noise_reduced.wav", istft_data, wav.getframerate())

with wave.open("sample_noise_reduced.wav") as wav:
    wave_loader.play(wav)
Example #47
0
    freq = np.fft.fftfreq(RATE * 2, 1.0 /
                          RATE)  #check2 seconds frequency to find the maximum
    #freq = freq[:int(len(freq)/2)]
    freqPeak = freq[np.where(fft == np.max(fft))[0][0]] + 1

    print("peak frequency: %d Hz" % freqPeak)

    plt.plot(freq, fft)
    # plt.axis([0,4000,None,None])
    plt.axis([0, 4000, None, None])
    plt.show()
    plt.close()

print("Finished Listning...")

# close the stream gracefully
stream.stop_stream()
stream.close()
p.terminate()

#stop Recording
stream.stop_stream()
stream.close()
audio.terminate()

waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
Example #48
0
if (not ubicoustics_model.is_file()):
    print("Downloading example_model.hdf5 [867MB]: ")
    wget.download(MODEL_URL,MODEL_PATH)

# Load Model
context = ubicoustics.everything
context_mapping = ubicoustics.context_mapping
trained_model = model_filename
other = True
selected_file = '../flusense_raw_audio/_0WKVY0n8aE.wav'
selected_context = 'everything'

print("Using deep learning model: %s" % (trained_model))
model = load_model(trained_model)
graph = tf.get_default_graph()
wf = wave.open(selected_file, 'rb')

context = context_mapping[selected_context]
label = dict()
for k in range(len(context)):
    label[k] = context[k]

# Setup Callback
def audio_samples(input, frame_count, time_info, status_flags):
    global graph
    in_data = wf.readframes(frame_count)
    np_wav = np.fromstring(in_data, dtype=np.int16) / 32768.0
    x = waveform_to_examples(np_wav, RATE)
    predictions = []
    with graph.as_default():
        if x.shape[0] != 0:
Example #49
0
 def __duration(self, path):
     with contextlib.closing(wave.open(path, 'r')) as f:
         frames = f.getnframes()
         rate = f.getframerate()
         duration = frames / float(rate)
         return duration
Example #50
0
import sys
import wave
import numpy as np
from gcc_phat import gcc_phat

print("start")
sig1 = wave.open("test2.wav", 'rb')#near
ref1 = wave.open("test3.wav", 'rb')#far
rate = sig1.getframerate()

N = rate
window = np.hanning(N)

while True:
    sig = sig1.readframes(N)
    if len(sig) != 2 * N:
        break
    ref = ref1.readframes(N)
    sig_buf = np.fromstring(sig, dtype='int16')
    ref_buf = np.fromstring(ref, dtype='int16')
    print len(sig_buf)
    print len(ref_buf)
    tau, _ = gcc_phat(sig_buf * window, ref_buf * window, fs=rate, max_tau=1)
    print(tau*340000)

Example #51
0
# plot_wave_file.py

import struct
import wave
from matplotlib import pyplot

# Specify wave file
wavfile = 'author.wav'
# wavfile = 'decay_cosine_mono.wav'
# wavfile = 'decay_cosine_mono_vibrato.wav'
# wavfile = 'sin01_mono.wav'
# wavfile = 'sin01_mono_vibrato.wav'
print('Play the wave file: {0:s}.'.format(wavfile))

# Open wave file
wf = wave.open( wavfile, 'rb')

# Read wave file properties
RATE        = wf.getframerate()     # Frame rate (frames/second)
WIDTH       = wf.getsampwidth()     # Number of bytes per sample
LEN         = wf.getnframes()       # Signal length
CHANNELS    = wf.getnchannels()     # Number of channels

print('The file has %d channel(s).'         % CHANNELS)
print('The file has %d frames/second.'      % RATE)
print('The file has %d frames.'             % LEN)
print('The file has %d bytes per sample.'   % WIDTH)

# exit()

BLOCKLEN = 1000    # Blocksize
Example #52
0
dev_index = 2  # device index found by p.get_device_info_by_index(ii)
wav_output_filename = 'test1.wav'  # name of .wav file

audio = pyaudio.PyAudio()  # create pyaudio instantiation

# create pyaudio stream
stream = audio.open(format = form_1,rate = samp_rate,channels = chans, \
                    input_device_index = dev_index,input = True, \
                    frames_per_buffer=chunk)
print("recording")
frames = []

# loop through stream and append audio chunks to frame array
for ii in range(0, int((samp_rate / chunk) * record_secs)):
    data = stream.read(chunk, exception_on_overflow=False)
    frames.append(data)

print("finished recording")

# stop the stream, close it, and terminate the pyaudio instantiation
stream.stop_stream()
stream.close()
audio.terminate()

# save the audio frames as .wav file
wavefile = wave.open(wav_output_filename, 'wb')
wavefile.setnchannels(chans)
wavefile.setsampwidth(audio.get_sample_size(form_1))
wavefile.setframerate(samp_rate)
wavefile.writeframes(b''.join(frames))
wavefile.close()
Example #53
0
File: pi.py Project: fabubaker/Hive
NUM = int(sys.argv[1])
BASEPORT = 9000
PORTS = map(lambda x: str(BASEPORT + x), range(0, NUM))
FILE = "sound.wav"
TOPIC = "AUDIO"
context = zmq.Context()

sockets = []
wf = None

for i, port in enumerate(PORTS):
    socket = context.socket(zmq.PUB)
    socket.bind("tcp://*:%s" % port)
    sockets.append(socket)

wf = wave.open(FILE, 'rb')

CHUNK = 4096
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100

p = pyaudio.PyAudio()

framecount = 1
while True:
    time.sleep(0.001)
    framecount += 1
    data = wf.readframes(CHUNK)

    for i, socket in enumerate(sockets):
Example #54
0
import wave

from sound_source_separation import wave_writer, wave_loader, specgram
from sound_source_separation.noise_generator import white_noise

with wave.open("./CMU_ARCTIC/cmu_us_aew_arctic/wav/arctic_a0003.wav") as wav:
    speech_data = wave_loader.load_to_mono_array(wav)

    n_speech = wav.getnframes()
    n_noise_only = 40000
    noise_data = white_noise(n_sample=n_noise_only + n_speech)

    mixed_data = noise_data
    mixed_data[n_noise_only:] += speech_data

    wave_writer.write("sample_noise_mixed.wav",
                      mixed_data,
                      f_rate=wav.getframerate())

with wave.open("sample_noise_mixed.wav") as wav:
    wave_loader.play(wav)

with wave.open("sample_noise_mixed.wav") as wav:
    specgram.show_spectrogram(wav)
        audio_data += bytes(audio_array)

    def onPlaybackAudioFrameBeforeMixing(self, uid, type1, samples, bytesPerSample, channels, samplesPerSec, buffer1, renderTimeMs, avsync_type):
        pass


rtc = agorartc.createRtcEngineBridge()
eventHandler = MyRtcEngineEventHandler(rtc)
rtc.initEventHandler(eventHandler)
# Please input your APP ID here.
rtc.initialize("YOUR_APPID", None, agorartc.AREA_CODE_GLOB & 0xFFFFFFFF)
afo = MyAudioFrameObserver()
rtc.joinChannel("", "myChannelName", "", 0)
rtc.startPreview()
rtc.enableVideo()
agorartc.registerAudioFrameObserver(rtc, afo)
input()  # Press any key to come to an end.
agorartc.unregisterAudioFrameObserver(rtc, afo)

# Save the audio data into a wave file.
if channel != -1 and samplewidth != -1 and framerate != -1:
    print(">>>>>>>> Start saving audio data.")
    with wave.open("sound.wav", "wb") as f:
        f.setnchannels(channel)
        f.setsampwidth(samplewidth)
        f.setframerate(framerate)
        f.writeframesraw(audio_data)

rtc.leaveChannel()
rtc.release()
from scipy import signal
import pyaudio
import wave
import Queue
import time
from ctypes import *
C_lib = CDLL("./C_lib.so")#load DLL

q = Queue.Queue()
plt_q = Queue.Queue()
tx_q = q
rx_q = q

#get the raw data
filepath="/home/sjh/Desktop/FM/1.wav"
wf = wave.open(filepath,'rb')
params = wf.getparams()
nchannels,samplewidth,framerate,nframes = params[:4]
data = wf.readframes(nframes)
raw_data = np.fromstring(data,dtype=np.short)

#set the buffer size
frame_duration = 20e-3
sample_rate = params[2]
file_sample_num = params[3]
sdr_rate = 1.92e6
osr = int(sdr_rate/sample_rate)

frame_len = sample_rate * frame_duration
frame_num = np.int16(file_sample_num/frame_len)
frame_len = int(frame_len)
Example #57
0
import wave
import struct
import numpy as np

if __name__=='__main__':
    data_size=40000
    fname="test.wav"
    frate=11025.0 
    wav_file=wave.open(fname,'r')
    data=wav_file.readframes(data_size)
    wav_file.close()
    data=struct.unpack('{n}h'.format(n=data_size), data)
    data=np.array(data)

    w = np.fft.fft(data)
    freqs = np.fft.fftfreq(len(w))
    print(freqs.min(),freqs.max())
    # (-0.5, 0.499975)

    # Find the peak in the coefficients
    idx=np.argmax(np.abs(w)**2)
    freq=freqs[idx]
    freq_in_hertz=abs(freq*frate)
    print(freq_in_hertz)
    # 439.8975
Example #58
0
    def sound_meter(self):

        freq = 0.0
        FORMAT = pyaudio.paInt16
        CHANNELS = 2
        RATE = 44100
        CHUNK = 1024
        RECORD_SECONDS = 10
        WAVE_OUTPUT_FILENAME = "file.wav"
        chunk = 2048
        counter = 0

        audio = pyaudio.PyAudio()

        firebaseObject = firebase.FirebaseApplication(
            'https://pukaar-9478a.firebaseio.com/', None)

        # start Recording
        stream = audio.open(format=FORMAT,
                            channels=CHANNELS,
                            rate=RATE,
                            input=True,
                            frames_per_buffer=CHUNK)
        print("recording...")
        frames = []

        for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
            data = stream.read(CHUNK)
            frames.append(data)
        print("finished recording")

        # stop Recording
        stream.stop_stream()
        stream.close()
        audio.terminate()

        waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
        waveFile.setnchannels(CHANNELS)
        waveFile.setsampwidth(audio.get_sample_size(FORMAT))
        waveFile.setframerate(RATE)
        waveFile.writeframes(b''.join(frames))
        waveFile.close()

        # Decibels recognizer

        # open up a wave
        wf = wave.open('bird.wav', 'rb')
        swidth = wf.getsampwidth()
        RATE = wf.getframerate()
        window = np.blackman(chunk)

        # open stream
        p = pyaudio.PyAudio()
        stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                        channels=wf.getnchannels(),
                        rate=RATE,
                        output=True)

        # read some data
        data = wf.readframes(chunk)

        # play stream and find the frequency of each chunk
        while len(data) == chunk * swidth:
            # unpack the data and times by the hamming window
            indata = np.array(
                wave.struct.unpack("%dh" %
                                   (len(data) / swidth), data)) * window
            # Take the fft and square each value
            fftData = abs(np.fft.rfft(indata))**2
            # find the maximum
            which = fftData[1:].argmax() + 1
            # use quadratic interpolation around the max
            if which != len(fftData) - 1:
                y0, y1, y2 = np.log(fftData[which - 1:which + 2:])
                x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
                # find the frequency and output it
                thefreq = (which + x1) * RATE / chunk
                freq = thefreq
                if thefreq > 2500:
                    if counter == 0:
                        print("First millisecond")
                        counter = counter + 1
                    else:
                        result = firebaseObject.post('/sound', {
                            'decibels': thefreq,
                            'tag': "High frequency"
                        })
                        print(thefreq)
                        counter = counter + 1
                else:
                    counter = counter + 1
                    result = firebaseObject.post('/sound', {
                        'decibels': thefreq,
                        'tag': "Low frequency"
                    })

            else:
                thefreq = which * RATE / chunk
                result = firebaseObject.post('/sound', {'decibels': thefreq})
            # read some more data
            data = wf.readframes(chunk)
        if data:
            stream.write(data)
        stream.close()
        p.terminate()
Example #59
0
import numpy
import scipy.io.wavfile as wav
import sys
import wave

#Variable inputs to Silence Detection module
WindowSize = 5		#milliseconds
SilenceThreshold = 350 	#milliseconds

try:
	#Read wav file	

	(SamplingRate,Wavdata) = wav.read("../wav/" + sys.argv[1] );
	TotalSamples = len(Wavdata);

	Wavobj = wave.open("../wav/" + sys.argv[1],'r');
	BytesPerSample = Wavobj.getsampwidth();
	SamplesPerFrame = int((SamplingRate*WindowSize)/1000); 		#Samples per frame = (samples/second)*(seconds/frame)
	
	#Normalize data to range [-1,+1)
	
	if BytesPerSample == 1:
		Wavdata = (Wavdata-128)/128.0;
	elif BytesPerSample == 2:
		Wavdata = Wavdata/32768.0;
	elif BytesPerSample == 3:
		Wavdata = Wavdata/(2.0^23);
	elif BytesPerSample == 4:
		Wavdata = Wavdata/32768.0; 			#Why? Shouldn't the scaling factor be 2^32?
	
	
if len(sys.argv) < 2:
    print("Plays a wave file.\n\nUsage: %s filename.wav volume_level" %
          sys.argv[0])
    sys.exit(-1)

gain = int(sys.argv[2])
print("gain is = ", str(gain))
multiplier = math.pow(10, (gain / 20))
print("multiplier is = ", multiplier)

#wf = wave.open(sys.argv[1], 'rb')
music = AudioSegment.from_wav(sys.argv[1])  #testestest
music = music + gain
music.export("modified_song_by_setvolume.py.wav", "wav")
wf = wave.open("modified_song_by_setvolume.py.wav", 'rb')

# open stream to play audio through
output_stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                       channels=wf.getnchannels(),
                       rate=wf.getframerate(),
                       output=True)


def play():
    global write_data, music_avg, gain
    #write_data = [min(255, d+gain) for d in wf.readframes(num_frames)]
    write_data = [
        min(255, int(d * multiplier)) for d in wf.readframes(num_frames)
    ]
    #print("write data: ", str(write_data))