Recent

Author Topic: MIDI commands  (Read 6297 times)

Chip

  • Newbie
  • Posts: 3
MIDI commands
« on: December 09, 2018, 01:02:06 pm »
Can I somehow send MIDI commands or other instructions
to the soundcard to select instruments, play notes etc.
Or is there some UNIT available for this.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1191
    • Burdjia
Re: MIDI commands
« Reply #1 on: December 10, 2018, 02:01:17 pm »
Yes, you can.  But AFAIK there's not an "standard" way.  You can use a game library, such as SDL (may be you'll need an add-on) or Allegro.pas (version 5 doesn't has MIDI support so you should use version 4 instead).
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: MIDI commands
« Reply #2 on: December 10, 2018, 03:34:48 pm »
Also, if you are in Windows, you can use the multimedia services of the system: there should be a JEDI translated unit somewheree in the FPC sources/binaries. Check Microsoft's docs to see how that works.

Linux (and presumably MacOS) shoud have something similar but AFAIK the headers are not (yet?) translated to Pascal ... unless it's in some framework or engine for games, as indicated by Ñuño.
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.

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: MIDI commands
« Reply #3 on: December 10, 2018, 05:26:13 pm »
Probably not very useful info for a beginner, but ...

There's a very promising C++ library RtMidi I wish I'd translate (currently finishing another project), and there are chances I will, at least I'm going to test it as is.

Interesting part it is crossplatform, has C interface (suitable for Pascal), supports multiple MIDI backends (Jack, Alsa) on Mac and Linux.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

morknot

  • Jr. Member
  • **
  • Posts: 52
  • still learning
Re: MIDI commands
« Reply #4 on: December 10, 2018, 05:30:33 pm »
There is the BASS audio library (BASSMIDI) for Windows, Apple and Linux. It is free for non-commercial use. I haven't used BASSMIDI but I have used BASS for playing mp3s etc. for which it works very well. For BASS itself there are quite a lot of examples for Delphi, perhaps there are some for BASSMIDI as well. There is a very useful site forum as well.

Find it at https://www.un4seen.com/.

PeterX

  • Sr. Member
  • ****
  • Posts: 424
Re: MIDI commands
« Reply #5 on: February 06, 2019, 04:47:33 pm »
And here the low level way :   TMidiInput and TMidiOutput

http://breakoutbox.de/pascal/pascal.html#TMidi
usually using latest Lazarus release version with Windows 10

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: MIDI commands
« Reply #6 on: February 06, 2019, 04:53:25 pm »
And here the low level Windows only way
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

finlazarus

  • New Member
  • *
  • Posts: 16
Re: MIDI commands
« Reply #7 on: December 09, 2024, 02:26:09 am »
The most simple demo which sends MIDI messages (notes) to the sound card.
Remember 'MMSystem' (Win) in Uses clause.
It needs one Button on the Form:



Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, MMSystem;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure FormDestroy(Sender: TObject);
  19.  
  20.   private
  21.     MidiOutHandle: HMIDIOUT;
  22.     procedure SelectInstrument(Instrument: Byte);
  23.     procedure PlayNote(Note: Byte; Velocity: Byte; Duration: Integer);
  24.  
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.FormCreate(Sender: TObject);
  39. begin
  40.   // Open default MIDI device
  41.   if midiOutOpen(@MidiOutHandle, MIDI_MAPPER, 0, 0, CALLBACK_NULL) <> MMSYSERR_NOERROR then
  42.     raise Exception.Create('MIDI open error');
  43.  
  44.   SelectInstrument(11); //Vibraphone
  45.  
  46. end;
  47.  
  48. procedure TForm1.Button1Click(Sender: TObject);
  49. begin
  50.   // Play notes: MIDI note nr, Velocity 100, Duration
  51.   PlayNote(60, 100, 500);
  52.   PlayNote(62, 100, 500);
  53.   PlayNote(64, 100, 250);
  54.   PlayNote(62, 100, 250);
  55.   PlayNote(60, 100, 500);
  56.  
  57. end;
  58.  
  59. procedure TForm1.FormDestroy(Sender: TObject);
  60. begin
  61.   // Close the MIDI device
  62.   midiOutClose(MidiOutHandle);
  63. end;
  64.  
  65. procedure TForm1.SelectInstrument(Instrument: Byte);
  66. var
  67.   ProgramChangeMsg: DWORD;
  68. begin
  69.   // MIDI Program Change message (channel 0)
  70.   ProgramChangeMsg := $C0 or (Instrument shl 8);
  71.   midiOutShortMsg(MidiOutHandle, ProgramChangeMsg);
  72. end;
  73.  
  74. procedure TForm1.PlayNote(Note: Byte; Velocity: Byte; Duration: Integer);
  75. var
  76.   NoteOnMsg: DWORD;
  77.   NoteOffMsg: DWORD;
  78. begin
  79.   // MIDI Note On (Channel 0)
  80.   NoteOnMsg := $90 or (Note shl 8) or (Velocity shl 16);
  81.   midiOutShortMsg(MidiOutHandle, NoteOnMsg);
  82.  
  83.   // Wait
  84.   Sleep(Duration);
  85.  
  86.   // MIDI Note Off (Channel 0)
  87.   NoteOffMsg := $90 or (Note shl 8) or (0 shl 16); // Velocity 0 = Note Off
  88.   midiOutShortMsg(MidiOutHandle, NoteOffMsg);
  89. end;
  90.  
  91. end.  
  92.  
« Last Edit: December 09, 2024, 06:04:47 am by finlazarus »

hukka

  • New Member
  • *
  • Posts: 36
    • Github
Re: MIDI commands
« Reply #8 on: December 09, 2024, 10:37:40 am »

finlazarus

  • New Member
  • *
  • Posts: 16
Re: MIDI commands
« Reply #9 on: December 09, 2024, 10:35:59 pm »
Use RtMidi for crossplatform compatibility: https://github.com/hukkax/Decks/blob/main/src/include/rtmidi/rtmidi.pas
So how does this work? I download the rtmidi.dll and add rtmidi.pas to my project, then I can send MIDI commands to the soundcard in a normal way?

TRon

  • Hero Member
  • *****
  • Posts: 3822
Re: MIDI commands
« Reply #10 on: December 09, 2024, 10:43:22 pm »
So how does this work? I download the rtmidi.dll and add rtmidi.pas to my project, then I can send MIDI commands to the soundcard in a normal way?
Refer to the official rtmidi repository, read the documentation/sdk and examples etc.
I do not have to remember anything anymore thanks to total-recall.

 

TinyPortal © 2005-2018