Sign in Join

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

Mailing lists / fpc-pascal / 2012-06

Playing sounds with standard components

25 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

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

luciano de souza

Hello all,

Bass is a very good library for sound playing and recording. However, it's not included as an standard feature of Freepascal. For whom wich wants to work with standard components, are there other options: (1) to play sounds and (2) to record sounds?

Regards,
Luciano

Reply by email

Jorge Aldo G. de F. Junior
to luciano de souza

You can use OpenAL or SDL...

2012/6/9 luciano de souza :

Show quoted text 12 lines
Hello all, Bass is a very good library for sound playing and recording. However, it's not included as an standard feature of Freepascal. For whom wich wants to work with standard components, are there other options: (1) to play sounds and (2) to record sounds? Regards, Luciano _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

luciano de souza
to luciano de souza: Bass is a very good library for sound playing and recording. However, it's not included…

I took a look in Openal examples. I need to confess that I expected something easier. Perhaps, something like:

PlaySound('file.wav', 0)

In MMSystem, we have something like that, but it's not cross platform.

The examples present hundreds of lines. It's something frightful!

Regarding the complexity, probably is's possible to do much more than simply play a sound. In spite of that, it would be intersting to have something easier.

The examples work with complex and for me, abstract structures. Which kind of content, do I need to study to understand Openal or SDL?

2012/6/9, luciano de souza :

Show quoted text 10 lines
Hello all, Bass is a very good library for sound playing and recording. However, it's not included as an standard feature of Freepascal. For whom wich wants to work with standard components, are there other options: (1) to play sounds and (2) to record sounds? Regards, Luciano

Reply by email

Ludo Brands
to luciano de souza
Show quoted text 17 lines
I took a look in Openal examples. I need to confess that I expected something easier. Perhaps, something like: PlaySound('file.wav', 0) In MMSystem, we have something like that, but it's not cross platform. The examples present hundreds of lines. It's something frightful! Regarding the complexity, probably is's possible to do much more than simply play a sound. In spite of that, it would be intersting to have something easier. The examples work with complex and for me, abstract structures. Which kind of content, do I need to study to understand Openal or SDL?

On the lazarus forum, kpjcomp made a wrapper for Openal. Haven't tested it but his example code is as simple as:

  openAl := TLazOpenAL.Create;
  wav1 := TLazOpenALWavFileSource.Create('drumloop.wav');
  wav1.looping := true;
  openal.SourceList.Add(wav1);
  wav2 := TLazOpenALWavFileSource.Create('chimes.wav');
  openal.SourceList.Add(wav2);
  wav1.play;
  wav2.play;

Perhaps worth looking at.

Ludo

Reply by email

Michalis Kamburelis
to luciano de souza: I took a look in Openal examples. I need to confess that I expected something easier.…
luciano de souza wrote 5 lines
I took a look in Openal examples. I need to confess that I expected something easier. Perhaps, something like: PlaySound('file.wav', 0)

You need to wrap OpenAL a little to get something so simple.

In my Castle Game Engine (http://castle-engine.sourceforge.net/), I have SoundEngine wrapper, with which you can play sound like

   Buffer := SoundEngine.LoadBuffer(FileName);
   SoundEngine.PlaySound(Buffer, false, ...);

See working example in SVN on
http://svn.code.sf.net/p/castle-engine/code/trunk/castle_game_engine/examples/audio/alplay.lpr

You may want to look at the source (LGPL) to see how it's done :)

Michalis

Reply by email

silvioprog
to Ludo Brands: On the lazarus forum, kpjcomp made a wrapper for Openal. Haven't tested it but his…

2012/6/11 Ludo Brands :
[...]

Show quoted text 18 lines
On the lazarus forum, kpjcomp made a wrapper for Openal. Haven't tested it but his example code is as simple as:  openAl := TLazOpenAL.Create;  wav1 := TLazOpenALWavFileSource.Create('drumloop.wav');  wav1.looping := true;  openal.SourceList.Add(wav1);  wav2 := TLazOpenALWavFileSource.Create('chimes.wav');  openal.SourceList.Add(wav2);  wav1.play;  wav2.play; Perhaps worth looking at. Ludo

Hello Ludo. :)

