2

I am using the SharpDX wrapper for the DirectX XAudio2 api. http://sharpdx.org/

I need to be able to seek a WAV file loaded into a SourceVoice to a given position in the track. I am struggling to work out what I should be setting PlayBegin to in order to skip a set amount of milliseconds, or a proportional amount of the track.

public void onMetronome(int bar)
{
   if (voice != null) voice.DestroyVoice();
   voice = new SourceVoice(Player.XAudio, buffer.WaveFormat, true);
   buffer.PlayBegin = (int)(bar * buffer.Stream.Length / 4);
   voice.SubmitSourceBuffer(buffer, buffer.DecodedPacketsInfo);
   voice.Start();
}

Can anyone tell me what value I should be setting play begin to?

I can see from the documentation that this value is the sample number and that it may need to be a multiple of 128 but think I am still doing something wrong.

http://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.ixaudio2sourcevoice.ixaudio2sourcevoice.submitsourcebuffer(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.xaudio2.xaudio2_buffer(v=vs.85).aspx

Tom
  • 12,591
  • 13
  • 72
  • 112
  • What makes you think you are doing something wrong? I suspect you need to take the sample rate into account - so offset = seconds x samplesPerSecond x bytesPerSample and maybe rounded to multiple of 128 – Floris May 20 '13 at 12:01

1 Answers1

2

Figured it out, think I have been multiplying by 1000 when I shouldn't have, the following works

var offset = (int)Math.Floor(buffer.WaveFormat.SampleRate * barDuration / 128) * 128 * bar;
Tom
  • 12,591
  • 13
  • 72
  • 112
  • I would have thought the `*bar` belonged inside the `Floor()` expression - but glad you figured it out! – Floris May 21 '13 at 02:47
  • @Floris Yep that makes sense, I was thinking that it may give a problem with zero division which was why I placed it outside but thinking about it, it shouldn't be a problem as isn't the denominator. Cheers :) – Tom May 24 '13 at 12:24