Solved

Generating MIDI Files...

Posted on 2002-07-29
9
1,070 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7187280
0
 
LVL 3

Expert Comment

by:LukA_YJK
ID: 7189820
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
ID: 7197746
I checked my comment above and it works good (at least here) so I propose it as an Answer...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mdavis1982
ID: 7198042
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
 
LVL 3

Expert Comment

by:LukA_YJK
ID: 7198076
Is it working (there) ?
0
 
LVL 3

Expert Comment

by:LukA_YJK
ID: 7202303
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
ID: 7212202
Ask what portion is not clear enough...
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7267829
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
ID: 7284907
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 160
Strange behavior when a form is closed 6 79
Convert MS Word document to a PDF file 9 136
enhance the following code 3 54
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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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