About Store Forum Documentation Contact



Post Reply 
[Mobile][struct Sound] Loading and storing audio files in RAM
Author Message
Mardok Offline
Member

Post: #1
[Mobile][struct Sound] Loading and storing audio files in RAM
I'm wondering how solve small issue associated with playing audio files. My observation is as follow. First i'll show you video with game which have this problem too. I played this Title long ago and remember things which annoyed me. So, wideo starts in 0:55, look carefully 1:02 when player trying shooting. There is small lag, imo lag is induced by loading sound file from SD to RAM.

http://www.youtube.com/watch?feature=pla...Ewc0#t=55s

Is it possible storing the most used sound files in RAM? not loading their every time from SD?

In EE [struct Sound] there is [create] method which working - but only once, after call
Code:
sound.play()
we need call
Code:
sound.create("params")
again if we want used our sound again, so it's not solve the problem, because i would like load all most used sound files once, when level is loading.

Any suggestions ?

Below is simple example, try press X once and wait until the end and press it again, sound isn't playing. Next press C and X again, sound is playing.

Is there any chances to modify create or play method?

All suggestions are welcome.

Code:
/******************************************************************************/
#include "stdafx.h"
/******************************************************************************/
Sound sound_metal;
/******************************************************************************/
void InitPre()
{
   App.name("Sound");
   Paks.add("../data/engine.pak");
}
/******************************************************************************/
Bool Init()
{
   Text_ds.color =BLACK;
   Text_ds.shadow=0;

   sound_metal.create("../data/sound/metal.ogg", 0);

   return true;
}
/******************************************************************************/
void Shut()
{
}
/******************************************************************************/
Bool Update()
{
   if(Kb.bp(KB_ESC  ))return false;

   if(Kb.bp(KB_X))sound_metal.play();
if(Kb.bp(KB_C))sound_metal.create("../data/sound/metal.ogg", 0);

   return true;
}
/******************************************************************************/
void Draw()
{
   D.clear(WHITE);
   D.text (0,  0.2f,   "Press X to play 'metal'");
}
/******************************************************************************/
(This post was last modified: 05-29-2012 07:49 PM by Mardok.)
05-29-2012 07:48 PM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #2
RE: [Mobile][struct Sound] Loading and storing audio files in RAM
Do you still have the issue if you're using WAV files?
05-30-2012 03:01 PM
Find all posts by this user Quote this message in a reply
Mardok Offline
Member

Post: #3
RE: [Mobile][struct Sound] Loading and storing audio files in RAM
It's not a big problem and occurs sometimes, i'm using wav files 120 - max 200 KB
Currently i'm testing different sounds for shot effect, explosions, etc... and this disadvantage is revealed SOMETIMES.
It's looks exactly the same like on attached video.

btw. My device have 8GB SD card and > 5.5 GB is free.

Is it big problem to modify Sound::play or add new? (I think this method doing something like Sound::delete after playing ?)

My project is small and problem occurs very occasionally but big projects with a lot of explosions and fast changing sounds may have problems... ??? Or not, i don't know... you have more experience, You decide.

thx for reply
05-30-2012 06:15 PM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #4
RE: [Mobile][struct Sound] Loading and storing audio files in RAM
how do you store the wav file in your resources folder ?
is it inside a pak file?
does pak have the .jet extension added?
if the sound is not played from pak, then does the sound file has .jet file extension added?
05-31-2012 11:50 AM
Find all posts by this user Quote this message in a reply
Mardok Offline
Member

Post: #5
RE: [Mobile][struct Sound] Loading and storing audio files in RAM
Data\Obj\Player\Sound\ (here are sound files)

Data folder is converted to *.pak with .jet extension and moved to assets folder.

In assets folder I have only two files: engine.pak.jet and all my game data in Data.pak.jet (pak file is without compression)


In your opinion create and play sound file in the same func may cause lag sometimes?
(This post was last modified: 05-31-2012 11:13 PM by Mardok.)
05-31-2012 09:01 PM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #6
RE: [Mobile][struct Sound] Loading and storing audio files in RAM
thanks, your way of storing files is correct.

