blob: c3fb320ec3ef3f0b513741687b63f77c24879706 [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();
165 mPVBackground.setName("PVBackground");
166 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
167 mPVBackground.bindAllocation(mPVAlloc);
168 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
169
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700170 mScript.set_gPVBackground(mPVBackground);
171
Marco Nelissen838dedb2009-11-03 17:01:21 -0800172 mTextures = new Allocation[8];
Marco Nelissen24c44572009-11-20 11:30:32 -0800173 mTextures[0] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.background, Element.RGBA_8888(mRS), true);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800174 mTextures[0].setName("Tvumeter_background");
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700175 mScript.set_gTvumeter_background(mTextures[0]);
Marco Nelissen24c44572009-11-20 11:30:32 -0800176 mTextures[1] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.frame, Element.RGBA_8888(mRS), true);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800177 mTextures[1].setName("Tvumeter_frame");
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700178 mScript.set_gTvumeter_frame(mTextures[1]);
Marco Nelissen24c44572009-11-20 11:30:32 -0800179 mTextures[2] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.peak_on, Element.RGBA_8888(mRS), true);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800180 mTextures[2].setName("Tvumeter_peak_on");
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700181 mScript.set_gTvumeter_peak_on(mTextures[2]);
Marco Nelissen24c44572009-11-20 11:30:32 -0800182 mTextures[3] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.peak_off, Element.RGBA_8888(mRS), true);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800183 mTextures[3].setName("Tvumeter_peak_off");
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700184 mScript.set_gTvumeter_peak_off(mTextures[3]);
Marco Nelissen24c44572009-11-20 11:30:32 -0800185 mTextures[4] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.needle, Element.RGBA_8888(mRS), true);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800186 mTextures[4].setName("Tvumeter_needle");
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700187 mScript.set_gTvumeter_needle(mTextures[4]);
188 mTextures[5] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.black, Element.RGB_565(mRS), true);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800189 mTextures[5].setName("Tvumeter_black");
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700190 mScript.set_gTvumeter_black(mTextures[5]);
Marco Nelissen24c44572009-11-20 11:30:32 -0800191 mTextures[6] = Allocation.createFromBitmapResource(mRS, mResources, R.drawable.albumart, Element.RGBA_8888(mRS), true);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800192 mTextures[6].setName("Tvumeter_album");
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700193 mScript.set_gTvumeter_album(mTextures[6]);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800194 mTextures[7] = Allocation.createFromBitmapResource(mRS, mResources, R.drawable.fire, Element.RGB_565(mRS), false);
195 mTextures[7].setName("Tlinetexture");
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700196 mScript.set_gTlinetexture(mTextures[7]);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800197
198 final int count = mTextures.length;
199 for (int i = 0; i < count; i++) {
200 mTextures[i].uploadToTexture(0);
201 }
Marco Nelissen24c44572009-11-20 11:30:32 -0800202
203 {
204 Sampler.Builder builder = new Sampler.Builder(mRS);
205 builder.setMin(Value.LINEAR);
206 builder.setMag(Value.LINEAR);
207 builder.setWrapS(Value.WRAP);
208 builder.setWrapT(Value.WRAP);
209 mSamplerNoMip = builder.create();
210 }
211
212 {
213 Sampler.Builder builder = new Sampler.Builder(mRS);
214 builder.setMin(Value.LINEAR_MIP_LINEAR);
215 builder.setMag(Value.LINEAR);
216 builder.setWrapS(Value.WRAP);
217 builder.setWrapT(Value.WRAP);
218 mSamplerMip = builder.create();
219 }
Marco Nelissen838dedb2009-11-03 17:01:21 -0800220
221 {
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 mPfBackgroundNoMip = builder.create();
226 mPfBackgroundNoMip.setName("PFBackgroundNoMip");
227 mPfBackgroundNoMip.bindSampler(mSamplerNoMip, 0);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700228 mScript.set_gPFBackgroundNoMip(mPfBackgroundNoMip);
Marco Nelissen24c44572009-11-20 11:30:32 -0800229 }
Jason Sams27b89e72009-12-17 18:46:45 -0800230
Marco Nelissen24c44572009-11-20 11:30:32 -0800231 {
Jason Sams27b89e72009-12-17 18:46:45 -0800232 ProgramFragment.Builder builder = new ProgramFragment.Builder(mRS);
233 builder.setTexture(ProgramFragment.Builder.EnvMode.REPLACE,
234 ProgramFragment.Builder.Format.RGBA, 0);
Marco Nelissen24c44572009-11-20 11:30:32 -0800235 mPfBackgroundMip = builder.create();
236 mPfBackgroundMip.setName("PFBackgroundMip");
237 mPfBackgroundMip.bindSampler(mSamplerMip, 0);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700238 mScript.set_gPFBackgroundMip(mPfBackgroundMip);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800239 }
240
241 {
242 ProgramStore.Builder builder = new ProgramStore.Builder(mRS, null, null);
243 builder.setDepthFunc(ProgramStore.DepthFunc.EQUAL);
244 //builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
245 builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ONE_MINUS_SRC_ALPHA);
246 builder.setDitherEnable(true); // without dithering there is severe banding
247 builder.setDepthMask(false);
248 mPfsBackground = builder.create();
249 mPfsBackground.setName("PFSBackground");
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700250
251 mScript.set_gPFSBackground(mPfsBackground);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800252 }
Jason Sams27b89e72009-12-17 18:46:45 -0800253
Marco Nelissen838dedb2009-11-03 17:01:21 -0800254 // Start creating the mesh
255 final SimpleMesh.Builder meshBuilder = new SimpleMesh.Builder(mRS);
256
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700257 mVertexBuffer = new ScriptField_Vertex(mRS, mPointData.length / 4);
258
259 final int vertexSlot = meshBuilder.addVertexType(mVertexBuffer.getType());
Marco Nelissen838dedb2009-11-03 17:01:21 -0800260 // Specify the type and number of indices we need. We'll allocate them later.
261 meshBuilder.setIndexType(Element.INDEX_16(mRS), mIndexData.length);
262 // This will be a line mesh
263 meshBuilder.setPrimitive(Primitive.LINE);
264
265 // Create the Allocation for the vertices
266 mCubeMesh = meshBuilder.create();
267 mCubeMesh.setName("CubeMesh");
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700268
269 mCubeMesh.bindVertexAllocation(mVertexBuffer.getAllocation(), 0);
270
271 mPointAlloc = mVertexBuffer.getAllocation();
272
Marco Nelissen838dedb2009-11-03 17:01:21 -0800273 mPointAlloc = mCubeMesh.createVertexAllocation(vertexSlot);
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700274
275 mScript.bind_gPoints(mPointAlloc);
276 mScript.set_gPointBuffer(mPointAlloc);
277 mScript.set_gCubeMesh(mCubeMesh);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800278
279 // Create the Allocation for the indices
280 mLineIdxAlloc = mCubeMesh.createIndexAllocation();
281
282 // Bind the allocations to the mesh
283 mCubeMesh.bindVertexAllocation(mPointAlloc, 0);
284 mCubeMesh.bindIndexAllocation(mLineIdxAlloc);
285
Jason Samsa811c212010-05-11 14:00:38 -0700286 // put the vertex and index data in their respective buffers
Marco Nelissen838dedb2009-11-03 17:01:21 -0800287 updateWave();
288 for(int i = 0; i < mIndexData.length; i ++) {
289 mIndexData[i] = (short) i;
290 }
291
Jason Samsa811c212010-05-11 14:00:38 -0700292 // upload the vertex and index data
Marco Nelissen838dedb2009-11-03 17:01:21 -0800293 mPointAlloc.data(mPointData);
294 mPointAlloc.uploadToBufferObject();
295 mLineIdxAlloc.data(mIndexData);
296 mLineIdxAlloc.uploadToBufferObject();
297
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700298 mScript.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800299
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700300 return mScript;
Marco Nelissen838dedb2009-11-03 17:01:21 -0800301 }
302
303 @Override
304 public void start() {
305 super.start();
306 mVisible = true;
307 updateWave();
308 }
309
310 @Override
311 public void stop() {
312 super.stop();
313 mVisible = false;
314 }
315
316 void updateWave() {
317 mHandler.removeCallbacks(mDrawCube);
Marco Nelissend760ff32009-11-10 14:49:08 -0800318 if (!mVisible) {
319 return;
Marco Nelissen838dedb2009-11-03 17:01:21 -0800320 }
Marco Nelissend760ff32009-11-10 14:49:08 -0800321 mHandler.postDelayed(mDrawCube, 20);
Marco Nelissen838dedb2009-11-03 17:01:21 -0800322
323 int len = MediaPlayer.snoop(mVizData, 0);
Jason Sams27b89e72009-12-17 18:46:45 -0800324
Marco Nelissen838dedb2009-11-03 17:01:21 -0800325 // Simulate running the signal through a rectifier by
326 // taking the average of the absolute sample values.
327 int volt = 0;
328 if (len > 0) {
329 for (int i = 0; i < len; i++) {
330 int val = mVizData[i];
331 if (val < 0) {
332 val = -val;
333 }
334 volt += val;
335 }
336 volt = volt * 4 / len; // arbitrary scalar to get better range
337 }
338
339 // There are several forces working on the needle: a force applied by the
340 // electromagnet, a force applied by the spring, and friction.
341 // The force from the magnet is proportional to the current flowing
342 // through its coil. We have to take in to account that the coil is an
343 // inductive load, which means that an immediate change in applied voltage
344 // will result in a gradual change in current, but also that current will
345 // be induced by the movement of the needle.
346 // The force from the spring is proportional to the position of the needle.
347 // The friction force is a function of the speed of the needle, but so is
348 // the current induced by the movement of the needle, so we can combine
349 // them.
Jason Sams27b89e72009-12-17 18:46:45 -0800350
351
Marco Nelissen838dedb2009-11-03 17:01:21 -0800352 // Add up the various forces, with some multipliers to make the movement
353 // of the needle more realistic
354 // 'volt' is for the applied voltage, which causes a current to flow through the coil
355 // mNeedleSpeed * 3 is for the movement of the needle, which induces an opposite current
356 // in the coil, and is also proportional to the friction
357 // mNeedlePos + mSpringForceAtOrigin is for the force of the spring pushing the needle back
358 int netforce = volt - mNeedleSpeed * 3 - (mNeedlePos + mSpringForceAtOrigin) ;
359 int acceleration = netforce / mNeedleMass;
360 mNeedleSpeed += acceleration;
361 mNeedlePos += mNeedleSpeed;
362 if (mNeedlePos < 0) {
363 mNeedlePos = 0;
364 mNeedleSpeed = 0;
365 } else if (mNeedlePos > 32767) {
366 if (mNeedlePos > 33333) {
367 mWorldState.mPeak = 10;
368 }
369 mNeedlePos = 32767;
370 mNeedleSpeed = 0;
371 }
372 if (mWorldState.mPeak > 0) {
373 mWorldState.mPeak--;
374 }
375
376 mWorldState.mAngle = 131f - (mNeedlePos / 410f); // ~80 degree range
Jason Sams27b89e72009-12-17 18:46:45 -0800377
Marco Nelissen838dedb2009-11-03 17:01:21 -0800378 // downsample 1024 samples in to 256
Jason Sams27b89e72009-12-17 18:46:45 -0800379
Marco Nelissen838dedb2009-11-03 17:01:21 -0800380 if (len == 0) {
381 if (mWorldState.mIdle == 0) {
382 mWorldState.mIdle = 1;
383 }
384 } else {
385 if (mWorldState.mIdle != 0) {
386 mWorldState.mIdle = 0;
387 }
388 // TODO: might be more efficient to push this in to renderscript
389 int outlen = mPointData.length / 8;
390 len /= 4;
391 if (len > outlen) len = outlen;
392 for(int i = 0; i < len; i++) {
393 int amp = (mVizData[i*4] + mVizData[i*4+1] + mVizData[i*4+2] + mVizData[i*4+3]);
394 mPointData[i*8+1] = amp;
395 mPointData[i*8+5] = -amp;
396 }
397 mPointAlloc.data(mPointData);
398 mWorldState.mWaveCounter++;
399 }
Jason Sams27b89e72009-12-17 18:46:45 -0800400
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700401 updateWorldState();
Marco Nelissen838dedb2009-11-03 17:01:21 -0800402 }
403
Alex Sakhartchouk7eb6bcc2010-05-11 14:22:20 -0700404 protected void updateWorldState() {
405 mScript.set_gAngle(mWorldState.mAngle);
406 mScript.set_gPeak(mWorldState.mPeak);
407 mScript.set_gRotate(mWorldState.mRotate);
408 mScript.set_gTilt(mWorldState.mTilt);
409 mScript.set_gIdle(mWorldState.mIdle);
410 mScript.set_gWaveCounter(mWorldState.mWaveCounter);
411 }
Marco Nelissen838dedb2009-11-03 17:01:21 -0800412}