首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java DSP synth奇怪的行为

java DSP synth奇怪的行为
EN

Stack Overflow用户
提问于 2013-02-14 18:52:44
回答 1查看 167关注 0票数 1

我正在尝试使用javax.sound.sampled.SourceDataLine播放保存在字节数组中的信号。我正在试着开始播放一个简单的正弦波。对于某些频率(例如1000 am,400 am),它工作得很好,但对于其他频率(1001,440),我只能得到几乎没有音调的嗡嗡声。采样率绝对足够高,可以防止混叠(16 The )。有什么想法吗?干杯。

代码语言:javascript
复制
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;

public class Player
{

    private static float SAMPLE_RATE = 16000;

    public static void main(String[] args)
    {
        playSound();
    }


    private static void playSound()
    {
        try
        {
            final AudioFormat audioFormat = new AudioFormat( SAMPLE_RATE, 8, 1, true,  true );
            SourceDataLine line = AudioSystem.getSourceDataLine( audioFormat );
            line.open( audioFormat );
            line.start();
        /* the last argument here is the frequency in Hz. works well with 1000, but with 1001 I get week and pitchless clicking sound sound*/
            byte[] signal = smpSin( 1, 1, 1000 );
            play( line, signal );
            line.drain();
            line.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    private static byte[] smpSin(double lenInSec, double amp, double signalFreq)
    {
        int len = (int)(SAMPLE_RATE * lenInSec);
        byte[] out = new byte[len];
        for (int i = 0; i < out.length; i++)
        {
            out[i] = (byte)(amp * Math.sin( ((2.0 * Math.PI * signalFreq) *  ((double)i)) / SAMPLE_RATE ));
        }
        return out;
    }

    private static void play(SourceDataLine line, byte[] array)
    {
        line.write( array, 0, array.length );
    }

}

EN

回答 1

Stack Overflow用户

发布于 2013-02-16 09:34:33

您没有在缓冲区调用之间保存正弦波的相位。因此,任何阶段的中断都会在调用play()的速率上引起嗡嗡声。没有嗡嗡声的频率恰好在默认的开始阶段结束。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14873143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档