Skip to content

Commit

Permalink
Merge pull request #1 from SaxxonPike/directsound
Browse files Browse the repository at this point in the history
Merge DS capability and UI improvements
  • Loading branch information
SaxxonPike committed Oct 19, 2016
2 parents e70d161 + 55474a3 commit 8484dc9
Show file tree
Hide file tree
Showing 33 changed files with 838 additions and 302 deletions.
6 changes: 6 additions & 0 deletions Playthrough2/Playthrough2.UI/ApplicationNotifyIcon.cs
@@ -0,0 +1,6 @@
namespace Playthrough2.UI
{
public class ApplicationNotifyIcon
{
}
}
17 changes: 17 additions & 0 deletions Playthrough2/Playthrough2.UI/BitmapExtensions.cs
@@ -0,0 +1,17 @@
using System.Drawing;

namespace Playthrough2.UI
{
public static class BitmapExtensions
{
public static Icon ToIcon(this Bitmap bitmap)
{
using (var tempBitmap = new Bitmap(bitmap))
{
tempBitmap.MakeTransparent(Color.Magenta);
var icon = tempBitmap.GetHicon();
return Icon.FromHandle(icon);
}
}
}
}
360 changes: 248 additions & 112 deletions Playthrough2/Playthrough2.UI/ConfigurationForm.Designer.cs

Large diffs are not rendered by default.

189 changes: 175 additions & 14 deletions Playthrough2/Playthrough2.UI/ConfigurationForm.cs
@@ -1,6 +1,8 @@
using System;
using System.Linq;
using System.Windows.Forms;
using NAudio.Wave;
using Playthrough2.UI.Properties;

namespace Playthrough2.UI
{
Expand All @@ -16,6 +18,29 @@ public ConfigurationForm()

Shown += OnFirstShown;
Closed += OnClosed;
notifyIcon.Click += OnNotifyIconClicked;
}

private void OnNotifyIconClicked(object sender, EventArgs e)
{
ShowAndBringToFront();
}

protected override void WndProc(ref Message m)
{
if (m.Msg == Win32.WM_SHOWME)
ShowAndBringToFront();
base.WndProc(ref m);
}

private void ShowAndBringToFront()
{
if (WindowState == FormWindowState.Minimized)
WindowState = FormWindowState.Normal;

var top = TopMost;
TopMost = true;
TopMost = top;
}

private void SetDefaultValues()
Expand All @@ -28,80 +53,128 @@ private void SetDefaultValues()

private IWavePipeConfiguration GetConfiguration()
{
int frequency;
int channels;

return new WavePipeConfiguration
{
WaveInDevice = inputDeviceComboBox.SelectedItem as WaveInDevice,
WaveOutDevice = outputDeviceComboBox.SelectedItem as WaveOutDevice,
WaveInDevice = inputDeviceComboBox.SelectedItem as IWaveInDevice,
WaveOutDevice = outputDeviceComboBox.SelectedItem as IWaveOutDevice,
InputBufferCount = inputBufferCountSlider.Value,
InputBufferLength = inputBufferSizeSlider.Value,
OutputBufferCount = outputBufferCountSlider.Value,
OutputLatency = outputLatencySlider.Value
OutputLatency = outputLatencySlider.Value,
InputFormat = inputFormatEnable.Checked &&
int.TryParse(inputFormatFrequency.Text, out frequency) &&
int.TryParse(inputFormatChannels.Text, out channels)
? new WaveFormat(frequency, channels)
: null
};
}

private void OnClosed(object sender, EventArgs eventArgs)
{
_wavePipeManager.Dispose();
notifyIcon.Visible = false;
}

private void OnFirstShown(object sender, EventArgs e)
{
Shown -= OnFirstShown;

PopulateDevices();

notifyIcon.Text = Text;
notifyIcon.Icon = Resources.Logo16.ToIcon();
notifyIcon.Visible = true;
}

private void PopulateDevices()
{
inputDeviceComboBox.Items.AddRange(_waveDeviceEnumerator.GetWaveInDevices().Cast<object>().ToArray());
inputDeviceComboBox.SelectedIndex = inputDeviceComboBox.Items.Count > 0 ? 0 : -1;

outputDeviceComboBox.Items.AddRange(_waveDeviceEnumerator.GetWaveOutDevices().Cast<object>().ToArray());
outputDeviceComboBox.SelectedIndex = outputDeviceComboBox.Items.Count > 0 ? 0 : -1;
}

