Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Play a sound from memory without creating a file?

885 views
Skip to first unread message

Holger Stratmann

unread,
May 22, 2002, 2:37:47 PM5/22/02
to
Hello everybody,

I'd like to play some very short sounds without creating files for them.
The sounds should be loaded by http...

I've already taken a look at the documentation, but can't find anything
useful:
1) I would prefer a short and clean WinAPI-call to using a component. My
needs are pretty simple: They include a short sound and a SMALL program
:-))
2) Looks like I could play a sound from a resource - how do I get a
resource for my sound?

Any hints appreciated,
Holger

Chirag Dalal

unread,
May 22, 2002, 6:48:09 PM5/22/02
to
ISTR sndPlaySound() has an option to play wave sounds from memory directly.

- Chirag

TTransitionFX - Perform a variety of image transition effects
http://chiragdalal.tripod.com/tranfx.html

"Holger Stratmann" <Hol...@cheerful.com> wrote in message
news:3CEBE57A...@cheerful.com...

Rafael Cotta

unread,
May 22, 2002, 7:58:13 PM5/22/02
to
PlaySound(Notas[i].Memory, 0, SND_MEMORY or SND_ASYNC);

Notas[index] os an array of TMemoryStream. And do not forget MMSystem on
your uses clause.

Rafael


"Holger Stratmann" <Hol...@cheerful.com> escreveu na mensagem
news:3CEBE57A...@cheerful.com...

Chris Thornton

unread,
May 23, 2002, 9:50:44 AM5/23/02
to
> 2) Looks like I could play a sound from a resource - how do I get a
> resource for my sound?


The other posts tell you how to play it. But here's how to make a resource.

Make a file: sounds.rc, containing lines such as:
SND_BOO WAVE "boo.wav"
SND_YAY WAVE "yay.wav"


Then run this:
D:\Borland\Delphi5\Bin\brcc32.exe sounds.rc

Adjust path as appropriate.
Now you have sounds.res
Place this in your Implementation of your form:
{$R SOUNDS.RES}

To execute:
PlaySound(PChar('SND_YAY'),0,SND_ASYNC or SND_RESOURCE);
PlaySound(PChar('SND_BOO'),0,SND_ASYNC or SND_RESOURCE);

If you have lots of them, then you may want to make a seperate sounds
folder, create the .res in there, and then copy it up to where your forms
reside.


--
Try ClipMate free for 30 days!
http://www.thornsoft.com

Fauschti

unread,
May 23, 2002, 10:21:01 AM5/23/02
to
Well, i also want to implement a short sound being played from memory and
this code works fine with WAV files, but i like to play MIDI files or MP3s.
(especially midi)
how can i change the code, so that i can use MIDI files ?

i tried to make my rc file like this:

SND0 WAVE "myWave.WAV" // just for testing
SND1 MIDI "MyMidi1.MID"
SND2 MIDI "MyMidi2.MID"

the compiler does not complain, but when i try to play the sounds, only the
WAVE will be played.

i also tried to use this code: (also using MMSystem)

var
OpenStruct : TMCI_OPEN_PARMS;
MciPlayParm : TMCI_PLAY_PARMS;
HAVI : Word;

procedure TForm1.FormCreate(Sender: TObject);
begin
OpenStruct.lpstrElementName := pchar(MyMidi); // loaded from HDD
mciSendCommand(0, MCI_OPEN, MCI_OPEN_element, LongInt(@OpenStruct));
HAVI := OpenStruct.wDeviceID;
Button1.Caption := 'Play';
Button2.Caption := 'Stop';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
mciSendCommand(HAVI, MCI_PLAY, 0, Longint(@MciPlayParm));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
mciSendCommand(HAVI, MCI_STOP, 0, 0);
end;

it works fine with all media files, but they have to be loaded from HDD.
can s.o. tell me how to make this code playing files from memory ?

--
Mit freundlichen Grüßen,
Michael Faust
Alpha Interactive


Urhixidur

unread,
May 23, 2002, 12:05:25 PM5/23/02
to
Holger Stratmann <Hol...@cheerful.com> wrote in message news:<3CEBE57A...@cheerful.com>...

Here's a D3 demo I grabbed from somewhere; create the PlaySnd
project and have its form read like this:

unit PlaySnd1;

interface

uses
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
StdCtrls;

