AudioTrack 没有声音并带有音调生成
我正在尝试使用以下代码让 Android 的 AudioTrack 播放方波
public Synthesizer() {
bufferSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
player = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize,
AudioTrack.MODE_STREAM);
}
public void writeSamples(byte[] samples) {
fillBuffer(samples);
player.write(buffer, 0, samples.length);
}
private void putBuffer(byte[] samples) {
if (buffer.length < samples.length)
buffer = new byte[samples.length];
for (int i = 0; i < samples.length; i++)
buffer[i] = samples[i];
即使样本将为负数,其他样本也会为正数,以获得方波:
btnPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int frequency = 44100;
byte[] sample = new byte[frequency];
for (int i = 0; i < frequency; i++) {
if (i % 2 == 0) {
sample[i] = Byte.MAX_VALUE;
}
else {
sample[i] = Byte.MIN_VALUE;
}
}
squareSynth.writeSamples(sample);
不幸的是,我根本听不到声音,我已经检查了音量,但无法'甚至不会产生静电或裂纹。我觉得这很奇怪。有人知道如何修复它吗?
I'm trying to get Android's AudioTrack play a squarewave with the following code
public Synthesizer() {
bufferSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
player = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize,
AudioTrack.MODE_STREAM);
}
public void writeSamples(byte[] samples) {
fillBuffer(samples);
player.write(buffer, 0, samples.length);
}
private void putBuffer(byte[] samples) {
if (buffer.length < samples.length)
buffer = new byte[samples.length];
for (int i = 0; i < samples.length; i++)
buffer[i] = samples[i];
Even samples will be negative, the others will be positive, to get a square wave:
btnPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int frequency = 44100;
byte[] sample = new byte[frequency];
for (int i = 0; i < frequency; i++) {
if (i % 2 == 0) {
sample[i] = Byte.MAX_VALUE;
}
else {
sample[i] = Byte.MIN_VALUE;
}
}
squareSynth.writeSamples(sample);
Unfortunately I get no sound at all, i've checked my volume but couldn't even get static or some crackle. I find this very strange. Anyone knows how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论