private void OnStartClicked(object sender, EventArgs e)
{
if (inputDeviceComboBox.SelectedItem == null || outputDeviceComboBox.SelectedItem == null)
return;

_wavePipeManager.Start(GetConfiguration());
try
{
_wavePipeManager.Start(GetConfiguration());
}
catch (Exception ex)
{
MessageBox.Show($"WavePipeManager could not start the route.\n{ex.Message}");
}

UpdateRoutes();
}

private void UpdateRoutes()
{
foreach (var newRoute in _wavePipeManager.Pipes.Except(routeList.Items.Cast<IWavePipeInfo>()))
routeList.Items.Add(newRoute);
foreach (var deadRoute in routeList.Items.Cast<IWavePipeInfo>().Except(_wavePipeManager.Pipes))
routeList.Items.Remove(deadRoute);
OnDeviceChanged();
}

private void OnStopClicked(object sender, EventArgs e)
{
if (inputDeviceComboBox.SelectedItem == null || outputDeviceComboBox.SelectedItem == null)
return;

_wavePipeManager.Stop(GetConfiguration());
try
{
_wavePipeManager.Stop(GetConfiguration());
}
catch (Exception ex)
{
MessageBox.Show($"WavePipeManager could not stop the route.\n{ex.Message}");
}
UpdateRoutes();
}

private void SetTrackbarValue(TrackBar trackBar, Control valueLabel, int value, string format)
private void SetTrackbarValue(TrackBar trackBar, Control valueLabel, int? value, string format)
{
SuspendLayout();
valueLabel.Text = string.Format(format, value);
if (trackBar.Value != value)
trackBar.Value = value;
if (value.HasValue && trackBar.Value != value)
trackBar.Value = value.Value;
ResumeLayout();
}

private void SetMillisecondValue(TrackBar trackBar, Control valueLabel, int value)
private void SetMillisecondValue(TrackBar trackBar, Control valueLabel, int? value)
{
SetTrackbarValue(trackBar, valueLabel, value, "{0}ms");
}

private void SetIntegerValue(TrackBar trackBar, Control valueLabel, int value)
private void SetIntegerValue(TrackBar trackBar, Control valueLabel, int? value)
{
SetTrackbarValue(trackBar, valueLabel, value, "{0}");
}

private void SetInputBufferSize(int value)
private void SetInputBufferSize(int? value)
{
SetMillisecondValue(inputBufferSizeSlider, inputBufferSizeValueLabel, value);
}

private void SetInputBufferCount(int value)
private void SetInputBufferCount(int? value)
{
SetIntegerValue(inputBufferCountSlider, inputBufferCountValueLabel, value);
}

private void SetOutputBufferCount(int value)
private void SetOutputBufferCount(int? value)
{
SetIntegerValue(outputBufferCountSlider, outputBufferCountValueLabel, value);
}

private void SetOutputLatency(int value)
private void SetOutputLatency(int? value)
{
SetMillisecondValue(outputLatencySlider, outputLatencyValueLabel, value);
}
Expand All @@ -125,5 +198,93 @@ private void OnOutputLatencyScroll(object sender, EventArgs e)
{
SetOutputLatency(outputLatencySlider.Value);
}

private void OnRouteListSelectedIndexChanged(object sender, EventArgs e)
{
var pipeInfo = routeList.SelectedItem as IWavePipeInfo;
if (pipeInfo == null)
return;

SuspendLayout();
UpdateInterfaceForPipe(pipeInfo);
ResumeLayout();
}

private void UpdateInterfaceForPipe(IWavePipeInfo pipeInfo)
{
if (pipeInfo.WaveInDevice?.SupportsBufferSize ?? false)
SetInputBufferSize(pipeInfo.Configuration.InputBufferLength);
if (pipeInfo.WaveInDevice?.SupportsBufferCount ?? false)
SetInputBufferCount(pipeInfo.Configuration.InputBufferCount);
if (pipeInfo.WaveOutDevice?.SupportsBufferSize ?? false)
SetOutputLatency(pipeInfo.Configuration.OutputLatency);
if (pipeInfo.WaveOutDevice?.SupportsBufferCount ?? false)
SetOutputBufferCount(pipeInfo.Configuration.OutputBufferCount);

if (inputDeviceComboBox.SelectedItem != pipeInfo.WaveInDevice ||
outputDeviceComboBox.SelectedItem != pipeInfo.WaveOutDevice)
{
inputDeviceComboBox.SelectedItem = pipeInfo.WaveInDevice;
outputDeviceComboBox.SelectedItem = pipeInfo.WaveOutDevice;
}
else
{
OnDeviceChanged();
}
}

