Skip to content

Commit f42218b

Browse files
committed
implemented option to set playback volume
1 parent 92f3f2a commit f42218b

7 files changed

Lines changed: 63 additions & 3 deletions

File tree

DemoHarness/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ private static void Main(string[] args)
4646
fileName = Console.ReadLine();
4747
ShowInstruction();
4848
break;
49+
case "volume":
50+
Console.WriteLine("Enter new volume in percent");
51+
byte volume = Convert.ToByte(Console.ReadLine());
52+
player.SetVolume(volume).Wait();
53+
ShowInstruction();
54+
break;
4955
case "exit":
5056
break;
5157
default:
@@ -75,6 +81,7 @@ private static void ShowInstruction()
7581
Console.WriteLine("resume - Resume the playback");
7682
Console.WriteLine("stop - Stop the playback");
7783
Console.WriteLine("change - Change the file name");
84+
Console.WriteLine("volume - Set the volume");
7885
Console.WriteLine("exit - Exit the app");
7986
}
8087

NetCoreAudio/Interfaces/IPlayer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public interface IPlayer
1414
Task Pause();
1515
Task Resume();
1616
Task Stop();
17+
Task SetVolume(byte percent);
1718
}
1819
}

NetCoreAudio/Player.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,14 @@ private void OnPlaybackFinished(object sender, EventArgs e)
8080
{
8181
PlaybackFinished?.Invoke(this, e);
8282
}
83+
84+
/// <summary>
85+
/// Sets the playing volume as percent
86+
/// </summary>
87+
/// <returns></returns>
88+
public async Task SetVolume(byte percent)
89+
{
90+
await _internalPlayer.SetVolume(percent);
91+
}
8392
}
84-
}
93+
}

NetCoreAudio/Players/LinuxPlayer.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using NetCoreAudio.Interfaces;
2+
using System;
23
using System.IO;
4+
using System.Threading.Tasks;
35

46
namespace NetCoreAudio.Players
57
{
@@ -16,5 +18,16 @@ protected override string GetBashCommand(string fileName)
1618
return "aplay -q";
1719
}
1820
}
21+
22+
public override Task SetVolume(byte percent)
23+
{
24+
if (percent > 100)
25+
throw new ArgumentOutOfRangeException(nameof(percent), "Percent can't exceed 100");
26+
27+
var tempProcess = StartBashProcess($"amixer -M set Headphone {percent}%");
28+
tempProcess.WaitForExit();
29+
30+
return Task.CompletedTask;
31+
}
1932
}
2033
}

NetCoreAudio/Players/MacPlayer.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using NetCoreAudio.Interfaces;
2+
using System;
3+
using System.Threading.Tasks;
24

35
namespace NetCoreAudio.Players
46
{
@@ -8,5 +10,16 @@ protected override string GetBashCommand(string fileName)
810
{
911
return "afplay";
1012
}
13+
14+
public override Task SetVolume(byte percent)
15+
{
16+
if (percent > 100)
17+
throw new ArgumentOutOfRangeException(nameof(percent), "Percent can't exceed 100");
18+
19+
var tempProcess = StartBashProcess($"osascript -e \"set volume output volume {percent}\"");
20+
tempProcess.WaitForExit();
21+
22+
return Task.CompletedTask;
23+
}
1124
}
1225
}

NetCoreAudio/Players/UnixPlayerBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,7 @@ internal void HandlePlaybackFinished(object sender, EventArgs e)
9999
PlaybackFinished?.Invoke(this, e);
100100
}
101101
}
102+
103+
public abstract Task SetVolume(byte percent);
102104
}
103105
}

NetCoreAudio/Players/WindowsPlayer.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ internal class WindowsPlayer : IPlayer
1717
[DllImport("winmm.dll")]
1818
private static extern int mciGetErrorString(int errorCode, StringBuilder errorText, int errorTextSize);
1919

20-
private Timer _playbackTimer;
20+
[DllImport("winmm.dll")]
21+
public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
22+
23+
private Timer _playbackTimer;
2124
private Stopwatch _playStopwatch;
2225

2326
private string _fileName;
@@ -119,5 +122,17 @@ private Task ExecuteMsiCommand(string commandString)
119122

120123
return Task.CompletedTask;
121124
}
122-
}
125+
126+
public Task SetVolume(byte percent)
127+
{
128+
// Calculate the volume that's being set
129+
int NewVolume = ushort.MaxValue / 100 * percent;
130+
// Set the same volume for both the left and the right channels
131+
uint NewVolumeAllChannels = ((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16);
132+
// Set the volume
133+
waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
134+
135+
return Task.CompletedTask;
136+
}
137+
}
123138
}

0 commit comments

Comments
 (0)