AudioTrack 没有声音并带有音调生成

发布于 2024-12-05 05:52:24 字数 1374 浏览 1 评论 0原文

我正在尝试使用以下代码让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文