Where do I find the openal_nt unit?

unit1.pas(9,3) Fatal: Can not find unit openal_nt used by Unit1.
Silvio Clécio My public projects - github.com/silvioprog

Reply by email

silvioprog
to Michalis Kamburelis: You need to wrap OpenAL a little to get something so simple. In my Castle Game Engine…

2012/6/11 Michalis Kamburelis :

Show quoted text 22 lines
luciano de souza wrote: > > I took a look in Openal examples. I need to confess that I expected > something easier. Perhaps, something like: > > PlaySound('file.wav', 0) > You need to wrap OpenAL a little to get something so simple. In my Castle Game Engine (http://castle-engine.sourceforge.net/), I have SoundEngine wrapper, with which you can play sound like  Buffer := SoundEngine.LoadBuffer(FileName);  SoundEngine.PlaySound(Buffer, false, ...); See working example in SVN on http://svn.code.sf.net/p/castle-engine/code/trunk/castle_game_engine/examples/audio/alplay.lpr You may want to look at the source (LGPL) to see how it's done :) Michalis

Nice project. :)

castle_game_engine-3.0.0-src.zip = +-70MB.

Hm... you could send only the files to play audio? :/

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

Michalis Kamburelis
to silvioprog
silvioprog wrote
Nice project. :) castle_game_engine-3.0.0-src.zip = +-70MB.

Most of the size comes from the examples, they contain also example 3D data files (models, animations etc.). I prefer to give people full package :) For those who want to get parts, they can always check out appropriate directories from SVN.

Hm... you could send only the files to play audio? :/

The directories src/base/ and src/audio/ from the engine source code should be all you need. And examples inside examples/audio/, and examples/3d_sound_game/ (only in SVN), are probably useful :) Note that if you only get a subset of src/ directories, you will need to prepare also special Lazarus package to compile them (as the default package inside packages/castle_base.lpk includes other units, to deal with 3D, images, scripting and such), or compile only with ./xxx_compile.sh scripts (Unix or Cygwin required). It will be easier to just get the full zip/tar.gz archive, and simply ignore (don't look inside :) directories that don't interest you :)

Michalis

Reply by email

silvioprog
to Michalis Kamburelis

2012/6/11 Michalis Kamburelis :

Show quoted text 27 lines
silvioprog wrote: > > Nice project. :) > > castle_game_engine-3.0.0-src.zip = +-70MB. > Most of the size comes from the examples, they contain also example 3D data files (models, animations etc.). I prefer to give people full package :) For those who want to get parts, they can always check out appropriate directories from SVN. > Hm... you could send only the files to play audio? :/ The directories src/base/ and src/audio/ from the engine source code should be all you need. And examples inside examples/audio/, and examples/3d_sound_game/ (only in SVN), are probably useful :) Note that if you only get a subset of src/ directories, you will need to prepare also special Lazarus package to compile them (as the default package inside packages/castle_base.lpk includes other units, to deal with 3D, images, scripting and such), or compile only with ./xxx_compile.sh scripts (Unix or Cygwin required). It will be easier to just get the full zip/tar.gz archive, and simply ignore (don't look inside :) directories that don't interest you :) Michalis

Does not seem very easy to use hehehe.., but I will try test it later. :)

Thx Michalis! ^^

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

Graeme Geldenhuys
to Unknown author
SDL is so easy, it's hardly necessary to create a wrapper.

+1

And because SDL is a cross platform framework, there is nothing special to do in your code to get it to work on multiple platforms.

Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Reply by email

silvioprog
to Graeme Geldenhuys

2012/6/12 Graeme Geldenhuys :

Show quoted text 14 lines
On 12 June 2012 09:49,   wrote: > > > SDL is so easy, it's hardly necessary to create a wrapper. +1 And because SDL is a cross platform framework, there is nothing special to do in your code to get it to work on multiple platforms. -- Regards,   - Graeme -

So, how to play audio (wav/mp3) with only SDL framework in Free Pascal?

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

Graeme Geldenhuys
to silvioprog
silvioprog wrote
So, how to play audio (wav/mp3) with only SDL framework in Free Pascal?

