![](/img/trans.png)
[英]In Python, how do I read an input file to then translate it to morse code?
[英]How do i edit/create a wave file for morse code in python?
我正在尝试制作一个 python 莫尔斯电码翻译器,它接收一个字符串并返回一个包含莫尔斯电码的 wav 文件。
现在我的问题是,如何从文本编辑或创建 wav 文件?
我想做这样的事情(不是真正的代码):
def morse_to_wav(morse):
for char in morse:
if char == '.':
wavfile add "short.wav" ## just adds the sound to an existing wav file
if char == '-':
wavfile add "long.wav" ## same here
if char == ' ':
wavfile add "pause.wav" ## same here
你知道我能做什么吗? 或任何其他解决方案?
Soooo,我找到了解决我的问题的方法:我混合了我发现的所有东西,这就是我得到的:感谢@Sembei Norimaki @Mathhew Walker(我知道如何标记人)我使用了这篇文章中的代码(来自 Mathhew Walker)基本上,我读了我的音频文件,把它们放在一个列表中,连接起来,然后使用 write 来制作一个 wav 文件。 它不是很漂亮,但它有效
from scipy.io.wavfile import write
import wave, numpy
def get_npaudiodata(audiofile):
# Read file to get buffer
ifile = wave.open(audiofile)
samples = ifile.getnframes()
audio = ifile.readframes(samples)
# Convert buffer to float32 using NumPy
audio_as_np_int16 = numpy.frombuffer(audio, dtype=numpy.int16)
audio_as_np_float32 = audio_as_np_int16.astype(numpy.float32)
# Normalise float32 array so that values are between -1.0 and +1.0
max_int16 = 2**15
audio_normalised = audio_as_np_float32 / max_int16
return audio_normalised
audio_short = get_npaudiodata("short.wav")
audio_long = get_npaudiodata("long.wav")
audio_pause = get_npaudiodata("pause.wav")
nlist = [audio_short,audio_short,audio_short,audio_long,audio_long,audio_long,audio_short,audio_short,audio_short] #Thats SOS in morse
nparray = numpy.concatenate(nlist, axis=None)
write("example.wav", 10000, nparray)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.