gpt4 book ai didi

Ruby:生成摩尔斯电码声音文件

转载 作者:太空宇宙 更新时间:2023-11-03 16:06:05 25 4
gpt4 key购买 nike

我正在训练用耳朵阅读摩尔斯电码 ;)

为了训练自己,我编写了一个小的 ruby​​ 脚本来使用命令行 sox 生成声音文件(见下文)。它基本上只需要预先制作的 ditdah 和多个空格(在 ditdah 之间)的声音文件>s,字母之间和单词之间)并将它们粘合在一起。它可以工作,但随着目标文件变大,速度会越来越慢。

是否有更智能的方法将 ruby​​ 的声音生成为 wav 文件?


#!/usr/bin/ruby

wordlength = 5
blocklength = 12
testlength = 5
alphabet = [ "G", "A", "S", "5", "D", "0", "R", "/", "2", "L", "9", "T", "V", "I", "O", "Q", "C", "4", "F", "X", "E", "N", "U" ]
playstring = ""
block = ""
page = ""

MORSE = {
"!" => "---.", "\"" => ".-..-.", "$" => "...-..-", "'" => ".----.",
"(" => "-.--.", ")" => "-.--.-", "+" => ".-.-.", "," => "--..--",
"-" => "-....-", "." => ".-.-.-", "/" => "-..-.", "0" => "-----",
"1" => ".----", "2" => "..---", "3" => "...--", "4" => "....-", "5" => ".....",
"6" => "-....", "7" => "--...", "8" => "---..", "9" => "----.", ":" => "---...",
";" => "-.-.-.", "=" => "-...-", "?" => "..--..", "@" => ".--.-.", "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" => "--..",
"[" => "-.--.", "]" => "-.--.-", "_" => "..--.-",
"ka" => "-.-.-.",
}

startblock = "dit.wav ditspace.wav dit.wav ditspace.wav dit.wav ditspace.wav dah.wav wordspace.wav dit.wav ditspace.wav dit.wav ditspace.wav dit.wav ditspace.wav dah.wav wordspace.wav dit.wav ditspace.wav dit.wav ditspace.wav dit.wav ditspace.wav dah.wav wordspace.wav dah.wav ditspace.wav dit.wav ditspace.wav dah.wav ditspace.wav dit.wav ditspace.wav dah.wav wordspace.wav wordspace.wav wordspace.wav "
endblock = "dit.wav ditspace.wav dah.wav ditspace.wav dit.wav ditspace.wav dah.wav ditspace.wav dit.wav blockspace.wav "

playstring << "uebungsanfang.wav wordspace.wav "
system ('/opt/local/bin/sox ' + playstring.to_s + ' tmp.wav 2>/dev/null')
system ('mv tmp.wav tmp2.wav')
playstring = ""

testlength.times do |line|
printf line.to_s + " "
playstring << "gruppe" + (line+1).to_s + ".wav wordspace.wav "
playstring << startblock
system ('/opt/local/bin/sox tmp2.wav ' + playstring.to_s + ' tmp.wav 2>/dev/null')
system ('mv tmp.wav tmp2.wav')
playstring = ""
blocklength.times do |count|
word = alphabet.sample.to_s + alphabet.sample.to_s + alphabet.sample.to_s + alphabet.sample.to_s + alphabet.sample.to_s
word.each_char do |char|
MORSE[char].each_char do |dahdit|
case dahdit
when '.' then playstring << "dit.wav "
when '-' then playstring << "dah.wav "
end
playstring << "ditspace.wav "
end
playstring << "letterspace.wav "
system ('/opt/local/bin/sox tmp2.wav ' + playstring.to_s + ' tmp.wav 2>/dev/null')
system ('mv tmp.wav tmp2.wav')
playstring = ""
end
playstring << "wordspace.wav "
block << word.to_s + " "
end
playstring << endblock
playstring << "blockspace.wav "
system ('/opt/local/bin/sox tmp2.wav ' + playstring.to_s + ' tmp.wav') #2>/dev/null
system ('mv tmp.wav tmp2.wav')
playstring = ""
page << "%02d" %(line+1).to_s + ": VVV <ak> " + block + "+" + "\n"
block = ""
end
playstring << "uebungsende.wav"
system ('/opt/local/bin/sox tmp2.wav ' + playstring.to_s + ' tmp.wav 2>/dev/null') # &
system ('rm tmp2.wav')
system ('mv tmp.wav cw.wav')
system ('lame --cbr -b 128 cw.wav cw.mp3')
system ('rm cw.wav')
puts
puts page

声音文件是使用 sox 预先生成的:

sox -e floating-point -r 22500 -n -t wav - synth 0.3 sin 0 > letterspace.wav

包含 Übungsende 等(测试开始、测试结束、第一组、第二组...)的文件是使用 MAc OS X 命令 say 生成的:

say -v Steffi --file-format=WAVE --data-format=LEF32@22500 -o uebungsanfang.wav "Anfang der Übung."

我已经在此处上传了文件(直到“Gruppe 5”,即“group 5”):https://docs.google.com/open?id=0B4QwMfBGRCjVbE5iZHpHMHBKT3c

最佳答案

我不懂 ruby​​,所以我不太确定我知道你在做什么,但看起来你正在生成表示点、破折号、空格等的音频文件,然后使用 sox 连接原始数据从这些文件到一个新文件,然后使用 lame 将其转换为 mp3。

总而言之,这是非常低效的,主要是因为您对可以即时生成的原始文件做了大量工作。也就是说,它可能不值得以最有效的方式重写——毕竟完成所有文件 IO 会使您的代码变得简单。所以我会重写代码,使文件 IO 尽可能快。在这种情况下,不得不一遍又一遍地打开和解析 WAV 文件,尤其是 32 位浮点 WAV 文件,可能会减慢您的速度。对于你正在做的事情,你绝对不需要那个。我将从使用 16 位整数 WAV 文件开始。

为了获得更快的速度,请尝试使用更简单的格式作为中间体,例如 au (如果足够好则使用 u-law,如果不够好则使用 16 位线性)。您也可以尝试“原始”文件,也就是“ headless ”文件——只需告诉 sox 和 lame 采样率、位深度、字节顺序和要使用的样本数。然后你不需要使用 sox 来连接原始数据,你可以只使用“cat”unix 工具,它应该更快一点。

关于Ruby:生成摩尔斯电码声音文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13612773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com