So I'm writing a code that imports pygame to play files of music notes. I set each note to a button, 24 notes for 24 buttons. However, the program will also have four more buttons: "Record", "Stop", "Playback", and "Quit". when the record button is clicked, it will append the file name to a songlist. Then the stop button will be pressed to terminate the record function. the songlist will then be played using the playback by having an index = 0 and in the function it will have:

while index < len(songList):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL            PROJECT/notes/"+songList[index]),0.0)

The function will go through each element of the list and play each note.

So my problem here, is creating the record function. I need to have it so that it knows when a note button is clicked so that it can append a name to the songlist. So can I write a code that tells whether or not a button is clicked or not.

for example in pseuodocode...

if "C# Button is clicked":
    note = 'Csharp.mp3'
if "D Button is clicked":
    note = 'D.mp3'

how would I write in actual coding ( if "D Button is clicked")

when each note button is pressed, it runs this function:

def playC(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[0]),0)
        note = '2C.mp3'

Each note button will have this format but with the note and the filename different of course. a friend of mine gave me the idea to set maybe a dummy statement such as
self.C = 'Go' after the function. So that I would write in the Record function....
if self.C = 'Go':

Any ideas? this is the actual code that I have so far... it's not quite finished yet and I've been commenting stuff out as well.

from tkinter import *
from pygame import *

NOTELIST = ['2C.mp3', '2C#.mp3', '2D.mp3', '2D#.mp3', '2E.mp3', '2F.mp3', '2F#.mp3', '2G.mp3', '2G#.mp3', '2A.mp3', '2A#.mp3', '2B.mp3', \
            '3C.mp3', '3C#.mp3', '3D.mp3', '3D#.mp3', '3E.mp3', '3F.mp3', '3F#.mp3', '3G.mp3', '3G#.mp3', '3A.mp3', '3A#.mp3', '3B.mp3']

KEY = ['z', 's', 'x', 'd', 'c', 'v', 'g', 'b', 'h', 'n', 'j', 'm',\
       'q', '2', 'w', '3', 'e', 'r', '5', 't', '6', 'y', '7', 'u']

class Editor:
    def __init__(self):
        #create the main window
        self.main_window = Tk()

        #create frames
        self.top_frame = Frame(self.main_window)
        self.middle_frame = Frame(self.main_window)
        self.octave1 = Frame(self.main_window)
        self.octave2 = Frame(self.main_window)
        self.bottom_frame = Frame(self.main_window)


        #Create widgest

        #create label for top frame
        self.Label = Label(self.top_frame, text = "Hit Record and record a tune")

        #pack label
        self.Label.pack(side = "top")


        #create buttons for middle frame

##        self.Record = Button(self.middle_frame, text = "Record", command = self.Record)
##        self.Stop = Button(self.middle_frame, text = "Stop", command = self.Stop)
##        self.Play = Button(self.middle_frame, text = "Play", command = self.Playback)
##        self.Quit = Button(self.middle_frame, text = "Quit", command = self.main_window.destroy)

        #Pack buttons
##        self.Record.pack(side = "left")
##        self.Stop.pack(side = "left")
##        self.Play.pack(side = "left")
##        self.Quit.pack(side = "left")

        #create buttons for keyboard octave 1
        self.C = Button(self.octave1, text = "C", command = self.playC)
        self.Csharp = Button(self.octave1, text = "C#", command = self.playCsharp)
        self.D = Button(self.octave1, text = "D", command = self.playD)
        self.Dsharp = Button(self.octave1, text = "D#", command = self.playDsharp)
        self.E = Button(self.octave1, text = "E", command = self.playE)
        self.F = Button(self.octave1, text = "F", command = self.playF)
        self.Fsharp = Button(self.octave1, text = "F#", command = self.playFsharp)
        self.G = Button(self.octave1, text = "G", command = self.playG)
        self.Gsharp = Button(self.octave1, text = "G#", command = self.playGsharp)
        self.A = Button(self.octave1, text = "A", command = self.playA)
        self.Asharp = Button(self.octave1, text = "A#", command = self.playAsharp)
        self.B = Button(self.octave1, text = "B", command = self.playB)


        #create buttons for keyboard octave 2
        self.octC = Button(self.octave2, text = "C", command = self.play2C)
        self.octCsharp = Button(self.octave2, text = "C#", command = self.play2Csharp)
        self.octD = Button(self.octave2, text = "D", command = self.play2D)
        self.octDsharp = Button(self.octave2, text = "D#", command = self.play2Dsharp)
        self.octE = Button(self.octave2, text = "E", command = self.play2E)
        self.octF = Button(self.octave2, text = "F", command = self.play2F)
        self.octFsharp = Button(self.octave2, text = "F#", command = self.play2Fsharp)
        self.octG = Button(self.octave2, text = "G", command = self.play2G)
        self.octGsharp = Button(self.octave2, text = "G#", command = self.play2Gsharp)
        self.octA = Button(self.octave2, text = "A", command = self.play2A)
        self.octAsharp = Button(self.octave2, text = "A#", command = self.play2Asharp)
        self.octB = Button(self.octave2, text = "B", command = self.play2B)


        #create legend in bottom frame
