Solved

Generating MIDI Files...

Posted on 2002-07-29
9
1,023 Views
Last Modified: 2010-04-04
Hi all...

I need a simple Delphi app that will write a MIDI file with a single note-on, followed by a single note-off.  It needs to just open the file, dump the contents into the file using Write(File, #$xx), close the file and then I should be able to play the file in another program (which you don't have to write).

The only proviso is that I need a full explanation of what each bit of the code does and how it is formed so that I can expand from there...

Hope someone can help me ASAP...

Matt
0
Comment
Question by:mdavis1982
9 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
0
 
LVL 3

Expert Comment

by:LukA_YJK
Comment Utility
Fortunately I just recently worked on an utility for changing custom song format to midi, so learned out some things. Not much, but hope enough... That is it... [Red Leader]

procedure TSngFile.SaveAsMidi(note: byte; FileName: string);
const
  midHdr: array[0..13]of byte = ($4D,$54,$68,$64,
     $00,$00,$00,$06,$00,$01,$00,$01,$01,$E0);
  midTrk: array[0..7]of byte = ($4D,$54,$72,$6B,$00,$00,$00,$0E);
  midEnd: array[0..3]of byte = ($00,$FF,$2F,$00);
  midCmd: array[0..4]of byte = ($80,$00,$90,$00,$00);
  Velocity = $FF;
  Time     = $FF;
var
  midFile: TFileStream;
begin
  // Create MIDI file
  midFile := TFileStream.Create(FileName +'.mid', fmCreate);
  with midFile do try
    // Write MIDI header
    Write(midHdr, SizeOf(midHdr));
    // Write MIDI track
    Write(midTrk, SizeOf(midTrk));
    // Write Note On
    midCmd[2] := $90;
    midCmd[3] := Note;
    midCmd[4] := Velocity;
    Write(midCmd, SizeOf(midCmd));
    // Write Note Off
    midCmd[0] := $80 or (Time div 128 mod 128);
    midCmd[1] := $00 or (Time mod 128);
    midCmd[2] := $80;
    Write(midCmd, SizeOf(midCmd));
    // Write MIDI track end
    Write(midEnd, SizeOf(midEnd));
  finally
    midFile.Free;
  end;
end;

Hope it will help...
0
 
LVL 3

Expert Comment

by:LukA_YJK
Comment Utility
I checked my comment above and it works good (at least here) so I propose it as an Answer...
0
 

Author Comment

by:mdavis1982
Comment Utility
I have not accepted it because nothing is explained like I asked for... If you could write a full description of the codes etc, then I would be more than happy to accept it
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:LukA_YJK
Comment Utility
Is it working (there) ?
0
 
LVL 3

Expert Comment

by:LukA_YJK
Comment Utility
This is it:

procedure TSngFile.SaveAsMidi(note: byte; FileName: string);
const
  // MIDI file header
  midHdr: array[0..13]of byte =
   // [0..3] Always MThd
   ($4D,$54,$68,$64,
   // [4..7] Always the same
   $00,$00,$00,$06,
   // [8..9] Two bytes for file format
   // $01 = synchronous multiple tracks
   $00,$01,
   // [10..11] Two bytes for number of tracks
   $00,$01,
   // [12..13] Two bytes for ticks per quarter note
   $01,$E0);

  // MIDI track header
  midTrk: array[0..7]of byte =
   // [0..3] Always MTrk
   ($4D,$54,$72,$6B,
   // [4..7] Four bytes for track size in bytes
   $00,$00,$00,$0E);
  // MIDI track end bytes

  midEnd: array[0..3]of byte =
   // [0..3] Always the same
   ($00,$FF,$2F,$00);

  // MIDI command
  midCmd: array[0..4]of byte =
   // [0..1] Two bytes for time in ticks
   ($80,$00,
   // [2] Command:
   // $90 = Note on at channel 0
   // $80 = Note off at channel 0
   $90,
   // [3] Note and [4] velocity
   $00,$00);

  // Constant velocity
  Velocity = $FF;
  // Constant duration
  Time     = $FF;
var
  midFile: TFileStream;
begin
  // Create MIDI file
  midFile := TFileStream.Create(FileName +'.mid', fmCreate);
  with midFile do try

    // Write MIDI header
    Write(midHdr, SizeOf(midHdr));

    // Write MIDI track
    Write(midTrk, SizeOf(midTrk));

    // Write Note On
    // Play immediately = time is 0
    midCmd[2] := $90;
    midCmd[3] := Note;
    midCmd[4] := Velocity;
    Write(midCmd, SizeOf(midCmd));

    // Write Note Off
    // Separate time into two bytes
    midCmd[0] := $80 or (Time div 128 mod 128);
    midCmd[1] := $00 or (Time mod 128);
    midCmd[2] := $80;
    Write(midCmd, SizeOf(midCmd));

    // Write MIDI track end
    Write(midEnd, SizeOf(midEnd));
  finally
    midFile.Free;
  end;
end;

To learn more visit the links supposed by DragonSlayer above...
0
 
LVL 3

Accepted Solution

by:
LukA_YJK earned 300 total points
Comment Utility
Ask what portion is not clear enough...
0
 
LVL 6

Expert Comment

by:Mindphaser
Comment Utility
mdavis1982

LukA_YJK gave you the code AND the documentation you requested. Please accept his proposed answer now or provide feedback if you any further help.

I will give you 3 days, after that I will force-accept this question.

** Mindphaser - Community Support Moderator **
0
 
LVL 6

Expert Comment

by:Mindphaser
Comment Utility
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now