コード例 #1
0
ファイル: main.py プロジェクト: anton-k/tiny-synth
    def __init__(self, parent, id, title, state):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(420, 500))

        self.recent = state.get_recent()
        self.favs = state.get_favs()
        self.tuning_id = state.get_tuning_id()
        self.current_track = None
        self.state = state
        self.font_objects = FontObjects()

        self.player = Player()
        self.player.set_tuning(self.tuning_id)

        vbox = wx.BoxSizer(wx.VERTICAL)
        panel1 = wx.Panel(self, -1)
        panel2 = wx.Panel(self, -1)
        piano  = Piano(self, wx.ID_ANY, self.player)

        def on_note_on(evt):
            pch, vol = evt.GetValue()
            piano.key_on(pch)

        def on_note_off(evt):
            pch, vol = evt.GetValue()
            piano.key_off(pch)     

        self.midis = midi.MidiThread(self, self.player)
        self.midis.run()
        self.Bind(midi.EVT_NOTE_ON, on_note_on)
        self.Bind(midi.EVT_NOTE_OFF, on_note_off)

        self.Bind(wx.EVT_CLOSE, self.on_exit)

        p = panel1
        nb = wx.Notebook(p)

        # create the page windows as children of the notebook
        page1 = PageOne(nb, self)
        page2 = PageTwo(nb, self, config.RECENT)
        page3 = PageTwo(nb, self, config.FAVS)

        self.recent_panel = page2
        self.favs_panel   = page3
        self.current_tab  = 0


        # add the pages to the notebook with the label to show on the tab
        nb.AddPage(page1, "Library")
        nb.AddPage(page2, "Recent")
        nb.AddPage(page3, "Favorites")
        self.tabs = nb

        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.on_page_changed)

        # finally, put the notebook in a sizer for the panel to manage
        # the layout
        sizer = wx.BoxSizer()
        sizer.Add(nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

        self.display = wx.StaticText(panel2, -1, '',(10,10), style=wx.ALIGN_CENTRE)
        font = state.get_font()
        font.SetWeight(wx.FONTWEIGHT_BOLD)
        self.display.SetFont(font)
        self.font_objects.set_title(self.display)

        hbox = hor([panel1, panel2])

        self.ui_faders = {}

        def make_slider(name):
            def cbk(val):
                self.player.setValue(name, val)
            ui = slider(panel2, 0, cbk)            
            self.ui_faders[name] = ui
            text = staticText(panel2, name, font = self.state.get_font())
            self.font_objects.append_text(text)
            return ver([Cell(ui, border = 7), Cell(text, flags = wx.ALIGN_CENTRE, border = 7)])
        
        vbox2 = ver([staticText(panel2, ''), make_slider(config.VOLUME), make_slider(config.FX), make_slider(config.CUT_OFF), make_slider(config.RESONANCE)])
        panel2.SetSizer(vbox2)
        self.SetSizer(ver([Cell(hbox, scale = 3.8), Cell(piano, scale = 1)]))
        self.Centre()

        self.init_player_file()
        
        def set_tuning(n):
            def go(evt):
                self.tuning_id = n
                self.player.set_tuning(n)
            return go

        make_menu_bar(self, [
              menuItem('&File',[
                  normalItem('Add to favs', cbk = self.on_add_to_favs)
                , normalItem('Remove from favs', cbk = self.on_remove_frome_favs)
               # , normalItem('Settings')
                ])
            , menuItem('&Edit', [
                  normalItem('Enlarge font', cbk = self.on_font_up)
                , normalItem('Shrink  font', cbk = self.on_font_down)
                , menuItem('Tuning', [ radioItem(name, cbk = set_tuning(i), is_check = self.tuning_id == i) for i, name in enumerate(config.TUNINGS) ])
            ])  
            , menuItem('&Help',[                  
                normalItem('About', cbk = self.on_about)  
                ])])


        up_font_id = wx.NewId()
        down_font_id = wx.NewId()        
        self.Bind(wx.EVT_MENU, self.on_font_up, id=up_font_id)
        self.Bind(wx.EVT_MENU, self.on_font_down, id=down_font_id)
        self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL,  ord('='), up_font_id), (wx.ACCEL_CTRL,  ord('-'), down_font_id)])
        self.SetAcceleratorTable(self.accel_tbl)
