blob: e118b640a96065c6c70f4b9ddabdaee9ebf40f70 [file] [log] [blame]
Marco Nelissen838dedb2009-11-03 17:01:21 -08001/*
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.vis5;
18
19import static android.renderscript.ProgramFragment.EnvMode.REPLACE;
20import static android.renderscript.Sampler.Value.LINEAR;
21import static android.renderscript.Sampler.Value.WRAP;
22
23import com.android.musicvis.R;
24import com.android.musicvis.RenderScriptScene;
25
26import android.media.MediaPlayer;
27import android.os.Handler;
28import android.renderscript.Allocation;
29import android.renderscript.Element;
30import android.renderscript.Primitive;
31import android.renderscript.ProgramFragment;
32import android.renderscript.ProgramStore;
33import android.renderscript.ProgramVertex;
34import android.renderscript.Sampler;
35import android.renderscript.ScriptC;
36import android.renderscript.SimpleMesh;
37import android.renderscript.Type;
38import android.renderscript.Element.Builder;
39import android.renderscript.ProgramStore.BlendDstFunc;
40import android.renderscript.ProgramStore.BlendSrcFunc;
41import android.util.Log;
42import android.view.MotionEvent;
43
44import java.util.TimeZone;
45
46class Visualization5RS extends RenderScriptScene {
47
48 private final Handler mHandler = new Handler();
49 private final Runnable mDrawCube = new Runnable() {
50 public void run() {
51 updateWave();
52 }
53 };
54 private boolean mVisible;
55
56 private int mNeedlePos = 0;
57 private int mNeedleSpeed = 0;
58 // tweak this to get quicker/slower response
59 private int mNeedleMass = 10;
60 private int mSpringForceAtOrigin = 200;
61
62 static class WorldState {
63 public float mAngle;
64 public int mPeak;
65 public float mRotate;
66 public float mTilt;
67 public int mIdle;
68 public int mWaveCounter;
69 }
70 WorldState mWorldState = new WorldState();
71 private Type mStateType;
72 private Allocation mState;
73
74 private ProgramStore mPfsBackground;
75 private ProgramFragment mPfBackground;
76 private Sampler mSampler;
77 private Allocation[] mTextures;
78
79 private ProgramVertex mPVBackground;
80 private ProgramVertex.MatrixAllocation mPVAlloc;
81
82 private SimpleMesh mCubeMesh;
83
84 protected Allocation mPointAlloc;
85 // 256 lines, with 4 points per line (2 space, 2 texture) each consisting of x and y,
86 // so 8 floats per line.
87 protected float [] mPointData = new float[256*8];
88
89 private Allocation mLineIdxAlloc;
90 // 2 indices per line
91 private short [] mIndexData = new short[256*2];
92
93 private short [] mVizData = new short[1024];
94
95 private static final int RSID_STATE = 0;
96 private static final int RSID_POINTS = 1;
97 private static final int RSID_LINES = 2;
98 private static final int RSID_PROGRAMVERTEX = 3;
99
100 private float mTouchY;
101
102 Visualization5RS(int width, int height) {
103 super(width, height);
104 mWidth = width;
105 mHeight = height;
106 // the x, s and t coordinates don't change, so set those now
107 int outlen = mPointData.length / 8;
108 int half = outlen / 2;
109 for(int i = 0; i < outlen; i++) {
110 mPointData[i*8] = i - half; // start point X (Y set later)
111 mPointData[i*8+2] = 0; // start point S
112 mPointData[i*8+3] = 0; // start point T
113 mPointData[i*8+4] = i - half; // end point X (Y set later)
114 mPointData[i*8+6] = 1.0f; // end point S
115 mPointData[i*8+7] = 0f; // end point T
116 }
117 }
118
119 @Override
120 public void resize(int width, int height) {
121 super.resize(width, height);
122 if (mPVAlloc != null) {
123 mPVAlloc.setupProjectionNormalized(width, height);
124 }
125 mWorldState.mTilt = -20;
126 }
127
128 @Override
129 public void onTouchEvent(MotionEvent event) {
130 switch(event.getAction()) {
131 case MotionEvent.ACTION_DOWN:
132 mTouchY = event.getY();
133 break;
134 case MotionEvent.ACTION_MOVE:
135 float dy = event.getY() - mTouchY;
136 mTouchY += dy;
137 dy /= 10;
138 dy += mWorldState.mTilt;
139 if (dy > 0) {
140 dy = 0;
141 } else if (dy < -45) {
142 dy = -45;
143 }
144 mWorldState.mTilt = dy;
145 mState.data(mWorldState);
146 }
147 }
148
149 @Override
150 public void setOffset(float xOffset, float yOffset, int xPixels, int yPixels) {
151 // update our state, then push it to the renderscript
152 mWorldState.mRotate = (xOffset - 0.5f) * 90;
153 mState.data(mWorldState);
154 }
155
156 @Override
157 protected ScriptC createScript() {
158
159 // Create a renderscript type from a java class. The specified name doesn't
160 // really matter; the name by which we refer to the object in RenderScript
161 // will be specified later.
162 mStateType = Type.createFromClass(mRS, WorldState.class, 1, "WorldState");
163 // Create an allocation from the type we just created.
164 mState = Allocation.createTyped(mRS, mStateType);
165
166 // First set up the coordinate system and such
167 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
168 mPVBackground = pvb.create();
169 mPVBackground.setName("PVBackground");
170 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
171 mPVBackground.bindAllocation(mPVAlloc);
172 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
173
174 mTextures = new Allocation[8];
175 mTextures[0] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.background, Element.RGBA_8888(mRS), false);
176 mTextures[0].setName("Tvumeter_background");
177 mTextures[1] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.frame, Element.RGBA_8888(mRS), false);
178 mTextures[1].setName("Tvumeter_frame");
179 mTextures[2] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.peak_on, Element.RGBA_8888(mRS), false);
180 mTextures[2].setName("Tvumeter_peak_on");
181 mTextures[3] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.peak_off, Element.RGBA_8888(mRS), false);
182 mTextures[3].setName("Tvumeter_peak_off");
183 mTextures[4] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.needle, Element.RGBA_8888(mRS), false);
184 mTextures[4].setName("Tvumeter_needle");
185 mTextures[5] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.black, Element.RGB_565(mRS), false);
186 mTextures[5].setName("Tvumeter_black");
187 mTextures[6] = Allocation.createFromBitmapResource(mRS, mResources, R.drawable.albumart, Element.RGBA_8888(mRS), false);
188 mTextures[6].setName("Tvumeter_album");
189 mTextures[7] = Allocation.createFromBitmapResource(mRS, mResources, R.drawable.fire, Element.RGB_565(mRS), false);
190 mTextures[7].setName("Tlinetexture");
191
192 final int count = mTextures.length;
193 for (int i = 0; i < count; i++) {
194 mTextures[i].uploadToTexture(0);
195 }
196
197 Sampler.Builder samplerBuilder = new Sampler.Builder(mRS);
198 samplerBuilder.setMin(LINEAR);
199 samplerBuilder.setMag(LINEAR);
200 samplerBuilder.setWrapS(WRAP);
201 samplerBuilder.setWrapT(WRAP);
202 mSampler = samplerBuilder.create();
203
204 {
205 ProgramFragment.Builder builder = new ProgramFragment.Builder(mRS, null, null);
206 builder.setTexEnable(true, 0);
207 builder.setTexEnvMode(REPLACE, 0);
208 mPfBackground = builder.create();
209 mPfBackground.setName("PFBackground");
210 mPfBackground.bindSampler(mSampler, 0);
211 }
212
213 {
214 ProgramStore.Builder builder = new ProgramStore.Builder(mRS, null, null);
215 builder.setDepthFunc(ProgramStore.DepthFunc.EQUAL);
216 //builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
217 builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
218 builder.setDitherEnable(true); // without dithering there is severe banding
219 builder.setDepthMask(false);
220 mPfsBackground = builder.create();
221 mPfsBackground.setName("PFSBackground");
222 }
223
224 // Start creating the mesh
225 final SimpleMesh.Builder meshBuilder = new SimpleMesh.Builder(mRS);
226
227 // Create the Element for the points
228 Builder elementBuilder = new Builder(mRS);
229 // By specifying a prefix, even an empty one, the members will be accessible
230 // in the renderscript. If we just called addFloatXYZ(), the members would be
231 // unnamed in the renderscript struct definition.
232 elementBuilder.addFloatXY("");
233 elementBuilder.addFloatST("");
234 final Element vertexElement = elementBuilder.create();
235 final int vertexSlot = meshBuilder.addVertexType(vertexElement, mPointData.length / 4);
236 // Specify the type and number of indices we need. We'll allocate them later.
237 meshBuilder.setIndexType(Element.INDEX_16(mRS), mIndexData.length);
238 // This will be a line mesh
239 meshBuilder.setPrimitive(Primitive.LINE);
240
241 // Create the Allocation for the vertices
242 mCubeMesh = meshBuilder.create();
243 mCubeMesh.setName("CubeMesh");
244 mPointAlloc = mCubeMesh.createVertexAllocation(vertexSlot);
245 mPointAlloc.setName("PointBuffer");
246
247 // Create the Allocation for the indices
248 mLineIdxAlloc = mCubeMesh.createIndexAllocation();
249
250 // Bind the allocations to the mesh
251 mCubeMesh.bindVertexAllocation(mPointAlloc, 0);
252 mCubeMesh.bindIndexAllocation(mLineIdxAlloc);
253
254 /*
255 * put the vertex and index data in their respective buffers
256 */
257 updateWave();
258 for(int i = 0; i < mIndexData.length; i ++) {
259 mIndexData[i] = (short) i;
260 }
261
262 /*
263 * upload the vertex and index data
264 */
265 mPointAlloc.data(mPointData);
266 mPointAlloc.uploadToBufferObject();
267 mLineIdxAlloc.data(mIndexData);
268 mLineIdxAlloc.uploadToBufferObject();
269
270 // Time to create the script
271 ScriptC.Builder sb = new ScriptC.Builder(mRS);
272 // Specify the name by which to refer to the WorldState object in the
273 // renderscript.
274 sb.setType(mStateType, "State", RSID_STATE);
275 sb.setScript(mResources, R.raw.many);
276 sb.setRoot(true);
277
278 ScriptC script = sb.create();
279 script.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
280 script.setTimeZone(TimeZone.getDefault().getID());
281
282 script.bindAllocation(mState, RSID_STATE);
283 script.bindAllocation(mPointAlloc, RSID_POINTS);
284 script.bindAllocation(mLineIdxAlloc, RSID_LINES);
285 script.bindAllocation(mPVAlloc.mAlloc, RSID_PROGRAMVERTEX);
286
287 return script;
288 }
289
290 @Override
291 public void start() {
292 super.start();
293 mVisible = true;
294 updateWave();
295 }
296
297 @Override
298 public void stop() {
299 super.stop();
300 mVisible = false;
301 }
302
303 void updateWave() {
304 mHandler.removeCallbacks(mDrawCube);
305 if (mVisible) {
306 mHandler.postDelayed(mDrawCube, 20);
307 }
308
309 int len = MediaPlayer.snoop(mVizData, 0);
310
311 // Simulate running the signal through a rectifier by
312 // taking the average of the absolute sample values.
313 int volt = 0;
314 if (len > 0) {
315 for (int i = 0; i < len; i++) {
316 int val = mVizData[i];
317 if (val < 0) {
318 val = -val;
319 }
320 volt += val;
321 }
322 volt = volt * 4 / len; // arbitrary scalar to get better range
323 }
324
325 // There are several forces working on the needle: a force applied by the
326 // electromagnet, a force applied by the spring, and friction.
327 // The force from the magnet is proportional to the current flowing
328 // through its coil. We have to take in to account that the coil is an
329 // inductive load, which means that an immediate change in applied voltage
330 // will result in a gradual change in current, but also that current will
331 // be induced by the movement of the needle.
332 // The force from the spring is proportional to the position of the needle.
333 // The friction force is a function of the speed of the needle, but so is
334 // the current induced by the movement of the needle, so we can combine
335 // them.
336
337
338 // Add up the various forces, with some multipliers to make the movement
339 // of the needle more realistic
340 // 'volt' is for the applied voltage, which causes a current to flow through the coil
341 // mNeedleSpeed * 3 is for the movement of the needle, which induces an opposite current
342 // in the coil, and is also proportional to the friction
343 // mNeedlePos + mSpringForceAtOrigin is for the force of the spring pushing the needle back
344 int netforce = volt - mNeedleSpeed * 3 - (mNeedlePos + mSpringForceAtOrigin) ;
345 int acceleration = netforce / mNeedleMass;
346 mNeedleSpeed += acceleration;
347 mNeedlePos += mNeedleSpeed;
348 if (mNeedlePos < 0) {
349 mNeedlePos = 0;
350 mNeedleSpeed = 0;
351 } else if (mNeedlePos > 32767) {
352 if (mNeedlePos > 33333) {
353 mWorldState.mPeak = 10;
354 }
355 mNeedlePos = 32767;
356 mNeedleSpeed = 0;
357 }
358 if (mWorldState.mPeak > 0) {
359 mWorldState.mPeak--;
360 }
361
362 mWorldState.mAngle = 131f - (mNeedlePos / 410f); // ~80 degree range
363
364 // downsample 1024 samples in to 256
365
366 if (len == 0) {
367 if (mWorldState.mIdle == 0) {
368 mWorldState.mIdle = 1;
369 }
370 } else {
371 if (mWorldState.mIdle != 0) {
372 mWorldState.mIdle = 0;
373 }
374 // TODO: might be more efficient to push this in to renderscript
375 int outlen = mPointData.length / 8;
376 len /= 4;
377 if (len > outlen) len = outlen;
378 for(int i = 0; i < len; i++) {
379 int amp = (mVizData[i*4] + mVizData[i*4+1] + mVizData[i*4+2] + mVizData[i*4+3]);
380 mPointData[i*8+1] = amp;
381 mPointData[i*8+5] = -amp;
382 }
383 mPointAlloc.data(mPointData);
384 mWorldState.mWaveCounter++;
385 }
386
387 mState.data(mWorldState);
388 }
389
390}