Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyaudio
- import math
- import string
- PyAudio = pyaudio.PyAudio
- p = PyAudio()
- morse_code = {'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': '--..',
- '0': '-----', '1': '.----', '2': '..---',
- '3': '...--', '4': '....-', '5': '.....',
- '6': '-....', '7': '--...', '8': '---..',
- '9': '----.'
- }
- def string_to_morse(str):
- strng = string.upper(str)
- morse = ""
- permitted_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
- for char in strng:
- if char in permitted_chars:
- morse += morse_code[char] + " "
- return morse[:-1]
- def play_tone(frequency, offset, length, bit_rate, tone_list = [True], *args):
- if frequency > bit_rate:
- bit_rate = frequency + 100
- tone_lists_list = [tone_list]
- for lst in args:
- tone_lists_list.append(lst)
- highest = 0
- for list in tone_lists_list:
- if len(list) > highest:
- highest = len(list)
- for list in tone_lists_list:
- while len(list) < highest:
- list.append(False)
- frame_num = int(bit_rate * length)
- rest_frames = frame_num / bit_rate
- divider = float(frame_num) / highest
- wave_data = ""
- for x in range(frame_num):
- print str(int(math.ceil(x / divider) -1 )) + " | " + str(highest)
- value = 0
- for index, val_list in enumerate(tone_lists_list):
- if val_list[int(math.ceil(x / divider) -1 )]:
- value += int(math.sin(x/((bit_rate/(frequency + index * offset))/math.pi))*50)
- wave_data += chr(value + 128)
- for x in range(rest_frames):
- wave_data = wave_data + chr(128)
- stream = p.open(format = p.get_format_from_width(1),
- channels = 1,
- rate = bit_rate,
- output = True)
- stream.write(wave_data)
- stream.stop_stream()
- stream.close()
- def string_to_tone(frequency, offset, dit_length, strng, *args):
- morse_list = [string_to_morse(strng)]
- for other_strng in args:
- morse_list.append(string_to_morse(other_strng))
- time = 0
- out_list = []
- for morse in morse_list:
- tone_list = []
- own_time = 0
- for char in morse:
- if char == ".":
- tone_list.append(True)
- tone_list.append(False)
- own_time += dit_length
- elif char == "-":
- tone_list.append(True)
- tone_list.append(True)
- tone_list.append(True)
- tone_list.append(False)
- own_time += dit_length * 2
- else:
- tone_list.append(False)
- tone_list.append(False)
- tone_list.append(False)
- tone_list.append(False)
- tone_list.append(False)
- tone_list.append(False)
- tone_list.append(False)
- own_time += dit_length * 7
- out_list.append(tone_list)
- if own_time > time:
- time = own_time
- play_tone(frequency, offset, time, 16000, out_list[0], *out_list[1:])
- string_to_tone(1000, 500, 0.05, raw_input("Text #1 to encode: "), raw_input("Text #2 to encode: "))
- p.terminate()
Add Comment
Please, Sign In to add comment