OO Design, Help in designing an abstract Sound System

Discussion about everything. New games, 3d math, development tips...
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

OO Design, Help in designing an abstract Sound System

Post by MasterGod »

As some of you might know I'm trying to make an interface for adding more then one sound engine to my project.
I've been trying hard and I've done my homework, can you please help me with what I have?
The problem currently is that when I uncomment _NGE_COMPILE_WITH_IRRKLANG_ I get compilation errors. It should be like irrlicht's DirectX/OpenGL so you won't need the libs/.h files to compile if you choose so.
What's wrong with that:

Code: Select all

// Copyright (c) 2007-2008 Tomer Nosrati
// This file is part of the "NUSoftware Game Engine".
// For conditions of distribution and use, see copyright notice in nge.h

#ifndef __I_AUDIO_DEVICE_H__
#define __I_AUDIO_DEVICE_H__

#include "../NGE_CompileConfig.h"
#include "irrlicht.h"

using namespace irr;

#ifdef _NGE_COMPILE_WITH_IRRKLANG_
#include "irrKlang.h"
using namespace irrklang;
#endif

enum E_AUDIO_DEVICE {EAD_IRRKLANG, EAD_FMOD};

class IAudioDevice : public virtual IReferenceCounted
{
public:
	virtual ~IAudioDevice() {}

#ifdef _NGE_COMPILE_WITH_IRRKLANG_
	virtual ISoundEngine* getSoundEngine() const = 0;
#endif
};

IAudioDevice* createAudioDevice(E_AUDIO_DEVICE AudioDeviceType, ILogger* irrLogger);

#endif // __I_AUDIO_DEVICE_H__

Code: Select all

// Copyright (c) 2007-2008 Tomer Nosrati
// This file is part of the "NUSoftware Game Engine".
// For conditions of distribution and use, see copyright notice in nge.h

#ifndef __I_AUDIO_DEVICE_STUB_H__
#define __I_AUDIO_DEVICE_STUB_H__

#include "IAudioDevice.h"

class CAudioDeviceStub : public IAudioDevice
{
public:
	virtual ~CAudioDeviceStub() {}
};

#endif // __I_AUDIO_DEVICE_STUB_H__

Code: Select all

// Copyright (c) 2007-2008 Tomer Nosrati
// This file is part of the "NUSoftware Game Engine".
// For conditions of distribution and use, see copyright notice in nge.h

#include "CAudioDeviceStub.h"

#ifdef _NGE_COMPILE_WITH_IRRKLANG_
#include "CIrrKlangDevice.h"
#endif

IAudioDevice* createAudioDevice(E_AUDIO_DEVICE AudioDeviceType, ILogger* irrLogger)
{
	IAudioDevice* AudioDevice = 0;

	switch(AudioDeviceType)
	{
		break;
	case EAD_IRRKLANG: 
#ifdef _NGE_COMPILE_WITH_IRRKLANG_
		AudioDevice = new CIrrKlangDevice();
		if(!AudioDevice)
			irrLogger->log("Could not create IrrKlang device", ELL_ERROR);
		break;
#endif
		irrLogger->log("IrrKlang device was not compiled into this lib. Try another one.", ELL_ERROR);
		break;		
	}

	return AudioDevice;
}

Code: Select all

// Copyright (c) 2007-2008 Tomer Nosrati
// This file is part of the "NUSoftware Game Engine".
// For conditions of distribution and use, see copyright notice in nge.h

#ifndef __C_IRRKLANG_DEVICE_H__
#define __C_IRRKLANG_DEVICE_H__

#include "CAudioDeviceStub.h"

#ifdef _NGE_COMPILE_WITH_IRRKLANG_
#include "irrKlang.h"

#if GAME_COMPILER == COMPILER_MSVC
#	pragma message ("Linking irrKlang")
#	pragma comment(lib, "irrKlang.lib")
#endif

using namespace irrklang;

