2011-06-08 60 views
5

我想通過音頻端口進行一些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的進一步修改。 那麼失敗在哪裏?我錯了什麼?Android中的FSK調製和播放正弦波音

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(); 
     } 
    } 
} 

}

+0

我發現失敗。 setLoopPoints(0,length/2,-1)但我不知道爲什麼這是正確的。? – 2011-06-08 12:44:09

+1

還沒有看過你的代碼,但我認爲當你說「竇波」時,你的意思是「正弦波」。起初我認爲這與你的鼻子有關。 :-) – 2011-06-08 15:52:56

+0

你是什麼意思「受偶數部件干擾」? – 2011-06-08 16:02:25

回答

1

因此,答案是從MODE_STATIC改變MODE_STREAM,不使用循環點。在低優先級的新線程中,忙碌循環寫入音軌。