From 6bce57d731d1063d91c5fd596fbf26d00cbe87b9 Mon Sep 17 00:00:00 2001 From: Weespin Date: Sun, 8 Nov 2020 15:44:53 +0200 Subject: [PATCH] Added a simple pitch shifter --- AcapellaDownloader/AcapellaDownloader.csproj | 4 +- AcapellaDownloader/Form1.Designer.cs | 40 +++++++++++++++++++ AcapellaDownloader/Form1.cs | 42 ++++++++++++++++---- AcapellaDownloader/packages.config | 2 +- 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/AcapellaDownloader/AcapellaDownloader.csproj b/AcapellaDownloader/AcapellaDownloader.csproj index d69cc26..e17bc57 100644 --- a/AcapellaDownloader/AcapellaDownloader.csproj +++ b/AcapellaDownloader/AcapellaDownloader.csproj @@ -38,8 +38,8 @@ icon.ico - - ..\packages\NAudio.1.8.4\lib\net35\NAudio.dll + + ..\packages\NAudio.1.10.0\lib\net35\NAudio.dll ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll diff --git a/AcapellaDownloader/Form1.Designer.cs b/AcapellaDownloader/Form1.Designer.cs index 6325aac..866f4ca 100644 --- a/AcapellaDownloader/Form1.Designer.cs +++ b/AcapellaDownloader/Form1.Designer.cs @@ -37,6 +37,10 @@ private void InitializeComponent() this.lbl_currentVoice = new System.Windows.Forms.Label(); this.slVolume = new NAudio.Gui.VolumeSlider(); this.lbl_Volume = new System.Windows.Forms.Label(); + this.tbPitch = new System.Windows.Forms.TrackBar(); + this.lbl_pitchValue = new System.Windows.Forms.Label(); + this.lbl_pitch = new System.Windows.Forms.Label(); + ((System.ComponentModel.ISupportInitialize)(this.tbPitch)).BeginInit(); this.SuspendLayout(); // // txtTextIn @@ -109,11 +113,43 @@ private void InitializeComponent() this.lbl_Volume.TabIndex = 8; this.lbl_Volume.Text = "Volume:"; // + // tbPitch + // + this.tbPitch.Location = new System.Drawing.Point(422, 234); + this.tbPitch.Maximum = 21; + this.tbPitch.Minimum = 1; + this.tbPitch.Name = "tbPitch"; + this.tbPitch.Size = new System.Drawing.Size(156, 45); + this.tbPitch.TabIndex = 9; + this.tbPitch.Value = 11; + this.tbPitch.Scroll += new System.EventHandler(this.tbPitch_Scroll); + // + // lbl_pitchValue + // + this.lbl_pitchValue.AutoSize = true; + this.lbl_pitchValue.Location = new System.Drawing.Point(501, 269); + this.lbl_pitchValue.Name = "lbl_pitchValue"; + this.lbl_pitchValue.Size = new System.Drawing.Size(13, 13); + this.lbl_pitchValue.TabIndex = 10; + this.lbl_pitchValue.Text = "1"; + // + // lbl_pitch + // + this.lbl_pitch.AutoSize = true; + this.lbl_pitch.Location = new System.Drawing.Point(464, 269); + this.lbl_pitch.Name = "lbl_pitch"; + this.lbl_pitch.Size = new System.Drawing.Size(34, 13); + this.lbl_pitch.TabIndex = 11; + this.lbl_pitch.Text = "Pitch:"; + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(756, 291); + this.Controls.Add(this.lbl_pitch); + this.Controls.Add(this.lbl_pitchValue); + this.Controls.Add(this.tbPitch); this.Controls.Add(this.lbl_Volume); this.Controls.Add(this.slVolume); this.Controls.Add(this.lbl_currentVoice); @@ -126,6 +162,7 @@ private void InitializeComponent() this.Name = "Form1"; this.Text = "AcapellaDownloader"; this.Load += new System.EventHandler(this.Form1_Load); + ((System.ComponentModel.ISupportInitialize)(this.tbPitch)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -141,6 +178,9 @@ private void InitializeComponent() private System.Windows.Forms.Label lbl_currentVoice; private NAudio.Gui.VolumeSlider slVolume; private System.Windows.Forms.Label lbl_Volume; + private System.Windows.Forms.TrackBar tbPitch; + private System.Windows.Forms.Label lbl_pitchValue; + private System.Windows.Forms.Label lbl_pitch; } } diff --git a/AcapellaDownloader/Form1.cs b/AcapellaDownloader/Form1.cs index 945ba24..505c8cf 100644 --- a/AcapellaDownloader/Form1.cs +++ b/AcapellaDownloader/Form1.cs @@ -7,19 +7,22 @@ using System.Threading.Tasks; using System.Windows.Forms; using NAudio.Wave; +using NAudio.Wave.SampleProviders; namespace AcapellaDownloader { public partial class Form1 : Form { - private float VoiceVolume = 1; - private const string _noText = "You did not enter the text"; + private float VoiceVolume = 1f; + private float Pitch = 1f; + private const string _noText = "You did not enter the text"; private const string _noVoice = "Please select a voice"; public const string downloadError = "A download error has occurred"; public const string downloaded = "Done!"; + SmbPitchShiftingSampleProvider PitchProvider; public Form1() { - InitializeComponent(); + InitializeComponent(); } private void btnDownload_Click(object sender, EventArgs e) { @@ -30,10 +33,12 @@ private void btnDownload_Click(object sender, EventArgs e) var s = dialog.ShowDialog(); if (s == DialogResult.OK) { - using (var web = new WebClient()) - { - web.DownloadFile(soundLink, dialog.FileName); - MessageBox.Show(downloaded); + using (var mf = new MediaFoundationReader(soundLink)) + { + PitchProvider = new SmbPitchShiftingSampleProvider(mf.ToSampleProvider().ToMono()); + PitchProvider.PitchFactor = Pitch; + MediaFoundationEncoder.EncodeToMp3(PitchProvider.ToWaveProvider(), dialog.FileName, 48000); + MessageBox.Show(downloaded); } } } @@ -99,11 +104,14 @@ void PlaySound(string link, int WaveOutDeviceId) using (var wo = new WaveOutEvent()) { wo.DeviceNumber = WaveOutDeviceId; - wo.Init(mf); + PitchProvider = new SmbPitchShiftingSampleProvider(mf.ToSampleProvider().ToMono()); + PitchProvider.PitchFactor = Pitch; + wo.Init(PitchProvider); wo.Volume = VoiceVolume; wo.Play(); while (wo.PlaybackState == PlaybackState.Playing) { + PitchProvider.PitchFactor = Pitch; wo.Volume = VoiceVolume; Thread.Sleep(500); } @@ -144,5 +152,23 @@ private void slVolume_VolumeChanged(object sender, EventArgs e) VoiceVolume = slVolume.Volume; } + private void tbPitch_Scroll(object sender, EventArgs e) + { + if (tbPitch.Value > 11) + { + Pitch =((tbPitch.Value - 1) / 10f); + } + else if (tbPitch.Value < 11) + { + Pitch = (((tbPitch.Value - 1) / 10f * 0.5f) + 0.5f); + } + else + { + Pitch = 1f; + } + + lbl_pitchValue.Text = Pitch.ToString(); + + } } } diff --git a/AcapellaDownloader/packages.config b/AcapellaDownloader/packages.config index bda3e76..92b82e0 100644 --- a/AcapellaDownloader/packages.config +++ b/AcapellaDownloader/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file