aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/voice/WaveformImage.java
blob: 08d87c8f30dd1f761cd55e9da9d54b0da19f94ab (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (C) 2008-2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.android.inputmethod.voice;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;

/**
 * Utility class to draw a waveform into a bitmap, given a byte array
 * that represents the waveform as a sequence of 16-bit integers.
 * Adapted from RecognitionActivity.java.
 */
public class WaveformImage {
    private static final int SAMPLING_RATE = 8000;

    private WaveformImage() {}

    public static Bitmap drawWaveform(
        ByteArrayOutputStream waveBuffer, int w, int h, int start, int end) {
        final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        final Canvas c = new Canvas(b);
        final Paint paint = new Paint();
        paint.setColor(0xFFFFFFFF); // 0xRRGGBBAA
        paint.setAntiAlias(true);
        paint.setStrokeWidth(0);

        final ShortBuffer buf = ByteBuffer
            .wrap(waveBuffer.toByteArray())
            .order(ByteOrder.nativeOrder())
            .asShortBuffer();
        buf.position(0);

        final int numSamples = waveBuffer.size() / 2;
        final int delay = (SAMPLING_RATE * 100 / 1000);
        int endIndex = end / 2 + delay;
        if (end == 0 || endIndex >= numSamples) {
            endIndex = numSamples;
        }
        int index = start / 2 - delay;
        if (index < 0) {
            index = 0;
        }
        final int size = endIndex - index;
        int numSamplePerPixel = 32;
        int delta = size / (numSamplePerPixel * w);
        if (delta == 0) {
            numSamplePerPixel = size / w;
            delta = 1;
        }

        final float scale = 3.5f / 65536.0f;
        // do one less column to make sure we won't read past
        // the buffer.
        try {
            for (int i = 0; i < w - 1 ; i++) {
                final float x = i;
                for (int j = 0; j < numSamplePerPixel; j++) {
                    final short s = buf.get(index);
                    final float y = (h / 2) - (s * h * scale);
                    c.drawPoint(x, y, paint);
                    index += delta;
                }
            }
        } catch (IndexOutOfBoundsException e) {
            // this can happen, but we don't care
        }

        return b;
    }
}