简体   繁体   English

混合和添加静音到音频Android / Java

[英]Mixing and Adding Silence to Audio Android/Java

I have 2 files. 我有2个档案。 Once is an mp3 being decoded to pcm into a stream and I have a wav being read into pcm also. 一次是将mp3解码为pcm成流,我也将wav读入pcm。 The samples are being held in a short data type. 样本以短数据类型保存。

Audio stats: 44,100 samples * 16 bits per sample * 2 channels = 1,411,200 bits/sec 音频状态:44,100个样本*每个样本16位* 2个通道= 1,411,200位/秒

I have X seconds of silence that I need to apply to the beginning of the mp3 pcm data and I am doing it like this: 我有X秒钟的沉默时间,我需要将其应用于mp3 pcm数据的开头,我正在这样做:

private short[] mp3Buffer = null;
private short[] wavBuffer = null;
private short[] mixedBuffer = null;

double silenceSamples = (audioInfo.rate * padding) * 2;
for (int i = 0; i < minBufferSize; i++){

    if (silenceSamples > 0 ){

        mp3Buffer[i] = 0; //Add 0 to the buffer as silence

        mixedBuffer[i] = (short)((mp3Buffer[i] + stereoWavBuffer[i])/2);  
        silenceSamples = silenceSamples - 0.5;
    }
    else
        mixedBuffer[i] = (short)((mp3Buffer[i] + stereoWavBuffer[i])/2);
}

The audio is always off. 音频始终关闭。 Sometimes its a second or two too fast, sometimes its a second or two too slow too slow. 有时一两秒太快,有时一两秒太慢太慢。 I dont think its a problem with the timing as I start the audiorecord(wav) first and then set a start timer->start mediaplayer(already prepared)->end timer and setting the difference to the "padding" variable. 我不认为这与计时有关,因为我先启动audiorecord(wav),然后设置启动计时器->启动mediaplayer(已准备好)->结束计时器,并将差异设置为“ padding”变量。 I am also skipping the 44kb when from the wav header. 从wav标头中,我也跳过了44kb。

Any help would be much appreciated. 任何帮助将非常感激。

I'm assuming you are wanting to align two sources of audio in some way by inserting padding at the start of one of the streams? 我假设您想通过在其中一个流的开头插入填充以某种方式对齐两个音频源? There are a few things wrong here. 这里有些错误。

mp3Buffer[i] = 0; //Add 0 to the buffer as silence

This is not adding silence to the beginning, is is just setting the entry at offest [i] in the array to 0. The next line: 这并不是在开始时增加沉默,只是将数组中最不重要的[i]项设置为0。下一行:

mixedBuffer[i] = (short)((mp3Buffer[i] + stereoWavBuffer[i])/2);

Then just overwrites this value. 然后只需覆盖此值。

If you are wanting to align the streams in some way, the best way to go about it is not to insert silence at the beginning of either stream, but to just begin mixing in one of the streams at an offset from the other. 如果您希望以某种方式对齐流,则实现此目的的最佳方法不是在任一流的开头插入静默,而只是开始在其中一个流中以彼此之间的偏移量混合。 Also it would be better to mix them into a 32 bit float and then normalise. 同样最好将它们混合成32位浮点数,然后进行标准化。 Something like: 就像是:

    int silenceSamples = (audioInfo.rate * padding) * 2;
            float[] mixedBuffer = new float[minBufferSize + silenceSamples]
    for (int i = 0; i < minBufferSize + silenceSamples; i++){

    if (i < silenceSamples )
    {       
        mixedBuffer[i] = (float) stereoWavBuffer[i];  
    }
    else if(i < minBufferSize)
    {
        mixedBuffer[i] = (float) (stereoWavBuffer[i] + mp3Buffer[i-silenceSamples]);
    }
    else 
    {
        mixedBuffer[i] = (float) (mp3Buffer[i-silenceSamples]);
    }

To normalise the data you need to run through the mixedBuffer and find the absolute largest value Math.abs(...) , and then multiple all the values in the array by 32,767/largestValue - this will give you a buffer where the largest value fits back into a short without clipping. 为了规范化数据,您需要运行mixedBuffer并找到绝对最大值Math.abs(...) ,然后将数组中的所有值乘以32,767 / largestValue-这将为您提供最大值的缓冲区放回短裤而不会剪裁。 Then iterate through your float array moving each value back into a short array. 然后遍历您的float数组,将每个值移回短数组。

I'm not sure what your minBufferSize is - this will need to be large enough to get all your data mixed. 我不确定您的minBufferSize是什么-它需要足够大才能混合所有数据。

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

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