public void CanAccessSamplesIndividuallyInAMonoFile()
 {
     var ms =  new MemoryStream();
     using (var writer = new WaveFileWriter( new IgnoreDisposeStream(ms), new WaveFormat(8000, 16, 1)))
     {
         writer.WriteSample(0.1f);
         writer.WriteSample(0.2f);
         writer.WriteSample(0.3f);
         writer.WriteSample(0.4f);
     }
     ms.Position = 0;
     using (var reader = new WaveFileReader(ms))
     {
         Assert.AreEqual(0.1f, reader.ReadNextSampleFrame()[0], 0.001f);
         Assert.AreEqual(0.2f, reader.ReadNextSampleFrame()[0], 0.001f);
         Assert.AreEqual(0.3f, reader.ReadNextSampleFrame()[0], 0.001f);
         Assert.AreEqual(0.4f, reader.ReadNextSampleFrame()[0], 0.001f);
         Assert.IsNull(reader.ReadNextSampleFrame());
     }
 }
        public void CanAccessSamplesIndividuallyInAStereo24BitFile()
        {
            var ms = new MemoryStream();
            using (var writer = new WaveFileWriter(new IgnoreDisposeStream(ms), new WaveFormat(44100, 24, 2)))
            {
                writer.WriteSample(0.1f);
                writer.WriteSample(0.2f);
                writer.WriteSample(0.3f);
                writer.WriteSample(0.4f);

            }
            ms.Position = 0;
            using (var reader = new WaveFileReader(ms))
            {
                var f1 = reader.ReadNextSampleFrame();
                Assert.AreEqual(0.1f, f1[0], 0.0001f);
                Assert.AreEqual(0.2f, f1[1], 0.0001f);
                var f2 = reader.ReadNextSampleFrame();
                Assert.AreEqual(0.3f, f2[0], 0.0001f);
                Assert.AreEqual(0.4f, f2[1], 0.0001f);
                Assert.IsNull(reader.ReadNextSampleFrame());
            }
        }
        public static int[] GetVolumesDiscrete(byte[] wav, double intervalSec)
        {
            using (var ms = new MemoryStream(wav))
            using (var fr = new WaveFileReader(ms))
            {
                var totalTime = fr.TotalTime;

                int[] result = new int[(int)(totalTime.TotalSeconds / intervalSec)];
                for (int i = 0; i < result.Length; i++)
                {
                    var normalizedVolumes = new List<float>();
                    var nextIntervalStart = TimeSpan.FromSeconds((i + 1) * intervalSec);
                    if(nextIntervalStart > totalTime)
                    {
                        nextIntervalStart = totalTime;
                    }

                    while (fr.CurrentTime < nextIntervalStart)
                    {
                        float[] volumes = fr.ReadNextSampleFrame();
                        foreach (var volume in volumes)
                        {
                            normalizedVolumes.Add(volume);
                        }
                    }

                    //見ての通り二乗値を平均して平方根を取る
                    double v = Math.Sqrt(normalizedVolumes
                        .Select(n => n * n)
                        .Sum() / normalizedVolumes.Count
                        );

                    result[i] = v < Thresh0 ? 0 :
                                v < Thresh1 ? 1 :
                                v < Thresh2 ? 2 :
                                v < Thresh3 ? 3 :
                                v < Thresh4 ? 4 : 5;

                }

                return result;
            }
        }
        /// <summary>
        /// Gets the maximum.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <returns>
        /// Max float value in file
        /// </returns>
        private static float GetMax(string file)
        {
            using (var reader = new WaveFileReader(file))
            {
                float[] buffer;
                float max = 0;

                while ((buffer = reader.ReadNextSampleFrame()) != null)
                {
                    for (var n = 0; n < buffer.Length; n++)
                    {
                        max = Math.Max(max, Math.Abs(buffer[n]));
                    }
                }

                return max;
            }
        }
