Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math, wave, array, itertools, time, struct, msvcrt, sys
- import numpy, pyaudio
- FREQUENCIES = [900, 800, 700, 666, 600, 500, 400]
- def open_wav_file(path):
- wav_file = wave.open(path, 'rb')
- return {'file': wav_file,
- 'sample width': wav_file.getsampwidth(),
- 'channels': wav_file.getnchannels(),
- 'rate': wav_file.getframerate()}
- def open_pyaudio_stream(wav_handle, is_output = True):
- p = pyaudio.PyAudio()
- stream = p.open(format = p.get_format_from_width(wav_handle['sample width']),
- channels = wav_handle['channels'],
- rate = wav_handle['rate'],
- output = is_output,
- input = not is_output)
- return [stream, p]
- def close_pyaudio_stream(stream, p):
- stream.stop_stream()
- stream.close()
- p.terminate()
- def read_wav_file(chunk_size, wav_handle):
- wav_file = wav_handle['file']
- sample_width = wav_handle['sample width']
- frame_rate = wav_handle['rate']
- data = wav_file.readframes(chunk_size)
- while len(data) == chunk_size * sample_width:
- yield data
- data = wav_file.readframes(chunk_size)
- if data:
- yield data
- def write_wav_file(path, wav_handle, frames):
- wav_file = wave.open(path, 'wb')
- wav_file.setnchannels(wav_handle['channels'])
- wav_file.setsampwidth(2)
- wav_file.setframerate(wav_handle['rate'])
- wav_file.writeframes(b''.join(frames))
- wav_file.close()
- def play_wav_file(wav_handle):
- chunk_size = 2048
- stream, p = open_pyaudio_stream(wav_handle)
- for wav_data in read_wav_file(chunk_size, wav_handle):
- stream.write(wav_data)
- close_pyaudio_stream(stream, p)
- def record_wav_file(path):
- chunk_size = 1024
- wav_handle = {'channels': 1, 'sample width': 2, 'rate': 44100}
- stream, p = open_pyaudio_stream(wav_handle, is_output = False)
- frames = []
- while True:
- try:
- data = stream.read(chunk_size)
- frames.append(data)
- except KeyboardInterrupt:
- write_wav_file(path, wav_handle, frames)
- break
- close_pyaudio_stream(stream, p)
- def write_abism_wav_file(abism_bin, path):
- frames = array.array('h')
- volume = 100
- duration = 0.1
- rate = 44100
- samples = rate * duration
- wav_handle = {'channels': 1, 'sample width': 2, 'rate': 44100}
- for byte in abism_bin:
- for bit in range(7):
- if byte[bit] != '0':
- freq = FREQUENCIES[bit]
- for i in range(int(samples)):
- sample = 32767 * float(volume) / 100
- sample *= math.sin(math.pi * 2 * (i % int(rate / freq)) / int(rate / freq))
- frames.append(int(sample))
- write_wav_file(path, wav_handle, frames.tostring())
- def read_abism_wav_file(wav_handle):
- result = []
- chunk_size = 512
- sample_width = wav_handle['sample width']
- rate = wav_handle['rate']
- window = numpy.blackman(chunk_size)
- freq_old = -1
- byte = []
- for data in read_wav_file(chunk_size, wav_handle):
- if len(data) == chunk_size * sample_width:
- indata = numpy.array(wave.struct.unpack("%dh" % (len(data) / sample_width), data)) * window
- fftData = abs(numpy.fft.rfft(indata)) ** 2
- fft_max = fftData[1:].argmax() + 1
- if fft_max != len(fftData)-1:
- y0, y1, y2 = numpy.log(fftData[fft_max - 1:fft_max + 2:])
- x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
- freq_current = (fft_max + x1) * rate / chunk_size
- else:
- freq_current = fft_max * rate / chunk_size
- if not math.isnan(freq_current):
- freq_current = (round(freq_current, -1))
- if freq_current in range(640, 671):
- freq_current = 666
- else:
- freq_current = round(freq_current, -2)
- freq_current = int(freq_current)
- if freq_current in FREQUENCIES:
- if freq_old != freq_current:
- bit = FREQUENCIES.index(freq_current)
- byte.append(bit)
- if bit == 6:
- result.append(''.join([str(int(i in byte)) for i in range(7)]))
- byte = []
- starting_freq = 0
- freq_old = freq_current
- return result
- def convert_to_abism_bin(text):
- result = []
- for c in text:
- c1 = bin(ord(c) + 32)[2:][::-1]
- if len(c1) == 8:
- c1 = c1[1:]
- result.append(c1)
- return result
- def convert_from_abism_bin(abism_bin):
- result1 = ''
- result2 = ''
- result3 = ''
- for byte in abism_bin:
- result1 += chr(int(byte[::-1], 2) - 32)
- result2 += chr(int(byte[::-1], 2))
- result3 += chr(int(byte[:-1][::-1], 2))
- return result1#, result2, result3
- def main():
- agent_sys_output_recording = "agent sys output.wav"
- user_input_recording = "user input.wav"
- while True:
- try:
- print 'Recording agent system output. Press CTRL+C to stop recording.'
- record_wav_file(agent_sys_output_recording)
- wav_handle = open_wav_file(agent_sys_output_recording)
- abism_output_data = read_abism_wav_file(wav_handle)
- print 'Agent system output: ' + convert_from_abism_bin(abism_output_data)
- wav_handle['file'].close()
- user_input_text = raw_input("Your input to system: ")
- user_input_abism_bin = convert_to_abism_bin(user_input_text)
- write_abism_wav_file(user_input_abism_bin, user_input_recording)
- wav_handle = open_wav_file(user_input_recording)
- play_wav_file(wav_handle)
- wav_handle['file'].close()
- except KeyboardInterrupt:
- break
- f = "2013-03-03 20-07 UTC - 202-999-3335 - 17011.wav"
- wav_handle = open_wav_file(f)
- d = read_abism_wav_file(wav_handle)
- print convert_from_abism_bin(d)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement