使用 Android 的 AudioTrack 类生成正弦波、方波、三角波、锯齿波音频信号
我的目标是使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这已经有一年了,但是……是的,signum 应该可以正常工作。放弃角度和增量业务。
该代码叠加了两个信号——不同频率的正方形和三角形。
顺便说一句,更好的平方方法可能是:
((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.
Btw, a better way to do square is probably:
((550.0*2*i/rate)%2<0?-1:1)