/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2013 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU General Public License, as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any
 * later version. Please see the file LICENSE-GPL for details.
 *
 * Web Page: http://mielke.cc/brltty/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

package org.a11y.brltty.android;

import android.util.Log;

import android.media.AudioManager;
import android.media.AudioFormat;
import android.media.AudioTrack;

public final class PcmDevice {
  private static final String LOG_TAG = PcmDevice.class.getName();

  private static final int streamType = AudioManager.STREAM_NOTIFICATION;
  private static final int trackMode = AudioTrack.MODE_STREAM;

  private int bufferSize = 0X1000;
  private int sampleRate = AudioTrack.getNativeOutputSampleRate(streamType);
  private int channelConfiguration = AudioFormat.CHANNEL_OUT_MONO;
  private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

  private AudioTrack audioTrack = null;

  private AudioTrack newAudioTrack () {
    return new AudioTrack(streamType, sampleRate, channelConfiguration,
                          audioFormat, bufferSize, trackMode);
  }

  public int getBufferSize () {
    return bufferSize;
  }

  public int getSampleRate () {
    return sampleRate;
  }

  public int getChannelCount () {
    switch (channelConfiguration) {
      default:
      case AudioFormat.CHANNEL_OUT_MONO:
        return 1;

      case AudioFormat.CHANNEL_OUT_STEREO:
        return 2;
    }
  }

  public boolean write (short[] samples) {
    if (audioTrack == null) {
      audioTrack = newAudioTrack();
      audioTrack.play();
    }

    Log.d(LOG_TAG, "track sample count: " + samples.length);
    Log.d(LOG_TAG, "track play state: " + audioTrack.getPlayState());
    return audioTrack.write(samples, 0, samples.length) == AudioTrack.SUCCESS;
  }

  public void close () {
    if (audioTrack != null) {
      audioTrack.pause();
      audioTrack.flush();
      audioTrack.release();
      audioTrack = null;
    }
  }

  public PcmDevice () {
  }
}

@@ -1031,17 +1031,16 @@
 STREAM;%0A
-%0A
   privat
@@ -1045,119 +1045,66 @@
 ate 
-int bufferSize = 0X1000;%0A  private int sampleRate = AudioTrack.getNativeOutputSampleRate(streamType);%0A  private
+static final int sampleRate = 8000;%0A  private static final
  int
@@ -1167,16 +1167,29 @@
 private 
+static final 
 int audi
@@ -1229,16 +1229,64 @@
 M_16BIT;
+%0A  private static final int bufferSize = 0X2000;
 %0A%0A  priv
@@ -1504,68 +1504,8 @@
  %7D%0A%0A
-  public int getBufferSize () %7B%0A    return bufferSize;%0A  %7D%0A%0A
   pu
@@ -1769,32 +1769,92 @@
 n 2;%0A    %7D%0A  %7D%0A%0A
+  public int getBufferSize () %7B%0A    return bufferSize;%0A  %7D%0A%0A
   public boolean
@@ -1903,32 +1903,24 @@
 ack == null)
- %7B%0A     
  audioTrack 
@@ -1942,248 +1942,117 @@
 ();%0A
+%0A
     
-  audioTrack.play();%0A    %7D%0A%0A    Log.d(LOG_TAG, %22track sample count: %22 + samples.length);%0A    Log.d(LOG_TAG, %22track play state: %22 + audioTrack.getPlayState());%0A    return audioTrack.write(samples, 0, samples.length) == AudioTrack.SUCCESS
+int result = audioTrack.write(samples, 0, samples.length);%0A    audioTrack.play();%0A    return result %3E= 0
 ;%0A  
@@ -2073,12 +2073,13 @@
 id c
-lose
+ancel
  () 
@@ -2076,36 +2076,32 @@
 cancel () %7B%0A    
-if (
 audioTrack != nu
@@ -2098,22 +2098,18 @@
 rack
- != null) %7B%0A  
+.pause();%0A
     
@@ -2123,45 +2123,61 @@
 ack.
-pause
+flush
 ();%0A  
-    audioTrack.flush();%0A  
+%7D%0A%0A  public void close () %7B%0A    cancel();%0A
     
@@ -2198,26 +2198,24 @@
 ease();%0A    
-  
 audioTrack =
@@ -2221,22 +2221,16 @@
 = null;%0A
-    %7D%0A
   %7D%0A%0A  p

Fix problems with Android PCM support. (dm)