Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > irrKlang
[Util] load sounds from Irrlicht filesystem

stefbuet
Registered User
Quote
2008-12-16 20:59:43

Hi everybody.

As I was looking for some code without finding anything to be able to load my sound files from the Irrlicht engine, I'm creating this thread to share my own code. This allow you for exemple to load all your sounds from archives loaded previously with Irrlicht.

Includes there 4 files into your project :
-IrrlichtSoundFactory.hpp
-IrrlichtSoundFactory.cpp
-IrrlichtSoundFileReader.hpp
-IrrlichtSoundFileReader.cpp

Content of files :

IrrlichtSoundFactory.hpp :



#ifndef IRRLICHT_SOUND_FACTORY
#define IRRLICHT_SOUND_FACTORY

#include <KLANG/irrKlang.h>
#include <cstdlib>
#include <iostream>
#include <IRR/irrlicht.h>

using namespace irrklang;
using namespace irr;

class IrrlichtSoundFactory : public irrklang::IFileFactory {

        public:
        IrrlichtSoundFactory(io::IFileSystem *irrlichtFileSystem);
        virtual IFileReader* createFileReader(const ik_c8* filename);
        
        private:
        io::IFileSystem *filesys;

};

#endif



IrrlichtSoundFactory.cpp :



#include <KLANG/irrKlang.h>
#include <cstdlib>
#include <iostream>
#include "IrrlichtSoundFactory.hpp"
#include "IrrlichtSoundFileReader.hpp"
#include <IRR/irrlicht.h>

using namespace irrklang;
using namespace irr;

IrrlichtSoundFactory::IrrlichtSoundFactory(io::IFileSystem *irrlichtFileSystem) {
        filesys=irrlichtFileSystem;
}

IFileReader* IrrlichtSoundFactory::createFileReader(const ik_c8* filename) {

        io::IReadFile *file;
        file=filesys->createAndOpenFile(filename);
        if (!file) {
                return 0;
        }
        return(new IrrlichtSoundFileReader(file));
        
}



IrrlichtSoundFileReader.hpp :



#ifndef IRRLICHT_SOUND_FILE_READER
#define IRRLICHT_SOUND_FILE_READER

#include <KLANG/irrKlang.h>
#include <cstdlib>
#include <iostream>
#include <IRR/irrlicht.h>

using namespace irrklang;
using namespace irr;

class IrrlichtSoundFileReader : public irrklang::IFileReader {

public:

   //functions
   IrrlichtSoundFileReader(io::IReadFile *openedFile);
   ~IrrlichtSoundFileReader();
   ik_s32 read(void* buffer, ik_u32 sizeToRead);
   bool seek(ik_s32 finalPos, bool relativeMovement);
   ik_s32 getSize();
   ik_s32 getPos();
   const ik_c8* getFileName();

   //variables
   io::IReadFile* file;
   char Filename[1024];
   ik_s32 FileSize;
  
};

#endif



IrrlichtSoundFileReader.cpp :

[code]

#include <KLANG/irrKlang.h>
#include <cstdlib>
#include <iostream>
#include "IrrlichtSoundFileReader.hpp"
#include <IRR/irrlicht.h>

using namespace irrklang;
using namespace irr;

IrrlichtSoundFileReader::Irrlich


niko
Moderator
Quote
2008-12-17 17:17:58

Thanks for sharing. But note that this will cause problems if you are using some streaming files from the same archive as other files, because the Irrlicht zip file reader is not thread safe. A simple workaround would be to put files which are streamed into another zip file, the correct solution of course would be to make the seeking and reading inside the irrlicht zip file reader thread safe.


stefbuet
Registered User
Quote
2008-12-17 18:34:17

Hi,
So, as irrKlang is multithreading, while we ask the irrlicht filesystem to load a file, for exemple a 3D Mesh from an archive witch contain also a sound file now playing in streaming, the irrklang engine could try to read a little bit of the sound file, and then it would occur a memory / file access conflit...

As i'm not experimented in C++ threading, I would try create a bool value, and turn it to true before loading a file from irrlicht, set it to false after the loading, something like that :



bool isLoading=true;
IReadFile *test=filesys->createAndOpenFile("testFile.3ds");
isLoading=false;



And then give a ref of this boolean to irrklang. Then in the IrrlichtSoundFileReader at the read() method, test this boolean value and don't load anything while the other loading isn't finished :



ik_s32 IrrlichtSoundFileReader::read(void* buffer, const ik_u32 sizeToRead) {
while(isLoading) { // wait... }
return(file->read(buffer, sizeToRead));
}



As the irrklang is multithreading, this while block wouldn't block the main program...
However this method is really weight as you have to change isLoading value each time you're doing a file loading in Irrlicht.

Personally, as I'll use only not streamed sounds, I will stay with the first part of my code. But I would be intrusted in seeing if this idea could works...

Thank,

Stef.


niko
Moderator
Quote
2008-12-19 15:02:33

Could work, but you really should use some of the multhreading functions of your os (Windows) to do this checking, setting a bool variable is not atomic, unfortunately.


floppyfreak
Registered User
Quote
2010-03-24 17:28:02

the last file in this post is incomplete. Where can I get the full code?


floppyfreak
Registered User
Quote
2010-03-25 13:13:09

problem is solved


cal
Registered User
Quote
2010-03-29 02:14:33

Please share the solution? :P


ultim
Registered User
Quote
2018-11-01 08:10:03

thanks for sharing
the last file IrrlichtSoundFileReader.cpp is incomplete!
can i have it , and an exemple how to use it ?


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "I?ternational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact