Stop the reading of notes created with fluidsynth with a button in tkinter












0















I'm on python2-7 I want to get a button in tkinter which stop the reading of the notes created with fluidsynth.



I found that the common solution is to use time.after like here: How do you create a Tkinter GUI stop button to break an infinite loop?



But in my case I can't use it because I need an amount of time between noteon and noteoff to give durations for my notes.
Moreover I want to play the notes ONLY if I click on start (and no at the beginning like the solution in the link).



So I created this code but it doesn't work because var_start is always initialized as int:



from tkinter import*
import fluidsynth
import time

fs=fluidsynth.Synth()
fs.start(driver='alsa', midi_driver='alsa_seq')
org_charge = fs.sfload("organ.sf2")
fs.program_select(0,org_charge, 0, 0)
time.sleep(1)

var_start=int

def start():
global var_start
var_start=1

def stop():
global var_start
var_start=0

root=Tk()

if var_start==1:
fs.noteon(0,67,127)
time.sleep(1)
fs.noteoff(0,67)
fs.noteon(0,71,127)
time.sleep(1)
fs.noteoff(0,71)
fs.noteon(0,74,127)
time.sleep(1)
fs.noteoff(0,74)

Button(root, text='start', command= start).pack(padx=10, pady=10)
Button(root, text='stop', command= stop).pack(padx=10, pady=10)

root.mainloop()


I don't have other idea to reshape my code...
Can someone help me ?



Thanks