Simply reading a SDL tutorial or two, will quickly let you understand the following code. TSoundEngine is just an convenience class we created to add extra sound events and possibly swap out sound library frameworks in future - without needing to change our application code. Simple design pattern practices to simply your application code and hide complexity.

This is not the complete code, but is the gist of it.

uses
sdl_mixer,

  sdl;
const
  // Global default extension for sound files. Set up as needed.
  SoundExt           = '.wav';
type
  PSoundBlock = {$IFDEF UseMusic}PMIX_Music{$ELSE}PMIX_Chunk{$ENDIF};
  ...
 { in our project, this code is in a TSoundPlayer class. Result is of
type PSoundBlock }
    Result := {$IFDEF
UseMusic}Mix_LoadMUS{$ELSE}Mix_LoadWAV{$ENDIF}(PChar(aFilename));
    if not Assigned(Result) then
      SoundEngine.MixError;
  ...
constructor TSoundEngine.Create;
begin
  if SDL_Init(SDL_INIT_AUDIO or SDL_INIT_TIMER) = -1 then
    SDLError;
  if Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT,
MIX_DEFAULT_CHANNELS, 1024) = -1 then
    MixError;
  {$IFDEF UseMusic}Mix_HookMusicFinished{$ELSE}Mix_ChannelFinished{$ENDIF}(@MusicFinished);
end;
destructor TSoundEngine.Destroy;
begin
  PlayingStop;
  MIX_CloseAudio;
  SDL_Quit;
  inherited Destroy;
end;
procedure TSoundEngine.PlayingStop;
begin
  {$IFDEF UseMusic}
  Mix_HaltMusic;
  {$ELSE}
  Mix_HaltChannel(1);
  {$ENDIF}
end;
procedure TSoundEngine.PlayingStart(aMusic: PSoundBlock);
begin
  {$IFDEF UseMusic}
  if Mix_PlayMusic(aMusic, 0)  0 then
    MixError;
  {$ELSE}
  Mix_PlayChannel(1,aMusic, 0)
  {$ENDIF}
end;
procedure TSoundEngine.PlayingPause;
begin
  {$IFDEF UseMusic}
  Mix_PauseMusic;
  {$ELSE}
  Mix_Pause (1);
  {$ENDIF}
end;
procedure TSoundEngine.PlayingContinue;
begin
  {$IFDEF UseMusic}
  Mix_ResumeMusic;
  {$ELSE}
  Mix_Resume (1);
  {$ENDIF}
end;
Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Reply by email

silvioprog
to Graeme Geldenhuys

2012/6/12 Graeme Geldenhuys :

Show quoted text 99 lines
On 12 June 2012 16:17, silvioprog wrote: > > So, how to play audio (wav/mp3) with only SDL framework in Free Pascal? Simply reading a SDL tutorial or two, will quickly let you understand the following code. TSoundEngine is just an convenience class we created to add extra sound events and possibly swap out sound library frameworks in future - without needing to change our application code. Simple design pattern practices to simply your application code and hide complexity. This is not the complete code, but is the gist of it. uses  sdl_mixer,  sdl; const  // Global default extension for sound files. Set up as needed.  SoundExt           = '.wav'; type  PSoundBlock = {$IFDEF UseMusic}PMIX_Music{$ELSE}PMIX_Chunk{$ENDIF};  ...  { in our project, this code is in a TSoundPlayer class. Result is of type PSoundBlock }    Result := {$IFDEF UseMusic}Mix_LoadMUS{$ELSE}Mix_LoadWAV{$ENDIF}(PChar(aFilename));    if not Assigned(Result) then      SoundEngine.MixError;  ... constructor TSoundEngine.Create; begin  if SDL_Init(SDL_INIT_AUDIO or SDL_INIT_TIMER) = -1 then    SDLError;  if Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024) = -1 then    MixError;  {$IFDEF UseMusic}Mix_HookMusicFinished{$ELSE}Mix_ChannelFinished{$ENDIF}(@MusicFinished); end; destructor TSoundEngine.Destroy; begin  PlayingStop;  MIX_CloseAudio;  SDL_Quit;  inherited Destroy; end; procedure TSoundEngine.PlayingStop; begin  {$IFDEF UseMusic}  Mix_HaltMusic;  {$ELSE}  Mix_HaltChannel(1);  {$ENDIF} end; procedure TSoundEngine.PlayingStart(aMusic: PSoundBlock); begin  {$IFDEF UseMusic}  if Mix_PlayMusic(aMusic, 0) 0 then    MixError;  {$ELSE}  Mix_PlayChannel(1,aMusic, 0)  {$ENDIF} end; procedure TSoundEngine.PlayingPause; begin  {$IFDEF UseMusic}  Mix_PauseMusic;  {$ELSE}  Mix_Pause (1);  {$ENDIF} end; procedure TSoundEngine.PlayingContinue; begin  {$IFDEF UseMusic}  Mix_ResumeMusic;  {$ELSE}  Mix_Resume (1);  {$ENDIF} end; -- Regards,   - Graeme -

