Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for detectspeakers.cs

(view source code of detectspeakers.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5. namespace RobvanderWoude
  6. {
  7. 	internal class DetectSpeakers
  8. 	{
  9. 		static readonly string progver = "1.00.2025";
  10.  
  11.  
  12. 		static int Main( string[] args )
  13. 		{
  14. 			if ( args.Length > 0 )
  15. 			{
  16. 				return ShowHelp( );
  17. 			}
  18.  
  19. 			try
  20. 			{
  21. 				int count = NAudio.Wave.WaveOut.DeviceCount;
  22. 				Console.WriteLine( "{0} Audio output device{1} detected", count, ( count == 1 ? "" : "s" ) );
  23.  
  24. 				NAudio.CoreAudioApi.MMDeviceEnumerator MMDE = new NAudio.CoreAudioApi.MMDeviceEnumerator( );
  25. 				NAudio.CoreAudioApi.MMDeviceCollection devices = MMDE.EnumerateAudioEndPoints( NAudio.CoreAudioApi.DataFlow.All, NAudio.CoreAudioApi.DeviceState.Active );
  26. 				if ( devices.Count == 1 )
  27. 				{
  28. 					Console.WriteLine( "Active device: {0}", devices[0].DeviceFriendlyName );
  29. 					Console.WriteLine( "Master volume: {0:F1}", devices[0].AudioEndpointVolume.MasterVolumeLevel );
  30. 				}
  31. 				else if ( devices.Count > 1 )
  32. 				{
  33. 					Console.WriteLine( "Active devices' master volume:" );
  34. 					foreach ( NAudio.CoreAudioApi.MMDevice device in devices )
  35. 					{
  36. 						Console.WriteLine( "{0}: {1:F1}", device.DeviceFriendlyName, device.AudioEndpointVolume.MasterVolumeLevel );
  37. 					}
  38. 				}
  39. 				return count;
  40. 			}
  41. 			catch ( Exception e )
  42. 			{
  43. 				return ShowHelp( e.Message );
  44. 			}
  45. 		}
  46.  
  47.  
  48. 		#region Error handling
  49.  
  50. 		static int ShowHelp( params string[] errmsg )
  51. 		{
  52. 			#region Error Message
  53.  
  54. 			if ( errmsg.Length > 0 )
  55. 			{
  56. 				List<string> errargs = new List<string>( errmsg );
  57. 				errargs.RemoveAt( 0 );
  58. 				Console.Error.WriteLine( );
  59. 				Console.ForegroundColor = ConsoleColor.Red;
  60. 				Console.Error.Write( "ERROR:\t" );
  61. 				Console.ForegroundColor = ConsoleColor.White;
  62. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  63. 				Console.ResetColor( );
  64. 			}
  65.  
  66. 			#endregion Error Message
  67.  
  68.  
  69. 			#region Help Text
  70.  
  71. 			/*
  72. 			DetectSpeakers.exe,  Version 1.00
  73. 			Check if speakers or headphones are connected
  74.  
  75. 			Usage:    DetectSpeakers.exe
  76.  
  77. 			Notes:    Return code ("errorlevel") equals the number of active
  78. 			          audio output devices detected, or -1 in case of errors.
  79. 			          This program uses NAudio by Mark Heath et al.
  80. 			          NAudio files required (in this program's parent folder) are
  81. 			          NAudio.Core.dll, NAudio.Wasapi.dll, NAudio.WinForms.dll and
  82. 			          NAudio.WinMM.dll, all included in this program's ZIP file.
  83. 			          Consider this program experimental: it had only limited
  84. 			          testing, due mainly to my computer's limited audio hardware.
  85.  
  86. 			Credits:  NAudio library for .NET by Mark Heath et al
  87. 			          https://github.com/naudio/NAudio
  88.  
  89. 			Written by Rob van der Woude
  90. 			https://www.robvanderwoude.com
  91. 			*/
  92.  
  93. 			#endregion Help Text
  94.  
  95.  
  96. 			#region Display Help Text
  97.  
  98. 			Console.Error.WriteLine( );
  99.  
  100. 			Console.Error.WriteLine( "DetectSpeakers.exe,  Version {0}", progver );
  101.  
  102. 			Console.Error.WriteLine( "Check if speakers or headphones are connected" );
  103.  
  104. 			Console.Error.WriteLine( );
  105.  
  106. 			Console.Error.Write( "Usage:    " );
  107. 			Console.ForegroundColor = ConsoleColor.White;
  108. 			Console.Error.WriteLine( "DetectSpeakers.exe" );
  109. 			Console.ResetColor( );
  110.  
  111. 			Console.Error.WriteLine( );
  112.  
  113. 			Console.Error.WriteLine( "Notes:    Return code (\"errorlevel\") equals the number of active" );
  114.  
  115. 			Console.Error.WriteLine( "          audio output devices detected, or -1 in case of errors." );
  116.  
  117. 			Console.Error.WriteLine( "          This program uses NAudio by Mark Heath et al." );
  118.  
  119. 			Console.Error.WriteLine( "          NAudio files required (in this program's parent folder) are" );
  120.  
  121. 			Console.Error.WriteLine( "          NAudio.Core.dll, NAudio.Wasapi.dll, NAudio.WinForms.dll and" );
  122.  
  123. 			Console.Error.WriteLine( "          NAudio.WinMM.dll, all included in this program's ZIP file." );
  124.  
  125. 			Console.Error.WriteLine( "          Consider this program experimental: it had only limited" );
  126.  
  127. 			Console.Error.WriteLine( "          testing, due mainly to my computer's limited audio hardware." );
  128.  
  129. 			Console.Error.WriteLine( );
  130.  
  131. 			Console.Error.WriteLine( "Credits:  NAudio library for .NET by Mark Heath et al" );
  132.  
  133. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  134. 			Console.Error.WriteLine( "          https://github.com/naudio/NAudio" );
  135. 			Console.ResetColor( );
  136.  
  137. 			Console.Error.WriteLine( );
  138.  
  139. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  140.  
  141. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  142.  
  143. 			#endregion Display Help Text
  144.  
  145.  
  146. 			return -1;
  147. 		}
  148.  
  149. 		#endregion Error handling
  150. 	}
  151. }

page last modified: 2025-10-11; loaded in 0.0101 seconds