2012-11-21 2 views
0

Pyo 및 WX 라이브러리를 기반으로하는 간단한 신호 생성기를 Python으로 작성합니다.Pyo 발진기 출력을 WX 이벤트에 바인딩

나는 각각에 대한 간단한 튜토리얼을 살펴보고 WX에서 WX 로의 버튼을 성공적으로 바인딩했다. 나는 이제 "오실레이터 1"이라고 표시된 버튼을 눌러 1 초 동안 간단한 사인파 (440Hz에서)를 생성하려고합니다. 그러나 main() 함수가 실행될 때 사인 톤이 재생되고 버튼이 wx 프레임에 표시되는 동안 사인 톤을 다시 트리거 할 수 없습니다. 이 두 가지 증상은 모두 원하지 않습니다.

사인 어조는 프로그램 실행 즉시 왜 재생됩니까? 왜 FirstOSC 버튼이 작동하지 않는 것입니까?

import wx 
from pyo import * 
import time 

pyoServer = Server().boot() 
pyoServer.start() 

class MainWindow(wx.Frame): 
    def __init__(self,parent,title): 
     wx.Frame.__init__(self,parent,title=title, size = (640,640)) 
     self.CreateStatusBar() # A StatusBar in the bottom of the window   

     # Signal Generator controls 
     oscillator = SoundOutput() 
     firstOSC = wx.Button(self, wx.ID_YES,"Oscillator 1 " + str(oscillator.str_osc1State)) 
     self.Bind(wx.EVT_BUTTON, oscillator.OnOff1(440), firstOSC) 

     #Menus 
     filemenu = wx.Menu() 
     menuExit = filemenu.Append(wx.ID_EXIT,"&Exit","Terminate the program") 
     menuBar = wx.MenuBar() 
     menuBar.Append(filemenu,"&File") 
     self.SetMenuBar(menuBar)  
     self.Bind(wx.EVT_MENU, self.OnExit, menuExit) 

     self.Show(True) 
    def OnExit(self,e): 
     self.Close(True) 


class SoundOutput(object): 
    def __init__(self): 
     self.osc1State = False 
     self.str_osc1State = "Off" 
     self.a = Sine(440, 0, 0.1)  
    def OnOff1(self, frequency): 
     self.a.freq = frequency 
     self.a.out() 
     time.sleep(1) 
     self.osc1State = True 

def Main(): 
    app = wx.App(False) 
    frame = MainWindow(None,"Signal Generator") 
    app.MainLoop() 

답변

1

WX가 이벤트를 처리하는 방법을 조사하여이를 해결했습니다. 어떤 이유로 클래스의 중첩되거나 분리 된 인스턴스에서 메서드를 호출하면 이벤트 대신 런타임에 톤이 재생됩니다. firstOSC에 대한 바인드 된 이벤트 핸들러 역할을하는 MainWindow 클래스에 대한 메소드를 작성하여이를 수정했습니다. 그런 다음이 메서드는 실제 발진기 클래스에 필요한 메서드를 호출합니다.

# Signal Generator controls 
    self.fOscillator = SoundOutput() 
    self.fOscillatorstatus = False 
    self.firstOSC = wx.Button(self, wx.ID_ANY,"Oscillator 1 On") 
    self.firstOSC.Bind(wx.EVT_BUTTON, self.OnFirstOSC) 

    def OnFirstOSC(self,e): 
    if not self.fOscillatorstatus: 
     self.fOscillator.OnOff1(440) 
     self.fOscillatorstatus = True 
     self.firstOSC.SetLabel("Oscillator 1 Off") 
    elif self.fOscillatorstatus: 
     self.fOscillator.OnOff1(0) 
     self.firstOSC.SetLabel("Oscillator 1 On") 
     self.fOscillatorstatus = False 
: 여기

는 새로운 코드
관련 문제