Sound Latency

BlitzMax Forums/BlitzMax Programming/Sound Latency

TartanTangerine (was Indiepath)(Posted 2005) [#1]
Has or is anyone else experiencing issues with latency when using the PlaySound Command? Is this a known issue with the homegrown sound library?


Robert(Posted 2005) [#2]
I have experienced this occassionally as well. It seems to be a slightly worse problem on my Linux PC.


Mark Tiffany(Posted 2005) [#3]
I rooted around in the code a while back, and I decided that the problem boils down to the sound buffer. In order to prevent choppy sounds, a number of bytes are loaded each Flip. The problem is that the sound buffer needs to be quite big to prevent it from coming out all stuttered, but as it gets bigger, it can introduce some latency.

Two things:
1) it would be nice if we could define that buffer (8K at the moment I think), possibly even on a per sound basis, such that short, time critical sounds get a short buffer and longer, less critical sounds get a long buffer (maybe that could even be auto-selected by bmax?)
2) it should be possible to have cuesound do exactly what it says on the tin, such that PlaySound really does start playing immediately.


TartanTangerine (was Indiepath)(Posted 2005) [#4]
Hmmn perhaps I shall try cuesound instead (but tomorrow).


Tibit(Posted 2005) [#5]
In order to prevent choppy sounds, a number of bytes are loaded each Flip


Where is the sound buffer? How do I get bytes from the buffer? How do I play them manually?


skidracer(Posted 2005) [#6]
The latency was introduced to provide compatability with extremely poor multimedia drivers that ship with some soundblaster cards. A DirectSound driver is planned for FreeAudio that will feature much more responsive performance on Windows machines.

There are also audio stream hooks coming that will allow you to capture and feed the mixer with your own raw audio data.


TartanTangerine (was Indiepath)(Posted 2005) [#7]
@Skidracer,

How soon is this coming? I've a commercial release to make and this is a big issue for me.


Mark Tiffany(Posted 2005) [#8]
Where is the sound buffer? How do I get bytes from the buffer? How do I play them manually?

"Hidden" in the module source, which for the actual grunt-work is C code. While I understand enough C to work out that there is a buffer, and to hack a bit of code, I don't understand enough of how audio stuff 'works' - especially when I hit the scary looking mixing code! I did manage to write a RestartSound function, which reset a non-looping sound such that you can play it again without loading or cueing it, i.e. aiming to avoid any potential setup delay. But I quickly realised that wasn't the true source of the latency problem (it's the fact it all happens in one thread, and has to buffer the sound).


ImaginaryHuman(Posted 2005) [#9]
Audio streams that you can feed data to sounds really cool!!!


Hotcakes(Posted 2005) [#10]
Those audiohooks sound mighty nice, skiddy...

How soon is this coming? I've a commercial release to make and this is a big issue for me.

Chances are your customers won't notice it as much as you. It may not even be a problem at all for many of them.


TartanTangerine (was Indiepath)(Posted 2005) [#11]
I have a professional soundcard so I doubt that is the issue.

I've played with the C code and found that a buffer size of 4096 is just right.
struct winmmdevice:audiodevice
{
	HWAVEOUT	device; 
	int 		buffersize;
	abuffer 	buffer[2];
	int 		bnum;
	int 		mode;			//0=8bit 1=16bit
	int 		playing;

	int reset()
	{
		int sz=4096;
		mix=new mixer(sz);
		device=0;
		buffersize=0;
		bnum=0;
		mode=0;
		playing=0;
		return init(44100,2,16,sz);
	}


Chances are your customers won't notice it as much as you. It may not even be a problem at all for many of them.


It was the testers who noticed it first.


Mark Tiffany(Posted 2005) [#12]
I believe 4096 is what it was originally set to, but got bumped to allow for some (presumably slower) machines. As I've said before, the neatest solution would be to be able to set the buffer size from BMax code. Which probably wouldn't be too hard to hack into the C code...


jp22(Posted 2007) [#13]
I'm new to blitzmax, so hopefully someone can help me

I've downloaded gthe demo of BlitzMax, having previously used BlitzPlus

I found that the latency for BlitzPlus was too large (trying to do things like wavetable lookups at 60fps just didn't swap between samples quickly enough..Think C64 Drum sounds here..)

I found this thread, so *think* that BlitzMax should allow me to change the latency to get around this problem, but would like to know how to go about changing the audio output buffer size, as described above.

From the files i've got from downloading the demo, I can't see anywhere in the module side of things which seems to initially set this up or anywhere which has the code as shown above..

Thanks in advance

JP


Grey Alien(Posted 2007) [#14]
Have you tried using OpenAL or DirectSound instead of FreeAudio as that was the driver with the latency issues. Look up EnableOpenALAudio and SetAudioDriver.


jp22(Posted 2007) [#15]
Thanks for this. I'll take a look at SetAudioDriver

Thing is, even though I can do a google search and find info on SetAudioDriver, I can't see this function mentioned at all in BlitzMax.. Again, making me wonder if the demo version is the same as the full price version?

Another thing, I've seen threads where people have said about compiling BlitzMax and getting problems when it gets to the freeAudio.cpp file..

With the demo version of BlitzMax that i've downloaded, there's no .cpp files at all. Can somebody tell me if I will get *full* source to this kind of thing if I buy the full BlitzMax version?

Thanks again
JP


jp22(Posted 2007) [#16]
hmmm the demos v 1.12, which i'm guessing isn't the latest version?

doesn't seem to recognise SetAudioDriver either...tsk tsk...


Yan(Posted 2007) [#17]
As you've realised, the demo version of BMax is pretty old. The current release version stands at v1.28 and does indeed include the source to all modules.


jp22(Posted 2007) [#18]
Thanks for the clarification, Ian

Much appreciated


Grey Alien(Posted 2007) [#19]
Sorry didn't know you had the demo. Think that stuff was added midway through V1.24, so it's in V1.26 upwards.