##        self.canvas = Canvas(width = 1000, height = 500)
##        image = PhotoImage(file = "keyboard.gif")
##        self.canvas.create_image(500,250, image = image)
##        self.canvas.pack()

        #pack Frames


    def Record(self):

    def Stop(self):

    def Playback(self):

    def playC(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[0]),0)
        note = '2C.mp3'
    def playCsharp(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[1]),0)
        note = '2C#.mp3'
    def playD(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[2]),0)
        note = '2D.mp3'
    def playDsharp(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[3]),0)
        note = '2D#.mp3'
    def playE(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[4]),0)
        note = '2E.mp3'
    def playF(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[5]),0)
        note = '2F.mp3'
    def playFsharp(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[6]),0)
        note = '2G.mp3'
    def playG(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[7]),0)
        note = '2G.mp3'
    def playGsharp(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[8]),0)
        note = '2G#.mp3'
    def playA(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[9]),0)
        note = '2A.mp3'
    def playAsharp(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[10]),0)
        note = '2A#.mp3'
    def playB(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[11]),0)
        note = '2B.mp3'
    def play2C(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[12]),0)
    def play2Csharp(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[13]),0)
    def play2D(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[14]),0)
    def play2Dsharp(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[15]),0)
    def play2E(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[16]),0)
    def play2F(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[17]),0)
    def play2Fsharp(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[18]),0)
    def play2G(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[19]),0)
    def play2Gsharp(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[20]),0)
    def play2A(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[21]),0)
    def play2Asharp(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[22]),0)
    def play2B(self):
        mixer.init()"C:/Python32/Python Programs/NEW FINAL PROJECT/notes/"+NOTELIST[23]),0)


mygui = Editor()

Edited 4 Years Ago by Koreakid101: n/a

Do not repeat code but use loop and argument with value from notelist by functools.partial.

This code wont run as you have place holder functions without any statements, add pass statement. Record/Stop should be toggle switch changing the self.recording between True and False. Add self.recording check into play function. You should probably use key up even to record the length of note. Also you should assoociate keyboard key so you possibly can push chords, even keyboard keys are not so reliable with multiple keys pressed (in same loop you associate the graphical button)

Here textual way worked on code (as I have not mp3 files).

import functools

from Tkinter import *
from pygame import *

NOTEPAYH = "G:/notes/"
NOTELIST = ['2C.mp3', '2C#.mp3', '2D.mp3', '2D#.mp3', '2E.mp3', '2F.mp3', '2F#.mp3', '2G.mp3', '2G#.mp3', '2A.mp3', '2A#.mp3', '2B.mp3',
            '3C.mp3', '3C#.mp3', '3D.mp3', '3D#.mp3', '3E.mp3', '3F.mp3', '3F#.mp3', '3G.mp3', '3G#.mp3', '3A.mp3', '3A#.mp3', '3B.mp3']

KEY = ['z', 's', 'x', 'd', 'c', 'v', 'g', 'b', 'h', 'n', 'j', 'm',
       'q', '2', 'w', '3', 'e', 'r', '5', 't', '6', 'y', '7', 'u']

from time import clock

class Editor:
    def __init__(self):

        self.notes = []
        self.keyboard = []
        self.recording = False
        #create the main window
        self.main_window = Tk()

        #create frames
        self.middle_frame = Frame(self.main_window)
        self.octave1 = Frame(self.main_window)
        self.octave2 = Frame(self.main_window)
        self.bottom_frame = Frame(self.main_window)       

        #Create widgest
        #create buttons for middle frame

        self.record = Button(self.middle_frame, text = "Start Recording", command = self.record_toggle) = Button(self.middle_frame, text = "Play", command = self.playback)
        self.quit = Button(self.middle_frame, text = "Quit", command = self.main_window.destroy)
        for widget in self.record,, self.quit:
            widget.pack(side=LEFT, expand=TRUE, fill=BOTH)

        #create buttons for keyboard octaves
        octave = self.octave1
        self.key_note = {}
        for note, key in zip(NOTELIST, KEY):
            if note.startswith('3C'):
                octave = self.octave2
            self.keyboard.append(Button(octave, text = note.split('.')[0], command= functools.partial(self.play_note, note=note)))
            self.main_window.bind_all(key, functools.partial(self.play_note, key=key))
            self.key_note[key] = note
            #octave.bind('ButtonRelease-1', self.stop_note) # does not function            
        #pack Frames


    def record_toggle(self):
        self.recording = not self.recording
        self.record['text'] =  ('Stop' if self.recording else 'Start') + ' Recording' 
        self.main_window.title('  '.join('Recording') if self.recording else 'Music')

    def playback(self):
        self.main_window.title('   '.join('Playback'))
        #to be done

    # stop_note should note the release time of button, to be done
    def stop_note(self, event):
        print event
    def play_note(self, note=0, key=''):
        if key:
            note = self.key_note[key]
        if self.recording:
            self.notes.append([note, clock()])
            print self.notes[-1]
            print note, clock()
##        mixer.init()
## + NOTELIST[note])
mygui = Editor()

Edited 4 Years Ago by pyTony: n/a

I need to have it so that it knows when a note button is clicked

The standard way is to set some variable. In Tony's code, a button click calls play_note() and sends it the note and key. You could use the key to tell which button is clicked and possibly link it to a dictionary that contains the function you want to execute.

I am saving clock time with note information and checking it whe key goes up. My newer version has multiple keypresses OK by keeping set of currently playing keys/mouse presses. Note and key are of course redundant, but now I finally needed the key_note dictionary also. The redundancy can be cleaned at end but it is usefull until code is finalized. Even nowadays it is not so big sin to waste memory for dictionary of all of 24 values... Sometimes my 64 kByte memory days...

This article has been dead for over six months. Start a new discussion instead.