Search code examples
windowsaudiowave

waveOutGetPosition always returns zero


I'm using waveOutWrite to write several small buffers (80ms each). As they are playing, I'm calling this function to get the playback position:

    uint GetWaveOutPosInMS()
    {
        WinMM.MMTIME mmtime = new WinMM.MMTIME();
        mmtime.wType = 1;
        WinMM.MMRESULT ret = WinMM.waveOutGetPosition(WaveOut, ref mmtime, (uint)Marshal.SizeOf(typeof(WinMM.MMTIME)));
        return (mmtime.val);
    }

Here are the relative extras as well:

    [DllImport("winmm.dll")]
    public static extern MMRESULT waveOutGetPosition(IntPtr hwo, ref MMTIME info, uint cbi);

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct MMTIME
    {
        public uint wType;
        public uint val;
    }

The waveOutGetPosition returns 0 (no error), but mmtime.val is always zero. MSDN isn't really clear on what "playback position" is relative to, just that it is reset on waveOutOpen and waveOutReset.. but does it always continue to increase across multiple waveOutWrite() calls? Any ideas as to why it would always be returning zero for me?


Solution

  • I had the MMTIME struct declared incorrectly. Odd that the function didn't report error, but based on other stuff I've read this function is dependant on the OEM so could see various weird results. It also appears asking for MS isn't as well tested as asking for SAMPLES, so I'll just ask for samples instead, and calculate MS myself.

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct MMTIME
        {
            public uint wType;
            public uint val;
    
            // Padding because val is actually a union
            public uint pad;
        }