【问题标题】:Python DTMF Encoder - Ignore Special CharactersPython DTMF 编码器 - 忽略特殊字符
【发布时间】:2017-10-06 23:41:44
【问题描述】:

我想忽略用户在字符串中输入的特殊字符,同时输出 DTMF 信号声音。

基本上,我正在构建一个 DTMF 编码器,对于电话键盘的每次按键,系统都会结合行音和列音并发送两者。

例如:“12 34aB-cd$%^&76”应输出“1234abcd76”的音调,忽略手机键盘上未找到的空格和所有其他字符。有没有更快和/或更简单的方法来做到这一点?

代码:

userInput = "12  34aB-cd$%^&76"

length = len(userInput)

sound = []

index = 0

time = 0.3
delayTime = 0.1

Fs = 8000

runningTime = np.linspace(0,time,time*Fs+1)
time2 = np.linspace(0, delayTime, delayTime*Fs+1)

lofreq = 0
hifreq = 0
totalfreq = 0

delay = np.sin(2*np.pi*20000*delayTime)

while index < length: 

if userInput[index] == 1:
    lofreq = np.sin(2*np.pi*697*runningTime) 
    hifreq = np.sin(2*np.pi*1209*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == 2:
    lofreq = np.sin(2*np.pi*697*runningTime) 
    hifreq = np.sin(2*np.pi*1336*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == 3:
    lofreq = np.sin(2*np.pi*697*runningTime) 
    hifreq = np.sin(2*np.pi*1447*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == 'A' or 'a':
    lofreq = np.sin(2*np.pi*697*runningTime) 
    hifreq = np.sin(2*np.pi*1633*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == 4:
    lofreq = np.sin(2*np.pi*770*runningTime) 
    hifreq = np.sin(2*np.pi*1209*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == 5:
    lofreq = np.sin(2*np.pi*770*runningTime) 
    hifreq = np.sin(2*np.pi*1336*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == 6:
    lofreq = np.sin(2*np.pi*770*runningTime) 
    hifreq = np.sin(2*np.pi*1447*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == 'B' or 'b':
    lofreq = np.sin(2*np.pi*770*runningTime) 
    hifreq = np.sin(2*np.pi*1633*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == 7:
    lofreq = np.sin(2*np.pi*852*runningTime) 
    hifreq = np.sin(2*np.pi*1209*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == 8:
    lofreq = np.sin(2*np.pi*852*runningTime) 
    hifreq = np.sin(2*np.pi*1336*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == 9:
    lofreq = np.sin(2*np.pi*852*runningTime) 
    hifreq = np.sin(2*np.pi*1477*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == 'C' or 'c':
    lofreq = np.sin(2*np.pi*852*runningTime) 
    hifreq = np.sin(2*np.pi*1633*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == '*':
    lofreq = np.sin(2*np.pi*941*runningTime) 
    hifreq = np.sin(2*np.pi*1209*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == 0:
    lofreq = np.sin(2*np.pi*941*runningTime) 
    hifreq = np.sin(2*np.pi*1336*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == '#':
    lofreq = np.sin(2*np.pi*941*runningTime) 
    hifreq = np.sin(2*np.pi*1447*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

elif userInput[index] == 'D' or 'd':
    lofreq = np.sin(2*np.pi*941*runningTime) 
    hifreq = np.sin(2*np.pi*1633*runningTime)
    totalfreq = lofreq + hifreq
    sound.append(lofreq + hifreq)
    sound.append(delay)

index = index + 1

sound_out = np.concatenate(声音)

打印(声音输出)

音频(sound_out,rate = Fs)

【问题讨论】:

    标签: python audio dtmf


    【解决方案1】:

    创建一个可接受的字符列表,例如

    chars = [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]
    

    并在您的代码中实现一个条件,检查每个字符是否不在此列表中。如果该字符不在此列表中,您可以确定它在您的输出中是不需要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-25
      • 2013-12-11
      • 2023-03-22
      • 1970-01-01
      • 2016-10-19
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多