0

我正在尝试使用 Pysound 在 Python 中制作软件合成器。第一步是让 Pysound 输出 wav 文件。我还有一个 Tkinter 窗口供用户与 Pysound 交互。这是我的代码:

#import libs
from tkinter import *
from tkinter import ttk
from pysound import buffer
from pysound import soundfile
from pysound import oscillators
from pysound import envelopes
from playsound import playsound
from time import sleep

def save():
    data=""
    data2=""
    #create buffers
    params = buffer.BufferParams()
    buffer.create_buffer(params,value=0.0)

    #voice 1
    env1=envelopes.attack_decay(params, attack=params.t2s(int(A1.get())/10))
    if selectedWave1=="Sine":
        data = oscillators.sine_wave(params,frequency=400,amplitude=env)
    elif selectedWave1=="Saw":
        data = oscillators.saw_wave(params,frequency=400,amplitude=env)
    elif selectedWave1=="Square":
        data = oscillators.square_wave(params,frequency=400,amplitude=env)
    elif selectedWave1=="Noise":
        data = oscillators.noise(params,frequency=400,amplitude=env)

    #voice 2
    env2=envelopes.attack_decay(params, attack=params.t2s(int(A2.get())/10))
    if selectedWave2=="Sine":
        data2=oscillators.sine_wave(params,frequency=400,amplitude=env2)
    elif selectedWave2=="Saw":
        data2=oscillators.saw_wave(params,frequency=400,amplitude=env2)
    elif selectedWave2=="Square":
        data2=oscillators.square_wave(params,frequency=400,amplitude=env2)
    elif selectedWave2=="Noise":
        data2=oscillators.noise(params,frequency=400,amplitude=env2)
    
    #save files
    soundfile.save(params, 'voice2.wav', data)
    soundfile.save(params,'voice1.wav',data2)

#create win
win=Tk()
win.geometry("800x450") #set size
win.title("Synthetica") #set title
win.configure(bg="#010") #set background colour

#create string vars for components
selectedWave1=StringVar()
selectedWave2=StringVar()
A1=StringVar()
D1=StringVar()
A2=StringVar()
D2=StringVar()
currentPreset=StringVar()

#create components
lblVoice1=Label(win,font=("Trebuchet MS",10),text="Voice 1:",bg="#010",fg="#fff")
lblWave1=Label(win,font=("Trebuchet MS",10),text="Waveform:",bg="#010",fg="#fff")
lblA1=Label(win,font=("Trebuchet MS",10),text="Attack:",bg="#010",fg="#fff")
lblD1=Label(win,font=("Trebuchet MS",10),text="Decay:",bg="#010",fg="#fff")
comboWave1=ttk.Combobox(win,font=("Trebuchet MS",10),width=10,textvariable=selectedWave1)
spinA1=Spinbox(win, font=("Trebuchet MS",10),width=5,from_=0,to=255,textvariable=A1)
spinD1=Spinbox(win, font=("Trebuchet MS",10),width=5,from_=0,to=255,textvariable=D1)

lblVoice2=Label(win,font=("Trebuchet MS",10),text="Voice 2:",bg="#010",fg="#fff")
lblWave2=Label(win,font=("Trebuchet MS",10),text="Waveform:",bg="#010",fg="#fff")
lblA2=Label(win,font=("Trebuchet MS",10),text="Attack:",bg="#010",fg="#fff")
lblD2=Label(win,font=("Trebuchet MS",10),text="Decay:",bg="#010",fg="#fff")
comboWave2=ttk.Combobox(win,font=("Trebuchet MS",10),width=10,textvariable=selectedWave2)
spinA2=Spinbox(win, font=("Trebuchet MS",10),width=5,from_=0,to=255,textvariable=A2)
spinD2=Spinbox(win, font=("Trebuchet MS",10),width=5,from_=0,to=255,textvariable=D2)

lblPreset=Label(win,font=("Trebuchet MS",10),text="Current Preset:",bg="#010",fg="#fff")
comboPreset=ttk.Combobox(win,font=("Trebuchet MS",10),width=10,textvariable=currentPreset)
txtPresetName=Entry(win,font=("Trebuchet MS",10),bg="#fff",fg="#010")
btnSavePreset=Button(win,font=("Trebuchet MS",10),text="Save New Preset",bg="#fff",fg="#010",command=save)

#place components
lblVoice1.place(x=10,y=10)
lblWave1.place(x=10,y=40)
lblA1.place(x=10,y=70)
lblD1.place(x=10,y=100)
comboWave1.place(x=100,y=40)
spinA1.place(x=100,y=70)
spinD1.place(x=100,y=100)

lblVoice2.place(x=200,y=10)
lblWave2.place(x=200,y=40)
lblA2.place(x=200,y=70)
lblD2.place(x=200,y=100)
comboWave2.place(x=300,y=40)
spinA2.place(x=300,y=70)
spinD2.place(x=300,y=100)

lblPreset.place(x=500,y=40)
comboPreset.place(x=600,y=40)
txtPresetName.place(x=500,y=100)
btnSavePreset.place(x=500,y=130)

#set options for comboboxes
comboWave1["values"]="Sine","Saw","Square","Noise"
comboWave2["values"]="Sine","Saw","Square","Noise"

win.mainloop()

它为语音 1 和语音 2 输出 0 秒的 wav 文件,我不知道为什么。

我收到这些错误消息

synthetica-C1.py:41: DeprecationWarning: tostring() is deprecated. Use tobytes() instead.
  soundfile.save(params, 'voice2.wav', data)
synthetica-C1.py:42: DeprecationWarning: tostring() is deprecated. Use tobytes() instead.
  soundfile.save(params,'voice1.wav',data2)

但我之前得到了相同的,声音文件很好。一旦我将下面的部分更改为使用 if 语句实际获取波形类型的用户输入,它就会停止工作。在此之前,用户只能控制攻击包络。

为什么它输出一个空文件?

4

0 回答 0