From fd7fb89305355e582a5d458a5f5881ffabc390fe Mon Sep 17 00:00:00 2001 From: Roman Glebsky Date: Tue, 11 May 2021 07:04:37 +0700 Subject: [PATCH] added volume control --- SWYH/App.xaml.cs | 11 +++ SWYH/Audio/WasapiProvider.cs | 10 ++- SWYH/Properties/Settings.Designer.cs | 32 +++++++- SWYH/SWYH.csproj | 7 ++ SWYH/Windows/Volume.xaml | 20 +++++ SWYH/Windows/Volume.xaml.cs | 108 +++++++++++++++++++++++++++ 6 files changed, 185 insertions(+), 3 deletions(-) create mode 100644 SWYH/Windows/Volume.xaml create mode 100644 SWYH/Windows/Volume.xaml.cs diff --git a/SWYH/App.xaml.cs b/SWYH/App.xaml.cs index 80ce879..0e494b0 100644 --- a/SWYH/App.xaml.cs +++ b/SWYH/App.xaml.cs @@ -56,6 +56,7 @@ public partial class App : Application private AboutWindow aboutWindow = null; private HTTPLiveStreamWindow httpWindow = null; private RecordWindow recordWindow = null; + private VolumeWindow volumeWindow = null; private System.Windows.Forms.NotifyIcon notifyIcon = null; private System.Windows.Forms.ToolStripMenuItem streamToMenu = null; private System.Windows.Forms.ToolStripMenuItem searchingItem = null; @@ -121,6 +122,7 @@ private void InitializeUI() this.aboutWindow = new AboutWindow(); this.httpWindow = new HTTPLiveStreamWindow(); this.recordWindow = new RecordWindow(); + this.volumeWindow = new VolumeWindow(); this.notifyIcon = new System.Windows.Forms.NotifyIcon() { Icon = SWYH.Properties.Resources.swyh32, @@ -143,6 +145,15 @@ private void InitializeUI() this.notifyIcon.ContextMenuStrip.Items.Add("About", null, (s, e2) => this.aboutWindow.Show()); this.notifyIcon.ContextMenuStrip.Items.Add("-"); this.notifyIcon.ContextMenuStrip.Items.Add("Exit", null, (s, e2) => this.Shutdown()); + this.notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(NotifyIcon_Click); + } + + private void NotifyIcon_Click(object sender, System.Windows.Forms.MouseEventArgs e) + { + if (e.Button == System.Windows.Forms.MouseButtons.Left) + { + this.volumeWindow.ShowWithPosition(); + } } private void CheckAutomaticDeviceStreamed(StartupEventArgs startupEvent) diff --git a/SWYH/Audio/WasapiProvider.cs b/SWYH/Audio/WasapiProvider.cs index 77df113..fa8e5e6 100644 --- a/SWYH/Audio/WasapiProvider.cs +++ b/SWYH/Audio/WasapiProvider.cs @@ -39,6 +39,7 @@ internal class WasapiProvider private WasapiCapture loopbackWaveIn = null; private PipeStream recordingStream = null; + private Wave32To16Stream rawWave16b = null; private WaveStream rawConvertedStream = null; private WaveStream pcmStream = null; private LameMP3FileWriter mp3Writer = null; @@ -85,9 +86,10 @@ public WasapiProvider() this.loopbackWaveIn = new WasapiCapture(captureDevice); } this.loopbackWaveIn.DataAvailable += new EventHandler(this.loopbackWaveIn_DataAvailable); - + // Init Raw Wav (16bit) - WaveStream rawWave16b = new Wave32To16Stream(new RawSourceWaveStream(this.recordingStream, NAudio.Wave.WaveFormat.CreateIeeeFloatWaveFormat(this.loopbackWaveIn.WaveFormat.SampleRate, this.loopbackWaveIn.WaveFormat.Channels))); + this.rawWave16b = new Wave32To16Stream(new RawSourceWaveStream(this.recordingStream, NAudio.Wave.WaveFormat.CreateIeeeFloatWaveFormat(this.loopbackWaveIn.WaveFormat.SampleRate, this.loopbackWaveIn.WaveFormat.Channels))); + SetVolume(SWYH.Properties.Settings.Default.Volume, SWYH.Properties.Settings.Default.Mute); // Convert Raw Wav to PCM with audio format in settings var audioFormat = AudioSettings.GetAudioFormat(); @@ -116,6 +118,10 @@ public WasapiProvider() waveProcessorThread.Start(); } + public void SetVolume(int volume, bool muted) + { + rawWave16b.Volume = muted ? 0 : (float)volume * 2 / 100; + } public void Dispose() { diff --git a/SWYH/Properties/Settings.Designer.cs b/SWYH/Properties/Settings.Designer.cs index 476d1f2..2cf647e 100644 --- a/SWYH/Properties/Settings.Designer.cs +++ b/SWYH/Properties/Settings.Designer.cs @@ -82,7 +82,37 @@ public string AudioDevice { this["AudioDevice"] = value; } } - + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("50")] + public int Volume + { + get + { + return ((int)(this["Volume"])); + } + set + { + this["Volume"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("False")] + public bool Mute + { + get + { + return ((bool)(this["Mute"])); + } + set + { + this["Mute"] = value; + } + } + [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("True")] diff --git a/SWYH/SWYH.csproj b/SWYH/SWYH.csproj index 1ff9fc7..9f51508 100644 --- a/SWYH/SWYH.csproj +++ b/SWYH/SWYH.csproj @@ -127,6 +127,9 @@ + + Volume.xaml + Designer MSBuild:Compile @@ -151,6 +154,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + diff --git a/SWYH/Windows/Volume.xaml b/SWYH/Windows/Volume.xaml new file mode 100644 index 0000000..5c0f823 --- /dev/null +++ b/SWYH/Windows/Volume.xaml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + diff --git a/SWYH/Windows/Volume.xaml.cs b/SWYH/Windows/Volume.xaml.cs new file mode 100644 index 0000000..44f4536 --- /dev/null +++ b/SWYH/Windows/Volume.xaml.cs @@ -0,0 +1,108 @@ +/* + * Stream What Your Hear + * Assembly: SWYH + * File: VolumeWindow.xaml.cs + * Web site: http://www.streamwhatyouhear.com + * Copyright (C) 2012-2019 - Sebastien Warin and others + * + * This file is part of Stream What Your Hear. + * + * Stream What Your Hear is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Stream What Your Hear is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Stream What Your Hear. If not, see . + */ + +namespace SWYH +{ + using System; + using System.Windows; + + /// + /// Interaction logic for VolumeWindow.xaml + /// + public partial class VolumeWindow : Window + { + public VolumeWindow() + { + InitializeComponent(); + } + private void Window_Loaded(object sender, RoutedEventArgs e) + { + this.slider1.Value = SWYH.Properties.Settings.Default.Volume; + this.checkBox1.IsChecked = SWYH.Properties.Settings.Default.Mute; + } + + private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) + { + if (e.Key == System.Windows.Input.Key.Escape) + { + this.Close(); + } + } + + private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) + { + e.Cancel = true; + + int volume = (int)this.slider1.Value; + if (SWYH.Properties.Settings.Default.Volume != volume || + SWYH.Properties.Settings.Default.Mute != this.checkBox1.IsChecked) + { + SWYH.Properties.Settings.Default.Volume = volume; + SWYH.Properties.Settings.Default.Mute = (bool)this.checkBox1.IsChecked; + SWYH.Properties.Settings.Default.Save(); + } + + this.Hide(); + } + + private void Window_Deactivated(object sender, EventArgs e) + { + this.Close(); + } + + public void ShowWithPosition() + { + this.Show(); + System.Drawing.Point point = System.Windows.Forms.Control.MousePosition; + var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice; + var mouse = transform.Transform(new System.Windows.Point(point.X, point.Y)); + Left = mouse.X - ActualWidth; + Top = mouse.Y - ActualHeight; + this.Activate(); + } + + private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) + { + if (App.CurrentInstance.wasapiProvider != null) + { + App.CurrentInstance.wasapiProvider.SetVolume((int)e.NewValue, (bool)this.checkBox1.IsChecked); + } + } + + private void CheckBox_Checked(object sender, RoutedEventArgs e) + { + if (App.CurrentInstance.wasapiProvider != null) + { + App.CurrentInstance.wasapiProvider.SetVolume((int)this.slider1.Value, true); + } + } + + private void CheckBox_Unchecked(object sender, RoutedEventArgs e) + { + if (App.CurrentInstance.wasapiProvider != null) + { + App.CurrentInstance.wasapiProvider.SetVolume((int)this.slider1.Value, false); + } + } + } +}