cleak's picture

Easy Ogg playback using DragonOgg

Hi everyone,

I've seen a few topics about Ogg/Vorbis support in OpenTK, so I'm posting this in the hopes that others will stumble across it when searching. I've recently resurrected and overhauled DragonOgg over at SourceForge to make it game friendly. I needed a good ogg/vorbis library for my game, so I decided to use DragonOgg as a basis. After a lot of customizations and additions to my own branch, I decided to join DragonOgg and roll my changes into the trunk. I've now done this and packaged the latest version over at https://sourceforge.net/projects/dragonogg/. It's really easy to use now and works well. For example, to load and play an .ogg file, all you need to do is:

AudioClip clip= new AudioClip("GuitarSample.ogg");
clip.Play();

which will then play the sound using a background thread. I've tried it out on Windows, Mac, and Linux, and thanks to the magic of OpenTK, it works on all of them. Anyways, if you've been looking for a good audio library (something that abstracts away the details of OpenAL and file formats) like I was, I highly recommend you give this a try. It's licensed under LGPL, so you're given pretty liberal use of it.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

This is great, thanks!

Consider adding the project to the gallery, to help users searching for ogg find it.

migueltk's picture

Great job, thanks.

Can you explain the following code fragment, taken from the project "InteractivePlayer. Why does a loop from 0 to 7? Why a loop from 0 to 4? Why if (i % 2 == 0)? How to choose the correct value to Thread.Sleep? Can this be automated?

for(int i = 0; i < 7; i++)
            {
                if (i % 2 == 0)
                {
                    boingClip.Play();
                }
 
                Thread.Sleep(1024);
            }
 
            // Finally play some overlapping sounds to demonstrate a sound can
            // have more than one instance played at a time.
            for (int i = 0; i < 4; i++)
            {
                boingClip.Play();
                Thread.Sleep(200);
            }
cleak's picture
migueltk wrote:

Great job, thanks.

Can you explain the following code fragment, taken from the project "InteractivePlayer. Why does a loop from 0 to 7? Why a loop from 0 to 4? Why if (i % 2 == 0)? How to choose the correct value to Thread.Sleep? Can this be automated?

It's automated on the backend, the example probably doesn't demonstrate it well though. I've cleaned up the code in this example to make it a bit more clear:

            // Play it.  The first time a clip is played, a background thread is
            // launched to handle all audio.  Playing the clip again or playing
            // a new clip will use the already existing thread.  To avoid 
            // threading, construct your own AudioManager.
            guitarClip.Play();
 
            // Play the "boing" clip every 2 seconds during the guitar clip
            boingClip.Play();
            Thread.Sleep(2000);
 
            boingClip.Play();
            Thread.Sleep(2000);
 
            boingClip.Play();
            Thread.Sleep(2000);
 
            // Finally play some overlapping sounds to demonstrate a sound can
            // have more than one instance played at a time.
            boingClip.Play();
            Thread.Sleep(200);
 
            boingClip.Play();
            Thread.Sleep(200);
 
            boingClip.Play();
            Thread.Sleep(200);
 
            boingClip.Play();
            Thread.Sleep(200);
 
            // Give the last "boing" time to finish
            Thread.Sleep(800);

It has the same functionality. Let me know what you think.

The values for Thread.Sleep are just delays in milliseconds. I decided to play the "boing" effect every 2 seconds, so I just delay 2000ms each time. The other ones are similar delays, and the last one (of 800ms) is just so the application doesn't exit before the last sound finishes. I've also just added an example using WinForms, which I think demonstrates better how this could be used in most OpenTK applications (it avoids any calls to Thread.Sleep).

migueltk's picture

Thanks for the explanation. Working with sound is new to me.

james_lohr's picture

It seems to be lacking basic functionality, such as Stop / Pause, or am I missing something?

[edit] Nevermind, I was being lazy and not looking properly. It would appear that this functionality is in there under MediaPlayer. :)