Recent

Author Topic: [SOLVED] Best way to play separate MIDI messages (LCL GUI)?  (Read 964 times)

GreatCorn

  • New Member
  • *
  • Posts: 38
    • GreatCorn
I've parsed a MIDI file into an array of notes (key, velocity, time). I need to play them back asynchronous of a GUI app (also convert to keystrokes, and get through a MIDI output, but that's no problem). The thing is, in MIDIs there are normally thousands of notes. What do you think would be the best way to play them back?
I've got some ideas, but they're slow and impractical:
  • Call a bunch of BeginThreads at once with a function that waits (Sleep or Delay) for the specified amount of time and plays the note (there's a limit on threads and it's really unstable).
  • Sorting the timestamps and then running them through a single thread that will Sleep/Delay the curretTime-previousTime and play the note. Might be slow, especially when trying to sort all of it.
  • Write another app to be a background process and do the job (unprofessional bodging).
Thank you for your time.
« Last Edit: May 28, 2020, 02:47:05 pm by GreatCorn »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Best way to play separate MIDI messages (LCL GUI)?
« Reply #1 on: May 28, 2020, 12:04:26 pm »

https://docs.microsoft.com/en-us/windows/win32/multimedia/multimedia-timers


The MMsytem has the interface functions for it..

you need to be careful but if you follow the guide lines between GUI and timer coding you should be ok.
 
  Also uncommon messages get filtered in the LCL (Form) so you may need to override the Window Procedure to capture the messages you need. This is ok because it allows you to capture the messages at the start before passing back control to the LCL which could lag..
The only true wisdom is knowing you know nothing

GreatCorn

  • New Member
  • *
  • Posts: 38
    • GreatCorn
Re: Best way to play separate MIDI messages (LCL GUI)?
« Reply #2 on: May 28, 2020, 01:39:53 pm »
The MMsytem has the interface functions for it..
Thank you very much, didn't know that existed. I guess I should look more into MMSystem. I'll try this approach and see how it will go.

GreatCorn

  • New Member
  • *
  • Posts: 38
    • GreatCorn
Re: Best way to play separate MIDI messages (LCL GUI)?
« Reply #3 on: May 28, 2020, 02:46:46 pm »
Thanks a lot, again, the MM timer approach worked like a charm! Made a 1ms timer and a pointer that moved through the notes array and checked if a note is on the time (if so - then it'd be played). Now all that's left is to sort the array so different tracks will be played simultaneously.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [SOLVED] Best way to play separate MIDI messages (LCL GUI)?
« Reply #4 on: May 28, 2020, 04:02:07 pm »
Use a different array for each track (and at least one for system) and save the notes/events at an index relative to its play time.

Even for long compositions you'll "waste" at most 8 to 10 MiB of memory (even 32 MiB, if you need it, isn't a lot these days) and it will be easier for you to keep the timing in synch not only for the whole but also for the tracks between themselves.

This is specially important if you're using more than one external synth and/or long runs of wire.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018