如何消除点击的在我的音调发生器?Python音调发生器

cl25kdpy  于 2023-04-28  发布在  Python
关注(0)|答案(1)|浏览(120)

当频率改变时,有时会听到咔嗒声。我试着用几种方法来消除卡嗒声。例如,仅当音量超过一次达到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())

我认为这个问题很难解决。有人能帮我解决这个问题吗?

oalqel3c

oalqel3c1#

我是如何修复它的:

phase = 0

def playNext(notused, stream, len):
    global nframes, phase
    phase = phase%(2 * math.pi)
    delta_phase = 2.0 * math.pi * basefreq / 44100
    for i in range(0, len, 4):
        phase += delta_phase
        left = int(math.sin(phase) * 32000)
        # other same as before

相关问题