使用 Android 的 AudioTrack 类生成正弦波、方波、三角波、锯齿波音频信号

发布于 2024-09-28 14:18:33 字数 1197 浏览 0 评论 0原文

我的目标是使用 Android 的 AudioTrack 生成正弦方波三角波锯齿波信号> 类。

我根据教程进行了尝试。它的工作效果相对较好,尽管我并不总是确定生成的频率与目标频率的对应程度有多准确。

我想做正方形、三角形等函数。本教程仅实现正弦。因此,我的问题是:

示例究竟如何工作

  new Thread( new Runnable( ) 
  {
     public void run( )
     {                
        final float frequency = 440;
        float increment = (float)(2*Math.PI) * frequency / 44100; // angular increment for each sample
        float angle = 0;
        AudioDevice device = new AndroidAudioDevice( );
        float samples[] = new float[1024];

        while( true )
        {
           for( int i = 0; i < samples.length; i++ )
           {
              samples[i] = (float)Math.sin( angle );
              angle += increment;
           }

           device.writeSamples( samples );
        }            
     }
  } ).start();

我可以将正弦变成如下所示的正方形(使用符号函数)吗?

samples[i] = (float)Math.signum(Math.sin( angle ));

基本上,我想从根本上了解正在编写的示例,这样我就可以生成各种信号并最终叠加它们。

谢谢您的宝贵时间!

My objective is to generate sine, square, triangle and sawtooth signals with Android's AudioTrack Class.

I have made an attempt based on a tutorial. It works relatively well, although I'm not always sure how accurate the frequency generated corresponds with the frequency aimed at.

I would like to make square, triangle etc. functions. The tutorial only implements sine. My question is therefore:

How exactly does the sample work

  new Thread( new Runnable( ) 
  {
     public void run( )
     {                
        final float frequency = 440;
        float increment = (float)(2*Math.PI) * frequency / 44100; // angular increment for each sample
        float angle = 0;
        AudioDevice device = new AndroidAudioDevice( );
        float samples[] = new float[1024];

        while( true )
        {
           for( int i = 0; i < samples.length; i++ )
           {
              samples[i] = (float)Math.sin( angle );
              angle += increment;
           }

           device.writeSamples( samples );
        }            
     }
  } ).start();

Can I make the sine into a square like follows (using the signum function)?

samples[i] = (float)Math.signum(Math.sin( angle ));

Basically, I would like to fundamentally understand the samples being written, so I can generate various signals and also eventually superposition them.

Thank you for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇柔作态 2024-10-05 14:18:33

我知道这已经有一年了,但是……是的,signum 应该可以正常工作。放弃角度和增量业务。

该代码叠加了两个信号——不同频率的正方形和三角形。

int rate = 44100;
AudioDevice device = new AndroidAudioDevice( );
float samples[] = new float[1024];

while( true )
{
    for( int i = 0; i < samples.length; i++ )
    {
        samples[i] = (float)(0.5*Math.signum(Math.sin(550*2*Math.PI*i/rate))+0.5*Math.asin(Math.sin(450*2*Math.PI*i/rate)));
    }
    device.writeSamples( samples );
}

顺便说一句,更好的平方方法可能是: ((550.0*2*i/rate)%2<0?-1:1)

I know this is a year old but... Yes, signum should work fine. Ditch the angle and incrementing business.

This code superimposes two signals - a square and a triangle at different frequencies.

int rate = 44100;
AudioDevice device = new AndroidAudioDevice( );
float samples[] = new float[1024];

while( true )
{
    for( int i = 0; i < samples.length; i++ )
    {
        samples[i] = (float)(0.5*Math.signum(Math.sin(550*2*Math.PI*i/rate))+0.5*Math.asin(Math.sin(450*2*Math.PI*i/rate)));
    }
    device.writeSamples( samples );
}

Btw, a better way to do square is probably: ((550.0*2*i/rate)%2<0?-1:1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文