Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exit Function - loop
#1
Hi , i'm actually coding a midi looper on raspberry pi.
I have a usb midi interface thar receive midi message (and send them to hardware synth) and a midi footswhich that send midi message to control the looper.

outport = mido.open_output('MIDISPORT 2x2 Anniv:MIDISPORT 2x2 Anniv MIDI 1 28:0')
songlist = (glob.glob("/home/pi/Documents/song/*.mid"));

#Function that play the midi file in loop 
def songplay(song):
    with outport as output:
        try:
            midifile = MidiFile(song)
            while True:
                for message in midifile.play():    
                    output.send(message)
    
 
#HERE THE CODE THAT LISTEN TO INCOMING MIDI MESSAGE(IF NOTE 36 CHOOSE THE MIDI FILE TO PLAY IN A LIST)(IF NOTE 27 PLAY THE SELECTED FILE)
try:
    with mido.open_input('arduino_midi:arduino_midi MIDI 1 20:0') as port:
        for message in port:
            if message.type in ('note_on') and message.note == 36:
                print(songlist[i])
                if i == (nbrsong-1):
                    i = 0
                else:
                    i += 1
           
            if message.type in ('note_on') and message.note == 37:
                songplay(songlist[i])

            if message.type in ('note_on') and message.note == 38:
                HOW TO STOP THE FONTION ???
It works but when the file is playing (when we are in the function) the instructions from the try doesn't work anymore. I'd like for add incoming message ex. IF note 38 = Stop playing the song... I don't know if the "try" is the good solution
Reply
#2
Does this work right?

def songplay(song):
    with outport as output:
        try:
            midifile = MidiFile(song)
            while True:
                for message in midifile.play():    
                    output.send(message)
Reply
#3
(Sep-17-2020, 08:05 AM)cnull Wrote: Does this work right?

def songplay(song):
    with outport as output:
        try:
            midifile = MidiFile(song)
            while True:
                for message in midifile.play():    
                    output.send(message)

Yes, in fact his is the code, actuall I have an expet KeyboardInterrupt ... but yes it Works

def songplay(song):
    with outport as output:
        try:
            midifile = MidiFile(song)
            while True:
                for message in midifile.play():    
                    output.send(message)
 
        except KeyboardInterrupt:
            print()
            output.reset() 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python difference between sys.exit and exit() mg24 1 1,854 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  Exit function from nested function based on user input Turtle 5 2,931 Oct-10-2021, 12:55 AM
Last Post: Turtle
  While loop doesn't exit KenHorse 3 2,028 Jun-20-2021, 11:05 PM
Last Post: deanhystad
  function fails to exit harig3 4 2,036 Aug-17-2020, 06:35 PM
Last Post: deanhystad
  Using Function in a For loop vp1989 5 2,816 May-19-2020, 03:53 PM
Last Post: vp1989
  Struggling to exit this while loop fatherted99 5 2,490 Feb-08-2020, 07:46 PM
Last Post: fatherted99
  won't exit a loop, please help hobbyprogrammer 9 4,409 Oct-22-2018, 05:22 PM
Last Post: hobbyprogrammer
  Using break to exit a loop help JJG 4 4,200 Dec-09-2017, 03:02 AM
Last Post: JJG
  How to exit infinite While Loop? Fran_3 3 4,112 Aug-10-2017, 05:29 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020