SFML community forums

Help => Audio => Topic started by: EFDos on March 05, 2014, 09:31:28 am

Title: [SOLVED]Trouble loading audio from memory.
Post by: EFDos on March 05, 2014, 09:31:28 am
Hello all, I've been using sfml for the development of a game, and I've been loving it so far (API Design, Documentation, ease of use, everything).

But now I've ran into the first big problem which I really can't find out how to solve, and I really hope somebody can help me with this. So let me start explaining:

On this point of the development we've decide to design our own binary file format to store resources - sounds and images used to just lay around - and make the game read and load resources from our files.

It works perfectly with images, but with sounds - music specifically - I get this error:

Quote
Failed to open sound file from memory (Supported file format but file is malformed.)

What I do is, I find the ogg music inside the file, load it to a buffer - as I understand, a soundstream needs it's source to be kept in memory for access - and tell sf::Music to load it with openFromMemory(), and that's where I get the error.

Important notes:
- The original ogg music files did play inside the game, when we were using openFromFile()
- I do not believe the music data can be messed up inside our file. I use the same process for Images - which works inside the game - and I even extracted a music file back -by the same means I use to load it into the game- to test. I can load and play this extracted file inside the game with openFromFile(), just like the original file. I even did a checksum on it with the original file, and they did match. So I really don't get why openFromMemory() can't load and play it.
-I haven't tested with non-stream sounds yet, don't know if I'd get a different result. I'll update this when I test.

I'm really sorry if my english is monstrously bad, and thanks in advance  :)
Title: Re: Trouble loading audio from memory.
Post by: Laurent on March 05, 2014, 10:10:30 am
So this is clearly about how you access the music data in your binary file. You should show us the corresponding code.
Title: Re: Trouble loading audio from memory.
Post by: kimci86 on March 05, 2014, 07:50:55 pm
Hello,

In your getMusic function,
Quote
if(musicBuffer[i])
{
    /* initialize */
}

Didn't you mean this ?
if(!musicBuffer[i])
(notice the negation '!')
Title: Re: Trouble loading audio from memory.
Post by: Jesper Juhl on March 05, 2014, 09:18:28 pm
size_t getMusicSize(short int id)
   {
      int i;
      for(i=0 ; i<n_files_music ; i++)
      {
         if(musicIndexes.ID == id)
            break;
      }
      return (size_t)musicIndexes.length;
   }
So if I pass in an invalid 'id' I have no way to detect that and I just get the size of the last music - doesn't seem very robust to me...
Title: Re: [SOLVED]Trouble loading audio from memory.
Post by: Nexus on March 07, 2014, 10:41:31 am
In general, your code is very error-prone, because low-level.

Why don't you use proper abstraction mechanisms such as STL containers and strings? That would also avoid your memory leaks. Furthermore, in the places where you really need raw byte interpretation, use reinterpret_cast to make that clear -- not C casts.