(view source code of detectsound.cs as plain text)
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Speech.Recognition;
namespace RobvanderWoude
{
internal class DetectSound
{
static readonly string progver = "1.01";
static SpeechRecognitionEngine recognizer;
static System.Timers.Timer timer;
static TimeSpan interval = TimeSpan.FromSeconds( 10 );
static int timeout = 10;
static int linelength;
static bool quiet = false;
static readonly string decimalseparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
static int Main( string[] args )
{
int rc = 0;
if ( args.Length == 1 || args.Length == 2 )
{
foreach ( string arg in args )
{
if ( arg == "/?" )
{
return ShowHelp( );
}
else if ( arg.ToUpper( ) == "/Q" )
{
if ( quiet )
{
return ShowHelp( "Duplicate command line switch /Q" );
}
quiet = true;
}
else if ( int.TryParse( arg, out timeout ) )
{
if ( timeout < 1 || timeout > 600 )
{
return ShowHelp( "Specified timeout out of range (0{0}5..600)", decimalseparator );
}
interval = TimeSpan.FromSeconds( timeout );
}
else if ( float.TryParse( arg.Replace( ".", decimalseparator ).Replace( ",", decimalseparator ), out float timeoutfloat ) )
{
if ( timeoutfloat < 0.5 || timeoutfloat > 600 )
{
return ShowHelp( "Specified timeout out of range (0{0}5..600)", decimalseparator );
}
timeout = (int)timeoutfloat;
interval = TimeSpan.FromSeconds( timeoutfloat );
}
else
{
return ShowHelp( "Invalid command line argument \"{0}\"", arg );
}
}
}
else if ( args.Length > 2 )
{
return ShowHelp( "Invalid command line arguments" );
}
if ( !quiet )
{
timer = new System.Timers.Timer( );
timer.Interval = 1000;
timer.Elapsed += Timer_Elapsed;
timer.Start( );
}
// Based on source code by Wendy Zang
// https://social.msdn.microsoft.com/Forums/vstudio/en-US/72f769f3-1465-402a-b090-86d0ce0530c5/c-console-application-how-to-detect-the-mic-input#3a1a664c-db49-4152-a2bd-080a03287b9f
recognizer = new SpeechRecognitionEngine( );
Grammar dictationGrammar = new DictationGrammar( );
recognizer.LoadGrammar( dictationGrammar );
try
{
if ( !quiet )
{
Console.Write( "Speak, you've got {0} seconds left \b", timeout );
}
recognizer.SetInputToDefaultAudioDevice( );
recognizer.SpeechDetected += Recognizer_SpeechDetected;
recognizer.Recognize( interval );
}
catch ( InvalidOperationException e )
{
rc = ShowHelp( "Default microphone not detected\n{0} - {1}.", e.Source, e.Message );
}
finally
{
if ( !quiet )
{
timer.Stop( );
}
recognizer.UnloadAllGrammars( );
}
if ( !quiet )
{
Console.Write( new string( '\b', linelength ) );
Console.Write( new string( ' ', linelength ) );
Console.Write( new string( '\b', linelength ) );
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine( "Silence only, no sound detected" );
Console.ResetColor( );
return rc;
}
private static void Timer_Elapsed( object sender, System.Timers.ElapsedEventArgs e )
{
string message = "Speak, you've got {0} seconds left \b";
linelength = string.Format( message, timeout ).Length;
Console.Write( new string( '\b', linelength ) );
Console.Write( new string( ' ', linelength ) );
Console.Write( new string( '\b', linelength ) );
timeout--;
if ( timeout >= 0 )
{
Console.Write( message, timeout );
}
}
private static void Recognizer_SpeechDetected( object sender, SpeechDetectedEventArgs e )
{
if ( !quiet )
{
timer.Stop( );
Console.Write( new string( '\b', linelength ) );
Console.Write( new string( ' ', linelength ) );
Console.Write( new string( '\b', linelength ) );
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine( "Sound detected" );
Console.ResetColor( );
Environment.Exit( 1 );
}
#region Error handling
static int ShowHelp( params string[] errmsg )
{
#region Error Message
if ( errmsg.Length > 0 )
{
List<string> errargs = new List<string>( errmsg );
errargs.RemoveAt( 0 );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
Console.ResetColor( );
}
#endregion Error Message
#region Help Text
/*
DetectSound.exe, Version 1.00
Detect sound input on default microphone before detection period expires
?
Usage: DetectSound.exe [ timeout ] [ /Q ]
?
Where: timeout detection period in seconds (0.5..600; default: 10)
/Q Quiet mode: no messages except result
Credits: Based on source code by Wendy Zang
https://social.msdn.microsoft.com/Forums/vstudio/en-US
/72f769f3-1465-402a-b090-86d0ce0530c5
/c-console-application-how-to-detect-the-mic-input
#3a1a664c-db49-4152-a2bd-080a03287b9f
?
Notes: This program uses Microsoft's speech recognition technology
to detect sound, so it will be sensitive to speech and yells,
but less so to other noises like clapping or whistling.
Decimal separator for timeout may be dot or comma.
Return code 0 if sound is detected within the timeout period,
1 if not, or -1 in case of errors
?
Written by Rob van der Woude
https://www.robvanderwoude.com
*/
#endregion Help Text
#region Display Help Text
Console.Error.WriteLine( );
Console.Error.WriteLine( "DetectSound.exe, Version {0}", progver );
Console.Error.WriteLine( "Detect sound input on default microphone before detection period expires" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "DetectSound.exe [ timeout ] [ /Q ]" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "timeout" );
Console.ResetColor( );
Console.Error.WriteLine( " detection period in seconds (0{0}5..600; default: 10)", decimalseparator );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( " /Q Q" );
Console.ResetColor( );
Console.Error.WriteLine( "uiet mode: no messages except result" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Credits: Based on source code by Wendy Zang" );
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Error.WriteLine( " https://social.msdn.microsoft.com/Forums/vstudio/en-US" );
Console.Error.WriteLine( " /72f769f3-1465-402a-b090-86d0ce0530c5" );
Console.Error.WriteLine( " /c-console-application-how-to-detect-the-mic-input" );
Console.Error.WriteLine( " #3a1a664c-db49-4152-a2bd-080a03287b9f" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Notes: This program uses Microsoft's speech recognition technology" );
Console.Error.WriteLine( " to detect sound, so it will be sensitive to speech and yells," );
Console.Error.WriteLine( " but less so to other noises like clapping or whistling." );
Console.Error.WriteLine( " Decimal separator for timeout may be dot or comma." );
Console.Error.WriteLine( " Return code 0 if sound is detected within the timeout period," );
Console.Error.WriteLine( " 1 if not, or -1 in case of errors" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "https://www.robvanderwoude.com" );
#endregion Display Help Text
return -1;
}
#endregion Error handling
}
}
page last modified: 2024-04-16; loaded in 0.0096 seconds