Play button for Midi Files not working

moosmahna
moosmahna Member Posts: 8 Member
edited October 2024 in Scripting Workshop

Hy,

I try to make an instrument where I can play my midi files. I tried and found some code and testet it but I had no luck. Here is a code I found but if I press the play button nothing happens. The midi files are loaded.


on ui_control ($play)
    if ($play = 1)
    end if
end on

on listener
    $cur_listener_id := $NI_CALLBACK_ID

    if ($play = 1)
         if ($mf_length = -1)
            mf_get_last($TRACK_IDX)
            $mf_length := mf_get_pos()
         end if
     
         if ($prev_ext_song_pos = $NI_SONG_POSITION)
             $cur_song_pos := $cur_song_pos + $LISTENER_BEAT_LENGTH_TICKS
         else
             $cur_song_pos := $NI_SONG_POSITION
         end if
         $prev_ext_song_pos := $NI_SONG_POSITION

         mf_get_next_at($TRACK_IDX, $cur_song_pos mod $mf_length)
         $end_of_beat := $cur_song_pos mod $mf_length + $LISTENER_BEAT_LENGTH_TICKS
         $last_pos := $cur_song_pos mod $mf_length

         while (mf_get_pos() < $end_of_beat and $play = 1 and $cur_listener_id = $NI_CALLBACK_ID)

             if (mf_get_command() = 0)
                 $end_of_beat := $end_of_beat mod $mf_length
                 mf_get_first($TRACK_IDX)
                 $diff := $mf_length - $last_pos + mf_get_pos()
             else
                 $diff := mf_get_pos() - $last_pos
             end if   

             if (ticks_to_ms($diff) > 0)
                wait_ticks($diff)
             end if   

             if ($play = 1)
                play_note(mf_get_byte_one(),mf_get_byte_two(),0,0)
      
             end if

             $last_pos := mf_get_pos()
             mf_get_next($TRACK_IDX)

         end while
    end if
end on

Can you help me please?

Thanks a lot

Kind Regards Roman

Comments

  • DimitrisG
    DimitrisG Member Posts: 21 Member

    UI Control Play Button: This part of the code is listening for button presses on the play button ($play).

    on ui_control ($play)
       if ($play = 1)
       end if
    end on
    

    This section of code doesn't seem to be doing anything. When the play button is pressed ($play = 1), there is no action inside the if statement.

    Listener Callback: This block of code runs on every beat and is attempting to sequence the playback of MIDI notes:

    on listener
       ...
    end on
    

    Based on the code you've shown, here are some potential issues:

    • Play Button Does Nothing: As mentioned above, there's no action in the $play UI control block. You probably need to initialize or set some variables there to signal the listener callback to start playing.
    • Initial Values: Before using certain variables, you should ensure that they are initialized to appropriate values. For example, variables like $mf_length, $cur_song_pos, $prev_ext_song_pos, etc., might not have been initialized.
    • Listener Triggering: The listener callback triggers on every beat. If there is no song playing or if KONTAKT's host transport (DAW transport) isn't running, the listener might not get called.

    To troubleshoot and fix:

    1. Make sure that KONTAKT's transport or your DAW's transport is running when you press play. The listener callback is dependent on it.
    2. Make sure the play button sets up necessary variables. For example:
    on ui_control ($play)
       if ($play = 1)
           $mf_length := -1
           $cur_song_pos := 0
           $prev_ext_song_pos := 0
           ... [other initializations] ...
       end if
    end on
    
    1. Ensure that your MIDI files are loaded correctly and that the $TRACK_IDX is referencing the correct track.
    2. If you're still having issues, consider adding some debug messages to your script using the message function to trace the flow of your script and understand where it might be breaking. For example:
    message("Play button pressed")
    

    or

    message("In listener callback")
    

    Make sure you're not getting any error messages in KONTAKT. If there are errors, they can give you more insights into what's going wrong.

This discussion has been closed.
Back To Top