コード例 #2
0
ファイル: main.py プロジェクト: anton-k/tiny-synth
class MyFrame(wx.Frame):
    def __init__(self, parent, id, title, state):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(420, 500))

        self.recent = state.get_recent()
        self.favs = state.get_favs()
        self.tuning_id = state.get_tuning_id()
        self.current_track = None
        self.state = state
        self.font_objects = FontObjects()

        self.player = Player()
        self.player.set_tuning(self.tuning_id)

        vbox = wx.BoxSizer(wx.VERTICAL)
        panel1 = wx.Panel(self, -1)
        panel2 = wx.Panel(self, -1)
        piano  = Piano(self, wx.ID_ANY, self.player)

        def on_note_on(evt):
            pch, vol = evt.GetValue()
            piano.key_on(pch)

        def on_note_off(evt):
            pch, vol = evt.GetValue()
            piano.key_off(pch)     

        self.midis = midi.MidiThread(self, self.player)
        self.midis.run()
        self.Bind(midi.EVT_NOTE_ON, on_note_on)
        self.Bind(midi.EVT_NOTE_OFF, on_note_off)

        self.Bind(wx.EVT_CLOSE, self.on_exit)

        p = panel1
        nb = wx.Notebook(p)

        # create the page windows as children of the notebook
        page1 = PageOne(nb, self)
        page2 = PageTwo(nb, self, config.RECENT)
        page3 = PageTwo(nb, self, config.FAVS)

        self.recent_panel = page2
        self.favs_panel   = page3
        self.current_tab  = 0


        # add the pages to the notebook with the label to show on the tab
        nb.AddPage(page1, "Library")
        nb.AddPage(page2, "Recent")
        nb.AddPage(page3, "Favorites")
        self.tabs = nb

        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.on_page_changed)

        # finally, put the notebook in a sizer for the panel to manage
        # the layout
        sizer = wx.BoxSizer()
        sizer.Add(nb, 1, wx.EXPAND)
        p.SetSizer(sizer)

        self.display = wx.StaticText(panel2, -1, '',(10,10), style=wx.ALIGN_CENTRE)
        font = state.get_font()
        font.SetWeight(wx.FONTWEIGHT_BOLD)
        self.display.SetFont(font)
        self.font_objects.set_title(self.display)

        hbox = hor([panel1, panel2])

        self.ui_faders = {}

        def make_slider(name):
            def cbk(val):
                self.player.setValue(name, val)
            ui = slider(panel2, 0, cbk)            
            self.ui_faders[name] = ui
            text = staticText(panel2, name, font = self.state.get_font())
            self.font_objects.append_text(text)
            return ver([Cell(ui, border = 7), Cell(text, flags = wx.ALIGN_CENTRE, border = 7)])
        
        vbox2 = ver([staticText(panel2, ''), make_slider(config.VOLUME), make_slider(config.FX), make_slider(config.CUT_OFF), make_slider(config.RESONANCE)])
        panel2.SetSizer(vbox2)
        self.SetSizer(ver([Cell(hbox, scale = 3.8), Cell(piano, scale = 1)]))
        self.Centre()

        self.init_player_file()
        
        def set_tuning(n):
            def go(evt):
                self.tuning_id = n
                self.player.set_tuning(n)
            return go

        make_menu_bar(self, [
              menuItem('&File',[
                  normalItem('Add to favs', cbk = self.on_add_to_favs)
                , normalItem('Remove from favs', cbk = self.on_remove_frome_favs)
               # , normalItem('Settings')
                ])
            , menuItem('&Edit', [
                  normalItem('Enlarge font', cbk = self.on_font_up)
                , normalItem('Shrink  font', cbk = self.on_font_down)
                , menuItem('Tuning', [ radioItem(name, cbk = set_tuning(i), is_check = self.tuning_id == i) for i, name in enumerate(config.TUNINGS) ])
            ])  
            , menuItem('&Help',[                  
                normalItem('About', cbk = self.on_about)  
                ])])


        up_font_id = wx.NewId()
        down_font_id = wx.NewId()        
        self.Bind(wx.EVT_MENU, self.on_font_up, id=up_font_id)
        self.Bind(wx.EVT_MENU, self.on_font_down, id=down_font_id)
        self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL,  ord('='), up_font_id), (wx.ACCEL_CTRL,  ord('-'), down_font_id)])
        self.SetAcceleratorTable(self.accel_tbl)


    def on_font_up(self, e):       
        self.state.up_font_size()
        self.font_objects.update(self.state.get_font())


    def on_font_down(self, e):        
        self.state.down_font_size()
        self.font_objects.update(self.state.get_font())

    def on_add_to_favs(self, event):
        if self.current_track:
            self.favs.add(self.current_track)

    def on_remove_frome_favs(self, event):
        if self.current_track:
            self.favs.remove(self.current_track)
        if self.is_favs_tab():
            fill_tree(self.favs_panel.tree, self.favs.items, config.FAVS, self.state.get_font())
        self.favs_panel.Refresh()

    def is_favs_tab(self):
        return self.current_tab == 2

    def on_about(self, e):    
        description = """Tiny synth is a collection of instruments designed with csound-expression library.
"""

        licence = """Tiny synth is licensed under Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License."""

        info = wx.AboutDialogInfo()

        info.SetName('Tiny Synth')
        info.SetVersion('0.1')
        info.SetDescription(description)
        info.SetCopyright('(C) 2015 Anton Kholomiov')
        info.SetWebSite('http://github.com/anton-k')
        info.SetLicence(licence)
        info.AddDeveloper('Anton Kholomiov')
        info.AddDocWriter('Anton Kholomiov')
        info.AddArtist('The Math Art')
        info.AddTranslator('Anton Kholomiov')       

        wx.AboutBox(info)


    def on_page_changed(self, event):
        old = event.GetOldSelection()
        new = event.GetSelection()
        self.current_tab = new

        if new == 1:
            fill_tree(self.recent_panel.tree, self.recent.items, config.RECENT, self.state.get_font())

        if new == 2:
            fill_tree(self.favs_panel.tree, self.favs.items, config.FAVS, self.state.get_font())

    def on_selector_changed(self, label, path):   
        if (str(label) == config.RECENT or str(label) == config.FAVS or len(path) == 1):
            return
        self.open_file(label, path)

    def set_current(self, track, path):
        self.current_track = (track, path)

    def init_player_file(self):
        xs = self.recent.items 
        if xs:
            (label, path) = xs[0]
            self.open_file(label, path)

    def open_file(self, label, path):
        self.close_file()
        self.display.SetLabel('   ' + label)
        faders = self.state.get_faders(path)
        self.player.load(path_to_csd_file(path), faders)
        faders.set_ui(self.ui_faders)
        self.set_current(label, path)
        self.recent.append((label, path))

    def close_file(self): 
        if self.current_track:       
            self.state.setFaders(self.get_current_path(), self.get_current_faders())

    def get_current_path(self):        
        return self.current_track[1]

    def get_current_faders(self):
        def get(name):
            return float(self.ui_faders[name].GetValue()) / 100

        return faders.Faders(get(config.VOLUME), get(config.FX), get(config.CUT_OFF), get(config.RESONANCE))

    def on_exit(self, evt):
        self.close_file()
        self.player.close()
        self.state.close(self.recent, self.favs, self.tuning_id)        
        self.Destroy()