如何消除Python音调发生器中的音频点击声?
音调发生器频率切换时的点击声问题求助
我开发的Python音调发生器在频率切换时偶尔会出现点击声。尝试过多种消除点击声的方法:仅在音量多次降至0时切换频率,或是精确计算单周期结束的时机,但这些方法都没有效果。恳请各位提供技术帮助。
以下是我的实现代码:
import sys import sdl2 import sdl2.ext import math import struct import ctypes basefreq = 110 nframes = 0 tab = [1] left=0 m= [0] x=[0] x2=[0] x3=[11025] x4=[0] i2=[1] x5=[0] @ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.POINTER(sdl2.Uint8), ctypes.c_int) def playNext(notused, stream, len): global nframes for i in range(0, len, 4): t = (nframes + i) / 44100 i2[0] = i2[0] + 1 t4 = (nframes + i2[0]) / 44100 left2 = math.sin(2 * math.pi * t * (basefreq + int(tab[0]))) left = int(math.sin(2 * math.pi * t * (basefreq + int(tab[0]) )) * 32000) right = int(math.sin(2 * math.pi * t * (basefreq + 1)) * 32000) #print(t*100000) x3[0]=x3[0]+1 t2 = 1 / (basefreq + tab[0]) d2 = i / (44100 * t2) t3 = x3[0] / (basefreq + tab[0]) d3 = i / (44100 * t3) width = 344/(basefreq + tab[0]) if (int(t * 100000) % 100000 == 0): #print(t, " : ", i, " : ", x3[0], " : ", t3," : ", t2," : ",basefreq + tab[0]) x4[0]=0 x3[0] = 0 if (int(d2 * 10) % 2 == 0): g = x2[0] x2[0] = 0 dd = g / (44100 * t2) # print(left, " : ", left) x2[0] = x2[0] + 1 e = dd * 44100 x4[0]=x4[0]+1 #print(x4[0]) #if(int(left)*1000==0): #if ((left * 100 <= 0 or left * 100 <= 0) and (left * 100 < 500 or left * 100 > -500)): print(int(left2*100)) if (int(left2*100) ==0): if (x5[0] == 10): #if(int(t*10000)%int(t2*10000)==0): #if (int(d2 * 10) % 2 == 0 and (i / 100) % int(e / 100) == 0 ): #if(i*1000%int(t3*220*1000)==0): tab[0] =math.sin(x[0] / 100)*100+100 x[0]=x[0]+1 i2[0] = i2[0] - 1 x5[0]=0 else: x5[0]=x5[0]+1 if((nframes + i)%4==0): stream[i] = left & 0xff stream[i+1] = (left >> 8) & 0xff stream[i+2] = right & 0xff stream[i+3] = (right >> 8) & 0xff nframes += len def initAudio(): spec = sdl2.SDL_AudioSpec(0, 0, 0, 0) spec.callback = playNext spec.freq = 44100 spec.format = sdl2.AUDIO_S16SYS spec.channels = 2 spec.samples = 1024 devid = sdl2.SDL_OpenAudioDevice(None, 0, spec, None, 0) sdl2.SDL_PauseAudioDevice(devid, 0) def run(): global basefreq sdl2.SDL_Init(sdl2.SDL_INIT_AUDIO | sdl2.SDL_INIT_TIMER | sdl2.SDL_INIT_VIDEO) window = sdl2.ext.Window("Tone Generator", size=(800, 600)) window.show() running = True initAudio() while running: events = sdl2.ext.get_events() for event in events: if event.type == sdl2.SDL_QUIT: running = False break elif event.type == sdl2.SDL_KEYDOWN: if event.key.keysym.sym == sdl2.SDLK_UP: basefreq *= 2 elif event.key.keysym.sym == sdl2.SDLK_DOWN: basefreq /= 2 break sdl2.SDL_Delay(1) return 0 if __name__ == "__main__": sys.exit(run())
内容的提问来源于stack exchange,提问作者Zamaskowany
相关产品推荐
相关产品推荐

