Python implements the mos code translation implementation method with sound, python MOS

Source: Internet
Author: User

Python implements the mos code translation implementation method with sound, python MOS

The example in this article describes how to implement the voice-carrying Moss translation program in python and share it with you for your reference. The specific analysis is as follows:

Here, you need to use PyGame to make a sound.

import pygameimport timeimport sysCODE = {'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': '----.'     }ONE_UNIT = 0.5THREE_UNITS = 3 * ONE_UNITSEVEN_UNITS = 7 * ONE_UNITPATH = 'morse_sound_files/'def verify(string):  keys = CODE.keys()  for char in string:    if char.upper() not in keys and char != ' ':      sys.exit('Error the charcter ' + char + ' cannot be translated to Morse Code')def main():  print 'Welcome to Alphabet to Morse Code Translator v.01\n'  msg = raw_input('Enter Message: ')  verify(msg)  print  pygame.init()  for char in msg:    if char == ' ':      print ' '*7,      time.sleep(SEVEN_UNITS)    else:      print CODE[char.upper()],      pygame.mixer.music.load(PATH + char.upper() + '_morse_code.ogg')      pygame.mixer.music.play()      time.sleep(THREE_UNITS)if __name__ == "__main__":  main()

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.