Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- 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 = 8
- 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):
- symbols = ['[NULL]', '!', "'", '#', '[SQUARE]',
- '%', '&', '"', '(', ')',
- '*', '+', ',', '-', '.',
- '/', '0', '1', '2', '3',
- '4', '5', '6', '7', '8',
- '9',
- '[unk]', '[unk]', '[unk]', '[unk]', '[unk]', '[unk]',
- ' ', 'A', 'B', 'C',
- 'D', 'E', 'F', 'G', 'H',
- 'I', 'J', 'K', 'L', 'M',
- 'N', 'O', 'P', 'Q', 'R',
- 'S', 'T', 'U', 'V', 'W',
- 'X', 'Y', 'Z', '[', '[CR]',
- ']', '[ESC]', '[LF]', 'Å', 'Α',
- 'Β', 'Ψ', 'Δ', 'Ε', 'Φ',
- 'Γ', 'Η', 'Ι', 'Ξ', 'Κ',
- 'Λ', 'Μ', 'Ν', 'Ο', 'Π',
- 'Æ', 'Ρ', 'Σ', 'Τ', 'Θ',
- 'Ω', 'Œ', 'Χ', 'Υ', 'Ζ',
- 'ß', 'Ç', 'Ø', 'Đ', 'þ',
- 'Ъ', 'А', 'Б', 'Ц', 'Д',
- 'Е', 'Ф', 'Г', 'Ч', 'И',
- 'Й', 'К', 'Л', 'М', 'Н',
- 'О', 'П', 'Я', 'Р', 'С',
- 'Т', 'У', 'В', 'Ш', 'Х',
- 'Ы', 'З', 'Ю', 'Ж', 'Э']
- result = []
- for c in text:
- if c in symbols:
- i = bin(symbols.index(c))[2:]
- i = ('0' * (7 - len(i))) + i
- result.append(i)
- else:
- return -1
- return result
- result = []
- for c in text:
- #c1 = bin(ord(c) + 32)[2:][::-1]
- #c1 = ('1' + bin(ord(c))[2:])[::-1]
- c1 = (bin(ord(c))[2:])[::-1] + '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 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 abism_output_data
- 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
- #main()
- f = 'Vocaroo_s1U78CHfUpXi.wav'
- handle = open_wav_file(f)
- abin = read_abism_wav_file(handle)
- print convert_from_abism_bin(abin)
Advertisement
Add Comment
Please, Sign In to add comment