2017-10-06 69 views
0

我想在輸出DTMF信號聲音時忽略用戶在字符串中輸入的特殊字符。Python 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)

音頻(sound_out,率= FS)

回答

0

創建列表可接受的字符,例如

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] 

並在你的代碼中實現一個檢查每個字符是否不在這個列表中的條件。如果角色不在這個列表中,你可以確定它在你的輸出中是不需要的。