Page 1 of 1

Streaming audio with Ogre + discussion about threads.

Posted: Mon Jul 07, 2014 12:35 pm
by thegooseking
Hi everyone.

I'm not really sure whether this should be in Ogre Help or non-Ogre help. I'm going to go with non-Ogre help.

I've managed to get a grip on streaming audio with Ogg Vorbis and OpenAL, and I was looking for some opinions on the best way to integrate that into Ogre.

As far as I can see it, my options are:-

(1) Do it quick and dirty, call my stream's update function from my Ogre handler class' frameRenderingQueued().
(2) Create my own implementation of FrameListener to handle my audio stream, register it with Ogre::Root and use its frameRenderingQueued() function to call the stream update.
(3) Run them in separate threads.

Every fibre of my programming instinct rejects option 1, not least because my Ogre handler class shouldn't know anything about audio streams.

I'm thinking I might have to do both 2 and 3. Updating the stream in my render thread maybe wouldn't be such a big deal (depending on how big my stream buffer is?), but if I'm going to need another thread anyway for playing the stream, it would make sense to update the stream in that thread, too. Is that right? But if I'm running it in a separate thread, is there any point in even using a FrameListener? I was never very confident with threads even in Java, and I've never actually used them in C++ (I have in C, but that was (a) once, (b) a long time ago and only half-remembered, and (c) on Solaris, using POSIX threads which Windows doesn't support natively), so any advice would be welcome.

Or am I completely on the wrong track? What did (the now-seemingly-defunct?) OgreAL do?

Thanks.

BTW: I have to admit I have done this a bit backwards, and learned about streaming before learning about loading sounds in their entirety.

Re: Streaming audio with Ogre + discussion about threads.

Posted: Mon Jul 07, 2014 12:48 pm
by Klaim
You should _not_ rely on Ogre like if it was a game engine. Don't use the frame listener if you don't need to update things in exact sync with the graphics (like physics).

You should either run your audio streaming in another thread, or have your main loop handle both the audio and graphic rendering by calling renderOneFrame() for Ogre, and your audio update code for audio.

Re: Streaming audio with Ogre + discussion about threads.

Posted: Mon Jul 07, 2014 1:05 pm
by thegooseking
Thanks. That's pretty much what I thought, but I wondered if there might be some reason I didn't know about that it would need to be synced with the graphics. Maybe something advanced like streaming dialogue that needs to be lip-synced in real time would, but I guess background music wouldn't.

Let's see if I can remember how threads work. This seems like a very good explanation, albeit Windows-specific (which is what I want right now, but may not want in future).

For some reason my C++ book (C++ for Game Programmers by Michael Dickheiser) doesn't even mention threads, which is weird - it goes over stuff that's a lot more basic than threads, so it's not simply "you should already know this", and it's aimed at game programmers, who are inevitably going to need threads. I suppose one could make an argument that threads are a feature of the OS rather than a feature of C++, but that's stretching it a bit.

Re: Streaming audio with Ogre + discussion about threads.

Posted: Mon Jul 07, 2014 3:03 pm
by Klaim
I recommend the Game Engine Architecture book for this kind of info.

However, if you don't know, just write your own main loop and make sure that graphics and audio are running in sync yourself. If you need synching then you need to have them all on the same thread (the main one or another reserved for output rendering, for example).

Re: Streaming audio with Ogre + discussion about threads.

Posted: Mon Jul 07, 2014 3:18 pm
by thegooseking
I've actually pretty quickly managed to split my audio off into a separate thread (I'm still rendering in the main thread at this early exploration stage). There is some synchronisation I'd like to do - right now the audio starts playing before Ogre's finished loading - but I think I can see how to address that. That was a lot less painful than I thought it was going to be.

I definitely think I'll need to stop coding and do some proper architecture design work before I go much further. Maybe I'll do that over the next week, since I'll be away from my computer, so I can't code anyway.

Thanks again for your help!