I am trying to create a program that converts text to Morse code and has a playback sound option. Issue seems to be that the program finishes correctly, reading exit code 0 but no sound is played. I have looked into other posts on this but they all seem to be for Windows, I am running Mac OS. Code is as follows, (sorry if it's a bit to long)
import pyaudio
from pydub import AudioSegment
from pydub.playback import play
MORSE_CODE_DICT = { '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':'--..',
'1':'.----', '2':'..---', '3':'...--',
'4':'....-', '5':'.....', '6':'-....',
'7':'--...', '8':'---..', '9':'----.',
'0':'-----', ', ':'--..--', '.':'.-.-.-',
'?':'..--..', '/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-'}
# Getting Pydub/PyAudio to edit length of sound
m_audio = AudioSegment.from_wav("Asine.wav")
long_burst = 0.40 * 1000
short_burst = 0.20 * 1000
longSine = m_audio[:long_burst]
shortSine = m_audio[:short_burst]
# Using a dictionary to store different sound lengths
sineSounds = {
"-":longSine,
".":shortSine,
}
# Function to create morse code output as string
def encrypt(message):
cipher = ''
for letter in message:
if letter != ' ':
cipher += MORSE_CODE_DICT[letter] + ' '
else:
cipher += ' '
return cipher
# Function to play morse code as sound
def play_sound(message):
for symbol in message:
if symbol == "-":
play(sineSounds['-'])
if symbol == ".":
play(sineSounds['.'])
# Main function to call both previous functions with prompts
def main():
message = input("What word do you want to encode?: ").upper()
print(encrypt(message))
sound = input("Play Morse Code? ").upper()
if sound == "y" or "yes":
play_sound(message)
main()
I have double checked to make sure the cipher function is outputting a string and that the play_sound function works on its own, ie using a morse code string for the "message" argument and that sound is produced. Not to sure why when it is placed inside the Main() it doesnt produce sound.