Solved

embed a midi file

Posted on 2002-03-04
5
1,333 Views
Last Modified: 2007-12-19
How can I embed a midi file into my exe. I want to use the midiaplayer component but I want the midi file to compiled within the exe.
Thank you,
Paul

Beautiful! Thank you tonqalite. Now how to I cause the resource file to "capture" the midi?
Thanks
Paul :)
0
Comment
Question by:annas
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
tongalite earned 50 total points
ID: 6840604
Hi
you can compile the mid file into a Resource
file then use ResourceStream  to play the file
with TMediaplayer...

//code snippet

implementation

{$R *.DFM}
{$R myres.res}

procedure TForm1.Button1Click(Sender: TObject);

var

 fileStr:TFileStream;
 restr:TResourcestream;
begin

 restr := TResourceStream.Create(hInstance, 'MYMIDI', RT_RCDATA);

 fileStr := TFileStream.Create('C:\temp.mid',fmCreate or fmOpenWrite);
 fileStr.CopyFrom(restr, restr.size);
 fileStr.free;
 restr.free;

     MediaPlayer1.filename := 'C:\temp.mid';
     MediaPlayer1.open;
     MediaPlayer1.play;
end;

end.

If you need any help with the resource file just ask.
Have fun
T.
0
 

Author Comment

by:annas
ID: 6842596
tonqualite how do i write the Resource file so that it will include my midi file?
Paul :)
0
 
LVL 6

Expert Comment

by:reddarin
ID: 6853635
Hi Paul,

You do it like so:

Put the midi file in your project directory.

Open a simple text editor (notepade) and add your resource line in this manner:

