(view source code of audioendpoints.cs as plain text)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using NAudio.CoreAudioApi;
namespace AudioEndPoints
{
public partial class FormAudioEndPoints : Form
{
public FormAudioEndPoints( )
{
InitializeComponent( );
}
static readonly string progver = "1.02";
static bool bgcolors = true;
static bool fgcolors = false;
static bool helprequested = false;
static bool linear = true;
static readonly Dictionary<DeviceState, Color> foregroundcolors = new Dictionary<DeviceState, Color>
{
[DeviceState.Active] = Color.Green,
[DeviceState.Disabled] = Color.Orange,
[DeviceState.NotPresent] = Color.SlateGray,
[DeviceState.Unplugged] = Color.Red
};
static readonly Dictionary<DeviceState, Color> backgroundcolors = new Dictionary<DeviceState, Color>
{
[DeviceState.Active] = Color.LightGreen,
[DeviceState.Disabled] = Color.Yellow,
[DeviceState.NotPresent] = Color.LightGray,
[DeviceState.Unplugged] = Color.Red
};
private void DataGridView1_KeyUp( object sender, KeyEventArgs e )
{
if ( e.KeyCode == Keys.F1 )
{
ShowHelp( false );
}
if ( e.KeyCode == Keys.Escape )
{
Application.Exit( );
}
if ( e.Control && e.KeyCode == Keys.P )
{
PrintDataGridView( );
}
if ( e.Control && e.KeyCode == Keys.S )
{
string filename = string.Format( "AudioEndPoints.{0}.{1}.txt", Environment.GetEnvironmentVariable( "COMPUTERNAME" ), DateTime.Now.ToString( "yyyy-MM-dd_HHmmss" ) );
string outfile = Path.Combine( Environment.CurrentDirectory, filename );
SaveDataGridView( outfile );
MessageBox.Show( "Saved as \"" + filename + "\" in folder \"" + Path.GetDirectoryName( outfile ) + "\"", "File Saved", MessageBoxButtons.OK, MessageBoxIcon.Information );
}
}
private void DataGridView1_SortCompare( object sender, DataGridViewSortCompareEventArgs e )
{
if ( e.Column.Index == 0 )
{
e.SortResult = int.Parse( e.CellValue1.ToString( ) ).CompareTo( int.Parse( e.CellValue2.ToString( ) ) );
e.Handled = true; // skip the default sorting
}
}
private void FormAudioEndPoints_FormClosed( object sender, FormClosedEventArgs e )
{
if ( helprequested )
{
ShowHelp( true );
Environment.ExitCode = 0;
Application.Exit( );
}
}
private void FormAudioEndPonts_Load( object sender, EventArgs e )
{
this.Text = "AudioEndPoints.exe, \u00A0 Version " + progver + " \u00A0?\u00A0 \u00A0 Copyright \u00A9 2025 Rob van der Woude";
string commandline = Environment.CommandLine.ToUpper( );
if ( commandline.Contains( "?" ) )
{
ShowHelp( true );
Environment.ExitCode = -1;
Application.Exit( );
}
if ( commandline.Contains( "/F" ) )
{
bgcolors = false;
fgcolors = true;
}
else if ( commandline.Contains( "/BW" ) )
{
bgcolors = false;
fgcolors = false;
}
linear = !commandline.Contains( "/DB" );
SetupDataGridView( );
PopulateDataGridView( );
}
private void PopulateDataGridView( )
{
MMDeviceEnumerator enumerator = new MMDeviceEnumerator( );
int row = 0;
foreach ( MMDevice endpoint in enumerator.EnumerateAudioEndPoints( DataFlow.All, DeviceState.All ) )
{
// Volume reading based on code by Mike de Klerk
// https://stackoverflow.com/a/12534584
string volume = string.Empty;
if ( endpoint.State == DeviceState.Active )
{
if ( linear )
{
volume = string.Format( "{0} %", (int)( endpoint.AudioEndpointVolume.MasterVolumeLevelScalar * 100 ) );
}
else
{
volume = string.Format( "{0} dB", (int)endpoint.AudioEndpointVolume.MasterVolumeLevel );
}
}
string[] line = { row.ToString( ), ( endpoint.DataFlow == DataFlow.Capture ? "IN" : "OUT" ), endpoint.FriendlyName, endpoint.State.ToString( ), endpoint.ID, volume };
dataGridView1.Rows.Add( line );
if ( bgcolors )
{
dataGridView1.Rows[row].DefaultCellStyle.BackColor = backgroundcolors[endpoint.State];
}
if ( fgcolors )
{
dataGridView1.Rows[row].DefaultCellStyle.ForeColor = foregroundcolors[endpoint.State];
}
row++;
}
dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dataGridView1.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
private void PrintDataGridView( )
{
string printout = Path.GetTempFileName( );
SaveDataGridView( printout );
ProcessStartInfo psi = new ProcessStartInfo
{
UseShellExecute = false,
Arguments = string.Format( "/p \"{0}\"", printout ),
FileName = "notepad.exe"
};
Process proc = Process.Start( psi );
proc.WaitForExit( );
File.Delete( printout );
}
private void SaveDataGridView( string outfile )
{
// line out colums
int[] cw = new int[] { 5, 6, 0, 0, 0 };
for ( int i = 0; i < dataGridView1.Columns.Count; i++ )
{
for ( int j = 0; j < dataGridView1.Rows.Count; j++ )
{
if ( dataGridView1.Rows[j].Cells[i].Value != null && dataGridView1.Rows[j].Cells[i].Value.ToString( ).Length > cw[i] )
{
cw[i] = dataGridView1.Rows[j].Cells[i].Value.ToString( ).Length;
}
}
}
// format printout
List<string> lines = new List<string>( );
string linetemplate = "{0,-" + cw[0] + "} {1,-" + cw[1] + "} {2,-" + cw[2] + "} {3,-" + cw[3] + "} {4,-" + cw[4] + "}";
// header
var cols = dataGridView1.Columns;
string headerline = string.Format( linetemplate, cols[0].HeaderText, cols[1].HeaderText.Replace( "\u00A0", "" ), cols[2].HeaderText, cols[3].HeaderText, cols[4].HeaderText );
lines.Add( headerline );
// rows
for ( int i = 0; i < dataGridView1.Rows.Count; i++ )
{
var cells = dataGridView1.Rows[i].Cells;
string line = string.Format( linetemplate, cells[0].Value, cells[1].Value, cells[2].Value, cells[3].Value, cells[4].Value );
lines.Add( line );
}
using ( StreamWriter print = new StreamWriter( outfile ) )
{
for ( int i = 0; i < lines.Count; i++ )
{
print.WriteLine( lines[i] );
}
}
}
private void SetupDataGridView( )
{
dataGridView1.ColumnCount = 6;
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font( dataGridView1.Font, FontStyle.Bold );
dataGridView1.Location = new Point( 8, 8 );
dataGridView1.Size = new Size( 500, 250 );
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
dataGridView1.GridColor = Color.Black;
dataGridView1.RowHeadersVisible = false;
dataGridView1.Columns[0].Name = "Index";
dataGridView1.Columns[1].Name = "In\u00A0/\u00A0Out";
dataGridView1.Columns[2].Name = "Name";
dataGridView1.Columns[3].Name = "Status";
dataGridView1.Columns[4].Name = "ID";
dataGridView1.Columns[5].Name = "Volume";
dataGridView1.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.MultiSelect = false;
dataGridView1.Dock = DockStyle.Fill;
}
private void ShowHelp( bool cli = false )
{
string message = string.Format( "\nAudioEndPoints.exe, Version {0}\n", progver );
message += "List the local computer's audio endpoints\n\n";
message += "Usage:\tAudioEndPoints.exe\t[ /F | /BW ] [ /DB ]\n\n";
message += "Where:\t \t/F \tuses status dependent\n";
message += " \t \t \tFOREGROUND colors,\n";
message += " \t \t/BW\tuses BLACK and WHITE,\n";
message += " \t \t \tdefault is status\n";
message += " \t \t \tdependent BACKGROUND\n";
message += " \t \t/DB\tshows volume in dB,\n";
message += " \t \t \tdefault is percentage\n\n";
message += "Click on ANY column header to sort the table by that column.\n";
message += "You can edit all cells, if you like.\n\n";
message += "Keys: \tCtrl+P will print the list,\n";
message += " \tCtrl+S will save it in a text file,\n";
message += " \tF1 will show this help text,\n";
message += " \tCtrl+C will copy the SELECTED LINE to the clipboard,\n";
message += " \tEscape will abort the program.\n\n";
message += "Credits:\tTo access audio endpoints, this program uses\n";
message += " \tNAudio created by Mark Heath:\n";
message += " \thttps://github.com/naudio/NAudio\n";
message += " \tVolume reading based on code by Mike de Klerk:\n";
message += " \thttps://stackoverflow.com/a/12534584\n";
message += " \tOutput to parent console by Timm:\n";
message += " \twww.csharp411.com/console-output-from-winforms-application/\n\n";
message += "Written by Rob van der Woude\n";
message += "https://www.robvanderwoude.com\n";
if ( !cli )
{
helprequested = true;
MessageBox.Show( message, "AudioEndPoints.exe, \u00A0 Version " + progver );
}
else
{
Console.WriteLine( message );
}
}
}
}
page last modified: 2024-04-16; loaded in 0.0080 seconds