Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以一定间隔从WAV文件读取振幅数据_C#_Wav_Amplitude - Fatal编程技术网

C# 以一定间隔从WAV文件读取振幅数据

C# 以一定间隔从WAV文件读取振幅数据,c#,wav,amplitude,C#,Wav,Amplitude,因此,我试图读取.wav文件的振幅数据,以便稍后在DFT中使用它,我使用这段代码以C#的形式获取振幅数据,并将其放入.txt文件中 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using NAudio.Wave; namespace SoundToAmplitudes {

因此,我试图读取.wav文件的振幅数据,以便稍后在DFT中使用它,我使用这段代码以C#的形式获取振幅数据,并将其放入.txt文件中

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;
                }
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用NAudio.波;
名称空间声音振幅
{
班级计划
{
私有静态int Main(字符串[]args)
{
#如果调试
Console.WriteLine(String.Join(“,”,args));
args=new[]{@“C:\Users\s550c\Documents\visualstudio2010\Projects\DFT\DFT\newrecord.wav”};
#恩迪夫
返回Cli(args);
}
静态int Cli(字符串[]args)
{
System.IO.StreamWriter file=new System.IO.StreamWriter(“d:\\test6.txt”);
字符串文件名=args[0];
var soundFile=新文件信息(文件名);
foreach(在AmplitudesFromFile(声音文件)中浮动s)
{
控制台。写入线(s);
文件写入线;
}
file.Close();
//Console.WriteLine();
#如果调试
Console.Read();
#恩迪夫
返回0;
}
公共静态IEnumerable AmplitudesFromFile(FileInfo声音文件)
{
var reader=newaudiofilereader(soundFile.FullName);
int count=4096;//任意
float[]缓冲区=新的float[计数];
整数偏移=0;
int numRead=0;
而((numRead=reader.Read(缓冲区、偏移量、计数))>0)
{
foreach(缓冲区中的浮点放大器。取数(numRead))
{
收益率返回放大器;
}
}
}
}
}
程序给了我一长串1秒钟声音文件的数据(确切地说是145920个数据),所以我的问题是:

  • 每个数据之间的时间间隔是多少?(比如1^-10秒或smt?),我怎么知道

  • 如果我想自己设置每个数据之间的间隔,我应该如何更改代码


  • 回答问题1:数据间隔由采样率决定。这可以通过
    reader.WaveFormat.SampleRate
    访问。这是每秒的样本数,因此两个样本之间的时间为1/s


    对于问题2:我不熟悉NAudio,所以我不知道它是否有任何这样做的功能,但您可以跳过示例,只获取您想要的示例。

    我不了解。为什么要将wave文件样本视为浮点数?假设您试图打开未压缩的PCM文件,缓冲区应为短int类型。看起来你只是找到了一些代码,不知道如何使用它。您知道页眉中有多少样本。我假设您的音频文件阅读器应该加载了此类信息。是的,例如,我将间隔设置为0.01秒,那么我得到的数据将是0秒时的振幅;0.01秒;0.02秒等等。@Tarec是的,就像我说的,我以前从来没有玩过音频文件,所以我完全不知道这些东西是如何工作的。我不能把它改成短的,因为函数AudioFileReader。从NAudio库中阅读,非常感谢,这对我很有帮助。但是,当我尝试查看我的wav文件的采样器时,它是44100,但程序返回给我145920个数据,如果它的采样率是44100个数据/秒,它将等于大约3.3秒,而我的声音文件只有略多于1秒,你知道为什么吗?^^@user3702271:
    WaveFormat.Channels
    指定流中的通道数。可能有两个(一个单声道文件可能会变成立体声),在这种情况下,采样是交错的(Left1、Right1、Left2等)。文件的持续时间将是1.6秒。哦,我明白了!是的,我的wav文件确实有两个通道,谢谢,这解释了很多XD@user3702271在这里你可以找到一些关于wave文件格式的信息,