AudioInputStream.read方法究竟返回什么? [英] What exactly does AudioInputStream.read method return?

查看:137
本文介绍了AudioInputStream.read方法究竟返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查找AudioInputStream的实际情况时遇到了一些问题.下面的程序只是打印出我得到的字节数组,但实际上我什至不知道,如果字节实际上是样本,那么字节数组就是音频 wave .

I have some problems finding out, what I actually read with the AudioInputStream. The program below just prints the byte-array I get but I actually don't even know, if the bytes are actually the samples, so the byte-array is the audio wave.

File fileIn;
AudioInputStream audio_in;
byte[] audioBytes;
int numBytesRead;
int numFramesRead;
int numBytes;
int totalFramesRead;
int bytesPerFrame;

try {
        audio_in = AudioSystem.getAudioInputStream(fileIn);
        bytesPerFrame = audio_in.getFormat().getFrameSize();


        if (bytesPerFrame == AudioSystem.NOT_SPECIFIED) {
            bytesPerFrame = 1;
        } 

        numBytes = 1024 * bytesPerFrame; 
        audioBytes = new byte[numBytes];
        try {
            numBytesRead = 0;
            numFramesRead = 0;   
        } catch (Exception ex) { 
            System.out.println("Something went completely wrong");
        }
    } catch (Exception e) {
        System.out.println("Something went completely wrong");
    }

在其他部分,我以此读取了一些字节:

and in some other part, I read some bytes with this:

try {
        if ((numBytesRead = audio_in.read(audioBytes)) != -1) {                 
              numFramesRead = numBytesRead / bytesPerFrame;                 
              totalFramesRead += numFramesRead;            
        }
    } catch (Exception e) {
        System.out.println("Had problems reading new content");
    }

因此,首先,此代码不是我提供的.这是我第一次阅读音频文件,因此我从网络间获得了一些帮助. (找到了链接: Java-读取,操作和写入WAV文件 stackoverflow,谁知道呢?

So first of all, this code is not from me. This is my first time, reading audio-files so I got some help from the inter-webs. (Found the link: Java - reading, manipulating and writing WAV files stackoverflow, who would have known.

问题是,audioBytes中的字节代表什么?由于源是44kHz立体声,因此必须在某处藏有2个波,对吗?那么如何从这些字节中过滤掉重要信息呢?

The question is, what are the bytes in audioBytes representing? Since the source is a 44kHz, stereo, there have to be 2 waves hiding in there somewhere, am I right? so how do I filter the important informations out of these bytes?

//编辑

所以我添加的是这个函数:

So what I added is this function:

public short[] Get_Sample() {
    if(samplesRead == 1024) {
        Read_Buffer();
        samplesRead = 4;
    } else {
        samplesRead = samplesRead + 4;
    }
    short sample[] = new short[2];
    sample[0] = (short)(audioBytes[samplesRead-4] + 256*audioBytes[samplesRead-3]);
    sample[1] = (short)(audioBytes[samplesRead-2] + 256*audioBytes[samplesRead-1]); 
    return sample;
}

其中,Read_Buffer()读取接下来的1024个(或更少)字节,并将其加载到audioBytes中. sample [0]用于左侧,sample [1]用于右侧.但是我仍然不确定,因为我从这种浪中看起来很嘈杂". (使用的WAV实际上使用小尾数字节顺序,因此我必须更改计算.)

where Read_Buffer() reads the next 1024 (or less) Bytes and loads them into audioBytes. sample[0] is used for the left side, sample[1] for the right side. But I'm still not sure since the waves i get from this look quite "noisy". ( the used WAV actually used little-endian byte order so I had to change the calculation.)

推荐答案

AudioInputStream read()方法返回原始音频数据.在使用getFormat()返回返回AudioFormat的音频格式之前,您不知道数据的构造"是什么.在AudioFormat中,您可以获取getChannels()和getSampleSizeInBits()以及更多...这是因为AudioInputStream是为已知格式制作的.

AudioInputStream read() method returns the raw audio data. You don't know what is the 'construction' of data before you read the audio format with getFormat() which returns AudioFormat. From AudioFormat you can getChannels() and getSampleSizeInBits() and more... This is because the AudioInputStream is made for known format.

如果计算样本值,则符号和符号的可能性不同. 数据的字节序(在16位样本的情况下).制作更通用的代码 使用从AudioInputStream返回的AudioFormat对象获取更多信息 关于数据缓冲区:

If you calculate a sample value you have different possibilities with signes and endianness of the data (in case of 16-bit sample). To make a more generic code use your AudioFormat object returned from AudioInputStream to get more info about the data buffer:

  • encoding():PCM_SIGNED,PCM_UNSIGNED ...
  • bigEndian():正确或错误

由于您已经发现不正确的样本构建,可能会导致某些干扰的声音.如果使用各种文件,将来可能会遇到问题.如果您不提供对某些格式的支持,请检查AudioFormat并抛出异常(例如javax.sound.sampled.UnsupportedAudioFileException).这样可以节省您的时间.

As you already discovered the incorrect sample building may lead to some disturbed sound. If you work with various files it may case a problems in the future. If you won't provide a support for some formats just check what says AudioFormat and throw exception (e.g. javax.sound.sampled.UnsupportedAudioFileException). It will save your time.

这篇关于AudioInputStream.read方法究竟返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