Пример #1
0
        /// <summary>
        /// Converts sound file to List of arrays. One array - data for 1 second
        /// </summary>
        /// <param name="v"></param>
        internal static List <float[]> GetArraysFromFile(string inFile)
        {
            var res = new List <float[]>();

            if (!File.Exists(inFile))
            {
                Program.LogError("No file " + inFile); return(null);
            }
            using (var reader = new AudioFileReader(inFile))
            {
                Program.LogInfo("TotalTime:" + reader.TotalTime);
                Program.LogInfo("Length:" + reader.Length);
                Program.LogInfo("Volume:" + reader.Volume);
                var waveFormat = reader.WaveFormat;
                Program.LogInfo("SampleRate:" + waveFormat.SampleRate);
                Program.LogInfo("AverageBytesPerSecond:" + waveFormat.AverageBytesPerSecond);
                Program.LogInfo("BitsPerSample:" + waveFormat.BitsPerSample);
                Program.LogInfo("BlockAlign:" + waveFormat.BlockAlign);
                Program.LogInfo("Channels:" + waveFormat.Channels);
                Program.LogInfo("Encoding:" + waveFormat.Encoding);

                int valuesPerSecond = (int)(reader.Length / reader.TotalTime.TotalSeconds);
                Program.LogInfo("Values per second:" + valuesPerSecond);

                var resampler = new WdlResamplingSampleProvider(reader, 44100);
                var wp        = new SampleToWaveProvider(reader);

                var rdr = wp.ToSampleProvider();


                var wf2 = rdr.WaveFormat;
                Program.LogInfo("SampleRate:" + wf2.SampleRate);
                Program.LogInfo("AverageBytesPerSecond:" + wf2.AverageBytesPerSecond);
                Program.LogInfo("BitsPerSample:" + wf2.BitsPerSample);
                Program.LogInfo("BlockAlign:" + wf2.BlockAlign);
                Program.LogInfo("Channels:" + wf2.Channels);
                Program.LogInfo("Encoding:" + wf2.Encoding);



                // 1 sec length buffer
                var buf = new float[rdr.WaveFormat.SampleRate * rdr.WaveFormat.Channels];
                while (true)
                {
                    int read = rdr.Read(buf, 0, buf.Length);
                    if (read > 0)
                    {
                        // only 1 channel
                        res.Add(buf.Where((x, i) => i % 2 == 0).ToArray());
                    }
                    else
                    {
                        break;
                    }
                }
                return(res);
            }
        }
