使用AudioTrack for Android解释怪异行为背后的原因

时间:2017-04-13 10:46:18

标签: android audiotrack

我正在尝试使用AudioTrack输出生成的声波。然而,产生的波浪并不相同,而且我已经完成了可能存在问题的想法。请参阅下面的附图。

我还没有尝试在其他Android上运行我的代码,所以我不能排除这只是这款手机的问题(Nexus 5 LG)。从iPhone播放相同的样本并使用此手机录制它会产生类似于下面大胆中所示的正确波形。

Audacity showing the generated sample

Recording of the sound that is produced by the phone

在这里可以看到波的第一部分是如何被正确再现的,然而,在峰值之后不久它似乎立即消失。如果信号的长度增加,则波形被正确再现,但是我也无法解释。

相关代码:

public static final int SAMPLE_RATE = 44100;
static final int AUDIOTRACK_BUFFER_SIZE = SAMPLE_RATE;

audioTrackSend = new AudioTrack(AudioManager.STREAM_MUSIC,
            SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, AUDIOTRACK_BUFFER_SIZE,
            AudioTrack.MODE_STATIC);
short pcm16bit[];
short flushBuffer = new short[AUDIOTRACK_BUFFER_SIZE];
pcm16bit = Encode.encodeData(frequency, pattern, SAMPLE_RATE);
// This is the same array that I print to a file and import into audacity. Therefore everything "should" be correct up to this part
Arrays.fill(flushBuffer, (short) 0);
// I noticed that previous sounds were still in the buffer during consecutive playbacks. After adding this that behavior disappeared. If there is a better way of accomplishing that I'm all ears.
audioTrackSend.write(flushBuffer, 0, flushBuffer.length);
audioTrackSend.write(pcm16bit, 0, pcm16bit.length);
audioTrackSend.play()

如果有更多信息需要回答这个问题,请询问。

由于

0 个答案:

没有答案