简体   繁体   English

如何使用Java从pcm字节数组.wav文件中获取频率和音高?

[英]How to get frequency and pitch from a pcm byte array .wav file using java?

I am currently new to this, so kindly keep it simple for me to understand. 我目前对此尚不陌生,所以请保持简单易懂。

I have a project in which I have to classify the voice as good, bad or neutral. 我有一个项目,必须将声音分类为好,坏或中性。 My plan is to get all the frequencies and pitch of the sample data set and train them using SVM. 我的计划是获取样本数据集的所有频率和音调,并使用SVM对其进行训练。

In order to get the pitch and frequency of all the .wav files. 为了获得所有.wav文件的音高和频率。 I did the code up to finding the PCM Data from a audio file. 我做了代码直到从音频文件中找到PCM数据。 Now how should I apply these data to the Fast Fourier Transform Algorithm for getting frequencies? 现在如何将这些数据应用于快速傅立叶变换算法以获取频率? Are there more things to consider before applying the byte array to FFT algorithm? 在将字节数组应用于FFT算法之前,还需要考虑更多事情吗?

Here is my code for the convertion of wav file to pcm byte array: 这是我的将wav文件转换为pcm字节数组的代码:

int totalFramesRead = 0;
File fileIn = new File(inputFile);
try {
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(fileIn);
    int bytesPerFrame = audioInputStream.getFormat().getFrameSize();
    if (bytesPerFrame == AudioSystem.NOT_SPECIFIED) {
        // some audio formats may have unspecified frame size
        // in that case we may read any amount of bytes
        bytesPerFrame = 1;
    }
    // Set an arbitrary buffer size of 1024 frames.
    int numBytes = 1024 * bytesPerFrame;
    byte[] audioBytes = new byte[numBytes];
    try {
        int numBytesRead = 0;
        int numFramesRead = 0;
        // Try to read numBytes bytes from the file.


        while ((numBytesRead = audioInputStream.read(audioBytes)) != -1) {
            // Calculate the number of frames actually read.
            numFramesRead = numBytesRead / bytesPerFrame;
            totalFramesRead += numFramesRead;
        }
        return audioBytes[];
    }

There's a lot to consider after or other than an FFT, since FFT frequency peaks are not necessarily the pitch frequency. 在FFT之后或除了FFT之外,还有很多要考虑的因素,因为FFT频率峰值不一定是基音频率。 Look up pitch detection/estimation algorithms instead of just using a bare FFT magnitude. 查找基音检测/估计算法,而不是仅使用裸FFT幅度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM