Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]: Qt and asynchronous c++ midi library

[Solved]: Qt and asynchronous c++ midi library

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 7.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrscope
    wrote on last edited by
    #1

    Hi guys,

    i'm looking for develop a simple app with a third part c++ library the libjdkmidi (http://www.jdkoftinoff.com/main/Free_Projects/C++_MIDI_Library/)

    Now i'm studying an example of the library that play a midifile. It works well in a console app...

    The problem (a common problem :-) is integrate it in GUI app.

    I've this code :

    @MIDIFileReadStreamFile rs ( "c:/temp/360.mid" );
    MIDIMultiTrack tracks ( 64 );
    MIDIFileReadMultiTrack track_loader ( &tracks );
    MIDIFileRead reader ( &rs, &track_loader );
    //MIDISequencerGUIEventNotifierText gui ( stdout );
    MIDISequencer seq ( &tracks );
    MIDIDriverWin32 driver ( 128 );
    MIDIManager mgr ( &driver );
    reader.Parse();
    driver.StartTimer ( 20 );
    driver.OpenMIDIOutPort ( MIDI_MAPPER );
    seq.GoToZero();
    mgr.SetSeq ( &seq );
    mgr.SetTimeOffset ( timeGetTime() );
    mgr.SeqPlay();
    getchar();
    mgr.SeqStop();@

    now, the mgr.SeqPlay play the midifile and the getchar() function wait for an input allowing the mgr.SeqPlay to finish play the midifile.

    obviously the getchar() freeze the GUI and i (for now) fix replacing it with
    @ while (mgr.IsSeqPlay()) {qApp->processEvents();};@

    The problem is it take a lot of CPU (50% on a dualcore) because the processEvents is a cpu intensive processment.

    Can you give me a point of start about it? What i must use, thread ? QtConcurrent? and ... how ?

    Thanks for your help!

    Luca

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Peleron
      wrote on last edited by
      #2

      Make derived class of QRunnable
      Reimplement method run() in your class:
      @void run()
      {
      MIDIFileReadStreamFile rs ( "c:/temp/360.mid" );
      MIDIMultiTrack tracks ( 64 );
      MIDIFileReadMultiTrack track_loader ( &tracks );
      MIDIFileRead reader ( &rs, &track_loader );
      //MIDISequencerGUIEventNotifierText gui ( stdout );
      MIDISequencer seq ( &tracks );
      MIDIDriverWin32 driver ( 128 );
      MIDIManager mgr ( &driver );
      reader.Parse();
      driver.StartTimer ( 20 );
      driver.OpenMIDIOutPort ( MIDI_MAPPER );
      seq.GoToZero();
      mgr.SetSeq ( &seq );
      mgr.SetTimeOffset ( timeGetTime() );
      mgr.SeqPlay();
      }@
      Its enough if calling SeqStop() procedure not necessary.
      Somewhere call this method via start()

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrscope
        wrote on last edited by
        #3

        @Peleron: thanks...

        i've tried this (as few examples found on the network)

        @PlayerRunnable *pl= new PlayerRunnable ();
        pl->setAutoDelete(false);
        QThreadPool::globalInstance()->start(pl);@

        after reimplemented method run() as you wrote, but it doesn't work. I think it quit after the mgr.SeqPlay

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrscope
          wrote on last edited by
          #4

          [quote author="LuckyLiuk" date="1320946506"]@Peleron: thanks...

          but it doesn't work.

          [/quote]

          The player doesn't sound

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrscope
            wrote on last edited by
            #5

            i've fixed it with:
            @ QEventLoop q;
            mgr.SeqPlay();
            q.exec();@

            but not sure if it's correct or can be give me problems . What do you think ?

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              For me, it looks like midi playback is already done in a thread. so I would make MIDIManager a member of my class, call mgr.SeqPlay(); and then return from the slot.
              So no need of process event, qrunnable, a thread or whatever.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mrscope
                wrote on last edited by
                #7

                @Gerolf

                Hmmm i think you have the reason. I'll try . Tnx

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mrscope
                  wrote on last edited by
                  #8

                  It works! Thanks.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SherifOmran
                    wrote on last edited by
                    #9

                    Hi LuckyLiuk,

                    I have the same problem, trying to play a midi file but it does not work . I would appreciate if you share you class .

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SherifOmran
                      wrote on last edited by
                      #10

                      Hi LuckyLiuk,

                      I have the same problem, trying to play a midi file but it does not work . I would appreciate if you share you class .

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved