void PlaySound(string link, int WaveOutDeviceId)
 {
     using (var mf = new MediaFoundationReader(link))
         using (var wo = new WaveOutEvent())
         {
             wo.DeviceNumber           = WaveOutDeviceId;
             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);
             }
         }
 }
        private void btnDownload_Click(object sender, EventArgs e)
        {
            string         soundLink = GetGUISoundLink();
            SaveFileDialog dialog    = new SaveFileDialog();

            dialog.Filter   = "MP3 File (*.mp3)|*.mp3";
            dialog.FileName = "tts.mp3";
            var s = dialog.ShowDialog();

            if (s == DialogResult.OK)
            {
                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);
                }
            }
        }
Beispiel #3
0
        private void btnVoicePlay_Click(object sender, EventArgs e)
        {
            // From https://github.com/naudio/NAudio/blob/master/Docs/SmbPitchShiftingSampleProvider.md. Pitch changing outside Unity is weird.
            string inPath      = DataDirectory.Path + @"\BaseVoices\" + cmbVoiceType.Text + ".wav";
            double semitone    = Math.Pow(2, 1.0 / 12);
            double upOneTone   = semitone * semitone;
            double downOneTone = 1.0 / upOneTone;
            double targetPitch = ((double)nudPitch.Value + (-0.1 + RNG.NextDouble() / 5)) * semitone;

            using (var reader = new MediaFoundationReader(inPath))
            {
                var pitch = new SmbPitchShiftingSampleProvider(reader.ToSampleProvider());
                using (var device = new WaveOutEvent())
                {
                    pitch.PitchFactor = (float)targetPitch;
                    device.Init(pitch.Take(TimeSpan.FromSeconds(5)));
                    //device.Init(new AudioFileReader(inPath));
                    device.Play();
                    System.Threading.Thread.Sleep(400);
                }
            }
        }
Beispiel #4
0
 public Task <float[]> LoadPeaksAsync()
 {
     return(Task.Run(() =>
     {
         var peaks = new List <float>();
         using (var reader = new MediaFoundationReader(currentEpisode.AudioFile))
         {
             var sampleProvider = reader.ToSampleProvider();
             var sampleBuffer = new float[reader.WaveFormat.SampleRate * reader.WaveFormat.Channels];
             int read;
             do
             {
                 read = sampleProvider.Read(sampleBuffer, 0, sampleBuffer.Length);
                 if (read > 0)
                 {
                     var max = sampleBuffer.Take(read).Select(Math.Abs).Max();
                     peaks.Add(max);
                 }
             } while (read > 0);
             return peaks.ToArray();
         }
     }));
 }
Beispiel #5
0
        public BPMDetector(string audioFile, int start = 0, int length = 0)
        {
            // Load the file
            using (MediaFoundationReader reader = new MediaFoundationReader(audioFile))
            {
                // Originally the sample rate was constant (44100), and the number of channels was 2.
                // Let's just in case take them from file's properties
                sampleRate = reader.WaveFormat.SampleRate;
                channels   = reader.WaveFormat.Channels;

                int bytesPerSample = reader.WaveFormat.BitsPerSample / 8;
                if (bytesPerSample == 0)
                {
                    bytesPerSample = 2; // assume 16 bit
                }

                int sampleCount = (int)reader.Length / bytesPerSample;

                // Read the wave data

                start  *= channels * sampleRate;
                length *= channels * sampleRate;
                if (start >= sampleCount)
                {
                    groups = new BPMGroup[0];
                    return;
                }
                if (length == 0 || start + length >= sampleCount)
                {
                    length = sampleCount - start;
                }

                length = (int)(length / channels) * channels;

                ISampleProvider sampleReader = reader.ToSampleProvider();
                float[]         samples      = new float[length];
                sampleReader.Read(samples, start, length);

                // Beats, or kicks, generally occur around the 100 to 150 hz range.
                // Below this is often the bassline.  So let's focus just on that.

                for (int ch = 0; ch < channels; ++ch)
                {
                    // First a lowpass to remove most of the song.

                    BiQuadFilter lowpass = BiQuadFilter.LowPassFilter(sampleRate, 150.0F, 1.0F);

                    // Now a highpass to remove the bassline.

                    BiQuadFilter highpass = BiQuadFilter.HighPassFilter(sampleRate, 100.0F, 1.0F);

                    for (int i = ch; i < length; i += channels)
                    {
                        samples[i] = highpass.Transform(lowpass.Transform(samples[i]));
                    }
                }

                Peak[] peaks = getPeaks(samples);

                BPMGroup[] allGroups = getIntervals(peaks);

                Array.Sort(allGroups, (x, y) => y.Count.CompareTo(x.Count));

                if (allGroups.Length > 5)
                {
                    Array.Resize(ref allGroups, 5);
                }

                this.groups = allGroups;
            }
        }
Beispiel #6
0
    static public void InitialiseFileStream(String inPath)
    {
        MediaFoundationReader reader = new MediaFoundationReader(inPath);

        input = new SmbPitchShiftingSampleProvider(reader.ToSampleProvider());
    }
