Android中的FSK调制和播放正弦音

时间:2011-06-08 12:35:27

标签: android audio frequency

我想通过音频端口进行一些FSK调制。所以问题是我的窦波不是很好。偶数部分会扰乱它。我使用了http://marblemice.blogspot.com/2010/04/generate-and-play-tone-in-android.html中的原始代码以及Playing an arbitrary tone with Androidhttps://market.android.com/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 个答案:

答案 0 :(得分:1)

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