class CIrrKlangDevice : public CAudioDeviceStub
{
public:
	CIrrKlangDevice();
	virtual ~CIrrKlangDevice();

	virtual ISoundEngine* getSoundEngine() const;

private:
	ISoundEngine* m_pSoundEngine;
};

#endif // _NGE_COMPILE_WITH_IRRKLANG_

#endif // __C_IRRKLANG_DEVICE_H__

Code: Select all

// Copyright (c) 2007-2008 Tomer Nosrati
// This file is part of the "NUSoftware Game Engine".
// For conditions of distribution and use, see copyright notice in nge.h

#include "CIrrKlangDevice.h"

CIrrKlangDevice::CIrrKlangDevice()
{
	m_pSoundEngine = createIrrKlangDevice();
}

CIrrKlangDevice::~CIrrKlangDevice()
{
	if(m_pSoundEngine)
	{
		m_pSoundEngine->drop();
		m_pSoundEngine = 0;
	}
}

ISoundEngine* CIrrKlangDevice::getSoundEngine() const
{
	return m_pSoundEngine;
}
Now it works, I mean what I do define the irrKlang define it compiles and works just fine, I can get the irrKlang device and I have no problems. It's when I uncomment the define when I get problems. I understand also WHY I get those errors as when ifdef-ing the declaration of getSoundEngine when I uncomment the define it doesn't declare that method hence the errors.
My question is how to design it wisely so I'll be able to uncomment and get the expected behavior (Which is like compiling irrlicht without DX8 and typing it as the desired device - getting a console msg and quitting).

Thanks!
Last edited by MasterGod on Sun Feb 10, 2008 3:15 pm, edited 1 time in total.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

I'm considering "The Abstract Factory Pattern".
Does anyone have any better idea?

P.S
Good tutorials of this pattern: (Read all of them to truly understand)
http://en.wikipedia.org/wiki/Abstract_factory
http://www.dofactory.com/Patterns/Patte ... spx#_self2
http://www.netobjectivesrepository.com/ ... oryPattern
http://www.lepus.org.uk/ref/companion/A ... actory.xml
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

<offtopic>
I just have a suggestion, MasterGod. Don't you think it would be better to have a NUSoftwareDevice, and then have the graphics, and audio, etc. as contexts. (Just a design suggestion.)
</offtopic>
TheQuestion = 2B || !2B
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Halifax wrote:<offtopic>
I just have a suggestion, MasterGod. Don't you think it would be better to have a NUSoftwareDevice, and then have the graphics, and audio, etc. as contexts. (Just a design suggestion.)
</offtopic>
Care to elaborate on my forum? :wink:
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

I've been thinking on a simple formula for the loudness of the sound relative to the listener position:
(Sound Intensity * Sound Volume * Master Volume)
/
abs(Distance from listener)
= Loudness value which I'll pass on to the chosen sound engine.

What flaws does this simple formula have?

Edit: Made the distance absolute.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

MasterGod wrote:I've been thinking on a simple formula for the loudness of the sound relative to the listener position:
(Sound Intensity * Sound Volume * Master Volume)
/
abs(Distance from listener)
= Loudness value which I'll pass on to the chosen sound engine.

What flaws does this simple formula have?

Edit: Made the distance absolute.
On a first tip, OpenAL does all that stuff for you. So if you wanted to find out their way of doing it, you could check out the source. And also I think there are some real formulas laying around on the internet, because iirc the decibal level dissipates depending on the logarithm of something.
Last edited by Halifax on Sun Feb 10, 2008 8:32 pm, edited 1 time in total.
TheQuestion = 2B || !2B
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

You should take the square of the distance for more realistic sound decreasal.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Dorth wrote:You should take the square of the distance for more realistic sound decreasal.
Good idea.
Halifax wrote:
MasterGod wrote:I've been thinking on a simple formula for the loudness of the sound relative to the listener position:
(Sound Intensity * Sound Volume * Master Volume)
/
abs(Distance from listener)
= Loudness value which I'll pass on to the chosen sound engine.

