-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
149 lines (133 loc) · 5.51 KB
/
Copy pathProgram.cs
File metadata and controls
149 lines (133 loc) · 5.51 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System.Data;
using System.Runtime.InteropServices;
using NAudio.CoreAudioApi;
namespace SetSoundVolume {
/// <summary>
/// Provides functionality to control system audio volume through command line interface.
/// </summary>
internal class Program {
/// <summary>
/// Displays the usage instructions for the application.
/// </summary>
/// <remarks>
/// Shows supported commands and arguments, using the actual executable name in the examples.
/// </remarks>
static void ShowUsage() {
string exeName = Path.GetFileName(Environment.ProcessPath ?? "volume.exe");
Console.WriteLine($"Usage: {exeName} [volume_percentage (0-100)] [-q|--quiet|/quiet] [-h|--help|/help]");
Console.WriteLine("If no arguments are provided, displays current volume");
Console.WriteLine("Options:");
Console.WriteLine(" -q, --quiet, /quiet Suppress output");
Console.WriteLine(" -h, --help, /help Show this help message");
}
/// <summary>
/// Main entry point for the application.
/// </summary>
/// <param name="args">Command line arguments:
/// <list type="bullet">
/// <item><description>No arguments: Display current volume</description></item>
/// <item><description>Number (0-100): Set volume to specified percentage</description></item>
/// <item><description>-q, --quiet, /quiet: Suppress output</description></item>
/// <item><description>-h, --help, /help: Show usage instructions</description></item>
/// </list>
/// </param>
/// <remarks>
/// The application can be used to either view or set the system's master volume.
/// When setting the volume, values must be between 0 and 100 inclusive.
/// </remarks>
static void Main(string[] args) {
bool quietMode = false;
float? volumeToSet = null;
// Process arguments
foreach (string arg in args) {
// Check for help argument
if (arg.Equals("-h", StringComparison.OrdinalIgnoreCase) ||
arg.Equals("--help", StringComparison.OrdinalIgnoreCase) ||
arg.Equals("/help", StringComparison.OrdinalIgnoreCase)) {
ShowUsage();
return;
}
// Check for quiet mode argument
else if (arg.Equals("-q", StringComparison.OrdinalIgnoreCase) ||
arg.Equals("--quiet", StringComparison.OrdinalIgnoreCase) ||
arg.Equals("/quiet", StringComparison.OrdinalIgnoreCase)) {
quietMode = true;
}
// Try to parse as volume value
else if (float.TryParse(arg, out float volume)) {
if (volume < 0 || volume > 100) {
if (!quietMode) {
Console.WriteLine("Error: Volume must be between 0 and 100");
}
return;
}
volumeToSet = volume;
}
// Invalid argument
else {
if (!quietMode) {
Console.WriteLine($"Error: Invalid argument '{arg}'");
ShowUsage();
}
return;
}
}
try {
// Initialize audio device
var deviceEnumerator = new MMDeviceEnumerator();
var device = deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
// If volume is to be set
if (volumeToSet != null) {
if (volumeToSet.Value == 0) {
device.AudioEndpointVolume.Mute = true;
device.AudioEndpointVolume.MasterVolumeLevelScalar = 0f;
if (!quietMode) {
Console.WriteLine("Volume muted (0%)");
}
}
else {
device.AudioEndpointVolume.Mute = false;
device.AudioEndpointVolume.MasterVolumeLevelScalar = volumeToSet.Value / 100.0f;
if (!quietMode) {
Console.WriteLine($"Volume set to: {volumeToSet:F1}%");
}
}
}
else if (!quietMode) // Just showing current volume
{
float currentVolume = device.AudioEndpointVolume.MasterVolumeLevelScalar * 100;
bool isMuted = device.AudioEndpointVolume.Mute;
Console.WriteLine($"Current Master Volume: {currentVolume:F1}% {(isMuted ? "(Muted)" : "")}");
}
}
catch (Exception ex) {
if (!quietMode) {
Console.WriteLine($"Error: Failed to access audio device: {ex.Message}");
}
}
}
}
}
// Original hand-written version
//using System.Data;
//using System.Runtime.InteropServices;
//using NAudio.CoreAudioApi;
//namespace SetSoundVolume {
// internal class Program {
// static void Main(string[] args) {
// // Ensure a command-line argument is provided and is a valid volume
// if (args.Length != 1 || !float.TryParse(args[0], out float newVolume) || newVolume < 0 || newVolume > 100) {
// Console.WriteLine("Usage: VolumeControl <volume_percentage (0-100)>");
// return;
// }
// var deviceEnumerator = new MMDeviceEnumerator();
// var device = deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
// //// Read the current volume
// //float currentVolume = device.AudioEndpointVolume.MasterVolumeLevelScalar * 100;
// //Console.WriteLine($"Current Master Volume: {currentVolume}%");
// // Set the new volume
// device.AudioEndpointVolume.MasterVolumeLevelScalar = newVolume / 100.0f;
// Console.WriteLine($"Volume set to: {newVolume}%");
// }
// }
//}