This commit is contained in:
faint 2024-04-23 11:53:53 +02:00 committed by GitHub
commit 05ec6b1c29
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 231 additions and 4 deletions

View File

@ -0,0 +1,92 @@
using System;
using System.IO;
namespace ShareX.HelpersLib.Audio
{
// The fields and their sizes were created according to WAV specification
// See: http://soundfile.sapp.org/doc/WaveFormat/
public class Wav
{
private const int Riff = 1179011410;
public readonly int Size;
private const int Format = 1163280727;
private const int FormatChunkId = 544501094;
public readonly int FormatSize;
public readonly short AudioFormat;
public readonly short Channels;
public readonly int SampleRate;
public readonly int ByteRate;
public readonly short BlockAlign;
public readonly short BitsPerSample;
private const int DataChunkId = 1635017060;
public readonly int DataSize;
public readonly byte[] SoundData;
public Wav(Stream stream)
{
var reader = new BinaryReader(stream);
if (reader.ReadInt32() != Riff)
throw new Exception("Invalid WAV file: wrong RIFF chunk ID");
Size = reader.ReadInt32();
if (reader.ReadInt32() != Format)
throw new Exception("Invalid WAV file: wrong RIFF chunk format");
if (reader.ReadInt32() != FormatChunkId)
throw new Exception("Invalid WAV file: wrong fmt subchunk ID");
FormatSize = reader.ReadInt32();
AudioFormat = reader.ReadInt16();
Channels = reader.ReadInt16();
SampleRate = reader.ReadInt32();
ByteRate = reader.ReadInt32();
BlockAlign = reader.ReadInt16();
BitsPerSample = reader.ReadInt16();
if (reader.ReadInt32() != DataChunkId)
throw new Exception("Invalid WAV file: wrong data subchunk ID");
DataSize = reader.ReadInt32();
SoundData = reader.ReadBytes(DataSize);
}
public Stream ToStream()
{
var stream = new MemoryStream();
var writer = new BinaryWriter(stream);
writer.Write(Riff);
writer.Write(Size);
writer.Write(Format);
writer.Write(FormatChunkId);
writer.Write(FormatSize);
writer.Write(AudioFormat);
writer.Write(Channels);
writer.Write(SampleRate);
writer.Write(ByteRate);
writer.Write(BlockAlign);
writer.Write(BitsPerSample);
writer.Write(DataChunkId);
writer.Write(DataSize);
writer.Write(SoundData);
stream.Position = 0;
return stream;
}
public Wav ChangeVolume(float factor)
{
for (var i = 0; i < DataSize; i += BitsPerSample/8)
{
var int16 = BitConverter.ToInt16(SoundData, i);
int16 = (short)Math.Round(int16 * factor);
BitConverter.GetBytes(int16).CopyTo(SoundData, i);
}
return this;
}
}
}

View File

