在我的代码中生成后,轨道变得非常慢

时间:2019-02-08 22:35:05

标签: android audio

什么会使本代曲目变得太慢?

基本上,我正在创建音频格式为“ wav”的音轨。

但是完成曲目后,它处于慢动作状态。

什么可以设置曲目的速度?

 resume of the code:

    for (int i = 0; i < output.length; i++) 
    {
        if ( i <= arrayMusic1.length - 1)
            samplef1 = arrayMusic1[i] / 128.0f; 

        if ( i <= arrayMusic2.length - 1)
            samplef2 = arrayMusic2[i] / 128.0f;

        output[i]          = outputSample;  

     } 

public void saveToFile(byte[] output, String globalName, String nameMix) 
{
        long mySubChunk1Size = 16;
        int myBitsPerSample  = 16;
        int myFormat         = 1;
        long myChannels      = 1;
        long mySampleRate    = 44400;
        long myByteRate      = mySampleRate * myChannels * myBitsPerSample /8;
        int myBlockAlign     = (int) (myChannels * myBitsPerSample/8);
        //long teste           = output.length;
        long myDataSize      = output.length - 100000;//output.length  //aqui ta o problema
        long myChunk2Size    = myDataSize * myChannels * myBitsPerSample/8;
        long myChunkSize     = 36 + myChunk2Size;

        OutputStream os = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+ "/ssmultitrackPlus/"+globalName.trim()+"/"+nameMix+".wav"));
        BufferedOutputStream bos = new BufferedOutputStream(os);
        DataOutputStream outFile = new DataOutputStream(bos);

        outFile.writeBytes("RIFF");                                     // 00 - RIFF
        outFile.write(intToByteArray((int)myChunkSize), 0, 4);          // 04 - how big is the rest of this file?
        outFile.writeBytes("WAVE");                                     // 08 - WAVE
        outFile.writeBytes("fmt ");                                     // 12 - fmt 
        outFile.write(intToByteArray((int)mySubChunk1Size), 0, 4);      // 16 - size of this chunk
        outFile.write(shortToByteArray((short)myFormat), 0, 2);         // 20 - what is the audio format? 1 for PCM = Pulse Code Modulation
        outFile.write(shortToByteArray((short)myChannels), 0, 2);       // 22 - mono or stereo? 1 or 2?  (or 5 or ???)
        outFile.write(intToByteArray((int)mySampleRate), 0, 4);         // 24 - samples per second (numbers per second)
        outFile.write(intToByteArray((int)myByteRate), 0, 4);           // 28 - bytes per second
        outFile.write(shortToByteArray((short)myBlockAlign), 0, 2);     // 32 - # of bytes in one sample, for all channels
        outFile.write(shortToByteArray((short)myBitsPerSample), 0, 2);  // 34 - how many bits in a sample(number)?  usually 16 or 24
        outFile.writeBytes("data");                                     // 36 - data
        outFile.write(intToByteArray((int)myDataSize), 0, 4);           // 40 - how big is this data chunk
        outFile.write(output);                                          // 44 - the actual data itself - just a long string of numbers

        outFile.flush();
        outFile.close();
}

1 个答案:

答案 0 :(得分:1)

字节率必须为mySampleRate*myChannels*2,并且写入字节率桅杆的功能应类似于:

writeInt(output, mySampleRate*2); // byte rate because 16bit and 1 channel

public static void writeInt(final DataOutputStream output, final int value) throws IOException {
    output.write(value >> 0);
    output.write(value >> 8);
    output.write(value >> 16);
    output.write(value >> 24);
}

标准采样率音频CD的质量为44100。如果音频减慢而没有“爆米花”之类的假象,则标题中的采样率或字节率存在问题。您可能会看到实际使用诸如GHex之类的二进制/十六进制读取器/写入器应用程序记录的内容。对于PCM,也应选择1。