Reputation: 399
I'm creating a game in XNA that will work with lots of music loops over each other but I don't seem to be able to synchronize these sounds.
I always miss by a few milliseconds can you help me out?
This is my first try of synchronizing the sounds. Be aware I will need to work with dozens of sounds...
Might this sync problem be related with caching the sound?
Is there an external library to make it easier?
public SoundEffectInstance loop1, loop2;
public int auxControl = 0;
public bool auxB = false, auxA = false;
public void LoadContent()
{
SoundEffect temp1 = Game.Content.Load<SoundEffect>("sounds/Synctest_1");
loop1 = temp1.CreateInstance();
loop1.IsLooped = true;
loop2 = temp1.CreateInstance();
loop2.IsLooped = true;
}
public override void Update(GameTime gameTime)
{
// start first sound
if (auxA == false)
loop1.Play(); auxA = true;
// start counting until 2 seconds
if (auxA)
auxControl += gameTime.ElapsedGameTime.Milliseconds;
// if 2 seconds have passed after first sound start second sound
if (auxControl >= 2000 && auxB == false)
{
loop2.Play();
auxB = true;
}
base.Update(gameTime);
}
thank you
Upvotes: 0
Views: 1038
Reputation: 399
For now I've decided that the best way to achieve this was using a condition inside the update, preferably the main update, so the verification is done the faster way it can be.
Still this brings a problem because you may hear a small "Tuk" between sounds but its hardly notice.
pseudo code would be something like this
Update(GameTime gameTime)
{
if (last sound ended)
{
play next;
decide on the next sound; // implement cache here if needed
}
}
Upvotes: 0
Reputation: 11469
Don't know anything about C#, but usually it's hard to sync these sorts of things with millisecond accuracy if the API doesn't support it. The solution is to mix them yourself and use the API only for playback, that way you have control over exactly when they sounds play and how they are combined.
There may be a simpler solution in C#, but you can build something like this with http://www.PortAudio.com or a number of other interfaces. You may want to search for something like Game audio API on google.
Upvotes: 1