Guest User

Untitled

a guest
Dec 13th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. import pyaudio
  2. import math
  3. import string
  4.  
  5. PyAudio = pyaudio.PyAudio
  6. p = PyAudio()
  7.  
  8. morse_code = {'A': '.-', 'B': '-...', 'C': '-.-.',
  9. 'D': '-..', 'E': '.', 'F': '..-.',
  10. 'G': '--.', 'H': '....', 'I': '..',
  11. 'J': '.---', 'K': '-.-', 'L': '.-..',
  12. 'M': '--', 'N': '-.', 'O': '---',
  13. 'P': '.--.', 'Q': '--.-', 'R': '.-.',
  14. 'S': '...', 'T': '-', 'U': '..-',
  15. 'V': '...-', 'W': '.--', 'X': '-..-',
  16. 'Y': '-.--', 'Z': '--..',
  17.  
  18. '0': '-----', '1': '.----', '2': '..---',
  19. '3': '...--', '4': '....-', '5': '.....',
  20. '6': '-....', '7': '--...', '8': '---..',
  21. '9': '----.'
  22. }
  23.  
  24. def string_to_morse(str):
  25. strng = string.upper(str)
  26. morse = ""
  27. permitted_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
  28.  
  29. for char in strng:
  30. if char in permitted_chars:
  31. morse += morse_code[char] + " "
  32.  
  33. return morse[:-1]
  34.  
  35. def play_tone(frequency, offset, length, bit_rate, tone_list = [True], *args):
  36.  
  37. if frequency > bit_rate:
  38. bit_rate = frequency + 100
  39.  
  40. tone_lists_list = [tone_list]
  41.  
  42. for lst in args:
  43. tone_lists_list.append(lst)
  44.  
  45. highest = 0
  46.  
  47. for list in tone_lists_list:
  48. if len(list) > highest:
  49. highest = len(list)
  50.  
  51. for list in tone_lists_list:
  52. while len(list) < highest:
  53. list.append(False)
  54.  
  55. frame_num = int(bit_rate * length)
  56. rest_frames = frame_num / bit_rate
  57. divider = float(frame_num) / highest
  58. wave_data = ""
  59.  
  60. for x in range(frame_num):
  61. print str(int(math.ceil(x / divider) -1 )) + " | " + str(highest)
  62. value = 0
  63.  
  64. for index, val_list in enumerate(tone_lists_list):
  65. if val_list[int(math.ceil(x / divider) -1 )]:
  66. value += int(math.sin(x/((bit_rate/(frequency + index * offset))/math.pi))*50)
  67.  
  68.  
  69. wave_data += chr(value + 128)
  70.  
  71. for x in range(rest_frames):
  72. wave_data = wave_data + chr(128)
  73.  
  74.  
  75. stream = p.open(format = p.get_format_from_width(1),
  76. channels = 1,
  77. rate = bit_rate,
  78. output = True)
  79. stream.write(wave_data)
  80. stream.stop_stream()
  81. stream.close()
  82.  
  83. def string_to_tone(frequency, offset, dit_length, strng, *args):
  84.  
  85. morse_list = [string_to_morse(strng)]
  86.  
  87. for other_strng in args:
  88. morse_list.append(string_to_morse(other_strng))
  89.  
  90. time = 0
  91. out_list = []
  92.  
  93. for morse in morse_list:
  94. tone_list = []
  95. own_time = 0
  96.  
  97. for char in morse:
  98. if char == ".":
  99. tone_list.append(True)
  100. tone_list.append(False)
  101. own_time += dit_length
  102. elif char == "-":
  103. tone_list.append(True)
  104. tone_list.append(True)
  105. tone_list.append(True)
  106. tone_list.append(False)
  107. own_time += dit_length * 2
  108. else:
  109. tone_list.append(False)
  110. tone_list.append(False)
  111. tone_list.append(False)
  112. tone_list.append(False)
  113. tone_list.append(False)
  114. tone_list.append(False)
  115. tone_list.append(False)
  116. own_time += dit_length * 7
  117.  
  118. out_list.append(tone_list)
  119.  
  120. if own_time > time:
  121. time = own_time
  122.  
  123. play_tone(frequency, offset, time, 16000, out_list[0], *out_list[1:])
  124.  
  125.  
  126. string_to_tone(1000, 500, 0.05, raw_input("Text #1 to encode: "), raw_input("Text #2 to encode: "))
  127. p.terminate()
Add Comment
Please, Sign In to add comment