forked from naudio/NAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaveOutUtils.cs
52 lines (46 loc) · 2 KB
/
WaveOutUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Runtime.InteropServices;
// ReSharper disable once CheckNamespace
namespace NAudio.Wave
{
public static class WaveOutUtils
{
public static float GetWaveOutVolume(IntPtr hWaveOut, object lockObject)
{
int stereoVolume;
MmResult result;
lock (lockObject)
{
result = WaveInterop.waveOutGetVolume(hWaveOut, out stereoVolume);
}
MmException.Try(result, "waveOutGetVolume");
return (stereoVolume & 0xFFFF) / (float)0xFFFF;
}
public static void SetWaveOutVolume(float value, IntPtr hWaveOut, object lockObject)
{
if (value < 0) throw new ArgumentOutOfRangeException(nameof(value), "Volume must be between 0.0 and 1.0");
if (value > 1) throw new ArgumentOutOfRangeException(nameof(value), "Volume must be between 0.0 and 1.0");
float left = value;
float right = value;
int stereoVolume = (int)(left * 0xFFFF) + ((int)(right * 0xFFFF) << 16);
MmResult result;
lock (lockObject)
{
result = WaveInterop.waveOutSetVolume(hWaveOut, stereoVolume);
}
MmException.Try(result, "waveOutSetVolume");
}
public static long GetPositionBytes(IntPtr hWaveOut, object lockObject)
{
lock (lockObject)
{
var 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.waveOutGetPosition(hWaveOut, ref mmTime, Marshal.SizeOf(mmTime)), "waveOutGetPosition");
if (mmTime.wType != MmTime.TIME_BYTES)
throw new Exception(string.Format("waveOutGetPosition: wType -> Expected {0}, Received {1}", MmTime.TIME_BYTES, mmTime.wType));
return mmTime.cb;
}
}
}
}