What flaws does this simple formula have?

Edit: Made the distance absolute.
On a first tip, OpenAL does all that stuff for you. So if you wanted to find out their way of doing it, you could check out the source.
Sorry I got lost in the source, can you help me with that?
Halifax wrote:And also I think there are some real formulas laying around on the internet, because iirc the decibal level dissipates depending on the logarithm of something.
IIRC?
I think I understand what you mean but lacking the right keywords I can't search right for that algorithm, a little help there would be very useful.

Thanks.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

IIRC refers to the saying "if I remember correctly". I am surprised I didn't spell that out, as I usually don't opt for acronyms. :lol:

But anyways, here is a link to a site that explains the formulas for distance rolloff, and stuff: http://openal.org/openal_webstf/specs/o ... spec3.html
TheQuestion = 2B || !2B
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Thanks Halifax, I'll be sure to look at it tomorrow!
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Halifax, I've read that site, exactly what I needed. Thanks a tons!

Now, Here is my design, please let me hear your opinions:
Image
Like FMOD Ex and IrrKlang other sound engines will be implemented.
IAudioDevice && ISoundObj && IPlayedSound will be : public virtual IReferenceCounted.
Of course the "client game" will use only the interfaces which lets me un-compile the engines just like irrlicht can be compiled without directx, opengl etc..
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Does no comments means you like that design? :D
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Vsk
Posts: 343
Joined: Thu Sep 27, 2007 4:43 pm

Post by Vsk »

Are you using abstract factory for deacopling those implementation from which is using it trhough the Interface? Or how are using the proper implementation selected?
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Okay, I have to admit MasterGod, I am sorry that I gave you that link above. The link I posted is basically just an exact copy of the .pdf documentation file provided in the OpenAL 1.1 SDK. I think it is called OpenAL Specificaiton or something like that. It might help you with a better description on some parts if you want to use it.

Also, about your implementation, I find an IrrKlang abstracted interface completely redundant, and with unnecessary overhead for the user. Your are implementing an abstracted interface over one that is already abstracted. So I must say that I find IrrKlang completely useless in the fact that it provides no cross-platform solution, it provides no hardware accelartion, and it provides no way for you to use your own low-pass filters.

OpenAL contains support for the use of sound card RAM use thanks to Creative Labs. Creative Labs also implemented some amazing solutions for more realistic 3D sound rendering. Overall OpenAL kills IrrKlang in ever possible way!

<offtopic>
What flow chart maker do you use? Is it free, and available for download? Any links?
</offtopic>
TheQuestion = 2B || !2B
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

@Vsk: If I understood you right you're asking how to select different implementation? Well, there's a simple enum for it.. Something like E_irrklang and e_fmod ex etc..

@Halifax:
Halifax wrote:Also, about your implementation, I find an IrrKlang abstracted interface completely redundant, and with unnecessary overhead for the user. Your are implementing an abstracted interface over one that is already abstracted. So I must say that I find IrrKlang completely useless in the fact that it provides no cross-platform solution, it provides no hardware accelartion, and it provides no way for you to use your own low-pass filters.

OpenAL contains support for the use of sound card RAM use thanks to Creative Labs. Creative Labs also implemented some amazing solutions for more realistic 3D sound rendering. Overall OpenAL kills IrrKlang in ever possible way!

<offtopic>
What flow chart maker do you use? Is it free, and available for download? Any links?
</offtopic>
About the "flow chart maker" - Office Visio 2003.
About all the rest:
1. I see IrrKlang's API very simple an so I'm going to use that for now.
2. The thing is that I don't like working on the audio part and so I delay it, giving it a much more lower priority. After the school's deadline I'm going to add more developers, if ones will be available, and let them handle the audio stuff :D so I've made a simple working system which will suffice for now.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Post Reply