Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Need a little help with playing sounds.

Author
Message
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 12th Apr 2011 21:04
So I can load sounds, even 3D sounds, and play them normally just fine. The problem comes when trying to play something like a gun firing. If you pull the trigger again before the first sound is done playing, it stops that sound and starts it over.

So I figured, ok, I'll just clone the sound and play the clone. Then if there is one already playing, it can finish playing while the second one starts. No big deal.

BUT, when I try and do that, the cloned sound either doesn't play at all, or makes this weird electronic/mechanical sound.

Anyone ever see this issue before?

WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 12th Apr 2011 21:20
If you make a clone of the sound then you can play it as many times as you've cloned it. I'd suggest load it once, and each time it needs playing, clone it, play the clone, and have a function that checks for finished sounds and then delete them.

Warning! May contain Nuts!
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 12th Apr 2011 21:27
I had thought of that too, but was concerned about how that would impact performance when there are 40-50 clones each of 5 different sounds. Although that would save on memory, these are very tiny sounds.

I figured out the problem anyway. It seems I always do once I post the question on the forum...

I'm keying the playing of the sound off of the start of the animation sequence that goes with it, and it was creating a new sound for each frame in the sequence. So when I fired the gun it was trying to play 38 copies of the sound at once.

Agent
21
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 12th Apr 2011 22:20
Oops.

Don't you hate when that happens

I've been meaning to create a sound cloning system for some time now, one in which you load all your unique sounds into a subset of the sound ID range, then each time you want to play sound, instead of using PLAY SOUND you call a function that takes as parameters the unique sound ID, volume, panning, other 3D elements, etc. The function then clones the sound to an available 'voice' and plays it, cleaning up after itself when the play is complete.

It's a simple thing but I never got around to coding it yet. Means that when I have two events that trigger a sound, one close and one far, you only hear one instance of the sound at the appropriate volume (for range) and the other sound effectively doesn't play - or you have a sound halfway through playing close by and loud, and a trigger event happens at range... the sound restarts and plays softly, leaving an annoying gap in the closerange play. It affected my RTS quite considerably, as range cues delivered by sound volume and panning were an integral part of gameplay.

A proper soundclone routine is almost essential for games development these days... but I *still* haven't done it yet lol.
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 12th Apr 2011 22:31 Edited at: 12th Apr 2011 22:33
This one is pretty simple. Probably to simple.



I'm sure I'll be adding a lot to this. It was just a means to get things started. After making sure I wasn't firing off an entire clip in 500 milliseconds, it actually works fairly well.

NewSound is the sound number of the sound to clone and play. sObj is a hidden cube that gets positioned where the sound is supposed to be coming from before this routine is called. So I can use sObj to place sounds coming from other players or objects.

What I have to add to this now is a time delay, so that if a sound isn't used for a period of time it gets deleted so a new sound can be cloned and played.

Agent
21
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 12th Apr 2011 23:17
Bugger it. I started typing up a post about the logic that was floating around in my head for the system I have in mind and decided to delete it all and just bloody code it

So I'll be back in half an hour with my soundclone system
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 12th Apr 2011 23:30 Edited at: 12th Apr 2011 23:33
I only found out in the GDK forums the other day about this, most of my work doesn't include using sounds. Cloning sounds only creates an instance of the original sound and doesn't take up extra memory.

I'm glad you guys have sussed it out though.

NB:
Playing a sound just send the pointer to the sound device. Once the sound device is in charge of playing the sound then there's no overhead at all. Most sound devices can play at least 256 simultaneous sounds and take no cpu time at all...

Warning! May contain Nuts!
Agent
21
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 12th Apr 2011 23:51 Edited at: 12th Apr 2011 23:56
Here we go! My Soundclone/Instancing solution:



Changing the number of voices is as easy as adjusting the variable with the obvious name at the very first line of functional code. If you want to use a different range of sounds for your actual plays, change the 100 and the 99 in the FOR...NEXT loop in Play_Sound() and Sound_Upkeep().

Should be a simple matter to modify Play_Sound() to accept additional parameters for more complex 3D soundplays.
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 13th Apr 2011 00:13
Quote: "Cloning sounds only creates an instance of the original sound and doesn't take up extra memory."


Good to know that it only copies a pointer, and not the whole sound. That makes the cloning and deleting plan a lot better on performance.

Agent
21
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 13th Apr 2011 00:19
Yeah, I didn't know that either. I always just assumed that each sound running consumed a small overhead to upkeep it. Good intel.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 13th Apr 2011 07:36
Did you do a forum search at all? I just posted code for this last week to help someone asking the same question on this board.

http://forum.thegamecreators.com/?m=forum_view&t=183703&b=6

The Internet: Where men are men, women are men, and children are federal agents
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 13th Apr 2011 19:29
I searched, but not for a prewritten sound system. I was just searching for others who had similar issues to what I was seeing. Which there weren't any because it was just a coding error.

Nice bit of code though.

Login to post a reply

Server time is: 2026-07-17 19:13:59
Your offset time is: 2026-07-17 19:13:59