Beispiel #5
0
 public Play2(string file)
 {
     WaveFileReader reader = new WaveFileReader(file);
     frame = (int)reader.Length / 2;
     sampleBufferL = new double[frame];
     sampleBufferR = new double[frame];
     float[] buffer;
     while ((buffer = reader.ReadNextSampleFrame()) != null)
     {
         for (int i = 0; i*2 < buffer.Length; i++)
         {
             sampleBufferL[n] = buffer[i*2];
             sampleBufferR[n] = buffer[i*2+1];
         }
         n++;
     }
     WaveFormat format = reader.WaveFormat;
     ch = format.Channels;
     n = 0;
     speed = 1;
 }
Beispiel #6
0
 public Play(string file)
 {
     WaveFileReader reader = new WaveFileReader(file);
     sampleBuffer = new double[reader.Length];
     float[] buffer;
     while ((buffer = reader.ReadNextSampleFrame()) != null)
     {
         for (int i = 0; i < buffer.Length; i++)
         {
             sampleBuffer[n++] = buffer[i];
         }
     }
     WaveFormat format = reader.WaveFormat;
     ch = format.Channels;
     frame = sampleBuffer.Length / ch;
     n = 0;
     speed = 1;
 }
Beispiel #7
0
        private IEnumerable<float[]> LoadSamplesWav(string aPath)
        {
            using (var reader = new WaveFileReader(aPath))
            {
                var frames = new List<float[]>();

                while (true)
                {
                    var frame = reader.ReadNextSampleFrame();

                    if (frame == null)
                    {
                        return frames;
                    }

                    frames.Add(frame);
                }
            }
        }
        /// <summary>指定したパーセンテージで音量を増減させた新しいwavデータを生成します。</summary>
        /// <param name="wav">元のwavデータ</param>
        /// <param name="factorPercent">音量のパーセンテージ(0~200くらいを想定)</param>
        /// <returns>音量を変更したあとのwavデータ</returns>
        public static byte[] ChangeVolume(byte[] wav, int factorPercent)
        {
            var result = new byte[wav.Length];

            using (var msIn = new MemoryStream(wav))
            using (var fr = new WaveFileReader(msIn))
            using (var msOut = new MemoryStream(result))
            using (var fw = new WaveFileWriter(msOut, fr.WaveFormat))
            {
                var allVolumes = new List<float>();
                while (fr.CurrentTime < fr.TotalTime)
                {
                    var vs = fr.ReadNextSampleFrame();
                    foreach (var v in vs)
                    {
                        allVolumes.Add(v);
                    }
                }

                fw.WriteSamples(
                    allVolumes.Select(v => v * factorPercent / 100.0f).ToArray(),
                    0, 
                    allVolumes.Count
                    );
            }

            return result;
        }
Beispiel #9
0
        private List<float> getWavWaveForm(WaveFileReader reader)
        {
            List<float> data = new List<float>();
            float[] buffer;

            while ((buffer = reader.ReadNextSampleFrame()) != null)
            {
                data.Add(buffer[0]);
            }
            return data;
        }
Beispiel #10
0
        public static void HighPassFilter(string innPath, string outtPath)
        {
            string inPath = "C:\\Users\\Administratorius\\Documents\\GitHub\\MusicEditor\\PawellsMusicEditor\\PawellsMusicEditor\\Content\\Songs\\NowEdited.WAV";
            string outPath = "C:\\Users\\Administratorius\\Documents\\GitHub\\MusicEditor\\PawellsMusicEditor\\PawellsMusicEditor\\Content\\Songs\\NowEdited3.WAV";

            Mp3ToWav(innPath, inPath);
            WaveFileReader reader = new WaveFileReader(inPath);
            var myFilter = BiQuadFilter.HighPassFilter(44F, 10F, 0.5F);
            WaveFileWriter writer = new WaveFileWriter(outPath, reader.WaveFormat);

            reader.Position = 0;
            var endPos = reader.Length;
            while (reader.Position < endPos)
            {
                int bytesRequired = (int)(endPos - reader.Position);
                if (bytesRequired > 0)
                {
                    float[] sample = new float[4];
                    for (int i = 0; i < 2; i++)
                    {
                        sample[i] = myFilter.Transform(reader.ReadNextSampleFrame()[i]);
                    }
                    writer.WriteSamples(sample, 0, 4);
                }
            }
            reader.Dispose();
            writer.Dispose();
            ConvertWavToMp3(outPath, outtPath);
            File.Delete(inPath);
            File.Delete(outPath);
        }