0

So I'm trying to read the amplitude data of a .wav file in order to use it in DFT later on, and I use this code to get the amplitude data in C# and put it into a .txt file

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAudio.Wave;

namespace SoundToAmplitudes
{
    class Program
    {
        private static int Main(string[] args)
        {
#if DEBUG
            Console.WriteLine(String.Join(", ", args));
            args = new[] { @"C:\Users\s550c\Documents\visual studio 2010\Projects\DFT\DFT\newrecord.wav" };
#endif
            return Cli(args);
        }

        static int Cli(string[] args)
        {
            System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\test6.txt");
            string fileName = args[0];
            var soundFile = new FileInfo(fileName);
            foreach (float s in AmplitudesFromFile(soundFile))
            {
                Console.WriteLine(s);

                file.WriteLine(s);


            }
            file.Close();
            //Console.WriteLine();
#if DEBUG
            Console.Read();
#endif
            return 0;
        }

        public static IEnumerable<float> AmplitudesFromFile(FileInfo soundFile)
        {
            var reader = new AudioFileReader(soundFile.FullName);
            int count = 4096; // arbitrary
            float[] buffer = new float[count];
            int offset = 0;
            int numRead = 0;
            while ((numRead = reader.Read(buffer, offset, count)) > 0)
            {
                foreach (float amp in buffer.Take(numRead))
                {
                    yield return amp;
                }
            }
        }
    }
}

The program give me a long list of data for a ~1 seconds sound file, (145920 data to be exact), so my questions is this:

  1. What's the time interval between each of those data? (like 1^-10 second or smt?), how do I know it?

  2. If I want to set the interval between each data by myself, how should I change the code?

Sam
  • 7,252
  • 16
  • 46
  • 65
Megazero
  • 35
  • 7
  • I'm not following. Why would you want to treat wave file samples as a floating point numbers? Assuming you're trying to open uncompressed PCM file, the buffer should be of a short int type. It seems like you just found some code and don't know how to use it. You know how much samples there are from the header. I assume your AudioFileReader should have that kind of information loaded. – Tarec Jun 29 '14 at 14:01
  • Yes, so for example I set the interval to become 0.01 second, then the data I got will be the amplitude at 0 second; 0.01 second; 0.02 second and so on. – Megazero Jun 29 '14 at 14:02
  • @Tarec yes, like I said I have never play with audio file before so I'm completely blind on how does these stuff work at all. And I cant change it to short since the function AudioFileReader.Read from the NAudio library require float – Megazero Jun 29 '14 at 14:07

1 Answers1

1

In response to question 1: The interval of the data is determined by the sample rate. This can be accessed through reader.WaveFormat.SampleRate. That is the number of samples per second, so the time between the samples is 1 / SampleRate.

For question 2: I am not familiar with NAudio so I do not know if it has any feature do that, but you could skip samples to just get only the samples you want.

Herman
  • 2,738
  • 19
  • 32
  • Thanks alot, this really help me. But still, when I try to see the samplerate of my wav file it's 44100, but the program return to me 145920 data, which if it the sample rate mean 44100 data/second, it would equal to around 3.3 second, while my sound file is only abit more than 1 second, do you have any idea why ^^? – Megazero Jun 29 '14 at 14:27
  • @user3702271: `WaveFormat.Channels` specifies the number of channels in the stream. Probably there are two (a mono file might be changed into stereo), in which case the samples are interleaved (Left1, Right1, Left2, etc). The duration of the file will then be 1.6 seconds. – Herman Jun 29 '14 at 19:38
  • Oh I see! Yes, my wav file indeed have 2 channels, thanks, that explained alot XD – Megazero Jun 30 '14 at 03:26
  • @user3702271 Here you can find some info about wave file format, https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ – Tarec Jun 30 '14 at 12:05