/ime/latinime/src/com/googlecode/eyesfree/inputmethod/voice/WaveformImage.java

http://eyes-free.googlecode.com/ · Java · 90 lines · 57 code · 10 blank · 23 comment · 8 complexity · ca1b0656f1eaf56322ce8383a7de0ffa MD5 · raw file

  1. /*
  2. * Copyright (C) 2008-2009 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.googlecode.eyesfree.inputmethod.voice;
  17. import android.graphics.Bitmap;
  18. import android.graphics.Canvas;
  19. import android.graphics.Paint;
  20. import java.io.ByteArrayOutputStream;
  21. import java.nio.ByteBuffer;
  22. import java.nio.ByteOrder;
  23. import java.nio.ShortBuffer;
  24. /**
  25. * Utility class to draw a waveform into a bitmap, given a byte array
  26. * that represents the waveform as a sequence of 16-bit integers.
  27. * Adapted from RecognitionActivity.java.
  28. */
  29. public class WaveformImage {
  30. private static final int SAMPLING_RATE = 8000;
  31. private WaveformImage() {}
  32. public static Bitmap drawWaveform(
  33. ByteArrayOutputStream waveBuffer, int w, int h, int start, int end) {
  34. final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  35. final Canvas c = new Canvas(b);
  36. final Paint paint = new Paint();
  37. paint.setColor(0xFFFFFFFF); // 0xRRGGBBAA
  38. paint.setAntiAlias(true);
  39. paint.setStrokeWidth(0);
  40. final ShortBuffer buf = ByteBuffer
  41. .wrap(waveBuffer.toByteArray())
  42. .order(ByteOrder.nativeOrder())
  43. .asShortBuffer();
  44. buf.position(0);
  45. final int numSamples = waveBuffer.size() / 2;
  46. final int delay = (SAMPLING_RATE * 100 / 1000);
  47. int endIndex = end / 2 + delay;
  48. if (end == 0 || endIndex >= numSamples) {
  49. endIndex = numSamples;
  50. }
  51. int index = start / 2 - delay;
  52. if (index < 0) {
  53. index = 0;
  54. }
  55. final int size = endIndex - index;
  56. int numSamplePerPixel = 32;
  57. int delta = size / (numSamplePerPixel * w);
  58. if (delta == 0) {
  59. numSamplePerPixel = size / w;
  60. delta = 1;
  61. }
  62. final float scale = 3.5f / 65536.0f;
  63. // do one less column to make sure we won't read past
  64. // the buffer.
  65. try {
  66. for (int i = 0; i < w - 1 ; i++) {
  67. final float x = i;
  68. for (int j = 0; j < numSamplePerPixel; j++) {
  69. final short s = buf.get(index);
  70. final float y = (h / 2) - (s * h * scale);
  71. c.drawPoint(x, y, paint);
  72. index += delta;
  73. }
  74. }
  75. } catch (IndexOutOfBoundsException e) {
  76. // this can happen, but we don't care
  77. }
  78. return b;
  79. }
  80. }