type
TfrmPlaySnd = class(TForm)
btnPlaySndFromFile: TButton;
btnPlaySndFromMemory: TButton;
btnPlaySndByLoadRes: TButton;
btnPlaySndFromRes: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnPlaySndFromFileClick(Sender: TObject);
procedure btnPlaySndFromMemoryClick(Sender: TObject);
procedure btnPlaySndByLoadResClick(Sender: TObject);
procedure btnPlaySndFromResClick(Sender: TObject);
private
{ Private declarations }
public
{ Run-Time }
end; { TfrmPlaySnd }

var
frmPlaySnd : TfrmPlaySnd;
pSound : pointer = Nil;
SoundSize : integer = 0;

implementation

{$R *.DFM}

{$R SndData.res}

uses
MMSystem;

procedure TfrmPlaySnd.FormCreate(Sender: TObject);
//This could also be done in an initialization block
var
f : file;
begin
// btnPlaySndFromMemory.Enabled := False;
AssignFile(f, 'HarpUp.wav');
FileMode := 0; //Set file access to Read-Only...
Reset(f, 1); //...Otherwise Reset will fail if the file is
Read-Only
try
SoundSize := FileSize(f);
GetMem(pSound, SoundSize);
BlockRead(f, pSound^, SoundSize);
btnPlaySndFromMemory.Enabled := True;
finally
CloseFile(f);
end; { try }
end; { FormCreate }

procedure TfrmPlaySnd.FormDestroy(Sender: TObject);
//This could also be done in a finalization block
begin
FreeMem(pSound, SoundSize);
end; { FormDestroy }

procedure TfrmPlaySnd.btnPlaySndFromFileClick(Sender: TObject);
begin
sndPlaySound('HarpUp.wav', SND_FILENAME or SND_SYNC);
end; { btnPlaySndFromFileClick }

procedure TfrmPlaySnd.btnPlaySndFromMemoryClick(Sender: TObject);
begin
sndPlaySound(pSound, SND_MEMORY or SND_SYNC);
end; { btnPlaySndFromMemoryClick }

procedure TfrmPlaySnd.btnPlaySndByLoadResClick(Sender: TObject);
{
This is very similar to btnPlaySoundFromMemory;
the only difference is in how the pointed memory is loaded with
data.
In fact, we could prepare p in FormCreate and free the resource in
FormDestroy, just like with pSound.
}
var
h : THandle;
p : pointer;
begin
SetLastError(0);
h := FindResource(hInstance, 'HARPUP', 'WAVE');
if h = 0 then raise Exception.CreateFmt('Could not find WAVE
resource HARPUP. Error %d: %s', [GetLastError,
SysErrorMessage(GetLastError)]);

SetLastError(0);
h := LoadResource(hInstance, h);
if h = 0 then raise Exception.CreateFmt('Could not load WAVE
resource HARPUP. Error %d: %s', [GetLastError,
SysErrorMessage(GetLastError)]);

try
SetLastError(0);
p := LockResource(h);
if p = Nil then raise Exception.CreateFmt('Could not lock WAVE
resource HARPUP. Error %d: %s', [GetLastError,
SysErrorMessage(GetLastError)]);

try
sndPlaySound(p, SND_MEMORY or SND_SYNC);
finally
//Not strictly necessary, actually
UnLockResource(h);
end; { try }
finally
FreeResource(h);
end; { try }
end; { btnPlaySndByLoadResClick }

procedure TfrmPlaySnd.btnPlaySndFromResClick(Sender: TObject);
begin
PlaySound('HARPUP', hInstance, SND_RESOURCE or SND_SYNC);
end; { btnPlaySndFromResClick }

end. { unit PlaySnd1 }

The form consists of four buttons: "Play Sound from File", "Play
Sound from Memory", "Play Sound by LoadRes", "Play Sound from Res".
The first one plays the HarpUp.wav file; the second plays a memory
image of that file (see FormCreate). The other two rely on the sound
resource (SndData.res); the last button plays the resource directly,
whereas the next-to-last button plays it from a resource memory image.
It should be pretty trivial to load the resource from a DLL instead
of from the executable itself.
The SndData.rc file is a one-liner:
HARPUP WAVE harpup.wav

E-mail me if you want the files (they zip down to 124,5 kb = 121,5
Kib).

Daniel U. Thibault
a.k.a. Urhixidur
a.k.a. Seigneur Bohémond de Nicée
ICQ: 4985610
AIM: Urhixidur
URL: http://www.bigfoot.com/~D.U.Thibault

0 new messages