在Java/Android中生成锯齿音 [英] Generate Sawtooth Tone in Java/Android

查看:116
本文介绍了在Java/Android中生成锯齿音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是制作了一个音调发生器,就能够找到产生指定频率的正弦波的代码:

I'm just making a tone generator and I was able to find code that makes a sine wave of a specified frequency:

private void genTone(){
    // fill out the array
    for (int i = 0; i < numSamples; ++i) {
        sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/freqOfTone));
    }

    // convert to 16 bit pcm sound array
    // assumes the sample buffer is normalised.
    int idx = 0;
    for (final double dVal : sample) {
        // scale to maximum amplitude
        final short val = (short) ((dVal * 32767));
        // in 16 bit wav PCM, first byte is the low order byte
        generatedSnd[idx++] = (byte) (val & 0x00ff);
        generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);

    }
}

private void playSound(){
    final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
            AudioTrack.MODE_STATIC);
    audioTrack.write(generatedSnd, 0, generatedSnd.length);
    audioTrack.play();
}

我从Wikipedia知道,锯齿音的公式是:

And I know from Wikipedia, the formula for a sawtooth tone is:

x(t) = t - floor(t);

如何将其实现到genTone函数中?确实,使我无法自己弄清这件事的是频率如何与周期相关(除非我错过了某些东西,否则周期不像频率那样涉及时间,除非我错过了一些东西.我对声音科学不太满意.)

How would I implement that into the genTone function? Really the thing that keeps me from figuring this out on my own is how frequency can be related to period (period doesn't involve time like frequency does unless I'm missing something. I'm not too great with sound science.)

谢谢您的帮助!

推荐答案

Period = 1/frequency,如果可以帮助您解决的话.

Period= 1/frequency, if that helps you figure it out.

您可以将其替换为样本生成表达式:

You can substitute this for your sample generation expression:

sample[i]= 2*(i%(sampleRate/freqOfTone))/(sampleRate/freqOfTone)-1;

这里是示例. 代码改编自此处

Here is an example of how it works. Code is adapted from here

这篇关于在Java/Android中生成锯齿音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