Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Generating MIDI Files...

Posted on 2002-07-29
9
Medium Priority
?
1,113 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
Technology Partners: 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 1200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

721 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