PDA

View Full Version : [DirectSound][FreePascal 2.4.x]Playing OGG in separate threads



Andru
30-09-2010, 10:47 AM
Hello everyone!

I have one problem with FreePascal 2.4.x and DirectSound - my application crash when I use two and more threads for playing OGG. But I have no problems when I compile my code by FreePascal 2.2.4 and any version of Delphi(7,2009,2010). Maybe this is a luck that I have no problems with Delphi, but maybe I do something wrong. Can you help me? :)

Here (http://www.andru-kun.inf.ua/tmp/dsound-ogg.7z) you can find source code and all needed dll's. Examples only for Lazarus, but you can adopt them for Delphi(there are only few lines of code in unit1.pas). Directory "source" contains "lite" version of modules from ZenGL library. "Problematic" functions - snd_PlayFile and snd_ProcFile(thread function) in zgl_sound.pas. DirectSound stuff contained in zgl_sound_dsound.pas. All other code works fine, because there are no problems with OpenAL variant.

Oh... there is one problem with ButtonClick function in unit1.pas, so here correct code for example:


var
t : Double;
a : Integer;
begin
wnd_Handle := Form1.Handle; // getting handle of Window for DirectSound

snd_Init(); // Sound subsystem initialization

snd_PlayFile( 'music.ogg' ); // creating first thread for playing
t := timer_GetTicks();

while timer_GetTicks() - t < 1000 do; // waiting one second
a := snd_PlayFile( 'music.ogg' ); // creating second thread for playing

while sfStream[ a ]._playing do; // waiting before second thread end playing
end;

chronozphere
30-09-2010, 11:18 AM
I don't have any experience with directsound, so I'm afraid I cant really help you.

But why are you using DirectSound? First of all, it will only work on windows. Moreover, I've heard that it's a very hard API to work with (allthough you seem to be using a wrapper). Have you looked into OpenAL or SDL Mixer? Those seem quite good to me, especially for games. :)

Andru
30-09-2010, 11:38 AM
But why are you using DirectSound? First of all, it will only work on windows.
Yeah, I know about this and I'm using Linux on my home PC :) Also I have OpenAL variant of OGG streaming(for Linux and MacOS X), but I want use DirectSound on Windows, because with it there is no need in 3rd party heavy dll's :)

chronozphere
30-09-2010, 11:49 AM
Why go through all the trouble, just to get rid of one or two DLL's? If you can make the game work with OpenAL on both linux and windows, you're done. Generally, users don't worry about DLL's.

Also, the OpenAL installer is really lightweight, as far as I can tell. I normally include the installer when I distribute my games. It's about 750kb. :)

Andru
30-09-2010, 11:54 AM
Why go through all the trouble, just to get rid of one or two DLL's?
Because of fun and I have some principles :) So, I want understand why my code didn't work correct with FreePascal 2.4.x

User137
30-09-2010, 07:26 PM
I don't know if this has anything to do with the problem but posting anyway. It's about pointer size and integers, which work differently than Delphi i presume:
http://wiki.freepascal.org/Integer

Andru
30-09-2010, 07:39 PM
User137
Thanks for answer, but no, problem not in pointer size or something like that, because I use my own type(when calculate an address), and if the problem was in it - it will be reproducibly with one thread and any version of FreePascal.

Andru
01-10-2010, 12:42 PM
So... if nobody don't want to watch my code, or can't help me with answer "why demo doesn't work in FreePascal 2.4.x"- who can show me some examples of ogg streaming with DirectSound? With Pascal source code of course :) Maybe problem only in this version of FreePascal, but developers on official bugzilla not interested in this "bug".

code_glitch
01-10-2010, 05:16 PM
Been annoyed with audio (especially mp3) in pascal, resorted to BassFPC. Could anyone suggest a good OpenAl tutorial and whether it can play mp3? Thanks.
And sorry for posting this here, but this was recent more or less on topic and that way I didn't clutter the forum.

Mods, if you feel this belongs elsewhere, please move it or if you don't have time mail me and I will move it.

cheers,
code_glitch

~EDIT~
Oh, and PS. I stuck with 2.2.4 since in 2.4.0 (i tried it early on) it really got to me with its new error messages and etc.. drove me insane. 120 lines code, 170 errors it went on a test program... Is it still this glitchy? Darn it FPC WHYYYYYYYYY?

chronozphere
01-10-2010, 05:38 PM
So... if nobody don't want to watch my code, or can't help me with answer "why demo doesn't work in FreePascal 2.4.x"- who can show me some examples of ogg streaming with DirectSound? With Pascal source code of course :) Maybe problem only in this version of FreePascal, but developers on official bugzilla not interested in this "bug".

Well, My guess is that nobody here is using directsound. Actually, I wouldn't recommend using it at all. Just read this:

http://en.wikipedia.org/wiki/DirectSound#Windows_Vista

Aside from being an awefull API to work with (as far as I've seen it), it is deprecated. Windows vista has a new audio stack that doesn't directly support the DirectSound drivers. It is still supported through emulation, but a lot of features won't work anymore. It doesn't really supprise me that nobody is interested in fixing this bug.
Overall, I think that OpenAL is a way better option. It has more features, better support and it doesn't suffer from these problems. :)

Andru
01-10-2010, 05:57 PM
Could anyone suggest a good OpenAl tutorial and whether it can play mp3?
What wrong with standard FreePascal OpenAL example? It simple and can play mp3(using libmad), ogg, dts and other formats. But mp3 sucks :)

chronozphere
01-10-2010, 08:04 PM
For OpenAL tutorials, you should have a look here. :)

http://www.noeska.com/doal/tutorials.aspx

There is an OGG playback tutorial that will explain how to stream and decode audio from your harddrive. This will almost work the same for MP3. You only need a different library for decoding. I believe I have some sourcecode for that (taken from a tutorial, but I can't find it now). :)

Edit: I noticed that one of the tutorials DOES cover MP3 briefly. It uses the mpg123 library. Have a look at that. ;)

WILL
02-10-2010, 08:37 AM
OpenAL is quite nice to work with. And it's 3D sound by default. :)

Andru
05-10-2010, 08:12 PM
I found where was a problem... So, now I can say - do not use CreateThread when you use FreePascal 2.4.x and later. Maybe in future releases there will be a fix, but for now better way to use BeginThread.