Beispiel #1
0
 public AudioFile(string filepath)
 {
     _reader     = new AudioFileReader(filepath);
     _sampleRate = _reader.ToWaveProvider().WaveFormat.SampleRate;
     _channels   = _reader.ToWaveProvider().WaveFormat.Channels;
     float[] data = new float[_reader.Length];
     _reader.Read(data, 0, data.Length);
     if (_channels == 1)
     {
         _channel1 = data;
     }
     else
     {
         _channel1 = new float[data.Length / 2];
         _channel2 = new float[data.Length / 2];
         var chidx = 0;
         for (int i = 0; i < data.Length; i++)
         {
             if (i % 2 == 0)
             {
                 _channel1[chidx] = data[i];
             }
             else
             {
                 _channel2[chidx] = data[i];
                 chidx++;
             }
         }
     }
     _length = _channel1.Length;
 }
Beispiel #2
0
        public CachedSound(string audioFileName)
        {
            using (var audioFileReader = new AudioFileReader(audioFileName))
            {
                // TODO: could add resampling in here if required

                MediaFoundationResampler resampler = new MediaFoundationResampler(audioFileReader.ToWaveProvider(), 44100);
                WaveFormat = resampler.WaveFormat;


                var wholeFile  = new List <float>((int)(audioFileReader.Length / 4));
                var readBuffer = new byte[resampler.WaveFormat.SampleRate * resampler.WaveFormat.Channels];
                int samplesRead;
                while ((samplesRead = resampler.Read(readBuffer, 0, readBuffer.Length)) > 0)
                {
                    var tempByteArray  = readBuffer.Take(samplesRead).ToArray();
                    var tempfloatarray = new float[tempByteArray.Count() / 4];
                    Buffer.BlockCopy(tempByteArray, 0, tempfloatarray, 0, tempByteArray.Count());

                    wholeFile.AddRange(tempfloatarray);
                }
                AudioData = wholeFile.ToArray();
            }
        }
        public static void BeginAudioPlayback(string filePath, AudioOutputDevice audioOutputDevice, float volume, double beginTime, double endTime)
        {
            try
            {
                AudioFileReader audioFileReader = new AudioFileReader(filePath);

                // Configure mixer
                MixingSampleProvider mixer = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(audioFileReader.WaveFormat.SampleRate, 2));

                // Force audio to stereo 2-channel
                MultiplexingWaveProvider waveProvider = new MultiplexingWaveProvider(new IWaveProvider[] { audioFileReader.ToWaveProvider() }, audioFileReader.WaveFormat.Channels > 1 ? 2 : 1);

                // Apply volume preferences
                VolumeSampleProvider volumeSampleProvider = new VolumeSampleProvider(waveProvider.ToSampleProvider())
                {
                    Volume = volume
                };

                // Force sample rate
                ISampleProvider convertedSampleProvider = ConvertToMixerSampleRate(mixer, ConvertToMixerChannelCount(mixer, volumeSampleProvider));

                // Seek
                OffsetSampleProvider offsetSampleProvider = new OffsetSampleProvider(convertedSampleProvider);
                offsetSampleProvider.SkipOver = TimeSpan.FromSeconds(beginTime);
                offsetSampleProvider.Take     = TimeSpan.FromSeconds(endTime) - offsetSampleProvider.SkipOver;

                // Configure mixer
                mixer.AddMixerInput(offsetSampleProvider);

                audioOutputDevice.InitializeAndPlay(mixer);
                audioOutputDevice.PlaybackStopped += (sender, e) =>
                {
                    //audioFileReader.Close();
                    audioFileReader.Dispose();
                };
            }
            catch (Exception ex)
            {
                ApplicationLogger.Log(ex.Message, ex.StackTrace);
            }
        }
Beispiel #4
0
        public void mixing()
        {
            /*
             * var mixer = new WaveMixerStream32 { AutoStop = true };
             * var wav1 = new WaveFileReader(_micPath);
             * var wav2 = new WaveFileReader(_loopBackPath);
             *
             * // float levelWav1_mic = 1.0f;
             * // float levelWav2_loopBack = 1.0f;
             * var waveChan1 = new WaveChannel32(wav1);
             * // waveChan1.Volume = 2.0f;
             * if (TextBox_LevelMic.Text.Length > 0)
             * {
             *  string strLevelMic = TextBox_LevelMic.Text;
             *  if(strLevelMic != "1.0")
             *  {
             *      float fLevelMic = float.Parse(strLevelMic);
             *      waveChan1.Volume = fLevelMic;
             *  }
             *
             * }
             * mixer.AddInputStream(waveChan1);
             * var waveChan2 = new WaveChannel32(wav2);
             * // waveChan2.Volume = 0.5f;
             * if (TextBox_levelLoopBack.Text.Length > 0)
             * {
             *  string strlevelLoopBack = TextBox_levelLoopBack.Text;
             *  if (strlevelLoopBack != "1.0")
             *  {
             *      float fLevelLoopBack = float.Parse(strlevelLoopBack);
             *      waveChan2.Volume = fLevelLoopBack;
             *  }
             * }
             * mixer.AddInputStream(waveChan2);
             * _outputPath = TextBox_area2.Text;
             *
             * _fileOutputWavFullExt = System.IO.Path.Combine(_outputPath, Path.GetFileName(_micPath).Replace("_mic", ""));
             * WaveFileWriter.CreateWaveFile(_fileOutputWavFullExt, mixer);
             *
             */
            ////////////////////////////////////////////////////////////////////////////////
            const int rate = 48000;
            //const int bits = 32;
            const int channels = 2;
            //WaveFormat wave_format = new WaveFormat(rate, bits, channels);
            WaveFormat wave_format = WaveFormat.CreateIeeeFloatWaveFormat(rate, channels);
            var        wav1        = new AudioFileReader(_micPath);
            var        wav2        = new AudioFileReader(_loopBackPath);

            if (TextBox_LevelMic.Text.Length > 0)
            {
                string strLevelMic = TextBox_LevelMic.Text;
                if (strLevelMic != "1.0")
                {
                    float fLevelMic = float.Parse(strLevelMic);
                    wav1.Volume = fLevelMic;
                }
            }
            if (TextBox_levelLoopBack.Text.Length > 0)
            {
                string strlevelLoopBack = TextBox_levelLoopBack.Text;
                if (strlevelLoopBack != "1.0")
                {
                    float fLevelLoopBack = float.Parse(strlevelLoopBack);
                    wav2.Volume = fLevelLoopBack;
                }
            }
            var resampler2 = new MediaFoundationResampler(wav2, wave_format);
            var mixer      = new MixingSampleProvider(wave_format);

            //      var mixer = new MixingSampleProvider(new[] { wav1, wav2 });
            mixer.AddMixerInput(resampler2);
            mixer.AddMixerInput(wav1.ToWaveProvider());

            _outputPath = TextBox_area2.Text;


            this.Dispatcher.BeginInvoke((Action)(() =>
            {
                if (_outputPath.Length == 0)
                {
                    _outputPath = _mainWindow.TextBox_path.Text;
                    TextBox_area2.Text = _outputPath;
                }
                _fileOutputWavFullExt = System.IO.Path.Combine(_outputPath, Path.GetFileName(_micPath).Replace("_mic", ""));
                WaveFileWriter.CreateWaveFile(_fileOutputWavFullExt, mixer.ToWaveProvider());
            }));
        }