Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2001-07

Playing sounds in Fpc?

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michael Lang

I wrote quite a lot of stuff in BPC for DOS, but do not have that much experience in Win32 programming. I'd like to have some examples of code to play wave and midi files. I'm not looking for finished players, where one just clicks with the mouse, to play a file. I'd like to have code, with which one can just play little sounds. I've heard of a programming language called Profan, in which one can use Midi, by just entering commands in the source code like play(note1, note2) etc. That would be a great thing in Fpc. Does anybody here perhaps have a unit, which provides similar functionality? I'd like to write Windows programs, that only need an Exe file, no additional sound files, and nevertheless play sounds. This was easy under DOS, by using the PC speaker, but seems to be a bit difficult under Win32.

Thanks

<*** Michael Lang ***>

Michael Van Canneyt
to Michael Lang
Michael Lang wrote 12 lines
I wrote quite a lot of stuff in BPC for DOS, but do not have that much experience in Win32 programming. I'd like to have some examples of code to play wave and midi files. I'm not looking for finished players, where one just clicks with the mouse, to play a file. I'd like to have code, with which one can just play little sounds. I've heard of a programming language called Profan, in which one can use Midi, by just entering commands in the source code like play(note1, note2) etc. That would be a great thing in Fpc. Does anybody here perhaps have a unit, which provides similar functionality? I'd like to write Windows programs, that only need an Exe file, no additional sound files, and nevertheless play sounds. This was easy under DOS, by using the PC speaker, but seems to be a bit difficult under Win32.

I don't have any knowledge of such a unit; Did you check the swag archive or the contributed units section of the web-page ?

Michael.

Andreas K. Foerster
to Michael Lang: I wrote quite a lot of stuff in BPC for DOS, but do not have that much experience in…
Michael Lang wrote 5 lines
I wrote quite a lot of stuff in BPC for DOS, but do not have that much experience in Win32 programming. I'd like to have some examples of code to play wave and midi files. I'm not looking for finished players, where one just clicks with the mouse, to play a file. I'd like to have code, with which one can just play little sounds.

It's very primitive, but it works for win32:

   uses Dos;
   procedure PlayWave(WaveFile:String);
   begin
     exec('C:\Windows\sndrec32.exe', '/play /close /embedding
'+WaveFile);
   end;

Does anybody know the equivalent parameters for the "media player"?

Thomas Schatzl
to Andreas K. Foerster

From: "Andreas K. Foerster"
Subject: Re: [fpc-pascal]Playing sounds in Fpc?

Show quoted text 15 lines
Michael Lang wrote: > [code to play wave and midi files] It's very primitive, but it works for win32: uses Dos; procedure PlayWave(WaveFile:String); begin exec('C:\Windows\sndrec32.exe', '/play /close /embedding '+WaveFile); end; Does anybody know the equivalent parameters for the "media player"?

Better use the Win32 PlaySound() method. It allows playback of all types of sound files registered within windows.
e.g.

{$mode delphi}
uses
    windows;
procedure PlayMediaFile(file : String);
begin
    // plays a sound in background from a file
    PlaySound(file, 0, SND_ASYNC or SND_FILENAME);
end;

should do the trick.

There are a lot more flags for this api call, like playing the sound from memory or resource, looping it or stop playing - it's probably best to check out the Win32 help for this function.
If you want a bit more control about the way the sound is played read the Media Control Interface (MCI) documentation. You may create some sort of "media player" using it easily (including video playback etc). Else you could try the DirectSound / DirectMusic API as well for most playback control.

Regards,
Thomas

Michael Lang
to Thomas Schatzl

Hi Thomas,

This doesn't work. Ppc386 says, that it doesn't know the PlaySound-Procedure. The "uses windows;" should import it, but it doesn't seem to work, at least not on my machine. Does it work on anybody else's here?

Thanks

<*** Michael Lang ***>

Thomas Schatzl wrote 27 lines
Better use the Win32 PlaySound() method. It allows playback of all types of sound files registered within windows. e.g. {$mode delphi} uses windows; procedure PlayMediaFile(file : String); begin // plays a sound in background from a file PlaySound(file, 0, SND_ASYNC or SND_FILENAME); end; should do the trick. There are a lot more flags for this api call, like playing the sound from memory or resource, looping it or stop playing - it's probably best to check out the Win32 help for this function. If you want a bit more control about the way the sound is played read the Media Control Interface (MCI) documentation. You may create some sort of "media player" using it easily (including video playback etc). Else you could try the DirectSound / DirectMusic API as well for most playback control. Regards, Thomas
Thomas Schatzl
to Michael Lang

Subject: [fpc-pascal]Playing sounds in Fpc?

Show quoted text 7 lines
Hi Thomas, This doesn't work. Ppc386 says, that it doesn't know the PlaySound-Procedure. The "uses windows;" should import it, but it doesn't seem to work, at least not on my machine. Does it work on anybody else's here?

Forgot that the MMSystem header stuff is not already translated for FPC: Try this code:

{$mode delphi}
uses
 windows;
// flag values for wFlags parameter
const
  SND_SYNC            = $0000;  // play synchronously (default)
  SND_ASYNC           = $0001;  // play asynchronously
  SND_NODEFAULT       = $0002;  // don't use default sound
  SND_MEMORY          = $0004;  // lpszSoundName points to a memory file
  SND_LOOP            = $0008;  // loop the sound until next sndPlaySound
  SND_NOSTOP          = $0010;  // don't stop any currently playing sound
  SND_NOWAIT          = $00002000;  // don't wait if the driver is busy
  SND_ALIAS           = $00010000;  // name is a registry alias
  SND_ALIAS_ID        = $00110000;  // alias is a predefined ID
  SND_FILENAME        = $00020000;  // name is file name
  SND_RESOURCE        = $00040004;  // name is resource name or atom
  SND_PURGE           = $0040;      // purge non-static events for task
  SND_APPLICATION     = $0080;     // look for application specific
association
function PlaySound(pszSound: PChar; hmod: HMODULE; fdwSound: DWORD): BOOL;
stdcall; external 'winmm.dll' name {$ifdef unicode}'PlaySoundW' {$else}
'PlaySoundA' {$endif};
procedure PlayMediaFile(_file : String);
begin
    // plays a sound from a file
    PlaySound(PChar(_file), 0, SND_SYNC or SND_FILENAME);
end;
begin
 PlayMediaFile(paramstr(1));
end.

This works for sure with wav and mp3 files (verified). Doesn't with midi's - didn't test other.

Regards,
Thomas

Thomas Schatzl
to Thomas Schatzl

Hi again,

[snipped PlaySound() example program] This works for sure with wav and mp3 files (verified). Doesn't with

midi's -

didn't test other.

The docs (MS Platform SDK, highly recommened to get the documentation part for free from MS ftp) read that it only works with waveform-audio based files, that's why midi files don't play.
Use the MCI api to do playback of those and other types as well.

Regards,
Thomas

Marco van de Voort
to Thomas Schatzl: Subject: [fpc-pascal]Playing sounds in Fpc? Forgot that the MMSystem header stuff is not…
Show quoted text 8 lines
> This doesn't work. Ppc386 says, that it doesn't know the > PlaySound-Procedure. The "uses windows;" should import it, but it > doesn't seem to work, at least not on my machine. Does it work on > anybody else's here? > Forgot that the MMSystem header stuff is not already translated for FPC: Try this code:

About the windows unit:

The advantage of the Windows unit is that it is entirely free, since it was reverse engineered by the djgpp/cygwin team.

However I have good experiences with Marcel van Brakel's JEDI headers for things that the Windows unit doesn't eat.