Beispiel #7
0
 private void SaveSong(Song songToSave, bool skipValidation = false)
 {
     try {
         Cursor.Current = Cursors.WaitCursor;
         bool reload = false;
         if (!skipValidation)
         {
             ValidateSong();
         }
         //might need to generate the lead in time here and add the lead in time to every note...
         if (!songToSave.LeadInTimeGenerated)
         {
             audioFile.Position = 0; //reset position else we'll just offset everything to where it left playing
             var offset = new OffsetSampleProvider(audioFile.ToSampleProvider());
             if (songToSave.LastLeadInTime != -1)
             {
                 var lastLeadInMS = songToSave.LastLeadInTime * 1000;
                 //need to remove the delay then readdd.
                 offset.SkipOver = TimeSpan.FromSeconds(songToSave.LastLeadInTime);
                 //audioFile.Position = CurrentSong.LastLeadInTime * 1000; //this skips the file forward by the amount of time in the last delay.
                 foreach (var beat in songToSave.EasyBeats)
                 {
                     beat.HitTime -= lastLeadInMS;
                 }
                 foreach (var beat in songToSave.AdvancedBeats)
                 {
                     beat.HitTime -= lastLeadInMS;
                 }
                 foreach (var beat in songToSave.ExpertBeats)
                 {
                     beat.HitTime -= lastLeadInMS;
                 }
                 if (songToSave.PreviewStart > lastLeadInMS)
                 {
                     songToSave.PreviewStart -= lastLeadInMS;
                 }
                 if (songToSave.PreviewEnd > lastLeadInMS)
                 {
                     songToSave.PreviewEnd -= lastLeadInMS;
                 }
             }
             offset.DelayBy = TimeSpan.FromSeconds(songToSave.LeadInTime);
             var tempFileName = Path.Combine(SongFolder, Path.GetFileNameWithoutExtension(songToSave.FileName) + "extend" + Path.GetExtension(songToSave.FileName));
             WaveFileWriter.CreateWaveFile(tempFileName, offset.ToWaveProvider());
             File.Delete(Path.Combine(SongFolder, songToSave.FileName));
             File.Move(tempFileName, Path.Combine(SongFolder, songToSave.FileName));
             var leadInMS = songToSave.LeadInTime * 1000;
             foreach (var beat in songToSave.EasyBeats)
             {
                 beat.HitTime += leadInMS;
             }
             foreach (var beat in songToSave.AdvancedBeats)
             {
                 beat.HitTime += leadInMS;
             }
             foreach (var beat in songToSave.ExpertBeats)
             {
                 beat.HitTime += leadInMS;
             }
             if (songToSave.PreviewStart > 0)
             {
                 songToSave.PreviewStart += leadInMS;
             }
             if (songToSave.PreviewEnd > 0)
             {
                 songToSave.PreviewEnd += leadInMS;
             }
             songToSave.LeadInTimeGenerated = true;
             reload = true; //need to reload to account for the new offset.
         }
         if (!String.IsNullOrWhiteSpace(songToSave.ImageData))
         {
             File.WriteAllBytes(Path.Combine(SongFolder, songToSave.ImageFileName), Convert.FromBase64String(songToSave.ImageData));
             songToSave.ImageData = String.Empty;
         }
         var json = JsonConvert.SerializeObject(songToSave);
         File.WriteAllText(Path.Combine(SongFolder, $"{Path.GetFileNameWithoutExtension(SongFile)}.js"), json);
         File.Delete(SongFile);
         ZipFile.CreateFromDirectory(SongFolder, SongFile);
         if (reload)
         {
             LoadSong(SongFile);
         }
     } catch (SaveValidationException e) {
         MessageBox.Show("Your song has not been saved due to an error: " + e.Message, "Save validation error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     } finally {
         Cursor.Current = Cursors.Default;
     }
 }
Beispiel #8
0
 private void toolStripMenuItem2_Click(object sender, EventArgs e)
 {
     try
     {
         outputDevice.Stop();
         SmbPitchShiftingSampleProvider provider = new SmbPitchShiftingSampleProvider(foundationReader.ToSampleProvider());
         provider.PitchFactor = 0.5f;
         if (provider.PitchFactor == 0)
         {
             provider.PitchFactor = 0.5f;
         }
         outputDevice.Init(provider);
         outputDevice.Play();
     }
     catch { }
 }
        private void btnDownload_Click(object sender, EventArgs e)
        {
            string soundLink = GetGUISoundLink();

            if (soundLink == "")
            {
                return;
            }
            SaveFileDialog dialog = new SaveFileDialog();

            if (radio_mp3.Checked)
            {
                dialog.Filter   = "MP3 File (*.mp3)|*.mp3";
                dialog.FileName = "tts.mp3";
            }
            else
            {
                dialog.Filter   = "WAV File (*.wav)|*.wav";
                dialog.FileName = "tts.wav";
            }

            var s = dialog.ShowDialog();

            if (s == DialogResult.OK)
            {
                if (!Program.bOldWindows && !(radio_mp3.Checked && Math.Abs(Pitch - 1f) <= 0.01f))
                {
                    using (var mf = new MediaFoundationReader(soundLink))
                    {
                        PitchProvider             = new SmbPitchShiftingSampleProvider(mf.ToSampleProvider().ToMono());
                        PitchProvider.PitchFactor = Pitch;
                        if (radio_mp3.Checked)
                        {
                            MediaFoundationEncoder.EncodeToMp3(PitchProvider.ToWaveProvider(), dialog.FileName, 48000);
                        }
                        else
                        {
                            WaveFileWriter.CreateWaveFile(dialog.FileName, PitchProvider.ToWaveProvider());
                        }

                        MessageBox.Show(downloaded);
                    }
                }
                else
                {
                    using (var wc = new WebClient())
                    {
                        wc.DownloadFile(soundLink, dialog.FileName);
                        MessageBox.Show(downloaded);
                    }
                }
            }
        }