Hellow Graeme! :)

I'll try to test it tomorrow and return here with the results ...

Thank you in advance guy! :)

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

silvioprog
to Graeme Geldenhuys: Simply reading a SDL tutorial or two, will quickly let you understand the following code.…

2012/6/12 Graeme Geldenhuys :

Show quoted text 10 lines
On 12 June 2012 16:17, silvioprog wrote: > So, how to play audio (wav/mp3) with only SDL framework in Free Pascal? Simply reading a SDL tutorial or two, will quickly let you understand the following code. TSoundEngine is just an convenience class we created to add extra sound events and possibly swap out sound library frameworks in future - without needing to change our application code. Simple design pattern practices to simply your application code and hide complexity. This is not the complete code, but is the gist of it.

[...]

I tried playing a wav file with this demo in attached, no errors, but no sound came out. :(

I downloaded all DLLs here:

http://www.libsdl.org/

http://www.libsdl.org/projects/SDL_mixer/

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

Graeme Geldenhuys
to silvioprog
silvioprog wrote
I tried playing a wav file with this demo in attached, no errors, but no sound came out. :(

You are releasing the TSoundEngine instance to quickly, and don't give SDL chance to initialize fully and play the sound. Simply add a sleep(1000) call [just for demonstration purposes] after you called PlayingStart(), then you will hear the sound.

A better idea would be to change your code as the attached unit. Keep the TSoundEngine instance in the form (or as a global variable), so then playing any sounds are instant.

Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Reply by email

Graeme Geldenhuys
silvioprog wrote 6 lines
With: {.$define UseMusic} I do not hear sound ... :/

I don't know what to say, other than.... It works here - with or without the UseMusic define. And as I mentioned, we have a project that uses SDL a lot, on Linux and Windows, and tested on many versions of each of those OS'es.

I'm not on my normal development PC now - I'm using a 8 year old P4 Dell laptop with the following setup....

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

c:\dev\fpc\2.5.1\bin\i386-win32>fpc -i
Free Pascal Compiler version 2.5.1

Compiler Date : 2011/11/26
Compiler CPU Target: i386

I'm also using Lazarus 0.9.30, but the version of Lazarus shouldn't make any difference with the SDL support.

Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Reply by email

Graeme Geldenhuys
to silvioprog

Hi,

silvioprog wrote
YYYYYYYYYEEEEEEEEEEEESSSSSSSS!!!!!!!!!! Worked fine with this demo in attached!

Glad you got it working in the end.

I found this demo in:

...and if you look carefully, my sample code called the exact same SDL API's in the same order as the demo link your referenced.

Anyway, glad you got the sound playing going. :)

Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Reply by email

silvioprog
to Graeme Geldenhuys

2012/6/14 Graeme Geldenhuys :

Show quoted text 24 lines
Hi, On 14 June 2012 02:19, silvioprog wrote: > YYYYYYYYYEEEEEEEEEEEESSSSSSSS!!!!!!!!!! > > Worked fine with this demo in attached! Glad you got it working in the end. > I found this demo in: ...and if you look carefully, my sample code called the exact same SDL API's in the same order as the demo link your referenced. Anyway, glad you got the sound playing going. :) -- Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Hello Graeme,

I changed your code to(*) and it worked fine now. :o

http://pastebin.com/wMRad25q (or in attached)

Thx again!

Silvio Clécio My public projects - github.com/silvioprog

Reply by email