Mac系统下Python文本转摩尔斯电码程序无音频输出(退出码0)
问题描述
我开发了一款将文本转换为摩尔斯电码并支持音频播放的Python程序,在Mac OS系统运行时,程序正常结束(返回退出码0)但没有声音输出。我已确认加密函数输出正常,单独调用play_sound函数传入摩尔斯电码字符串能正常发声,但整合到main函数后无法播放声音,此前查阅的相关解决方案多针对Windows系统。
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()
解决方案
你的代码存在两个核心问题:
- 参数传递错误:
main函数里调用play_sound时传入的是原始输入文本(比如"HELLO"),但play_sound需要遍历摩尔斯符号(.和-)来播放声音,传原始文本进去自然找不到对应符号,不会触发任何播放逻辑。 - 逻辑判断不严谨:
if sound == "y" or "yes"的写法错误,这个条件永远为真,正确的判断应该是检查输入是否在指定的同意选项中。
修复后的代码如下:
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, " ": AudioSegment.silent(duration=short_burst) # 添加空格对应的静音段,符合摩尔斯电码间隔规范 } # 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(morse_code): for symbol in morse_code: if symbol in sineSounds: play(sineSounds[symbol]) # Main function to call both previous functions with prompts def main(): message = input("What word do you want to encode?: ").upper() morse_code = encrypt(message) print(morse_code) sound = input("Play Morse Code? ").upper() if sound in ("Y", "YES"): play_sound(morse_code) if __name__ == "__main__": main()
修复要点说明
- 将加密后的
morse_code传入play_sound函数,确保播放的是摩尔斯符号序列 - 修正判断逻辑为
sound in ("Y", "YES"),避免无效判断 - 添加空格对应的静音段,让摩尔斯电码播放更符合规范
- 使用
if __name__ == "__main__":规范程序入口,避免模块导入时意外执行
如果Mac上仍存在声音播放问题,可尝试以下额外措施:
- 确认
Asine.wav文件路径正确,或改用绝对路径 - 更新依赖库:
pip install --upgrade pydub pyaudio - 若
pydub的play函数仍有问题,可替换为simpleaudio库(pip install simpleaudio)并修改播放逻辑
内容的提问来源于stack exchange,提问作者Rmarsh89
相关产品推荐
相关产品推荐

