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";
  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}", 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}", 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. 		#region Error handling
  48.  
  49. 		static int ShowHelp( params string[] errmsg )
  50. 		{
  51. 			#region Error Message
  52.  
  53. 			if ( errmsg.Length > 0 )
  54. 			{
  55. 				List<string> errargs = new List<string>( errmsg );
  56. 				errargs.RemoveAt( 0 );
  57. 				Console.Error.WriteLine( );
  58. 				Console.ForegroundColor = ConsoleColor.Red;
  59. 				Console.Error.Write( "ERROR:\t" );
  60. 				Console.ForegroundColor = ConsoleColor.White;
  61. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  62. 				Console.ResetColor( );
  63. 			}
  64.  
  65. 			#endregion Error Message
  66.  
  67.  
  68. 			#region Help Text
  69.  
  70. 			/*
  71. 			DetectSpeakers.exe,  Version 1.00
  72. 			Check if speakers or headphones are connected
  73.  
  74. 			Usage:    DetectSpeakers.exe
  75.  
  76. 			Notes:    Return code ("errorlevel") equals the number of active
  77. 			          audio output devices detected, or -1 in case of errors.
  78. 			          This program uses NAudio by Mark Heath et al.
  79. 			          NAudio files required (in this program's parent folder) are
  80. 			          NAudio.Core.dll, NAudio.Wasapi.dll, NAudio.WinForms.dll and
  81. 			          NAudio.WinMM.dll, all included in this program's ZIP file.
  82. 			          Consider this program experimental: it had only limited
  83. 			          testing, due mainly to my computer's limited audio hardware.
  84.  
  85. 			Credits:  NAudio library for .NET by Mark Heath et al
  86. 			          https://github.com/naudio/NAudio
  87.  
  88. 			Written by Rob van der Woude
  89. 			https://www.robvanderwoude.com
  90. 			*/
  91.  
  92. 			#endregion Help Text
  93.  
  94.  
  95. 			#region Display Help Text
  96.  
  97. 			Console.Error.WriteLine( );
  98.  
  99. 			Console.Error.WriteLine( "DetectSpeakers.exe,  Version {0}", progver );
  100.  
  101. 			Console.Error.WriteLine( "Check if speakers or headphones are connected" );
  102.  
  103. 			Console.Error.WriteLine( );
  104.  
  105. 			Console.Error.Write( "Usage:    " );
  106. 			Console.ForegroundColor = ConsoleColor.White;
  107. 			Console.Error.WriteLine( "DetectSpeakers.exe" );
  108. 			Console.ResetColor( );
  109.  
  110. 			Console.Error.WriteLine( );
  111.  
  112. 			Console.Error.WriteLine( "Notes:    Return code (\"errorlevel\") equals the number of active" );
  113.  
  114. 			Console.Error.WriteLine( "          audio output devices detected, or -1 in case of errors." );
  115.  
  116. 			Console.Error.WriteLine( "          This program uses NAudio by Mark Heath et al." );
  117.  
  118. 			Console.Error.WriteLine( "          NAudio files required (in this program's parent folder) are" );
  119.  
  120. 			Console.Error.WriteLine( "          NAudio.Core.dll, NAudio.Wasapi.dll, NAudio.WinForms.dll and" );
  121.  
  122. 			Console.Error.WriteLine( "          NAudio.WinMM.dll, all included in this program's ZIP file." );
  123.  
  124. 			Console.Error.WriteLine( "          Consider this program experimental: it had only limited" );
  125.  
  126. 			Console.Error.WriteLine( "          testing, due mainly to my computer's limited audio hardware." );
  127.  
  128. 			Console.Error.WriteLine( );
  129.  
  130. 			Console.Error.WriteLine( "Credits:  NAudio library for .NET by Mark Heath et al" );
  131.  
  132. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  133. 			Console.Error.WriteLine( "          https://github.com/naudio/NAudio" );
  134. 			Console.ResetColor( );
  135.  
  136. 			Console.Error.WriteLine( );
  137.  
  138. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  139.  
  140. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  141.  
  142. 			#endregion Display Help Text
  143.  
  144.  
  145. 			return -1;
  146. 		}
  147.  
  148. 		#endregion Error handling
  149. 	}
  150. }
  151.  

page last uploaded: 2021-01-27