Пример #2
0
        /// <summary>
        /// demo function fo reading and saving sound files
        /// </summary>
        /// <param name="testWavFile"></param>
        /// <param name="testOutWavFile"></param>
        public static void ReadAndWriteSound(string testWavFile, string testOutWavFile)
        {
            string testFile = testWavFile;

            if (!File.Exists(testFile))
            {
                Program.LogError("No file " + testFile); return;
            }
            using (var reader = new AudioFileReader(testFile))
            {
                Program.LogInfo("TotalTime:" + reader.TotalTime);
                Program.LogInfo("Length:" + reader.Length);
                Program.LogInfo("Volume:" + reader.Volume);
                var waveFormat = reader.WaveFormat;
                Program.LogInfo("SampleRate:" + waveFormat.SampleRate);
                Program.LogInfo("AverageBytesPerSecond:" + waveFormat.AverageBytesPerSecond);
                Program.LogInfo("BitsPerSample:" + waveFormat.BitsPerSample);
                Program.LogInfo("BlockAlign:" + waveFormat.BlockAlign);
                Program.LogInfo("Channels:" + waveFormat.Channels);
                Program.LogInfo("Encoding:" + waveFormat.Encoding);
                Program.LogInfo("Values per second:" + reader.Length / reader.TotalTime.TotalSeconds);

                var resampler = new WdlResamplingSampleProvider(reader, 44100);
                var wp        = new SampleToWaveProvider(reader);

                var rdr = wp.ToSampleProvider();


                var wf2 = rdr.WaveFormat;
                Program.LogInfo("SampleRate:" + wf2.SampleRate);
                Program.LogInfo("AverageBytesPerSecond:" + wf2.AverageBytesPerSecond);
                Program.LogInfo("BitsPerSample:" + wf2.BitsPerSample);
                Program.LogInfo("BlockAlign:" + wf2.BlockAlign);
                Program.LogInfo("Channels:" + wf2.Channels);
                Program.LogInfo("Encoding:" + wf2.Encoding);


                var wf = WaveFormat.CreateIeeeFloatWaveFormat(rdr.WaveFormat.SampleRate, rdr.WaveFormat.Channels);
                using (WaveFileWriter writer = new WaveFileWriter(testOutWavFile, wf))
                {
                    var buf = new float[rdr.WaveFormat.SampleRate];
                    while (true)
                    {
                        int read = rdr.Read(buf, 0, buf.Length);
                        if (read > 0)
                        {
                            writer.WriteSamples(buf, 0, read);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Saving part of sound file
        /// </summary>
        /// <param name="inFile"></param>
        /// <param name="outFile"></param>
        /// <param name="startSec"></param>
        /// <param name="endSec"></param>
        public static void ReadAndSaveSoundPart(string inFile, string outFile, int startSec, int endSec)
        {
            if (!File.Exists(inFile))
            {
                Program.LogError("No file " + inFile); return;
            }
            using (var reader = new AudioFileReader(inFile))
            {
                Program.LogInfo("TotalTime:" + reader.TotalTime);
                Program.LogInfo("Length:" + reader.Length);
                Program.LogInfo("Volume:" + reader.Volume);
                var waveFormat = reader.WaveFormat;
                Program.LogInfo("SampleRate:" + waveFormat.SampleRate);
                Program.LogInfo("AverageBytesPerSecond:" + waveFormat.AverageBytesPerSecond);
                Program.LogInfo("BitsPerSample:" + waveFormat.BitsPerSample);
                Program.LogInfo("BlockAlign:" + waveFormat.BlockAlign);
                Program.LogInfo("Channels:" + waveFormat.Channels);
                Program.LogInfo("Encoding:" + waveFormat.Encoding);

                int valuesPerSecond = (int)(reader.Length / reader.TotalTime.TotalSeconds);
                Program.LogInfo("Values per second:" + valuesPerSecond);

                var resampler = new WdlResamplingSampleProvider(reader, 44100);
                var wp        = new SampleToWaveProvider(reader);

                var rdr = wp.ToSampleProvider();


                var wf2 = rdr.WaveFormat;
                Program.LogInfo("SampleRate:" + wf2.SampleRate);
                Program.LogInfo("AverageBytesPerSecond:" + wf2.AverageBytesPerSecond);
                Program.LogInfo("BitsPerSample:" + wf2.BitsPerSample);
                Program.LogInfo("BlockAlign:" + wf2.BlockAlign);
                Program.LogInfo("Channels:" + wf2.Channels);
                Program.LogInfo("Encoding:" + wf2.Encoding);


                var wf = WaveFormat.CreateIeeeFloatWaveFormat(rdr.WaveFormat.SampleRate, rdr.WaveFormat.Channels);
                using (WaveFileWriter writer = new WaveFileWriter(outFile, wf))
                {
                    // 1 sec length buffer
                    var buf        = new float[rdr.WaveFormat.SampleRate * rdr.WaveFormat.Channels];
                    int currentSec = 0;
                    while (true)
                    {
                        if (currentSec > endSec)
                        {
                            break;
                        }

                        int read = rdr.Read(buf, 0, buf.Length);
                        if (read > 0)
                        {
                            if (currentSec > startSec)
                            {
                                writer.WriteSamples(buf, 0, read);
                            }
                        }
                        else
                        {
                            break;
                        }
                        currentSec++;
                    }
                }
            }
        }
Пример #4
0
        /// <summary>
        /// demo function for reading files
        /// </summary>
        /// <param name="testWavFile"></param>
        /// <param name="testOutWavFile"></param>
        /// <param name="testOutWavFile2"></param>
        public static void ReadSound(string testWavFile, string testOutWavFile, string testOutWavFile2)
        {
            string testFile = testWavFile;

            if (!File.Exists(testFile))
            {
                Program.LogError("No file " + testFile); return;
            }
            string outFile = testOutWavFile2;

            using (var reader = new AudioFileReader(testFile))
            {
                Program.LogInfo("TotalTime:" + reader.TotalTime);
                Program.LogInfo("Length:" + reader.Length);
                Program.LogInfo("Volume:" + reader.Volume);
                var waveFormat = reader.WaveFormat;
                Program.LogInfo("SampleRate:" + waveFormat.SampleRate);
                Program.LogInfo("AverageBytesPerSecond:" + waveFormat.AverageBytesPerSecond);
                Program.LogInfo("BitsPerSample:" + waveFormat.BitsPerSample);
                Program.LogInfo("BlockAlign:" + waveFormat.BlockAlign);
                Program.LogInfo("Channels:" + waveFormat.Channels);
                Program.LogInfo("Encoding:" + waveFormat.Encoding);
                Program.LogInfo("Values per second:" + reader.Length / reader.TotalTime.TotalSeconds);

                var resampler = new WdlResamplingSampleProvider(reader, 44100);
                var wp        = new SampleToWaveProvider(reader);

                var rdr = wp.ToSampleProvider();


                var wf2 = rdr.WaveFormat;
                Program.LogInfo("SampleRate:" + wf2.SampleRate);
                Program.LogInfo("AverageBytesPerSecond:" + wf2.AverageBytesPerSecond);
                Program.LogInfo("BitsPerSample:" + wf2.BitsPerSample);
                Program.LogInfo("BlockAlign:" + wf2.BlockAlign);
                Program.LogInfo("Channels:" + wf2.Channels);
                Program.LogInfo("Encoding:" + wf2.Encoding);


                //var buf = new float[rdr.WaveFormat.SampleRate* rdr.WaveFormat.BitsPerSample/4*32];
                //  var buf = new float[1024*32*32];

                /*
                 * var wf = WaveFormat.CreateIeeeFloatWaveFormat(rdr.WaveFormat.SampleRate, rdr.WaveFormat.Channels);
                 *
                 * Console.WriteLine(buf[1024 * 32 * 2].ToString());
                 *
                 * int read = reader.Read(buf, 0, buf.Length);
                 *
                 * using (WaveFileWriter writer = new WaveFileWriter("out.wav", wf))
                 * {
                 *  writer.WriteSamples(buf, 0, buf.Length);
                 * }
                 */

                // var wf = WaveFormat.CreateIeeeFloatWaveFormat(rdr.WaveFormat.SampleRate, rdr.WaveFormat.Channels);
                var wf  = WaveFormat.CreateIeeeFloatWaveFormat(rdr.WaveFormat.SampleRate, 1);
                var buf = new float[rdr.WaveFormat.SampleRate];
                using (WaveFileWriter writer = new WaveFileWriter(testOutWavFile, wf))
                {
                    while (true)
                    {
                        int read = rdr.Read(buf, 0, buf.Length);
                        var buf2 = (new List <float>(buf)).Where((value, index) => index % rdr.WaveFormat.Channels == 0).ToArray();
                        if (read > 0)
                        {
                            writer.WriteSamples(buf2, 0, read / 2);
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                /*
                 * // downsample to 22kHz
                 * var resampler = new WdlResamplingSampleProvider(reader, 44100);
                 * var wp = new SampleToWaveProvider(reader);
                 * using (var writer = new WaveFileWriter(outFile, wp.WaveFormat))
                 * {
                 *  byte[] b = new byte[wp.WaveFormat.AverageBytesPerSecond];
                 *  while (true)
                 *  {
                 *      int read = wp.Read(b, 0, b.Length);
                 *      if (read > 0)
                 *          writer.Write(b, 0, read);
                 *      else
                 *          break;
                 *  }
                 * }*/
                //WaveFileWriter.CreateWaveFile(outFile, );
            }
        }