弦音cp愁凑:Android中的 FSK调制和播放正弦音调

我想在音频端口上做一些 FSK 调制。所以问题是我的正弦波不是很好。它被偶数部分干扰。我使用了://marblemice.blogspot/2010/04/generate-and-play-tone-in-android.html的原始代码,并进一步修改了Playing an arbitrary tone with Androids://market.android/details?id=re.serialout&feature=search_result。那么故障在哪里?我错了什么?

private static int bitRate=300;
private static int sampleRate=48000;
private static int freq1=600;
public static void loopOnes(){
    playque.add(UARTHigh);
    athread.interrupt();
}
    private static byte[] UARTHigh() {
    int numSamples=sampleRate/bitRate;
    double sample[]=new double[numSamples];
    byte[] buffer=new byte[numSamples*2];
    for(int i=0; i<numSamples;++i){
        sample[i]=Math.sin(2*Math.PI*i*freq1/sampleRate);
    }
    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
        buffer[idx++] = (byte) (val & 0x00ff);
        buffer[idx++] = (byte) ((val & 0xff00) >>> 8);
    }
    return buffer;
}
private static void playSound(){
    active = true;
    while(active)
    {
        try {Thread.sleep(Long.MAX_VALUE);} catch (InterruptedException e) {
            while (playque.isEmpty() == false)
            {
                if (atrk != null)
                {
                    if (generatedSnd != null)
                    {   
                        // Das letzte Sample erst fertig abspielen lassen
                        // systemClock.sleep(xx) xx könnte angepasst werden
                        while (atrk.getPlaybackHeadPosition() < (generatedSnd.length))
                            SystemClock.sleep(50);  // let existing sample finish first: this can probably be set to a smarter number using the information above
                    }
                    atrk.release();
                }
                UpdateParameters(); // might as well do it at every iteration, it's cheap
                generatedSnd = playque.poll();
                length = generatedSnd.length;
                if (minbufsize<length)
                    minbufsize=length;
                atrk = new AudioTrack(AudioManager.STREAM_MUSIC,
                        sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                        AudioFormat.ENCODING_PCM_16BIT, minbufsize,
                        AudioTrack.MODE_STATIC);
                atrk.setStereoVolume(1,1);
                atrk.write(generatedSnd, 0, length); 
                atrk.play();    
            }
            // Playque is Empty =>send StopBit!
            // Set Loop Points
            int setLoopError=atrk.setLoopPoints(0, length, -1);
            atrk.play();
        }
    }
}

}

1

因此,答案是从 MODE_STATIC 更改为 MODE_STREAM,并且不使用循环点。在具有低优先级的新线程中,忙循环写入轨道。

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(521)
新ces学法是真是假:什么是假脑袋 (dummy head)
上一篇
淬火的淬是cui还是zhan:尝试修改AutoCADCUI宏命令
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(73条)