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.