5

I want to do some FSK Modulation over the audio port. So the problem is that my sinus wave isn't very good. It is disturb by even parts. I used the code original from http://marblemice.blogspot.com/2010/04/generate-and-play-tone-in-android.html with the further modification from Playing an arbitrary tone with Android and https://market.android.com/details?id=re.serialout&feature=search_result . So where is the failure? What do I wrong?

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

}

Community
  • 1
  • 1
Ingo Nieur
  • 61
  • 1
  • 5
  • I found the failure. setLoopPoints(0,length/2,-1) but I don't know why this correct.? – Ingo Nieur Jun 08 '11 at 12:44
  • 1
    Haven't looked at your code yet, but I assume when you say "sinus wave" you mean "sine wave". At first I thought it had something to do with your nose. :-) – Jim Clay Jun 08 '11 at 15:52
  • What do you mean by "disturbed by even parts"? – Jim Clay Jun 08 '11 at 16:02
  • oh yeah, I mean sine wave. I already found a solution for this problem. I mean with "disturbed by even parts" that if I watch the output at a scope there is a sine wave, a line and that afterwards a sine wave. The problem was that I used MODE_STATIC and for loading new data the output goes to a line. – Ingo Nieur Jun 16 '11 at 10:43
  • How can I change the title of my question? – Ingo Nieur Jun 16 '11 at 10:49
  • 1
    @Ingo I don't know if you can change the title or not. Try the "edit" link at the bottom of the question. Also, it would be good if "answered" your own question with the solution and then accepted the answer. – Jim Clay Jun 16 '11 at 15:45

1 Answers1

1

So the answer is to change from MODE_STATIC to MODE_STREAM and don't use Looping Points. In a new thread with low priority a busy loop writes the tracks.

Ingo Nieur
  • 61
  • 1
  • 5