@ -48,6 +48,7 @@ using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
using System.Xml;
using ShareX.HelpersLib.Audio;
namespace ShareX.HelpersLib
{
@ -410,6 +411,21 @@ namespace ShareX.HelpersLib
}
}
public static void PlaySoundAsync(Stream stream, float factor)
{
if (stream != null)
{
Task.Run(() =>
{
using (stream)
using (SoundPlayer soundPlayer = new SoundPlayer(new Wav(stream).ChangeVolume(factor).ToStream()))
{
soundPlayer.PlaySync();
}
});
}
}
public static void PlaySoundAsync(string filePath)
{
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
@ -424,6 +440,21 @@ namespace ShareX.HelpersLib
}
}
public static void PlaySoundAsync(string filePath, float factor)
{
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
Task.Run(() =>
{
using (StreamReader reader = new StreamReader(filePath))
using (SoundPlayer soundPlayer = new SoundPlayer(new Wav(reader.BaseStream).ChangeVolume(factor).ToStream()))
{
soundPlayer.PlaySync();
}
});
}
}
public static bool WaitWhile(Func<bool> check, int interval, int timeout = -1)
{
Stopwatch timer = Stopwatch.StartNew();

View File

@ -97,6 +97,8 @@
this.cbDisableNotifications = new System.Windows.Forms.CheckBox();
this.cbPlaySoundAfterCapture = new System.Windows.Forms.CheckBox();
this.cbPlaySoundAfterUpload = new System.Windows.Forms.CheckBox();
this.nudSoundAfterUploadVolume = new System.Windows.Forms.NumericUpDown();
this.lblSoundAfterUploadVolume = new System.Windows.Forms.Label();
this.tpImage = new System.Windows.Forms.TabPage();
this.tcImage = new System.Windows.Forms.TabControl();
this.tpQuality = new System.Windows.Forms.TabPage();
@ -314,6 +316,7 @@
((System.ComponentModel.ISupportInitialize)(this.nudToastWindowSizeWidth)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nudToastWindowFadeDuration)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nudToastWindowDuration)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nudSoundAfterUploadVolume)).BeginInit();
this.tpImage.SuspendLayout();
this.tcImage.SuspendLayout();
this.tpQuality.SuspendLayout();
@ -627,6 +630,8 @@
this.tpNotifications.Controls.Add(this.cbDisableNotifications);
this.tpNotifications.Controls.Add(this.cbPlaySoundAfterCapture);
this.tpNotifications.Controls.Add(this.cbPlaySoundAfterUpload);
this.tpNotifications.Controls.Add(this.nudSoundAfterUploadVolume);
this.tpNotifications.Controls.Add(this.lblSoundAfterUploadVolume);
resources.ApplyResources(this.tpNotifications, "tpNotifications");
this.tpNotifications.Name = "tpNotifications";
this.tpNotifications.UseVisualStyleBackColor = true;
@ -905,7 +910,20 @@
this.cbPlaySoundAfterUpload.Name = "cbPlaySoundAfterUpload";
this.cbPlaySoundAfterUpload.UseVisualStyleBackColor = true;
this.cbPlaySoundAfterUpload.CheckedChanged += new System.EventHandler(this.cbPlaySoundAfterUpload_CheckedChanged);
//
//
// nudSoundAfterUploadVolume
//
resources.ApplyResources(this.nudSoundAfterUploadVolume, "nudSoundAfterUploadVolume");
this.nudSoundAfterUploadVolume.Maximum = new decimal(new int[] { 200, 0, 0, 0 });
this.nudSoundAfterUploadVolume.Name = "nudSoundAfterUploadVolume";
this.nudSoundAfterUploadVolume.Value = new decimal(new int[] { 100, 0, 0, 0 });
this.nudSoundAfterUploadVolume.ValueChanged += new System.EventHandler(this.nudSoundAfterUploadVolume_ValueChanged);
//
// lblSoundAfterUploadVolume
//
resources.ApplyResources(this.lblSoundAfterUploadVolume, "lblSoundAfterUploadVolume");
this.lblSoundAfterUploadVolume.Name = "lblSoundAfterUploadVolume";
//
// tpImage
//
this.tpImage.BackColor = System.Drawing.SystemColors.Window;
@ -2652,6 +2670,7 @@
((System.ComponentModel.ISupportInitialize)(this.nudToastWindowSizeWidth)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nudToastWindowFadeDuration)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nudToastWindowDuration)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nudSoundAfterUploadVolume)).EndInit();
this.tpImage.ResumeLayout(false);
this.tcImage.ResumeLayout(false);
this.tpQuality.ResumeLayout(false);
@ -2811,6 +2830,8 @@
private System.Windows.Forms.TabPage tpGeneral;
private System.Windows.Forms.CheckBox cbPlaySoundAfterCapture;
private System.Windows.Forms.CheckBox cbPlaySoundAfterUpload;
private System.Windows.Forms.NumericUpDown nudSoundAfterUploadVolume;
private System.Windows.Forms.Label lblSoundAfterUploadVolume;
private System.Windows.Forms.CheckBox cbOverrideGeneralSettings;
private System.Windows.Forms.TabPage tpTools;
private System.Windows.Forms.NumericUpDown nudScreenRecorderStartDelay;

View File

@ -190,7 +190,10 @@ namespace ShareX
#region Notifications
cbPlaySoundAfterCapture.Checked = TaskSettings.GeneralSettings.PlaySoundAfterCapture;
cbPlaySoundAfterUpload.Checked = TaskSettings.GeneralSettings.PlaySoundAfterUpload;
nudSoundAfterUploadVolume.Enabled = cbPlaySoundAfterUpload.Checked = TaskSettings.GeneralSettings.PlaySoundAfterUpload;
nudSoundAfterUploadVolume.SetValue(TaskSettings.GeneralSettings.SoundAfterUploadVolume);
nudSoundAfterUploadVolume.Maximum = 200;
nudSoundAfterUploadVolume.Minimum = 0;
cbShowToastNotificationAfterTaskCompleted.Checked = TaskSettings.GeneralSettings.ShowToastNotificationAfterTaskCompleted;
gbToastWindow.Enabled = TaskSettings.GeneralSettings.ShowToastNotificationAfterTaskCompleted;
nudToastWindowDuration.SetValue((decimal)TaskSettings.GeneralSettings.ToastWindowDuration);
@ -825,6 +828,12 @@ namespace ShareX
private void cbPlaySoundAfterUpload_CheckedChanged(object sender, EventArgs e)
{
TaskSettings.GeneralSettings.PlaySoundAfterUpload = cbPlaySoundAfterUpload.Checked;
nudSoundAfterUploadVolume.Enabled = cbPlaySoundAfterUpload.Checked;
}
private void nudSoundAfterUploadVolume_ValueChanged(object sender, EventArgs e)
{
TaskSettings.GeneralSettings.SoundAfterUploadVolume = (int)nudSoundAfterUploadVolume.Value;
}
private void cbShowToastNotificationAfterTaskCompleted_CheckedChanged(object sender, EventArgs e)

