Playing embedded sound lazarus / 1-Linux ---------------------------------------------------------------------- Ocye · 2010-06-03 01:02 I'd like to play short samples that are included as resource. On Windows I can execute sndplay() with SND_MEMORY feature but what should be used on Linux? Code: [Select]procedure TMain.PlaySound; var s:string; begin   s:=LazarusResources.Find('Chat').Value;   if (s<>'') then   begin     {$IFDEF Windows}     sndPlaySound(PChar(s),SND_NOWAIT or SND_MEMORY);     {$ELSE}     with TProcess.Create(nil) do     try       Options:=[poUsePipes];       CommandLine:='paplay '+s;       //CommandLine:='cat '+s+' | paplay';       //CommandLine:='paplay <'+s;       Execute;     finally       Free;     end;     {$ENDIF}   end;//s<>'' end; PS: Of course, "paplay" is implemented optional. ---------------------------------------------------------------------- Troodon · 2010-06-03 08:56 I hope someone has the answer that you need. Meanwhile, here is what I have been able to find out. The short answer is, I don't think you can, not with the libraries that are available for Lazarus/FPC at this time. FPC provides the mechanism for retrieving the sound string from the resource but the OS sound server (mixer) must provide an API for playing the sound from the string. There are Object Pascal headers available for the EsounD server but they don't seem to include a function similar to sndPlaySound w/ SND_MEMORY on Windows. GNOME uses PulseAudio and KDE uses Phonon; both have EsounD compatibility layers so that's not an issue. While EsounD supports sound sample caching it is unclear how that can be used to play a sound from a string variable. However, an application can play a wav file on Linux using play_file. ---------------------------------------------------------------------- Ocye · 2010-06-03 10:03 Quote from: Troodon on June 03, 2010, 10:56:02 am...However, an application can play a wav file on Linux using play_file. Would it be possible to load resource into a MemoryStream and use it for play_file()? ---------------------------------------------------------------------- Troodon · 2010-06-03 10:53 Wow! I have just realized that there is an example for Delphi in the Demo included with the EsounD headers. I don't have Linux at work so I cannot run some tests right now. Here is just the relevant part: Code: [Select]procedure TForm1.btPlayClick(Sender: TObject); var   stream: Integer;   ReadBytes: Integer;   Buffer: Pointer;   f: File; begin   GetMem(Buffer, 80000);   try     stream := esd_play_stream(ESD_PLAY or ESD_STREAM or ESD_MONO or ESD_BITS16, 44100, '', 'JEDI ESD DEMO');     AssignFile(F, 'test.raw');     Reset(F, 1);     while not eof(f) do     begin       BlockRead(f, Buffer^, 10000, ReadBytes);       __write(stream, Buffer^, ReadBytes);     end;     esd_close(stream);   finally     FreeMem(Buffer);   end; end; ---------------------------------------------------------------------- Troodon · 2010-06-05 09:54 I can build the application with the demo code but, upon trying to play the sound ('test.raw'), I get an AV exception. Two questions that I can think of at this point are: 1. Is the FPC "old" libc unit still compatible with the Kylix code, especially since there is also a newlibc unit 2. Is the RAW format compatible with a Lazarus resource string