使用Xuggler获取1秒样本

时间:2013-05-02 11:33:13

标签: java audio xuggler

我有一些关于Xuggler API的问题,

我想创建一个小类,一旦打开mp3文件,就会从中获取原始字节,但是,有多少?我想从原始文件中获得1秒样本。

这是我目前的代码:

public byte[] getSamples(int iBytesQtty){
    byte[] rawBytes = null;

    /** Go to the correct packet */
    while (this._inputContainer.readNextPacket(this._packet) >= 0){
        System.out.println(this._packet.getDuration());
        /** Once we have a packet, let's see if it belongs to the audio stream */

        if (this._packet.getStreamIndex() == this._iAudioStreamId){
            IAudioSamples samples = IAudioSamples.make(iBytesQtty, this._audioCoder.getChannels());


            //System.out.println(">> " + samples.toString());
            /** Because a packet can contain multiple set of samples (frames of samples). We may need to call
             * decode audio multiple times at different offsets in the packet's data */

            int iCurrentOffset = 0;

            while(iCurrentOffset < this._packet.getSize()){

                int iBytesDecoded = this._audioCoder.decodeAudio(samples, this._packet, iCurrentOffset);
                iCurrentOffset += iBytesDecoded;

                if (samples.isComplete()){
                    rawBytes = samples.getData().getByteArray(0, samples.getSize());
                }
            }
            return rawBytes;
        }
        else{
            /** Otherwise drop it */
            do{}while(false); /** WTF? **/
        }
    }
    return rawBytes; /** This will return null at this point */
}

任何提示都会受到赞赏,提前谢谢。

编辑:

实际上这段代码对我有用,但并非在所有情况下都适用

Mp3File song = new Mp3File("sample.mp3");
        byte[] buff = null;
        int totalBytes = 0;
int iNumBytes = 1024;

        while( (buff = song.getSamples(iNumBytes)) != null ){
            totalBytes += buff.length;
    //      System.out.println("Length buff: " + buff.length + " TOTAL: " + totalBytes);
        }

System.out.println("Bytes: " + totalBytes + " (" + totalBytes/1024 + " Kb.)");
        float lengthSecs = (float) (totalBytes / ((1411*1000)/8)); 
        float lengthMins = lengthSecs/60;

        System.out.println("song duration: " + lengthMins + " mins. (" + lengthSecs + " secs.)");

我认为mp3文件实际上是内部转换为常规WAV文件,所以每秒我的速率为1411kbps。但正如我所说,它并不适用于所有情况。

0 个答案:

没有答案