public void Initialize(ISampleProvider BaseProvider)
        {
            baseProv = BaseProvider;

            PSE.VolumeRequest         += PSE_VolumeRequest;
            PSE.ExtractedDegreeOfRisk += PSE_ExtractedDegreeOfRisk;
            PSE.Enable = true;

            PSEFadeTimer.Interval = 3000;
            PSEFadeTimer.Tick    += PSEFadeTimer_Tick;

            BufferedWaveProvider = new BufferedWaveProvider(WaveFormat);

            List <ISampleProvider> sources = new List <ISampleProvider>();

            sources.Add(baseProv);
            sources.Add(WaveExtensionMethods.ToSampleProvider(BufferedWaveProvider));

            Mixer = new NAudio.Wave.SampleProviders.MixingSampleProvider(sources);
        }
Esempio n. 2
0
        /// <summary>
        /// Mixing 2 files starting from time @secDelay
        /// </summary>
        public static void OverlapFiles(string file1, string file2, int secDelay, string outFile)
        {
            if (secDelay < 0)
            {
                var tmp = file1;
                file1    = file2;
                file2    = tmp;
                secDelay = -secDelay;
            }


            if (!File.Exists(file1))
            {
                Program.LogError("No file " + file1); return;
            }
            if (!File.Exists(file2))
            {
                Program.LogError("No file " + file2); return;
            }

            using (var reader1 = new AudioFileReader(file1))
            {
                //var resampler1 = new WdlResamplingSampleProvider(reader1, SampleRate);
                var outFormat = new WaveFormat(SampleRate, Channels);
                using (var resampler1 = new MediaFoundationResampler(reader1, outFormat))
                {
                    using (var reader2 = new AudioFileReader(file2))
                    {
                        using (var resampler2 = new MediaFoundationResampler(reader2, outFormat))
                        {
                            // standard sample rate and channels
                            var res1 = WaveExtensionMethods.ToSampleProvider(resampler1);
                            var res2 = WaveExtensionMethods.ToSampleProvider(resampler2);

                            PrintFileInfo(reader1, "Info of file: " + file1);
                            PrintFileInfo(reader2, "Info of file: " + file2);


                            var wf = WaveFormat.CreateIeeeFloatWaveFormat(SampleRate, Channels);
                            using (WaveFileWriter writer = new WaveFileWriter(outFile, wf))
                            {
                                // 1 second buffer
                                var buf1          = new float[SampleRate * Channels];
                                var buf2          = new float[SampleRate * Channels];
                                int currentSec    = 0;
                                var file2finished = false;
                                while (true)
                                {
                                    int read1 = res1.Read(buf1, 0, buf1.Length);
                                    if (read1 > 0)
                                    {
                                        if (currentSec < secDelay || file2finished)
                                        {
                                            writer.WriteSamples(buf1, 0, read1);
                                        }
                                        else
                                        {
                                            int read2 = res2.Read(buf2, 0, buf2.Length);
                                            WriteMix(read1, buf1, read2, buf2, writer);
                                        }
                                    }
                                    else
                                    {
                                        break;
                                    }
                                    currentSec++;
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        private void Start_AddTask_Btn_Click(object sender, EventArgs e)
        {
            /*
             * //start concat test
             *
             * var extList = new string[] { ".wav" };
             * var files = Directory.GetFiles("D:/share/concat", "*.wav").ToList();
             * var sampleRate = 48000;
             * var bit = 16;
             * var channel = 1;
             * var outFormat = new WaveFormat(sampleRate, bit, channel);
             * var writer = new WaveFileWriter("concat.wav", outFormat);
             * foreach (var name in files)
             * {
             *  using (WaveFormatConversionStream stream = new WaveFormatConversionStream(outFormat, new WaveFileReader(name)))
             *  {
             *      byte[] taskBuffer = new byte[stream.WaveFormat.BitsPerSample * stream.WaveFormat.Channels / 4];
             *      int read;
             *      try
             *      {
             *          while ((read = stream.Read(taskBuffer, 0, taskBuffer.Length)) > 0)
             *          {
             *              writer.Write(taskBuffer, 0, read);
             *          }
             *      }
             *      catch (Exception ex)
             *      {
             *          addText(ex.Message + "\n" + "filename: " + name, Color.Black);
             *          if (writer != null)
             *              writer.Dispose();
             *          return;
             *      }
             *  }
             * }
             * writer.Dispose();
             */

            SaveFileDialog dialog = new SaveFileDialog();

            dialog.Title                        = "Save";
            dialog.InitialDirectory             = ".\\";
            dialog.SupportMultiDottedExtensions = true;
            dialog.Filter                       = "wav files (*.wav)|*.wav|All files (*.*)|*.*";
            dialog.FileName                     = "Untitled.wav";
            dialog.OverwritePrompt              = true;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    //read the cut location
                    var location = new List <int>();
                    using (StreamReader sr = new StreamReader(Keyword_Sample_TextBox.Text.Trim()))
                    {
                        String line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            location.Add(Int32.Parse(line));
                        }
                    }

                    //read the task filenames
                    var taskFilenames = new List <String>();
                    using (StreamReader sr = new StreamReader(Task_Source_TextBox.Text.Trim()))
                    {
                        String line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            if (line != "")
                            {
                                taskFilenames.Add(line.Trim());
                            }
                        }
                    }

                    if (location.Count > taskFilenames.Count)
                    {
                        throw new ArgumentException("the number cut location must equal or less than the number of input task files");
                    }

                    using (var keywordWaveReader = new WaveFileReader(Keyword_Source_TextBox.Text.Trim()))
                    {
                        var keywordSampleProvider = WaveExtensionMethods.ToSampleProvider(keywordWaveReader);

                        //set writer
                        var sampleRate = keywordWaveReader.WaveFormat.SampleRate;
                        var bit        = keywordWaveReader.WaveFormat.BitsPerSample;
                        var channel    = 1;
                        var outFormat  = new WaveFormat(sampleRate, bit, channel);
                        var writer     = new WaveFileWriter(dialog.FileName, outFormat);

                        //start writing file
                        float[] buffer = new float[2 * keywordWaveReader.WaveFormat.Channels]; //2 samples per buffer
                        int     samplesRead;
                        int     currentLocation = 0;                                           //number of samples has been read
                        int     taskCount       = 0;
                        int     cutLocation     = location[taskCount];
                        while ((samplesRead = keywordSampleProvider.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            writer.WriteSamples(buffer, 0, samplesRead);
                            currentLocation += samplesRead;
                            //when encounter the cut location, write the task stream
                            if (currentLocation > cutLocation)
                            {
                                //convert the task stream into the same waveFormat as keyword stream's
                                var taskFilename = $@"task/{taskFilenames[taskCount]}";
                                using (WaveFormatConversionStream taskStream = new WaveFormatConversionStream(outFormat, new WaveFileReader(taskFilename)))
                                {
                                    byte[] taskBuffer = new byte[taskStream.WaveFormat.BitsPerSample * taskStream.WaveFormat.Channels / 4];
                                    int    taskBytesRead;
                                    while ((taskBytesRead = taskStream.Read(taskBuffer, 0, taskBuffer.Length)) > 0)
                                    {
                                        writer.Write(taskBuffer, 0, taskBytesRead);
                                    }
                                    //insert 5sec silence
                                    for (int sec = 0; sec < 5; sec++)
                                    {
                                        float[] silenceBuffer = Enumerable.Repeat((float)0, writer.WaveFormat.SampleRate).ToArray();
                                        writer.WriteSamples(silenceBuffer, 0, silenceBuffer.Length);
                                    }
                                }
                                taskCount++;
                                cutLocation = location[taskCount];
                            }
                        }
                        writer.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    MessageBox.Show(ex.ToString());
                }
            }
        }