private void OnDeviceChanged()
{
var selectedRoute = GetConfiguration();
var routeExists = _wavePipeManager.ContainsPipeWithDevices(selectedRoute);
startButton.Text = routeExists ? "Restart" : "Start";
stopButton.Enabled = routeExists;

inputDeviceBufferSizePanel.Enabled = selectedRoute.WaveInDevice?.SupportsBufferSize ?? false;
inputDeviceBufferCountPanel.Enabled = selectedRoute.WaveInDevice?.SupportsBufferCount ?? false;
inputFormatPanel.Enabled = selectedRoute.WaveInDevice?.SupportsFormat ?? false;

if (selectedRoute.InputFormat != null)
{
inputFormatEnable.Checked = true;
var format = selectedRoute.InputFormat;
inputFormatFrequency.Text = $"{format.SampleRate}";
inputFormatChannels.Text = $"{format.Channels}";
}
else
{
inputFormatEnable.Checked = false;
}

outputDeviceBufferSizePanel.Enabled = selectedRoute.WaveOutDevice?.SupportsBufferSize ?? false;
outputDeviceBufferCountPanel.Enabled = selectedRoute.WaveOutDevice?.SupportsBufferCount ?? false;
}

private void OnInputDeviceChanged(object sender, EventArgs e)
{
OnDeviceChanged();
}

private void OnOutputDeviceChanged(object sender, EventArgs e)
{
OnDeviceChanged();
}

private void OnInputFormatEnableChanged(object sender, EventArgs e)
{
inputFormatChannels.Enabled = inputFormatFrequency.Enabled = inputFormatEnable.Checked;
}

private void OnRouteListClicked(object sender, EventArgs e)
{
OnRouteListSelectedIndexChanged(sender, e);
}

private void OnClearButtonClick(object sender, EventArgs e)
{
foreach (var pipe in _wavePipeManager.Pipes.ToList())
_wavePipeManager.Stop(pipe.Configuration);
UpdateRoutes();
}
}
}
9 changes: 9 additions & 0 deletions Playthrough2/Playthrough2.UI/ConfigurationForm.resx
Expand Up @@ -117,4 +117,13 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>123, 17</value>
</metadata>
<metadata name="notifyIcon.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>123, 17</value>
</metadata>
</root>
Binary file added Playthrough2/Playthrough2.UI/Icons/Logo16.bmp
Binary file not shown.
7 changes: 7 additions & 0 deletions Playthrough2/Playthrough2.UI/Playthrough2.UI.csproj
Expand Up @@ -66,14 +66,17 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ApplicationNotifyIcon.cs" />
<Compile Include="ConfigurationForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="ConfigurationForm.Designer.cs">
<DependentUpon>ConfigurationForm.cs</DependentUpon>
</Compile>
<Compile Include="BitmapExtensions.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Win32.cs" />
<EmbeddedResource Include="ConfigurationForm.resx">
<DependentUpon>ConfigurationForm.cs</DependentUpon>
</EmbeddedResource>
Expand All @@ -85,6 +88,7 @@
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
Expand All @@ -103,6 +107,9 @@
<Name>Playthrough2</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="Icons\Logo16.bmp" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
24 changes: 18 additions & 6 deletions Playthrough2/Playthrough2.UI/Program.cs
@@ -1,19 +1,31 @@
using System;
using System.Threading;
using System.Windows.Forms;

namespace Playthrough2.UI
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
private static readonly Mutex ApplicationMutex = new Mutex(true, "{15057894-1411-41ae-8323-7224fd4fc9ca}");

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ConfigurationForm());
if (ApplicationMutex.WaitOne(TimeSpan.Zero, true))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ConfigurationForm());
ApplicationMutex.ReleaseMutex();
}
else
{
Win32.PostMessage(
(IntPtr)Win32.HWND_BROADCAST,
Win32.WM_SHOWME,
IntPtr.Zero,
IntPtr.Zero);
}
}
}
}

0 comments on commit 8484dc9

Please sign in to comment.