gpt4 book ai didi

android - 使用 Android 的 AudioTrack 组合声音样本的字节会产生噪音

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:13:24 27 4
gpt4 key购买 nike

我正在构建一个相当简单的 Android 应用程序(sdk 修订版 14:ICS),它允许用户一次选择两个音频剪辑(都是 RIFF/WAV 格式,little-endian,签名的 PCM-16 位编码)和以各种方式组合它们以创造新的声音。我用于此组合的最基本方法如下:

//...sound samples are read in to memory as raw byte arrays elsewhere
//...offset is currently set to 45 so as to skip the 44 byte header of basic
//RIFF/WAV files
...
//Actual combination method
public byte[] makeChimeraAll(int offset){
for(int i=offset;i<bigData.length;i++){
if(i < littleData.length){
bigData[i] = (byte) (bigData[i] + littleData[i]);
}
else{
//leave bigData alone
}
}
return bigData;
}

然后可以通过 AudioTrack 类播放返回的字节数组:

....
hMain.setBigData(hMain.getAudioTransmutation().getBigData()); //set the shared bigData
// to the bigData in AudioTransmutation object
hMain.getAudioProc().playWavFromByteArray(hMain.getBigData(), 22050 + (22050*
(freqSeekSB.getProgress()/100)), 1024); //a SeekBar allows the user to adjust the freq
//ranging from 22050 hz to 44100 hz
....
public void playWavFromByteArray(byte[] audio,int sampleRate, int bufferSize){
int minBufferSize = AudioTrack.getMinBufferSize(sampleRate,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,
minBufferSize, AudioTrack.MODE_STREAM);

int i = 0;

at.play();
at.write(audio, 0, audio.length);
at.stop();
at.release();

for(i=0;i<audio.length;i++){
Log.d("me","the byte value at audio index " + i + " is " + audio[i]);
}

}

使用上面的代码组合和播放的结果接近我想要的(两个样本在生成的混合声音中仍然可以辨别)但也有很多裂纹、砰砰声和其他噪音。

那么,三个问题:首先,我是否正确使用了 AudioTrack?其次,字节顺序在 AudioTrack 配置中的什么位置?声音本身播放得很好,听起来几乎就像我在组合时所期望的那样,所以 RIFF/WAV 格式的小端性质似乎在某处传达,但我不确定在哪里。最后,对于带符号的 16 位 PCM 编码,我应该期望看到的字节值范围是多少?我希望在上面的 Log.d(...) 调用的 logcat 中看到从 −32768 到 32767 的值,但结果往往在 -100 到 100 的范围内(有一些异常值超出该范围)。超过 16 位范围的组合字节值是否可以解释噪声?

谢谢,CCJ

更新:主要感谢 Bjorne Roche 和 William the Coderer!我现在读入音频数据到 short[] 结构,DataInputStream 的字节顺序是使用 William 的 EndianInputStream (http://stackoverflow.com/questions/8028094/java-datainputstream-replacement-for-endianness) 和组合方法已更改为:

//Audio Chimera methods!
public short[] makeChimeraAll(int offset){
//bigData and littleData are each short arrays, populated elsewhere
int intBucket = 0;
for(int i=offset;i<bigData.length;i++){
if(i < littleData.length){
intBucket = bigData[i] + littleData[i];
if(intBucket > SIGNED_SHORT_MAX){
intBucket = SIGNED_SHORT_MAX;
}
else if (intBucket < SIGNED_SHORT_MIN){
intBucket = SIGNED_SHORT_MIN;
}
bigData[i] = (short) intBucket;
}
else{
//leave bigData alone
}
}
return bigData;
}

经过这些改进后的混合音频输出质量非常棒!

最佳答案

我不熟悉android音频,所以我不能回答你所有的问题,但我可以告诉你根本问题是什么:逐字节添加音频数据是行不通的。由于它有点工作,并且通过查看您的代码以及它最常见的事实,我将假设您有 16 位 PCM 数据。然而在任何地方,你都在处理字节。字节不适合处理音频(除非音频恰好是 8 位)

字节大约为 +/- 128。您说“我希望在上面的 Log.d(...) 调用的 logcat 中看到从 −32768 到 32767 的值,但结果往往在-100 到 100 的范围(还有一些异常值)”那么,当您从字节数组中打印值时,您怎么可能达到该范围? 16 位有符号数据的正确数据类型是短整型,而不是字节。如果您打印短值,您会看到预期的范围。

您必须将字节转换为短裤并对短裤求和。这将处理您听到的大部分杂音。但是,既然您正在阅读文件,为什么还要转换呢?为什么不使用类似这样的方法将其作为短片从文件中读出 http://docs.oracle.com/javase/1.4.2/docs/api/java/io/DataInputStream.html#readShort ()

下一个问题是您必须处理超出范围的值,而不是让它们“环绕”。最简单的解决方案是简单地将总和作为整数,“剪辑”到短范围内,然后存储剪辑后的输出。这将消除您的咔哒声和砰砰声。

在伪代码中,整个过程看起来像这样:

file1 = Open file 1
file2 = Open file 2
output = Open output for writing

numSampleFrames1 = file1.readHeader()
numSampleFrames2 = file2.readHeader()
numSampleFrames = min( numSampleFrames1, numSampleFrames2 )
output.createHeader( numSampleFrames )

for( int i=0; i<numSampleFrames * channels; ++i ) {
//read data from file 1
int a = file1.readShort();
//read data from file 2, and add it to data we read from file 1
a += file2.readShort();
//clip into range
if( a > Short.MAX_VALUE )
a = Short.MAX_VALUE;
if( a < Short.MIN_VALUE )
a = Short.MIN_VALUE;
//write it to the output
output.writeShort( (Short) a );
}

您会在“裁剪”步骤中得到一点失真,但没有简单的解决方法,而且裁剪比环绕要好得多。 (也就是说,除非你的音轨非常“热”,并且低频很重,否则失真应该不会太明显。如果这是一个问题,你可以做其他事情:例如将 a 乘以 .5 然后跳过剪裁,但随后您的输出会安静得多,这在手机上可能不是您想要的)。

关于android - 使用 Android 的 AudioTrack 组合声音样本的字节会产生噪音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11000933/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com