private void FillSampleBuffers() { const double divider = 32768; var buffer = new byte[4]; for (int i = 0; i < SamplesPerUpdate / 2; i++) { var read = trackReader.Read(buffer, 0, 4); if (read == 4) { samplesReal[i] = BitConverter.ToInt16(buffer, 0) / divider; samplesImaginary[i] = BitConverter.ToInt16(buffer, 2) / divider; } else { samplesReal[i] = samplesImaginary[i] = 0.0; } } }
private static void TrimWavFile(MediaFoundationReader reader, WaveFileWriter writer, int startPos, int endPos) { reader.Position = startPos; var buffer = new byte[reader.BlockAlign * 1024]; while (reader.Position < endPos) { var bytesRequired = (int)(endPos - reader.Position); if (bytesRequired <= 0) { continue; } var bytesToRead = Math.Min(bytesRequired, buffer.Length); var bytesRead = reader.Read(buffer, 0, bytesToRead); if (bytesRead > 0) { writer.Write(buffer, 0, bytesRead); } } }
public unsafe void SampleBlockConversions() { SampleBlock reference16Bit; using (MediaFoundationReader reference16BitReader = new MediaFoundationReader(TestConstant.ReferenceFilePath16Bit)) { byte[] buffer16Bit = new byte[reference16BitReader.Length]; int bytesRead = reference16BitReader.Read(buffer16Bit, 0, buffer16Bit.Length); reference16Bit = new SampleBlock(buffer16Bit, bytesRead, SampleTypeExtensions.FromBitsPerSample(reference16BitReader.WaveFormat.BitsPerSample)); } SampleBlock reference24Bit; using (MediaFoundationReader reference24BitReader = new MediaFoundationReader(TestConstant.ReferenceFilePath24Bit)) { byte[] buffer24Bit = new byte[reference24BitReader.Length]; int bytesRead = reference24BitReader.Read(buffer24Bit, 0, buffer24Bit.Length); reference24Bit = new SampleBlock(buffer24Bit, bytesRead, SampleTypeExtensions.FromBitsPerSample(reference24BitReader.WaveFormat.BitsPerSample)); } Assert.IsTrue(reference16Bit.Int16Samples == reference24Bit.SamplesInUse); SampleBlock reference16BitAsDouble = reference16Bit.ConvertTo(SampleType.Double); SampleBlock reference16BitAsQ31 = reference16Bit.ConvertTo(SampleType.Int32); SampleBlock reference24BitAsDouble = reference24Bit.ConvertTo(SampleType.Double); SampleBlock reference24BitAsQ31 = reference24Bit.ConvertTo(SampleType.Int32); Assert.IsTrue(reference16Bit.Int16Samples == reference16BitAsDouble.DoubleSamples); Assert.IsTrue(reference16Bit.Int16Samples == reference16BitAsQ31.Int32Samples); Assert.IsTrue(reference16Bit.Int16Samples == reference24BitAsDouble.DoubleSamples); Assert.IsTrue(reference16Bit.Int16Samples == reference24BitAsQ31.Int32Samples); for (int sample = 0; sample < reference16Bit.Int16Samples; ++sample) { Assert.IsTrue(((int)reference16Bit.Int16s[sample] << TestConstant.ShiftBetween16BitSamplesAndQ31) == reference16BitAsQ31.Int32s[sample]); Assert.IsTrue((int)reference16BitAsDouble.Doubles[sample] == reference16BitAsQ31.Int32s[sample]); Assert.IsTrue((reference24Bit.GetInt24AsInt32(sample) << TestConstant.ShiftBetween24BitSamplesAndQ31) == reference24BitAsQ31.Int32s[sample]); Assert.IsTrue((int)reference24BitAsDouble.Doubles[sample] == reference24BitAsQ31.Int32s[sample]); } }
public void CanReadAnAac() { var testFile = @"C:\Users\mheath\Downloads\NAudio\AAC\halfspeed.aac"; if (!File.Exists(testFile)) { Assert.Ignore("Missing test file"); } var reader = new MediaFoundationReader(testFile); Console.WriteLine(reader.WaveFormat); var buffer = new byte[reader.WaveFormat.AverageBytesPerSecond]; int bytesRead; long total = 0; while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0) { total += bytesRead; } Assert.IsTrue(total > 0); }
public bool FilterData() { if (m_bufferPos == m_writtenBufferLength) { m_writtenBufferLength = m_fileReader.Read(m_buffer, 0, m_bufferSize); Debug.WriteLine($"IN: Position: {Position}"); if (m_writtenBufferLength == 0) { Debug.WriteLine("IN: No more data."); return(false); } else if (m_writtenBufferLength < m_bufferSize) { Debug.WriteLine($"IN: Bytes Partially Read: {m_writtenBufferLength}"); m_bufferPos = 0; } else //if (m_writtenBufferLength == m_bufferSize) { Debug.WriteLine($"IN: Bytes Fully Read: {m_writtenBufferLength}"); m_bufferPos = 0; } } if (!OutSample.WriteBlockFromBuffer(m_buffer, m_bufferPos, InFormat)) { Debug.WriteLine($"IN: m_writtenBufferLength: {m_writtenBufferLength}"); Debug.WriteLine($"IN: m_buffer.Length: {m_buffer.Length}"); Debug.WriteLine($"IN: m_bufferPos: {m_bufferPos}"); Debug.WriteLine($"IN: Position: {Position}"); throw new InvalidOperationException("Could not write block from buffer."); } //Debug.Write("f"); m_bufferPos += m_blockLength; return(true); }
private void splitMp3(string trackPath, int startPos, int endPos, MediaFoundationReader reader, ID3TagData tagData) { int progress = 0; using (var writer = new NAudio.Lame.LameMP3FileWriter(trackPath, reader.WaveFormat, NAudio.Lame.LAMEPreset.V3, tagData)) { reader.Position = startPos; byte[] buffer = new byte[1024]; while (reader.Position < endPos) { int bytesRequired = (int)(endPos - reader.Position); if (bytesRequired > 0) { int bytesToRead = Math.Min(bytesRequired, buffer.Length); int bytesRead = reader.Read(buffer, 0, bytesToRead); if (bytesRead > 0) { writer.Write(buffer, 0, bytesRead); progress += bytesRead; } } } } }
private unsafe void VerifyWaveFilesEquivalent(string actualFilePath, string expectedFilePath, double expectedToActualScaleFactor, double sampleMatchTolerance, bool verifySampleSize) { // load data in files SampleBlock actual; WaveFormat actualFormat; using (MediaFoundationReader actualReader = new MediaFoundationReader(actualFilePath)) { byte[] buffer = new byte[actualReader.Length]; int bytesRead = actualReader.Read(buffer, 0, buffer.Length); actual = new SampleBlock(buffer, bytesRead, SampleTypeExtensions.FromBitsPerSample(actualReader.WaveFormat.BitsPerSample)); actualFormat = actualReader.WaveFormat; } SampleBlock expected; WaveFormat expectedFormat; using (MediaFoundationReader expectedReader = new MediaFoundationReader(expectedFilePath)) { byte[] buffer = new byte[expectedReader.Length]; int bytesRead = expectedReader.Read(buffer, 0, buffer.Length); expected = new SampleBlock(buffer, bytesRead, SampleTypeExtensions.FromBitsPerSample(expectedReader.WaveFormat.BitsPerSample)); expectedFormat = expectedReader.WaveFormat; } // check data format matches Assert.IsTrue(actual.SamplesInUse == expected.SamplesInUse); if (verifySampleSize) { Assert.IsTrue(actual.SampleType == expected.SampleType); Assert.IsTrue(actualFormat.AverageBytesPerSecond == expectedFormat.AverageBytesPerSecond); Assert.IsTrue(actualFormat.BitsPerSample == expectedFormat.BitsPerSample); Assert.IsTrue(actualFormat.BlockAlign == expectedFormat.BlockAlign); } Assert.IsTrue(actualFormat.Channels == expectedFormat.Channels); Assert.IsTrue(actualFormat.Encoding == expectedFormat.Encoding); Assert.IsTrue(actualFormat.ExtraSize == expectedFormat.ExtraSize); Assert.IsTrue(actualFormat.SampleRate == expectedFormat.SampleRate); double largestMismatch = 0.0; double maxExpectedValue = Math.Pow(2.0, expected.SampleType.BitsPerSample() - 1) - 1; double minExpectedValue = -Math.Pow(2.0, expected.SampleType.BitsPerSample() - 1); double mismatchStandardDevation = 0.0; using (FileStream samplesStream = new FileStream(String.Format("{0} - {1}.csv", Path.GetFileNameWithoutExtension(actualFilePath), Path.GetFileNameWithoutExtension(expectedFilePath)), FileMode.Create)) { using (StreamWriter samplesWriter = new StreamWriter(samplesStream)) { samplesWriter.WriteLine("actual,expected"); int clippedSamples = 0; int mismatchedSamples = 0; int samples = expected.SamplesInUse; for (int sample = 0; sample < samples; ++sample) { // read samples as ints and convert to doubles regardless of sample type in files double actualSample; switch (actual.SampleType) { case SampleType.Int16: actualSample = actual.Int16s[sample]; break; case SampleType.Int24: actualSample = actual.GetInt24AsInt32(sample); break; case SampleType.Int32: actualSample = actual.Int32s[sample]; break; default: throw new NotSupportedException(String.Format("Unhandled sample type {0}.", expected.SampleType)); } actualSample = actualSample / expectedToActualScaleFactor; double expectedSample; switch (expected.SampleType) { case SampleType.Int16: expectedSample = expected.Int16s[sample]; break; case SampleType.Int24: expectedSample = expected.GetInt24AsInt32(sample); break; case SampleType.Int32: expectedSample = expected.Int32s[sample]; break; default: throw new NotSupportedException(String.Format("Unhandled sample type {0}.", expected.SampleType)); } samplesWriter.WriteLine("{0},{1}", actualSample, expectedSample); // it's OK if the expected data is clipped and the actual data isn't if (expectedSample >= maxExpectedValue && actualSample > expectedSample) { ++clippedSamples; continue; } if (expectedSample <= minExpectedValue && actualSample < expectedSample) { ++clippedSamples; continue; } // check samples for equivalence // standard deviation calculation is naive as it assumes the average mismatch is zero double mismatch = Math.Abs(expectedSample - actualSample); mismatchStandardDevation += mismatch * mismatch; if (mismatch > largestMismatch) { largestMismatch = mismatch; } if (mismatch > sampleMatchTolerance) { ++mismatchedSamples; } } mismatchStandardDevation = Math.Sqrt(mismatchStandardDevation / (double)expected.SamplesInUse); this.TestContext.WriteLine("Largest mismatch in samples between {0} and {1} was {2:0.0} with a standard deviation of {3:0.0}.", actualFilePath, expectedFilePath, largestMismatch, mismatchStandardDevation); this.TestContext.WriteLine(">>> {0} samples exceeded threshold of {1:0.0}.", mismatchedSamples, sampleMatchTolerance); Assert.IsTrue(mismatchedSamples == 0, "{0} of {1} ({2:#0.0%}) samples did not match within tolerance {3}.", mismatchedSamples, samples, (double)mismatchedSamples / (double)samples, sampleMatchTolerance); if (verifySampleSize) { Assert.IsTrue(clippedSamples < 0.0001 * samples, "{0} (1:#0.000%) samples were clipped; this is unexpectedly high.", clippedSamples, (double)clippedSamples / (double)samples); } } } // check metadata Tag actualMetadata; using (FileStream inputMetadataStream = new FileStream(actualFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (TagFile inputTagFile = TagFile.Create(new StreamFileAbstraction(inputMetadataStream.Name, inputMetadataStream, inputMetadataStream))) { actualMetadata = inputTagFile.Tag; } } Tag expectedMetadata; using (FileStream outputMetadataStream = new FileStream(expectedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (TagFile outputTagFile = TagFile.Create(new StreamFileAbstraction(outputMetadataStream.Name, outputMetadataStream, outputMetadataStream))) { expectedMetadata = outputTagFile.Tag; } } Assert.AreEqual(expectedMetadata.Album, actualMetadata.Album, String.Format("Expected artist '{0}' but got '{1}'.", expectedMetadata.Album, actualMetadata.Album)); Assert.AreEqual(expectedMetadata.AmazonId, actualMetadata.AmazonId, String.Format("Expected Amazon ID '{0}' but got '{1}'.", expectedMetadata.AmazonId, actualMetadata.AmazonId)); Assert.AreEqual(expectedMetadata.Conductor, actualMetadata.Conductor, String.Format("Expected conductor '{0}' but got '{1}'.", expectedMetadata.Conductor, actualMetadata.Conductor)); Assert.AreEqual(expectedMetadata.Copyright, actualMetadata.Copyright, String.Format("Expected copyright '{0}' but got '{1}'.", expectedMetadata.Copyright, actualMetadata.Copyright)); Assert.AreEqual(expectedMetadata.Disc, actualMetadata.Disc, String.Format("Expected disc '{0}' but got '{1}'.", expectedMetadata.Disc, actualMetadata.Disc)); Assert.AreEqual(expectedMetadata.DiscCount, actualMetadata.DiscCount, String.Format("Expected disc count '{0}' but got '{1}'.", expectedMetadata.DiscCount, actualMetadata.DiscCount)); Assert.AreEqual(expectedMetadata.Grouping, actualMetadata.Grouping, String.Format("Expected grouping '{0}' but got '{1}'.", expectedMetadata.Grouping, actualMetadata.Grouping)); Assert.AreEqual(expectedMetadata.Lyrics, actualMetadata.Lyrics, String.Format("Expected lyrics '{0}' but got '{1}'.", expectedMetadata.Lyrics, actualMetadata.Lyrics)); Assert.AreEqual(expectedMetadata.JoinedAlbumArtists, actualMetadata.JoinedAlbumArtists, String.Format("Expected artists '{0}' but got '{1}'.", expectedMetadata.JoinedAlbumArtists, actualMetadata.JoinedAlbumArtists)); Assert.AreEqual(expectedMetadata.JoinedComposers, actualMetadata.JoinedComposers, String.Format("Expected composers '{0}' but got '{1}'.", expectedMetadata.JoinedComposers, actualMetadata.JoinedComposers)); Assert.AreEqual(expectedMetadata.JoinedGenres, actualMetadata.JoinedGenres, String.Format("Expected genres '{0}' but got '{1}'.", expectedMetadata.JoinedGenres, actualMetadata.JoinedGenres)); Assert.AreEqual(expectedMetadata.JoinedPerformersSort, actualMetadata.JoinedPerformersSort, String.Format("Expected performers '{0}' but got '{1}'.", expectedMetadata.JoinedPerformersSort, actualMetadata.JoinedPerformersSort)); // Pictures is not checked Assert.AreEqual(expectedMetadata.Title, actualMetadata.Title, String.Format("Expected title '{0}' but got '{1}'.", expectedMetadata.Title, actualMetadata.Title)); Assert.AreEqual(expectedMetadata.Track, actualMetadata.Track, String.Format("Expected track '{0}' but got '{1}'.", expectedMetadata.Track, actualMetadata.Track)); Assert.AreEqual(expectedMetadata.TrackCount, actualMetadata.TrackCount, String.Format("Expected track count '{0}' but got '{1}'.", expectedMetadata.TrackCount, actualMetadata.TrackCount)); Assert.AreEqual(expectedMetadata.Year, actualMetadata.Year, String.Format("Expected year '{0}' but got '{1}'.", expectedMetadata.Year, actualMetadata.Year)); }
public override int Read(byte[] buffer, int offset, int count) { return(m_reader.Read(buffer, offset, count)); }
//input buffer data and effect processing public void signal_processing() { double max = 0; double[][] InProcessbuffer0 = new double[audioreader_temp.WaveFormat.Channels][]; double[][] OutProcessbuffer = new double[audioreader_temp.WaveFormat.Channels][]; double[][] temp_buffer = new double[audioreader_temp.WaveFormat.Channels][]; byte[] InBuffer = new byte[in_total_byte_per_frame]; byte[] OutBuffer = new byte[out_total_byte_per_frame]; // buffer for loading the raw data for (int ch = 0; ch < audioreader_temp.WaveFormat.Channels; ch++) { InProcessbuffer0[ch] = new double[Samples_per_frame]; OutProcessbuffer[ch] = new double[Samples_per_frame]; temp_buffer[ch] = new double[Samples_per_frame]; } audioreader_temp.Read(InBuffer, 0, (int)in_total_byte_per_frame); sig_conv_temp.ByteToDouble(InBuffer, InProcessbuffer0); //find peak for (int k = 0; k < OutProcessbuffer[0].Length; k++) { if (Math.Abs(InProcessbuffer0[0][k]) > max) { max = Math.Abs(InProcessbuffer0[0][k]); } } peak = (int)(max * volume * 120); if (peak >= 100) { peak = 100; } ////effect equalizer_temp.eq_filtering(InProcessbuffer0); temp_buffer = equalizer_temp.filter_out; //DRC if (three_band_drc_temp.DRC_enable == true) { three_band_drc_temp.drc_filtering(temp_buffer); for (int i = 0; i < OutProcessbuffer[0].Length; i++) { for (int k = 0; k < audioreader_temp.WaveFormat.Channels; k++) { temp_buffer[k][i] = three_band_drc_temp.data_buffer[4][k][i]; } } } //FFT real_time_fft_temp.fft(temp_buffer); for (int i = 0; i < OutProcessbuffer[0].Length; i++) { for (int k = 0; k < audioreader_temp.WaveFormat.Channels; k++) { OutProcessbuffer[k][i] = temp_buffer[k][i]; } } sig_conv_temp.ScalarBack(OutProcessbuffer); sig_conv_temp.DoubleToByte(OutBuffer, OutProcessbuffer); BufferAddSample(OutBuffer); InProcessbuffer0 = null; OutProcessbuffer = null; InBuffer = null; OutBuffer = null; }