any lag could exist only because of system accessing the file, or some delay in creating the sound buffers.
I have an idea, how to solve one problem, I will schedule it on the roadmap
06-02-2012 02:18 PM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #7
RE: [Mobile][struct Sound] Loading and storing audio files in RAM
in the meantime can you let me know if this helps:

in the Init parse the whole sound file
Code:
Init()
{
   File f; f.read("...sound.wav");
   for(;!f.end();)f.getByte();
}

I want to know if the OS will cache the sound file in the memory for later usage.
06-05-2012 01:32 PM
Find all posts by this user Quote this message in a reply
Mardok Offline
Member

Post: #8
RE: [Mobile][struct Sound] Loading and storing audio files in RAM
Hi Esenthel, sory for LAG in answer but i bought new hard disks and i had a lot of work with reinstall software creating RAID1 and moving some important data.

btw. I saw your tips last night and i did test with sounds.

I have simple flying helicopter.
First sfx starts playing when helicopter is on ground and starting fly up. Next when helicopter reached Y position, sfx is changed to idle.wav and in this moment sometimes occur lag, not always but sometimes (takes ~150~200 ms) - like on video. When i'm playing additional sounds (explosions, or weapon sfx) sometimes lags isn't occur and sometimes occur. If i'm playing long time freezes disappear.

I'm wondering if i understand you. If i create:
Code:
File f;
and load one by one the wav file and run loop one by one then part of memory reserved for variable f will be erased by next looped file?

And next doubt:

File f; is in Init { } so when Init is performed, variable f and part of memory reserved for it has importance for rest of application?

So, my last verdict and observation: Better solution is playing long while, then freeze isn't occur and playing sound is smooth.

I have an idea, You have a lot of work and now only one person (me) see inconveniences, so i'll be continue my work and if problem will grow (now is acceptable) then i'll report in this thread.

Sory for my ignorance but programming is my hobby and i'm still learning new things wink

good night
(This post was last modified: 06-07-2012 12:42 AM by Mardok.)
06-07-2012 12:41 AM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #9
RE: [Mobile][struct Sound] Loading and storing audio files in RAM
yes just read through all the sound file bytes in the Init
Code:
Init()
{
    File f; f.read("...sound.wav");
    for(;!f.end();)f.getByte();
}
you can do this for other sound files as well.
yes the 'f' file object will be deleted, however operating systems, after reading a file, cache it to RAM for later usage, this is at least in Windows, though I don't know about Android, that's why I asked to try it out.
Did doing this helped at all with the lag?
06-07-2012 12:51 AM
Find all posts by this user Quote this message in a reply
Mardok Offline
Member

Post: #10
RE: [Mobile][struct Sound] Loading and storing audio files in RAM
(06-07-2012 12:51 AM)Esenthel Wrote:  Did doing this helped at all with the lag?
I can't see difference after this. Better solution is playing long while. Then all is fine.
06-07-2012 01:03 AM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #11
RE: [Mobile][struct Sound] Loading and storing audio files in RAM
thanks, I'll investigate this, and will write a helpful engine function
06-10-2012 01:14 PM
Find all posts by this user Quote this message in a reply
Mardok Offline
Member

Post: #12
RE: [Mobile][struct Sound] Loading and storing audio files in RAM
(06-10-2012 01:14 PM)Esenthel Wrote:  thanks, I'll investigate this, and will write a helpful engine function
Today i installed game from video above FinalStrike3D 1.0.5 on my Xperia ARC. Game running very smooth and fast (55-60 fps) but freezes sometimes when enemy explode or when i'm shooting or when i change weapon and different sound is need. Freezes is sporadic but there is.

Cache for most important sounds will be smart functionality for Your Engine.

thx for interest and good night

EOT
06-10-2012 10:50 PM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #13
RE: [Mobile][struct Sound] Loading and storing audio files in RAM
next SDK will have CacheSound(Str name) function
07-27-2012 03:13 PM
Find all posts by this user Quote this message in a reply
Mardok Offline
Member

Post: #14
RE: [Mobile][struct Sound] Loading and storing audio files in RAM
Great!
Good job.
07-27-2012 03:21 PM
Find all posts by this user Quote this message in a reply
Post Reply