blob: cbe7d3fecc6cae1387d41b40d6db7eaf94c27028 [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
Marco Nelissen838dedb2009-11-03 17:01:21 -080019import com.android.musicvis.R;
20import com.android.musicvis.RenderScriptScene;
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -070021import com.android.musicvis.ScriptField_Vertex;
Marco Nelissen838dedb2009-11-03 17:01:21 -080022
23import android.media.MediaPlayer;
24import android.os.Handler;
25import android.renderscript.Allocation;
26import android.renderscript.Element;
27import android.renderscript.Primitive;
28import android.renderscript.ProgramFragment;
29import android.renderscript.ProgramStore;
30import android.renderscript.ProgramVertex;
31import android.renderscript.Sampler;
32import android.renderscript.ScriptC;
33import android.renderscript.SimpleMesh;
34import android.renderscript.Type;
35import android.renderscript.Element.Builder;
36import android.renderscript.ProgramStore.BlendDstFunc;
37import android.renderscript.ProgramStore.BlendSrcFunc;
Marco Nelissen24c44572009-11-20 11:30:32 -080038import android.renderscript.Sampler.Value;
Marco Nelissen838dedb2009-11-03 17:01:21 -080039import android.util.Log;
40import android.view.MotionEvent;
41
42import java.util.TimeZone;
43
44class Visualization5RS extends RenderScriptScene {
45
46 private final Handler mHandler = new Handler();
47 private final Runnable mDrawCube = new Runnable() {
48 public void run() {
49 updateWave();
50 }
51 };
52 private boolean mVisible;
53
54 private int mNeedlePos = 0;
55 private int mNeedleSpeed = 0;
56 // tweak this to get quicker/slower response
57 private int mNeedleMass = 10;
58 private int mSpringForceAtOrigin = 200;
Jason Sams27b89e72009-12-17 18:46:45 -080059
Marco Nelissen838dedb2009-11-03 17:01:21 -080060 static class WorldState {
61 public float mAngle;
62 public int mPeak;
63 public float mRotate;
64 public float mTilt;
65 public int mIdle;
66 public int mWaveCounter;
67 }
68 WorldState mWorldState = new WorldState();
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -070069
70 ScriptC_Many mScript;
71 private ScriptField_Vertex mVertexBuffer;
Marco Nelissen838dedb2009-11-03 17:01:21 -080072
73 private ProgramStore mPfsBackground;
Marco Nelissen24c44572009-11-20 11:30:32 -080074 private ProgramFragment mPfBackgroundMip;
75 private ProgramFragment mPfBackgroundNoMip;
76 private Sampler mSamplerMip;
77 private Sampler mSamplerNoMip;
Marco Nelissen838dedb2009-11-03 17:01:21 -080078 private Allocation[] mTextures;
Jason Sams27b89e72009-12-17 18:46:45 -080079
Marco Nelissen838dedb2009-11-03 17:01:21 -080080 private ProgramVertex mPVBackground;
81 private ProgramVertex.MatrixAllocation mPVAlloc;
82
83 private SimpleMesh mCubeMesh;
84
85 protected Allocation mPointAlloc;
86 // 256 lines, with 4 points per line (2 space, 2 texture) each consisting of x and y,
87 // so 8 floats per line.
88 protected float [] mPointData = new float[256*8];
89
90 private Allocation mLineIdxAlloc;
91 // 2 indices per line
92 private short [] mIndexData = new short[256*2];
Jason Sams27b89e72009-12-17 18:46:45 -080093
Marco Nelissen838dedb2009-11-03 17:01:21 -080094 private short [] mVizData = new short[1024];
95
96 private static final int RSID_STATE = 0;
97 private static final int RSID_POINTS = 1;
98 private static final int RSID_LINES = 2;
99 private static final int RSID_PROGRAMVERTEX = 3;
100
101 private float mTouchY;
Jason Sams27b89e72009-12-17 18:46:45 -0800102
Marco Nelissen838dedb2009-11-03 17:01:21 -0800103 Visualization5RS(int width, int height) {
104 super(width, height);
105 mWidth = width;
106 mHeight = height;
107 // the x, s and t coordinates don't change, so set those now
108 int outlen = mPointData.length / 8;
109 int half = outlen / 2;
110 for(int i = 0; i < outlen; i++) {
111 mPointData[i*8] = i - half; // start point X (Y set later)
112 mPointData[i*8+2] = 0; // start point S
113 mPointData[i*8+3] = 0; // start point T
114 mPointData[i*8+4] = i - half; // end point X (Y set later)
115 mPointData[i*8+6] = 1.0f; // end point S
116 mPointData[i*8+7] = 0f; // end point T
117 }
118 }
119
120 @Override
121 public void resize(int width, int height) {
122 super.resize(width, height);
123 if (mPVAlloc != null) {
124 mPVAlloc.setupProjectionNormalized(width, height);
125 }
126 mWorldState.mTilt = -20;
127 }
128
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700129 /*@Override
Marco Nelissen838dedb2009-11-03 17:01:21 -0800130 public void onTouchEvent(MotionEvent event) {
131 switch(event.getAction()) {
132 case MotionEvent.ACTION_DOWN:
133 mTouchY = event.getY();
134 break;
135 case MotionEvent.ACTION_MOVE:
136 float dy = event.getY() - mTouchY;
137 mTouchY += dy;
138 dy /= 10;
139 dy += mWorldState.mTilt;
140 if (dy > 0) {
141 dy = 0;
142 } else if (dy < -45) {
143 dy = -45;
144 }
145 mWorldState.mTilt = dy;
146 mState.data(mWorldState);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700147 //updateWorldState();
Marco Nelissen838dedb2009-11-03 17:01:21 -0800148 }
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700149 }*/
Jason Sams27b89e72009-12-17 18:46:45 -0800150
Marco Nelissen838dedb2009-11-03 17:01:21 -0800151 @Override
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700152 public void setOffset(float xOffset, float yOffset, int xPixels, int yPixels) {
Marco Nelissen838dedb2009-11-03 17:01:21 -0800153 // update our state, then push it to the renderscript
154 mWorldState.mRotate = (xOffset - 0.5f) * 90;
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700155 updateWorldState();
Marco Nelissen838dedb2009-11-03 17:01:21 -0800156 }
157
158 @Override
159 protected ScriptC createScript() {
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700160 mScript = new ScriptC_Many(mRS, mResources, R.raw.many_bc, true);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800161
162 // First set up the coordinate system and such
163 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
164 mPVBackground = pvb.create();
Marco Nelissen838dedb2009-11-03 17:01:21 -0800165 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
166 mPVBackground.bindAllocation(mPVAlloc);
167 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
168
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700169 mScript.set_gPVBackground(mPVBackground);
170
Marco Nelissen838dedb2009-11-03 17:01:21 -0800171 mTextures = new Allocation[8];
Marco Nelissen24c44572009-11-20 11:30:32 -0800172 mTextures[0] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.background, Element.RGBA_8888(mRS), true);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700173 mScript.set_gTvumeter_background(mTextures[0]);
Marco Nelissen24c44572009-11-20 11:30:32 -0800174 mTextures[1] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.frame, Element.RGBA_8888(mRS), true);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700175 mScript.set_gTvumeter_frame(mTextures[1]);
Marco Nelissen24c44572009-11-20 11:30:32 -0800176 mTextures[2] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.peak_on, Element.RGBA_8888(mRS), true);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700177 mScript.set_gTvumeter_peak_on(mTextures[2]);
Marco Nelissen24c44572009-11-20 11:30:32 -0800178 mTextures[3] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.peak_off, Element.RGBA_8888(mRS), true);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700179 mScript.set_gTvumeter_peak_off(mTextures[3]);
Marco Nelissen24c44572009-11-20 11:30:32 -0800180 mTextures[4] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.needle, Element.RGBA_8888(mRS), true);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700181 mScript.set_gTvumeter_needle(mTextures[4]);
182 mTextures[5] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.black, Element.RGB_565(mRS), true);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700183 mScript.set_gTvumeter_black(mTextures[5]);
Marco Nelissen24c44572009-11-20 11:30:32 -0800184 mTextures[6] = Allocation.createFromBitmapResource(mRS, mResources, R.drawable.albumart, Element.RGBA_8888(mRS), true);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700185 mScript.set_gTvumeter_album(mTextures[6]);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800186 mTextures[7] = Allocation.createFromBitmapResource(mRS, mResources, R.drawable.fire, Element.RGB_565(mRS), false);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700187 mScript.set_gTlinetexture(mTextures[7]);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800188
189 final int count = mTextures.length;
190 for (int i = 0; i < count; i++) {
191 mTextures[i].uploadToTexture(0);
192 }
Marco Nelissen24c44572009-11-20 11:30:32 -0800193
194 {
195 Sampler.Builder builder = new Sampler.Builder(mRS);
196 builder.setMin(Value.LINEAR);
197 builder.setMag(Value.LINEAR);
198 builder.setWrapS(Value.WRAP);
199 builder.setWrapT(Value.WRAP);
200 mSamplerNoMip = builder.create();
201 }
202
203 {
204 Sampler.Builder builder = new Sampler.Builder(mRS);
205 builder.setMin(Value.LINEAR_MIP_LINEAR);
206 builder.setMag(Value.LINEAR);
207 builder.setWrapS(Value.WRAP);
208 builder.setWrapT(Value.WRAP);
209 mSamplerMip = builder.create();
210 }
Marco Nelissen838dedb2009-11-03 17:01:21 -0800211
212 {
Jason Sams27b89e72009-12-17 18:46:45 -0800213 ProgramFragment.Builder builder = new ProgramFragment.Builder(mRS);
214 builder.setTexture(ProgramFragment.Builder.EnvMode.REPLACE,
215 ProgramFragment.Builder.Format.RGBA, 0);
Marco Nelissen24c44572009-11-20 11:30:32 -0800216 mPfBackgroundNoMip = builder.create();
Marco Nelissen24c44572009-11-20 11:30:32 -0800217 mPfBackgroundNoMip.bindSampler(mSamplerNoMip, 0);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700218 mScript.set_gPFBackgroundNoMip(mPfBackgroundNoMip);
Marco Nelissen24c44572009-11-20 11:30:32 -0800219 }
Jason Sams27b89e72009-12-17 18:46:45 -0800220
Marco Nelissen24c44572009-11-20 11:30:32 -0800221 {
Jason Sams27b89e72009-12-17 18:46:45 -0800222 ProgramFragment.Builder builder = new ProgramFragment.Builder(mRS);
223 builder.setTexture(ProgramFragment.Builder.EnvMode.REPLACE,
224 ProgramFragment.Builder.Format.RGBA, 0);
Marco Nelissen24c44572009-11-20 11:30:32 -0800225 mPfBackgroundMip = builder.create();
Marco Nelissen24c44572009-11-20 11:30:32 -0800226 mPfBackgroundMip.bindSampler(mSamplerMip, 0);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700227 mScript.set_gPFBackgroundMip(mPfBackgroundMip);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800228 }
229
230 {
231 ProgramStore.Builder builder = new ProgramStore.Builder(mRS, null, null);
232 builder.setDepthFunc(ProgramStore.DepthFunc.EQUAL);
233 //builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
234 builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
235 builder.setDitherEnable(true); // without dithering there is severe banding
236 builder.setDepthMask(false);
237 mPfsBackground = builder.create();
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700238
239 mScript.set_gPFSBackground(mPfsBackground);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800240 }
Jason Sams27b89e72009-12-17 18:46:45 -0800241
Marco Nelissen838dedb2009-11-03 17:01:21 -0800242 // Start creating the mesh
243 final SimpleMesh.Builder meshBuilder = new SimpleMesh.Builder(mRS);
244
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700245 mVertexBuffer = new ScriptField_Vertex(mRS, mPointData.length / 4);
246
247 final int vertexSlot = meshBuilder.addVertexType(mVertexBuffer.getType());
Marco Nelissen838dedb2009-11-03 17:01:21 -0800248 // Specify the type and number of indices we need. We'll allocate them later.
Jason Sams0bb902e2010-06-08 15:39:36 -0700249 meshBuilder.setIndexType(Element.U16(mRS), mIndexData.length);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800250 // This will be a line mesh
251 meshBuilder.setPrimitive(Primitive.LINE);
252
253 // Create the Allocation for the vertices
254 mCubeMesh = meshBuilder.create();
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700255
256 mCubeMesh.bindVertexAllocation(mVertexBuffer.getAllocation(), 0);
257
258 mPointAlloc = mVertexBuffer.getAllocation();
259
Marco Nelissen838dedb2009-11-03 17:01:21 -0800260 mPointAlloc = mCubeMesh.createVertexAllocation(vertexSlot);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700261
Shih-wei Liao6e3b8112010-06-17 18:06:08 -0700262 mScript.bind_gPoints(mVertexBuffer);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700263 mScript.set_gPointBuffer(mPointAlloc);
264 mScript.set_gCubeMesh(mCubeMesh);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800265
266 // Create the Allocation for the indices
267 mLineIdxAlloc = mCubeMesh.createIndexAllocation();
268
269 // Bind the allocations to the mesh
270 mCubeMesh.bindVertexAllocation(mPointAlloc, 0);
271 mCubeMesh.bindIndexAllocation(mLineIdxAlloc);
272
Jason Samsa811c212010-05-11 14:00:38 -0700273 // put the vertex and index data in their respective buffers
Marco Nelissen838dedb2009-11-03 17:01:21 -0800274 updateWave();
275 for(int i = 0; i < mIndexData.length; i ++) {
276 mIndexData[i] = (short) i;
277 }
278
Jason Samsa811c212010-05-11 14:00:38 -0700279 // upload the vertex and index data
Marco Nelissen838dedb2009-11-03 17:01:21 -0800280 mPointAlloc.data(mPointData);
281 mPointAlloc.uploadToBufferObject();
282 mLineIdxAlloc.data(mIndexData);
283 mLineIdxAlloc.uploadToBufferObject();
284
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700285 return mScript;
Marco Nelissen838dedb2009-11-03 17:01:21 -0800286 }
287
288 @Override
289 public void start() {
290 super.start();
291 mVisible = true;
292 updateWave();
293 }
294
295 @Override
296 public void stop() {
297 super.stop();
298 mVisible = false;
299 }
300
301 void updateWave() {
302 mHandler.removeCallbacks(mDrawCube);
Marco Nelissend760ff32009-11-10 14:49:08 -0800303 if (!mVisible) {
304 return;
Marco Nelissen838dedb2009-11-03 17:01:21 -0800305 }
Marco Nelissend760ff32009-11-10 14:49:08 -0800306 mHandler.postDelayed(mDrawCube, 20);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800307
308 int len = MediaPlayer.snoop(mVizData, 0);
Jason Sams27b89e72009-12-17 18:46:45 -0800309
Marco Nelissen838dedb2009-11-03 17:01:21 -0800310 // Simulate running the signal through a rectifier by
311 // taking the average of the absolute sample values.
312 int volt = 0;
313 if (len > 0) {
314 for (int i = 0; i < len; i++) {
315 int val = mVizData[i];
316 if (val < 0) {
317 val = -val;
318 }
319 volt += val;
320 }
321 volt = volt * 4 / len; // arbitrary scalar to get better range
322 }
323
324 // There are several forces working on the needle: a force applied by the
325 // electromagnet, a force applied by the spring, and friction.
326 // The force from the magnet is proportional to the current flowing
327 // through its coil. We have to take in to account that the coil is an
328 // inductive load, which means that an immediate change in applied voltage
329 // will result in a gradual change in current, but also that current will
330 // be induced by the movement of the needle.
331 // The force from the spring is proportional to the position of the needle.
332 // The friction force is a function of the speed of the needle, but so is
333 // the current induced by the movement of the needle, so we can combine
334 // them.
Jason Sams27b89e72009-12-17 18:46:45 -0800335
336
Marco Nelissen838dedb2009-11-03 17:01:21 -0800337 // Add up the various forces, with some multipliers to make the movement
338 // of the needle more realistic
339 // 'volt' is for the applied voltage, which causes a current to flow through the coil
340 // mNeedleSpeed * 3 is for the movement of the needle, which induces an opposite current
341 // in the coil, and is also proportional to the friction
342 // mNeedlePos + mSpringForceAtOrigin is for the force of the spring pushing the needle back
343 int netforce = volt - mNeedleSpeed * 3 - (mNeedlePos + mSpringForceAtOrigin) ;
344 int acceleration = netforce / mNeedleMass;
345 mNeedleSpeed += acceleration;
346 mNeedlePos += mNeedleSpeed;
347 if (mNeedlePos < 0) {
348 mNeedlePos = 0;
349 mNeedleSpeed = 0;
350 } else if (mNeedlePos > 32767) {
351 if (mNeedlePos > 33333) {
352 mWorldState.mPeak = 10;
353 }
354 mNeedlePos = 32767;
355 mNeedleSpeed = 0;
356 }
357 if (mWorldState.mPeak > 0) {
358 mWorldState.mPeak--;
359 }
360
361 mWorldState.mAngle = 131f - (mNeedlePos / 410f); // ~80 degree range
Jason Sams27b89e72009-12-17 18:46:45 -0800362
Marco Nelissen838dedb2009-11-03 17:01:21 -0800363 // downsample 1024 samples in to 256
Jason Sams27b89e72009-12-17 18:46:45 -0800364
Marco Nelissen838dedb2009-11-03 17:01:21 -0800365 if (len == 0) {
366 if (mWorldState.mIdle == 0) {
367 mWorldState.mIdle = 1;
368 }
369 } else {
370 if (mWorldState.mIdle != 0) {
371 mWorldState.mIdle = 0;
372 }
373 // TODO: might be more efficient to push this in to renderscript
374 int outlen = mPointData.length / 8;
375 len /= 4;
376 if (len > outlen) len = outlen;
377 for(int i = 0; i < len; i++) {
378 int amp = (mVizData[i*4] + mVizData[i*4+1] + mVizData[i*4+2] + mVizData[i*4+3]);
379 mPointData[i*8+1] = amp;
380 mPointData[i*8+5] = -amp;
381 }
382 mPointAlloc.data(mPointData);
383 mWorldState.mWaveCounter++;
384 }
Jason Sams27b89e72009-12-17 18:46:45 -0800385
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700386 updateWorldState();
Marco Nelissen838dedb2009-11-03 17:01:21 -0800387 }
388
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700389 protected void updateWorldState() {
390 mScript.set_gAngle(mWorldState.mAngle);
391 mScript.set_gPeak(mWorldState.mPeak);
392 mScript.set_gRotate(mWorldState.mRotate);
393 mScript.set_gTilt(mWorldState.mTilt);
394 mScript.set_gIdle(mWorldState.mIdle);
395 mScript.set_gWaveCounter(mWorldState.mWaveCounter);
396 }
Marco Nelissen838dedb2009-11-03 17:01:21 -0800397}