Visualization3RS.java revision 4341cb656df71c56ea15d17d0119cbb4407a6858
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.musicvis.vis3;
18
19import com.android.musicvis.GenericWaveRS;
20import com.android.musicvis.R;
21import com.android.musicvis.RenderScriptScene;
22import com.android.musicvis.AudioCapture;
23
24import android.graphics.Canvas;
25import android.graphics.Rect;
26import android.os.Handler;
27import android.renderscript.Allocation;
28import android.renderscript.Element;
29import android.renderscript.Mesh.Primitive;
30import android.renderscript.ProgramVertex;
31import android.renderscript.ScriptC;
32import android.renderscript.Type;
33import android.renderscript.Element.Builder;
34import android.util.Log;
35import android.view.SurfaceHolder;
36
37import java.util.TimeZone;
38
39class Visualization3RS extends GenericWaveRS {
40
41    private short [] mAnalyzer = new short[512];
42
43    float lastOffset;
44
45    Visualization3RS(int width, int height) {
46        super(width, height, R.drawable.ice);
47        lastOffset = 0;
48    }
49
50    @Override
51    public void setOffset(float xOffset, float yOffset, int xPixels, int yPixels) {
52        mWorldState.yRotation = (xOffset * 4) * 360;
53        updateWorldState();
54    }
55
56    @Override
57    public void start() {
58        if (mAudioCapture == null) {
59            mAudioCapture = new AudioCapture(AudioCapture.TYPE_FFT, 512);
60        }
61        super.start();
62    }
63
64    @Override
65    public void stop() {
66        super.stop();
67        if (mAudioCapture != null) {
68            mAudioCapture.release();
69            mAudioCapture = null;
70        }
71    }
72
73    @Override
74    public void update() {
75
76        int len = 0;
77        if (mAudioCapture != null) {
78            mVizData = mAudioCapture.getFormattedData(64, 1);
79            len = mVizData.length;
80        }
81        if (len == 0) {
82            if (mWorldState.idle == 0) {
83                mWorldState.idle = 1;
84                updateWorldState();
85            }
86            return;
87        }
88
89        if (len > mAnalyzer.length) len = mAnalyzer.length;
90
91        if (mWorldState.idle != 0) {
92            mWorldState.idle = 0;
93            updateWorldState();
94        }
95
96        for (int i = 0; i < len; i++) {
97            short newval = (short)(mVizData[i] * (i/16+2));
98            short oldval = mAnalyzer[i];
99            if (newval >= oldval - 800) {
100                // use new high value
101            } else {
102                newval = (short)(oldval - 800);
103            }
104            mAnalyzer[i] = newval;
105        }
106
107        // distribute the data over mWidth samples in the middle of the mPointData array
108        final int outlen = mPointData.length / 8;
109        final int width = mWidth > outlen ? outlen : mWidth;
110        final int skip = (outlen - width) / 2;
111
112        int srcidx = 0;
113        int cnt = 0;
114        for (int i = 0; i < width; i++) {
115            float val = mAnalyzer[srcidx] / 50;
116            if (val < 1f && val > -1f) val = 1;
117            mPointData[(i + skip) * 8 + 1] = val;
118            mPointData[(i + skip) * 8 + 5] = -val;
119            cnt += mAnalyzer.length;
120            if (cnt > width) {
121                srcidx++;
122                cnt -= width;
123            }
124        }
125        mPointAlloc.copyFrom(mPointData);
126    }
127
128}
129