首页 > 解决方案 > 在我的代码中生成轨道后变得非常慢

问题描述

是什么让这一代的赛道变得太慢了?

基本上我正在创建音频格式的轨道:“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();
}

标签: androidaudio

解决方案


Byterate 必须是mySampleRate*myChannels*2并且编写 byterate mast 的函数如下所示:

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。


推荐阅读