share|improve this question



























    0















    I'm on python2-7 I want to get a button in tkinter which stop the reading of the notes created with fluidsynth.



    I found that the common solution is to use time.after like here: How do you create a Tkinter GUI stop button to break an infinite loop?



    But in my case I can't use it because I need an amount of time between noteon and noteoff to give durations for my notes.
    Moreover I want to play the notes ONLY if I click on start (and no at the beginning like the solution in the link).



    So I created this code but it doesn't work because var_start is always initialized as int:



    from tkinter import*
    import fluidsynth
    import time

    fs=fluidsynth.Synth()
    fs.start(driver='alsa', midi_driver='alsa_seq')
    org_charge = fs.sfload("organ.sf2")
    fs.program_select(0,org_charge, 0, 0)
    time.sleep(1)

    var_start=int

    def start():
    global var_start
    var_start=1

    def stop():
    global var_start
    var_start=0

    root=Tk()

    if var_start==1:
    fs.noteon(0,67,127)
    time.sleep(1)
    fs.noteoff(0,67)
    fs.noteon(0,71,127)
    time.sleep(1)
    fs.noteoff(0,71)
    fs.noteon(0,74,127)
    time.sleep(1)
    fs.noteoff(0,74)

    Button(root, text='start', command= start).pack(padx=10, pady=10)
    Button(root, text='stop', command= stop).pack(padx=10, pady=10)

    root.mainloop()


    I don't have other idea to reshape my code...
    Can someone help me ?



    Thanks










    share|improve this question

























      0












      0








      0








      I'm on python2-7 I want to get a button in tkinter which stop the reading of the notes created with fluidsynth.



      I found that the common solution is to use time.after like here: How do you create a Tkinter GUI stop button to break an infinite loop?



      But in my case I can't use it because I need an amount of time between noteon and noteoff to give durations for my notes.
      Moreover I want to play the notes ONLY if I click on start (and no at the beginning like the solution in the link).



      So I created this code but it doesn't work because var_start is always initialized as int:



      from tkinter import*
      import fluidsynth
      import time

      fs=fluidsynth.Synth()
      fs.start(driver='alsa', midi_driver='alsa_seq')
      org_charge = fs.sfload("organ.sf2")
      fs.program_select(0,org_charge, 0, 0)
      time.sleep(1)

      var_start=int

      def start():
      global var_start
      var_start=1

      def stop():
      global var_start
      var_start=0

      root=Tk()

      if var_start==1:
      fs.noteon(0,67,127)
      time.sleep(1)
      fs.noteoff(0,67)
      fs.noteon(0,71,127)
      time.sleep(1)
      fs.noteoff(0,71)
      fs.noteon(0,74,127)
      time.sleep(1)
      fs.noteoff(0,74)

      Button(root, text='start', command= start).pack(padx=10, pady=10)
      Button(root, text='stop', command= stop).pack(padx=10, pady=10)

      root.mainloop()


      I don't have other idea to reshape my code...
      Can someone help me ?



      Thanks










      share|improve this question














      I'm on python2-7 I want to get a button in tkinter which stop the reading of the notes created with fluidsynth.



      I found that the common solution is to use time.after like here: How do you create a Tkinter GUI stop button to break an infinite loop?



      But in my case I can't use it because I need an amount of time between noteon and noteoff to give durations for my notes.
      Moreover I want to play the notes ONLY if I click on start (and no at the beginning like the solution in the link).



      So I created this code but it doesn't work because var_start is always initialized as int:



      from tkinter import*
      import fluidsynth
      import time

      fs=fluidsynth.Synth()
      fs.start(driver='alsa', midi_driver='alsa_seq')
      org_charge = fs.sfload("organ.sf2")
      fs.program_select(0,org_charge, 0, 0)
      time.sleep(1)

      var_start=int

      def start():
      global var_start
      var_start=1

      def stop():
      global var_start
      var_start=0

      root=Tk()

      if var_start==1:
      fs.noteon(0,67,127)
      time.sleep(1)
      fs.noteoff(0,67)
      fs.noteon(0,71,127)
      time.sleep(1)
      fs.noteoff(0,71)
      fs.noteon(0,74,127)
      time.sleep(1)
      fs.noteoff(0,74)

      Button(root, text='start', command= start).pack(padx=10, pady=10)
      Button(root, text='stop', command= stop).pack(padx=10, pady=10)

      root.mainloop()


      I don't have other idea to reshape my code...
      Can someone help me ?



      Thanks







      python-2.7 tkinter fluidsynth






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 21:06









      CaptainCoding7CaptainCoding7

      52




      52
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You initiated var_start to int in statement var_start=int and so the code block in if var_start==1: will never be executed. And your start() function is just changing the var_start to 1 and never start playing the notes, therefore nothing will happen.



          Never call time.sleep() in main thread as it will block the tkinter main loop. You can use .after(...) to simulate the play loop and below is a sample code block:



          playing = False

          def play_notes(notes, index, noteoff):
          global playing
          if noteoff:
          fs.noteoff(0, notes[index])
          index += 1 # next note
          if playing and index < len(notes):
          fs.noteon(0, notes[index], 127)
          # call noteoff one second later
          root.after(1000, play_notes, notes, index, True)
          else:
          # either stopped or no more note to play
          playing = False
          print('done playing')

          def start_playing():
          global playing
          if not playing:
          print('start playing')
          playing = True
          notes = [67, 71, 74, 88, 80, 91]
          play_notes(notes, 0, False)
          else:
          print('already playing')

          def stop_playing():
          global playing
          if playing:
          playing = False
          print('stop playing')
          else:
          print('nothing playing')

          Button(root, text='Start', command=start_playing).pack(padx=10, pady=10)
          Button(root, text='Stop', command=stop_playing).pack(padx=10, pady=10)


          It is just an example you can modify to suit your need.






          share|improve this answer
























          • Thanks for your help acw1668 ! It works perfectly now !

            – CaptainCoding7
            Jan 4 at 20:47












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013183%2fstop-the-reading-of-notes-created-with-fluidsynth-with-a-button-in-tkinter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You initiated var_start to int in statement var_start=int and so the code block in if var_start==1: will never be executed. And your start() function is just changing the var_start to 1 and never start playing the notes, therefore nothing will happen.



          Never call time.sleep() in main thread as it will block the tkinter main loop. You can use .after(...) to simulate the play loop and below is a sample code block:



          playing = False

          def play_notes(notes, index, noteoff):
          global playing
          if noteoff:
          fs.noteoff(0, notes[index])
          index += 1 # next note
          if playing and index < len(notes):
          fs.noteon(0, notes[index], 127)
          # call noteoff one second later
          root.after(1000, play_notes, notes, index, True)
          else:
          # either stopped or no more note to play
          playing = False
          print('done playing')

          def start_playing():
          global playing
          if not playing:
          print('start playing')
          playing = True
          notes = [67, 71, 74, 88, 80, 91]
          play_notes(notes, 0, False)
          else:
          print('already playing')

          def stop_playing():
          global playing
          if playing:
          playing = False
          print('stop playing')
          else:
          print('nothing playing')

          Button(root, text='Start', command=start_playing).pack(padx=10, pady=10)
          Button(root, text='Stop', command=stop_playing).pack(padx=10, pady=10)


          It is just an example you can modify to suit your need.






          share|improve this answer
























          • Thanks for your help acw1668 ! It works perfectly now !

            – CaptainCoding7
            Jan 4 at 20:47
















          0














          You initiated var_start to int in statement var_start=int and so the code block in if var_start==1: will never be executed. And your start() function is just changing the var_start to 1 and never start playing the notes, therefore nothing will happen.



          Never call time.sleep() in main thread as it will block the tkinter main loop. You can use .after(...) to simulate the play loop and below is a sample code block:



          playing = False

          def play_notes(notes, index, noteoff):
          global playing
          if noteoff:
          fs.noteoff(0, notes[index])
          index += 1 # next note
          if playing and index < len(notes):
          fs.noteon(0, notes[index], 127)
          # call noteoff one second later
          root.after(1000, play_notes, notes, index, True)
          else:
          # either stopped or no more note to play
          playing = False
          print('done playing')

          def start_playing():
          global playing
          if not playing:
          print('start playing')
          playing = True
          notes = [67, 71, 74, 88, 80, 91]
          play_notes(notes, 0, False)
          else:
          print('already playing')

          def stop_playing():
          global playing
          if playing:
          playing = False
          print('stop playing')
          else:
          print('nothing playing')

          Button(root, text='Start', command=start_playing).pack(padx=10, pady=10)
          Button(root, text='Stop', command=stop_playing).pack(padx=10, pady=10)


          It is just an example you can modify to suit your need.






          share|improve this answer
























          • Thanks for your help acw1668 ! It works perfectly now !

            – CaptainCoding7
            Jan 4 at 20:47














          0












          0








          0







          You initiated var_start to int in statement var_start=int and so the code block in if var_start==1: will never be executed. And your start() function is just changing the var_start to 1 and never start playing the notes, therefore nothing will happen.



          Never call time.sleep() in main thread as it will block the tkinter main loop. You can use .after(...) to simulate the play loop and below is a sample code block:



          playing = False

          def play_notes(notes, index, noteoff):
          global playing
          if noteoff:
          fs.noteoff(0, notes[index])
          index += 1 # next note
          if playing and index < len(notes):
          fs.noteon(0, notes[index], 127)
          # call noteoff one second later
          root.after(1000, play_notes, notes, index, True)
          else:
          # either stopped or no more note to play
          playing = False
          print('done playing')

          def start_playing():
          global playing
          if not playing:
          print('start playing')
          playing = True
          notes = [67, 71, 74, 88, 80, 91]
          play_notes(notes, 0, False)
          else:
          print('already playing')

          def stop_playing():
          global playing
          if playing:
          playing = False
          print('stop playing')
          else:
          print('nothing playing')

          Button(root, text='Start', command=start_playing).pack(padx=10, pady=10)
          Button(root, text='Stop', command=stop_playing).pack(padx=10, pady=10)


          It is just an example you can modify to suit your need.






          share|improve this answer













          You initiated var_start to int in statement var_start=int and so the code block in if var_start==1: will never be executed. And your start() function is just changing the var_start to 1 and never start playing the notes, therefore nothing will happen.



          Never call time.sleep() in main thread as it will block the tkinter main loop. You can use .after(...) to simulate the play loop and below is a sample code block:



          playing = False

          def play_notes(notes, index, noteoff):
          global playing
          if noteoff:
          fs.noteoff(0, notes[index])
          index += 1 # next note
          if playing and index < len(notes):
          fs.noteon(0, notes[index], 127)
          # call noteoff one second later
          root.after(1000, play_notes, notes, index, True)
          else:
          # either stopped or no more note to play
          playing = False
          print('done playing')

          def start_playing():
          global playing
          if not playing:
          print('start playing')
          playing = True
          notes = [67, 71, 74, 88, 80, 91]
          play_notes(notes, 0, False)
          else:
          print('already playing')

          def stop_playing():
          global playing
          if playing:
          playing = False
          print('stop playing')
          else:
          print('nothing playing')

          Button(root, text='Start', command=start_playing).pack(padx=10, pady=10)
          Button(root, text='Stop', command=stop_playing).pack(padx=10, pady=10)


          It is just an example you can modify to suit your need.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 7:22









          acw1668acw1668

          2,7502716




          2,7502716













          • Thanks for your help acw1668 ! It works perfectly now !

            – CaptainCoding7
            Jan 4 at 20:47



















          • Thanks for your help acw1668 ! It works perfectly now !

            – CaptainCoding7
            Jan 4 at 20:47

















          Thanks for your help acw1668 ! It works perfectly now !

          – CaptainCoding7
          Jan 4 at 20:47





          Thanks for your help acw1668 ! It works perfectly now !

          – CaptainCoding7
          Jan 4 at 20:47




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013183%2fstop-the-reading-of-notes-created-with-fluidsynth-with-a-button-in-tkinter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]