2011-06-08 1 views
5

Je souhaite effectuer une modulation FSK via le port audio. Donc, le problème est que mon onde sinusale n'est pas très bonne. Il est dérangé par des parties égales. J'ai utilisé le code original de http://marblemice.blogspot.com/2010/04/generate-and-play-tone-in-android.html avec la modification supplémentaire de Playing an arbitrary tone with Android et https://market.android.com/details?id=re.serialout&feature=search_result. Alors, où est l'échec? Qu'est-ce que j'ai tort?Modulation FSK et lecture de la sinusoïde dans Android

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

J'ai trouvé l'erreur. setLoopPoints (0, longueur/2, -1) mais je ne sais pas pourquoi c'est correct. –

+1

N'avez pas encore regardé votre code, mais je suppose que lorsque vous dites "onde sinusoïdale", vous voulez dire "onde sinusoïdale". Au début, je pensais que ça avait quelque chose à voir avec ton nez. :-) –

+0

Que voulez-vous dire par "perturbé par des parties égales"? –

Répondre

1

La réponse est de changer de MODE_STATIC à MODE_STREAM et ne pas utiliser des points Looping. Dans un nouveau thread avec une faible priorité, une boucle occupée écrit les pistes.

Questions connexes