public static TimeSpan AudioLength(this Wavefile wave, float level) { if (wave == null) { throw new ArgumentNullException("wave"); } if (level < 0 || level > 1) { throw new ArgumentOutOfRangeException("level", level, " "); } wave.WaitUntilLoaded(); short shLevel = (short)(32767 * level); int lastPoint = 0; int i = 0; foreach (var sample in wave.m_Samples) { if (sample.Left <= -shLevel || sample.Left >= shLevel || sample.Right <= -shLevel || sample.Right >= shLevel) { lastPoint = i; } i++; } return(TimeSpan.FromSeconds(lastPoint / (double)wave.SampleRate)); }
public static Wavefile Normalise(this Wavefile wave) { if (wave == null) { throw new ArgumentNullException("wave"); } Wavefile newWave = new Wavefile(); newWave.m_SampleRate = wave.SampleRate; newWave.m_Samples = new BlockArray <Sample>(12); ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { wave.WaitUntilLoaded(); int peak = 0; foreach (var sample in wave.m_Samples) { peak = Math.Max(peak, Math.Max(Math.Abs(sample.Left), Math.Abs(sample.Right))); } int factor = 256 * 32767 / peak; Sample[] newSample = new Sample[1]; foreach (var sample in wave.m_Samples) { newSample[0] = new Sample((short)((sample.Left * factor) >> 8), (short)((sample.Right * factor) >> 8)); newWave.m_Samples.Write(newSample); } newWave.FinishedLoading = true; })); //thread.Start(); return(newWave); }