View File

@ -1671,6 +1671,57 @@
<data name="&gt;&gt;cbPlaySoundAfterUpload.ZOrder" xml:space="preserve">
<value>14</value>
</data>
<data name="nudSoundAfterUploadVolume.Location" type="System.Drawing.Point, System.Drawing">
<value>248, 29</value>
</data>
<data name="nudSoundAfterUploadVolume.Size" type="System.Drawing.Size, System.Drawing">
<value>54, 20</value>
</data>
<data name="nudSoundAfterUploadVolume.TabIndex" type="System.Int32, mscorlib">
<value>15</value>
</data>
<data name="&gt;&gt;nudSoundAfterUploadVolume.Name" xml:space="preserve">
<value>nudSoundAfterUploadVolume</value>
</data>
<data name="&gt;&gt;nudSoundAfterUploadVolume.Type" xml:space="preserve">
<value>System.Windows.Forms.NumericUpDown, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;nudSoundAfterUploadVolume.Parent" xml:space="preserve">
<value>tpNotifications</value>
</data>
<data name="&gt;&gt;nudSoundAfterUploadVolume.ZOrder" xml:space="preserve">
<value>15</value>
</data>
<data name="lblSoundAfterUploadVolume.AutoSize" type="System.Boolean, mscorlib">
<value>True</value>
</data>
<data name="lblSoundAfterUploadVolume.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms">
<value>NoControl</value>
</data>
<data name="lblSoundAfterUploadVolume.Location" type="System.Drawing.Point, System.Drawing">
<value>308, 33</value>
</data>
<data name="lblSoundAfterUploadVolume.Size" type="System.Drawing.Size, System.Drawing">
<value>15, 13</value>
</data>
<data name="lblSoundAfterUploadVolume.TabIndex" type="System.Int32, mscorlib">
<value>19</value>
</data>
<data name="lblSoundAfterUploadVolume.Text" xml:space="preserve">
<value>%</value>
</data>
<data name="&gt;&gt;lblSoundAfterUploadVolume.Name" xml:space="preserve">
<value>lblSoundAfterUploadVolume</value>
</data>
<data name="&gt;&gt;lblSoundAfterUploadVolume.Type" xml:space="preserve">
<value>System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;lblSoundAfterUploadVolume.Parent" xml:space="preserve">
<value>tpNotifications</value>
</data>
<data name="&gt;&gt;lblSoundAfterUploadVolume.ZOrder" xml:space="preserve">
<value>16</value>
</data>
<data name="tpNotifications.Location" type="System.Drawing.Point, System.Drawing">
<value>4, 22</value>
</data>

View File

@ -1550,13 +1550,35 @@ namespace ShareX
{
if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings();
var volume = taskSettings.GeneralSettings.SoundAfterUploadVolume;
if (taskSettings.GeneralSettings.UseCustomTaskCompletedSound && !string.IsNullOrEmpty(taskSettings.GeneralSettings.CustomTaskCompletedSoundPath))
{
Helpers.PlaySoundAsync(taskSettings.GeneralSettings.CustomTaskCompletedSoundPath);
switch (volume)
{
case var i when volume <= 0:
return;
case 100:
Helpers.PlaySoundAsync(taskSettings.GeneralSettings.CustomTaskCompletedSoundPath);
break;
default:
Helpers.PlaySoundAsync(taskSettings.GeneralSettings.CustomTaskCompletedSoundPath, volume / 100f);
break;
}
}
else
{
Helpers.PlaySoundAsync(Resources.TaskCompletedSound);
switch (volume)
{
case var i when volume <= 0:
return;
case 100:
Helpers.PlaySoundAsync(Resources.TaskCompletedSound);
break;
default:
Helpers.PlaySoundAsync(Resources.TaskCompletedSound, volume / 100f);
break;
}
}
}

View File

@ -308,6 +308,7 @@ namespace ShareX
public bool PlaySoundAfterCapture = true;
public bool PlaySoundAfterUpload = true;
public int SoundAfterUploadVolume = 100;
public bool ShowToastNotificationAfterTaskCompleted = true;
public float ToastWindowDuration = 3f;
public float ToastWindowFadeDuration = 1f;