Skip to content

Commit

Permalink
Merge pull request naudio#399 from Mostlypyjamas/master
Browse files Browse the repository at this point in the history
Added WaveIn.GetPosition and WaveInEvent.GetPosition calling through …
  • Loading branch information
markheath authored Oct 15, 2018
2 parents 549648b + b8743d1 commit e3e9d3c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
4 changes: 2 additions & 2 deletions NAudio/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.8.4.0")]
[assembly: AssemblyFileVersion("1.8.4.0")]
[assembly: AssemblyVersion("1.8.5.0")]
[assembly: AssemblyFileVersion("1.8.5.0")]
17 changes: 11 additions & 6 deletions NAudio/Wave/MmeInterop/WaveInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ public enum WaveMessage
public static extern MmResult waveOutUnprepareHeader(IntPtr hWaveOut, WaveHeader lpWaveOutHdr, int uSize);
[DllImport("winmm.dll")]
public static extern MmResult waveOutWrite(IntPtr hWaveOut, WaveHeader lpWaveOutHdr, int uSize);

// http://msdn.microsoft.com/en-us/library/dd743866%28VS.85%29.aspx
[DllImport("winmm.dll")]
public static extern MmResult waveOutOpen(out IntPtr hWaveOut, IntPtr uDeviceID, WaveFormat lpFormat, WaveCallback dwCallback, IntPtr dwInstance, WaveInOutOpenFlags dwFlags);
[DllImport("winmm.dll", EntryPoint = "waveOutOpen")]
public static extern MmResult waveOutOpenWindow(out IntPtr hWaveOut, IntPtr uDeviceID, WaveFormat lpFormat, IntPtr callbackWindowHandle, IntPtr dwInstance, WaveInOutOpenFlags dwFlags);

[DllImport("winmm.dll")]
public static extern MmResult waveOutReset(IntPtr hWaveOut);
[DllImport("winmm.dll")]
Expand All @@ -114,7 +114,7 @@ public enum WaveMessage
// http://msdn.microsoft.com/en-us/library/dd743874%28VS.85%29.aspx
[DllImport("winmm.dll")]
public static extern MmResult waveOutSetVolume(IntPtr hWaveOut, int dwVolume);

[DllImport("winmm.dll")]
public static extern MmResult waveOutGetVolume(IntPtr hWaveOut, out int dwVolume);

Expand All @@ -128,13 +128,13 @@ public enum WaveMessage
// http://msdn.microsoft.com/en-us/library/dd743841%28VS.85%29.aspx
[DllImport("winmm.dll", CharSet = CharSet.Auto)]
public static extern MmResult waveInGetDevCaps(IntPtr deviceID, out WaveInCapabilities waveInCaps, int waveInCapsSize);

// http://msdn.microsoft.com/en-us/library/dd743838%28VS.85%29.aspx
[DllImport("winmm.dll")]
public static extern MmResult waveInAddBuffer(IntPtr hWaveIn, WaveHeader pwh, int cbwh);
[DllImport("winmm.dll")]
public static extern MmResult waveInClose(IntPtr hWaveIn);

// http://msdn.microsoft.com/en-us/library/dd743847%28VS.85%29.aspx
[DllImport("winmm.dll")]
public static extern MmResult waveInOpen(out IntPtr hWaveIn, IntPtr uDeviceID, WaveFormat lpFormat, WaveCallback dwCallback, IntPtr dwInstance, WaveInOutOpenFlags dwFlags);
Expand All @@ -144,7 +144,7 @@ public enum WaveMessage
// http://msdn.microsoft.com/en-us/library/dd743848%28VS.85%29.aspx
[DllImport("winmm.dll")]
public static extern MmResult waveInPrepareHeader(IntPtr hWaveIn, WaveHeader lpWaveInHdr, int uSize);

[DllImport("winmm.dll")]
public static extern MmResult waveInUnprepareHeader(IntPtr hWaveIn, WaveHeader lpWaveInHdr, int uSize);

Expand All @@ -160,5 +160,10 @@ public enum WaveMessage
[DllImport("winmm.dll")]
public static extern MmResult waveInStop(IntPtr hWaveIn);

