如何从mp4文件获取PCM数据而不转换为wav

时间:2018-03-08 11:11:58

标签: c# wav mp4 naudio pcm

我试图从mp4文件中获取PCM数据而不转换为wav文件,这样我就可以执行FFT。我目前正在使用NAudio执行此操作而AForge用于FFT,但我愿意使用其他库来执行此操作。

与wav相比,将FFT应用于mp4时的输出值大不相同且不正确,因为它从mp4而不是wav读取值。

这是百万美元的问题 - 如何在不将其转换为wav的情况下从mp4文件中获取pcm值,这将允许FFT输出正确的值?

这是我目前在C#中的代码:

public void PCMdata()
    {
        //create media foundation reader to read the AAC encoded file
        using (MediaFoundationReader reader = new MediaFoundationReader(@"M:\Practice\Sound_Recording\y.mp4"))
        // resample the file to PCM with same sample rate, channels and bits per sample
        using (ResamplerDmoStream resampledReader = new ResamplerDmoStream(reader, new WaveFormat(reader.WaveFormat.SampleRate, reader.WaveFormat.BitsPerSample, reader.WaveFormat.Channels)))
            pcm.Add(resampledReader.ToString());


        foreach (Object r in pcm)
        {
            Console.WriteLine(r);
        }

        Console.WriteLine("STARTING PCM DATA METHOD ");

        System.IO.FileStream WaveFile = System.IO.File.OpenRead(@"M:\Practice\Sound_Recording\y.mp4");
        wave = new byte[WaveFile.Length];
        data = new Double[(wave.Length - 44) / 4]; //shifting the headers out of the PCM data;
        WaveFile.Read(wave, 0, Convert.ToInt32(WaveFile.Length));

        for (int i = 0; i < data.Length; i += 1)
        {
            //16 bit sample rate
            data[i] = BitConverter.ToInt16(wave, i) / 65536.0;
            //Console.WriteLine(" I " + data[i].ToString());
        }
 }
public void fftCalc()
    {
        complex = new Complex[16384];

        for (int i = 0; i < complex.Length; i++)
        {
            Complex tmp = new Complex(data[i], 0);
            complex[i] = tmp;
        }


        //run FFT
        FourierTransform.FFT(complex, FourierTransform.Direction.Backward);


        //print result and determine the amplitute
        for (int i = 0; i < complex.Length; i+=1)
        {
            //calculate the decibel 
            decibel = (20 * (Math.Log10(complex[i].Magnitude)));
            decibelCount.Add(decibel);
        }

        decibelTotal = decibelCount.Sum();
        average = decibelTotal / complex.Length;

        Console.ReadLine();
    }

0 个答案:

没有答案