[Name you'll refer to in program] [filetype] [actual file in quotes]

It will look like this

MyMidi MIDI "MidiFile.mid"

...then save and close the text file. Rename the text file to whatever you like and make the extension .rc

Then shell to Dos and run the resource compiler, brcc32 with the file name of the file you just created:

brcc32 MidiRes.rc (for example)

That compiles it, which creates the .res file, and now it is ready to be included in your program as tongalite has shown.

Here is a site with an example similar to what I said:

http://www.jansfreeware.com/articles/delphiresource.html

reddarin
0
 
LVL 3

Expert Comment

by:tongalite
ID: 6853648

Hi annas...
Here is some useful info I put together from various sources plus my own section "Setting up a Resource Workshop"
hope thia helps.
tongalite. sorry it's taken so long to get back to you :)

*****************************************
RESOURCE FILES .tongalite 2001

Resources
Bitmaps, Wav, MIDI, Icons, Cursors, String tables... Every Windows program uses resources. Resources are those elements of a program that support the program but are not executable code. In this article, we will walk through some examples of the use of bitmaps, icons and cursors from resources.

Placing resources in the .exe file has two main advantages:
- The resources can be accessed more quickly because it takes less time to locate a resource in the executable file than it does to load it from a disk file.
- The program file and resources can be contained in a single unit (the .exe file) without the need for a lot of supporting files.

The Image Editor
First off all, we need to create a resource file. Default extension for resource files is .RES. Resource files can be created with Delphi's Image Editor. Read in Help files on working with the Image Editor.
 

 Step 1, Setting up a resource workshop
First create a new folder and name it RESOURCES (this will be the base for your resources workshop, and all future resource files that you need to create.)
Next, from your Delphi bin locate and copy these two files: BRCC32 .exe and Rw32core.dll. Paste them into your newly created RESOURCES folder.
Also create a shortcut to Windows Notepad in the folder. (you will use this to create .rc files used to make resource files) You now have the tools at your disposal to begin creating Res files. The next step is to create a .rc file which will tell the resource compiler (BRCC32 .exe) what to look for.
Step 2, Making .rc files
Open Notepad and type in the the details of the file you wish to compile into a res file. This could be a Bitmap, a wav, Midi,icon, jpeg, avi or whatever. For this first example we will use a WAV file. Let's assume you have a Wav file called Mywave. You would type this into Notepad: TEST WAVE "Mywave.wav" The first reference <TEST> is the name you will refer to in your code. The 2nd parameter <WAVE> could be anything, and the 3rd parameter "MyWave.wav" is the actual name of your file with its extension. (another example could be TEST BITMAP "MyBitmap.bmp") OK, you have typed your info in to Notepad, now save the text file as (example) Myres.rc. Now make sure your file to be converted is in the RESOURCES folder. then drag the .rc file onto the BRCC32.exe icon, your resource file will be automatically created.
Step3, Adding the res file to your project:  
export the file < Myres.Res > to your Delphi Project folder. Open a new Delphi project and add this compiler directive: {$R Myres.RES} beneath the Implementation and under {$R *.DFM}. Also add MMSystem to your uses clause.Save your project to the new project folder where you exported the RES file earlier.
When ready to play your sounds, use the following line of code in the begin and end block (on a button click event):

PlaySound(PChar('TEST'), hInstance, snd_ASync or snd_Resource);
 

The TEST is the name you gave your sound in the *.RC file. Look in Help for more details on PlaySound.
This technique will make your executable a bit larger because the *.wav files are now an integral part. However, it makes it easy to add sound to your applications, and you still have no DLL or WAV files that must be kept in
synch.

Other resource types.
param1                        param2                     param3
RESOURCENAME RESOURCETYPE FILENAME

For example, to include a bitmap in the resource file: IMAGE BITMAP MyBitmap.bmp

To include a cursor:
POINTER CURSOR MyCursor.cur
You use the same technique to include AVI, WAV and ICO files.

For other filetypes, you can use the type RC_DATA.

You can name the resource file anything you want, as long as it has the extension ".RES" and the filename without the extension is not the same as any unit or project filename. This is important, because by default, each Delphi project that compiles into an application has a resource file with the same name as the project file, but with the extension .RES. It's best to save the file to the same directory as your project file.

   Including resources in applications
In order to access our own resource file we have to tell Delphi to link our resource file in with our application. This is accomplished by adding a compiler directive to the source code. This directive needs to immediately follow the form directive, like the following:Do not accidentally erase {$R *.DFM} part, as this is the line of code that tells Delphi to link in the form's visual part. When you choose bitmaps for speed buttons, Image components or Button components, Delphi includes the bitmap file you chose as part of the form's resource. Delphi isolates your user interface elements into the .DFM file.To actually use the resource, you must make a few Windows API calls. Bitmaps, cursors and icons stored in RES files can be retrieved by using the API functions LoadBitmap, LoadCursor and LoadIcon respectively.
   Pictures in resources
The first example shows how to load a bitmap stored as a resource and display it in a TImage component.
  procedure TfrMain.btnCanvasPic(Sender: TObject);
var bBitmap : TBitmap;
begin
 bBitmap := TBitmap.Create;
 try
  bBitmap.Handle := LoadBitmap(hInstance, 'ATHENA');
  Image1.Width := bBitmap.Width;
  Image1.Height := bBitmap.Height;
  Image1.Canvas.Draw(0,0,bBitmap);
 finally
  bBitmap.Free;
 end;
end;

 
Note: If the bitmap that is to be loaded is not in the resource file, the program will still run, it just won't display the bitmap. This situation can be avoided by testing to see if the bBitmap.Handle is zero after a call to LoadBitmap() and taking the appropriate steps. The try/finally part in the previous code doesn't solve this problem, it is just here to make sure that the bBitmap is destroyed and its associated memory is freed.

Another way we can use to display a bitmap from resource is as follows:

procedure TfrMain.btnLoadPicClick(Sender: TObject);
begin
 Image1.Picture.Bitmap.
   LoadFromResourceName(hInstance,'MYIMAGE');
end;

 
 Cursors in resources
As discussed in the previous article Screen.Cursors[] is an array of cursors supplied by Delphi. By using resource files, we can add custom cursor to the Cursors property. Unless we wish to replace any of the defaults, the best strategy is to use cursor numbers starting from 1.
procedure TfrMain.btnUseCursorClick(Sender: TObject);
 const NewCursor = 1;
begin
 Screen.Cursors[NewCursor] :=
    LoadCursor(hInstance,'WAND');
 Image1.Cursor := NewCursor;
end;

 
 


Icons in Resources
If we look at Delphi's Project-Options-Application settings, we can find that Delphi supplies the default icon for a project. This icon represents the application in the Windows Explorer and when the application is minimized. We can easily change this by clicking the 'Load Icon' button.
If we want, for example, to animate the program's icon when the program is minimized than the following code will do the job.
For the animation, we need a TTimer component on a form. The code loads two icons from resource file into an array of TIcon objects; this array needs to be declared in the public part of the main form. We'll also need NrIco, that is an Integer type variable, declared in the public part. The NrIco is used to keep track of the next icon to show.
  public
    nrIco : Integer;
  MinIcon : array[0..1] of TIcon;
...
procedure TfrMain.FormCreate(Sender: TObject);
begin
 MinIcon[0]:=TIcon.Create;
 MinIcon[1]:=TIcon.Create;
 MinIcon[0].Handle:=LoadIcon(hInstance,'ICOOK');
 MinIcon[1].Handle:=LoadIcon(hInstance,'ICOFOLD');
 NrIco:=0;
 Timer1.Interval:=200;
end;
...
procedure TfrMain.Timer1Timer(Sender: TObject);
begin
 if IsIconic(Application.Handle) then begin
  NrIco:=(NrIco+1) mod 2;
  Application.Icon:=MinIcon[NrIco];
 end;
end;
...
procedure TfrMain.FormDestroy(Sender: TObject);
begin
 MinIcon[0].Free;
 MinIcon[1].Free;
end;

 
 


 

 Final words
Anything (well, not everything) can be put in resource files. This article has shown you how to use resources to use/display bitmap, cursor or an icon in your Delphi application.  
Note: When we save a Delphi project to the disk, Delphi automatically creates one .RES file that has the same name as the project (if nothing else, the main icon of the project is inside). Although you can alter this resource file, this is not advisable.
 
 
0
 

Author Comment

by:annas
ID: 6854031
Thank you reddarin and tongalite! With this much information I should haveno trouble including a midi (or any other file) in my Exe. Again Thank you very much!
Paul (annas)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

18 Experts available now in Live!

Get 1:1 Help Now