// https://msdn.microsoft.com/en-us/library/Dd743845(v=VS.85).aspx
[DllImport("winmm.dll")]
public static extern MmResult waveInGetPosition(IntPtr hWaveIn, out MmTime mmTime, int uSize);


}
}
25 changes: 21 additions & 4 deletions NAudio/Wave/WaveInputs/WaveIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private void Callback(IntPtr waveInHandle, WaveInterop.WaveMessage message, IntP
var hBuffer = (GCHandle)waveHeader.userData;
var buffer = (WaveInBuffer)hBuffer.Target;
if (buffer == null) return;

lastReturnedBufferIndex = Array.IndexOf(buffers, buffer);
RaiseDataAvailable(buffer);
try
Expand All @@ -148,7 +148,7 @@ private void Callback(IntPtr waveInHandle, WaveInterop.WaveMessage message, IntP
RaiseRecordingStopped(e);
}
}

}
}

Expand Down Expand Up @@ -219,7 +219,7 @@ public void StopRecording()
// report the last buffers, sometimes more than one, so taking care to report them in the right order
for (int n = 0; n < buffers.Length; n++)
{
int index = (n + lastReturnedBufferIndex + 1)%buffers.Length;
int index = (n + lastReturnedBufferIndex + 1) % buffers.Length;
var buffer = buffers[index];
if (buffer.Done)
{
Expand All @@ -232,11 +232,28 @@ public void StopRecording()
// Don't actually close yet so we get the last buffer
}

/// <summary>
/// Gets the current position in bytes from the wave input device.
/// it calls directly into waveInGetPosition)
/// </summary>
/// <returns>Position in bytes</returns>
public long GetPosition()
{
MmTime mmTime = new MmTime();
mmTime.wType = MmTime.TIME_BYTES; // request results in bytes, TODO: perhaps make this a little more flexible and support the other types?
MmException.Try(WaveInterop.waveInGetPosition(waveInHandle, out mmTime, Marshal.SizeOf(mmTime)), "waveInGetPosition");

if (mmTime.wType != MmTime.TIME_BYTES)
throw new Exception(string.Format("waveInGetPosition: wType -> Expected {0}, Received {1}", MmTime.TIME_BYTES, mmTime.wType));

return mmTime.cb;
}

/// <summary>
/// WaveFormat we are recording in
/// </summary>
public WaveFormat WaveFormat { get; set; }

/// <summary>
/// Dispose pattern
/// </summary>
Expand Down
25 changes: 21 additions & 4 deletions NAudio/Wave/WaveInputs/WaveInEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void CreateBuffers()
private void OpenWaveInDevice()
{
CloseWaveInDevice();
MmResult result = WaveInterop.waveInOpenWindow(out waveInHandle, (IntPtr)DeviceNumber, WaveFormat,
MmResult result = WaveInterop.waveInOpenWindow(out waveInHandle, (IntPtr)DeviceNumber, WaveFormat,
callbackEvent.SafeWaitHandle.DangerousGetHandle(), IntPtr.Zero, WaveInterop.WaveInOutOpenFlags.CallbackEvent);
MmException.Try(result, "waveInOpen");
CreateBuffers();
Expand All @@ -108,7 +108,7 @@ private void OpenWaveInDevice()
public void StartRecording()
{
if (captureState != CaptureState.Stopped)
throw new InvalidOperationException("Already recording");
throw new InvalidOperationException("Already recording");
OpenWaveInDevice();
MmException.Try(WaveInterop.waveInStart(waveInHandle), "waveInStart");
captureState = CaptureState.Starting;
Expand Down Expand Up @@ -191,11 +191,28 @@ public void StopRecording()
}
}

/// <summary>
/// Gets the current position in bytes from the wave input device.
/// it calls directly into waveInGetPosition)
/// </summary>
/// <returns>Position in bytes</returns>
public long GetPosition()
{
MmTime mmTime = new MmTime();
mmTime.wType = MmTime.TIME_BYTES; // request results in bytes, TODO: perhaps make this a little more flexible and support the other types?
MmException.Try(WaveInterop.waveInGetPosition(waveInHandle, out mmTime, Marshal.SizeOf(mmTime)), "waveInGetPosition");

if (mmTime.wType != MmTime.TIME_BYTES)
throw new Exception(string.Format("waveInGetPosition: wType -> Expected {0}, Received {1}", MmTime.TIME_BYTES, mmTime.wType));

return mmTime.cb;
}

/// <summary>
/// WaveFormat we are recording in
/// </summary>
public WaveFormat WaveFormat { get; set; }

/// <summary>
/// Dispose pattern
/// </summary>
Expand All @@ -205,7 +222,7 @@ protected virtual void Dispose(bool disposing)
{
if (captureState != CaptureState.Stopped)
StopRecording();

CloseWaveInDevice();
}
}
Expand Down

0 comments on commit e3e9d3c

Please sign in to comment.