1

How can print and modify audio sample value in C#?

byte[] array = File.ReadAllBytes("C:\\audioclip.wav");

for (int i = 44; i < array.Length - 1; i++)
{
    Console.WriteLine(" byte " + i + "  value  " + array[i]);
    System.Threading.Thread.Sleep(100);
}

Is it right?

Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Yas
  • 21
  • 4

1 Answers1

0

.wav files may be compressed, if so the sample values won't be stored as simple bytes. You would need to use a library (as mentioned in the comments).

If your file is uncompressed then you are almost correct. Uncompressed .wav files contain 16bit samples, not 8bit. So ReadAllBytes isn't very helpful.

Instead use a BinaryReader and reader.ReadInt16() to read each sample, eg.

List<Int16> sampleBuffer = new List<Int16>();
using(var inputStream = File.Open(inputFile)){
 using (var reader = new BinaryReader(inputStream))      
 {           
     while (inputStream.Position < inputStream.Length)
         sampleBuffer.Add(reader.ReadInt16());      
 }
}
Jim W
  • 4,866
  • 1
  • 27
  • 43
  • Thank you, but why when I print the result of list (sampleBuffer) value there is a negative number in the beginning of results. – Yas Jan 27 '19 at 13:46
  • @Yas I believe that is correct, see https://stackoverflow.com/questions/5890499/pcm-audio-amplitude-values – Jim W Jan 28 '19 at 01:38