Solved

Generating MIDI Files...

Posted on 2002-07-29
9
1,087 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month6 days, 5 hours left to enroll

627 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