Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.musicvis.vis5; |
| 18 | |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 19 | import com.android.musicvis.R; |
| 20 | import com.android.musicvis.RenderScriptScene; |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 21 | import com.android.musicvis.ScriptField_Vertex; |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 22 | |
| 23 | import android.media.MediaPlayer; |
| 24 | import android.os.Handler; |
| 25 | import android.renderscript.Allocation; |
| 26 | import android.renderscript.Element; |
| 27 | import android.renderscript.Primitive; |
| 28 | import android.renderscript.ProgramFragment; |
| 29 | import android.renderscript.ProgramStore; |
| 30 | import android.renderscript.ProgramVertex; |
| 31 | import android.renderscript.Sampler; |
| 32 | import android.renderscript.ScriptC; |
| 33 | import android.renderscript.SimpleMesh; |
| 34 | import android.renderscript.Type; |
| 35 | import android.renderscript.Element.Builder; |
| 36 | import android.renderscript.ProgramStore.BlendDstFunc; |
| 37 | import android.renderscript.ProgramStore.BlendSrcFunc; |
Marco Nelissen | 24c4457 | 2009-11-20 11:30:32 -0800 | [diff] [blame] | 38 | import android.renderscript.Sampler.Value; |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 39 | import android.util.Log; |
| 40 | import android.view.MotionEvent; |
| 41 | |
| 42 | import java.util.TimeZone; |
| 43 | |
| 44 | class 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 Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 59 | |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 60 | 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 Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 69 | |
| 70 | ScriptC_Many mScript; |
| 71 | private ScriptField_Vertex mVertexBuffer; |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 72 | |
| 73 | private ProgramStore mPfsBackground; |
Marco Nelissen | 24c4457 | 2009-11-20 11:30:32 -0800 | [diff] [blame] | 74 | private ProgramFragment mPfBackgroundMip; |
| 75 | private ProgramFragment mPfBackgroundNoMip; |
| 76 | private Sampler mSamplerMip; |
| 77 | private Sampler mSamplerNoMip; |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 78 | private Allocation[] mTextures; |
Jason Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 79 | |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 80 | 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 Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 93 | |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 94 | 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 Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 102 | |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 103 | 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 Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 129 | /*@Override |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 130 | 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 Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 147 | //updateWorldState(); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 148 | } |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 149 | }*/ |
Jason Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 150 | |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 151 | @Override |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 152 | public void setOffset(float xOffset, float yOffset, int xPixels, int yPixels) { |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 153 | // update our state, then push it to the renderscript |
| 154 | mWorldState.mRotate = (xOffset - 0.5f) * 90; |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 155 | updateWorldState(); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | protected ScriptC createScript() { |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 160 | mScript = new ScriptC_Many(mRS, mResources, R.raw.many_bc, true); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 161 | |
| 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 Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 170 | mScript.set_gPVBackground(mPVBackground); |
| 171 | |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 172 | mTextures = new Allocation[8]; |
Marco Nelissen | 24c4457 | 2009-11-20 11:30:32 -0800 | [diff] [blame] | 173 | mTextures[0] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.background, Element.RGBA_8888(mRS), true); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 174 | mTextures[0].setName("Tvumeter_background"); |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 175 | mScript.set_gTvumeter_background(mTextures[0]); |
Marco Nelissen | 24c4457 | 2009-11-20 11:30:32 -0800 | [diff] [blame] | 176 | mTextures[1] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.frame, Element.RGBA_8888(mRS), true); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 177 | mTextures[1].setName("Tvumeter_frame"); |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 178 | mScript.set_gTvumeter_frame(mTextures[1]); |
Marco Nelissen | 24c4457 | 2009-11-20 11:30:32 -0800 | [diff] [blame] | 179 | mTextures[2] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.peak_on, Element.RGBA_8888(mRS), true); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 180 | mTextures[2].setName("Tvumeter_peak_on"); |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 181 | mScript.set_gTvumeter_peak_on(mTextures[2]); |
Marco Nelissen | 24c4457 | 2009-11-20 11:30:32 -0800 | [diff] [blame] | 182 | mTextures[3] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.peak_off, Element.RGBA_8888(mRS), true); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 183 | mTextures[3].setName("Tvumeter_peak_off"); |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 184 | mScript.set_gTvumeter_peak_off(mTextures[3]); |
Marco Nelissen | 24c4457 | 2009-11-20 11:30:32 -0800 | [diff] [blame] | 185 | mTextures[4] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.needle, Element.RGBA_8888(mRS), true); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 186 | mTextures[4].setName("Tvumeter_needle"); |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 187 | mScript.set_gTvumeter_needle(mTextures[4]); |
| 188 | mTextures[5] = Allocation.createFromBitmapResourceBoxed(mRS, mResources, R.drawable.black, Element.RGB_565(mRS), true); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 189 | mTextures[5].setName("Tvumeter_black"); |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 190 | mScript.set_gTvumeter_black(mTextures[5]); |
Marco Nelissen | 24c4457 | 2009-11-20 11:30:32 -0800 | [diff] [blame] | 191 | mTextures[6] = Allocation.createFromBitmapResource(mRS, mResources, R.drawable.albumart, Element.RGBA_8888(mRS), true); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 192 | mTextures[6].setName("Tvumeter_album"); |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 193 | mScript.set_gTvumeter_album(mTextures[6]); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 194 | mTextures[7] = Allocation.createFromBitmapResource(mRS, mResources, R.drawable.fire, Element.RGB_565(mRS), false); |
| 195 | mTextures[7].setName("Tlinetexture"); |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 196 | mScript.set_gTlinetexture(mTextures[7]); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 197 | |
| 198 | final int count = mTextures.length; |
| 199 | for (int i = 0; i < count; i++) { |
| 200 | mTextures[i].uploadToTexture(0); |
| 201 | } |
Marco Nelissen | 24c4457 | 2009-11-20 11:30:32 -0800 | [diff] [blame] | 202 | |
| 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 Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 220 | |
| 221 | { |
Jason Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 222 | ProgramFragment.Builder builder = new ProgramFragment.Builder(mRS); |
| 223 | builder.setTexture(ProgramFragment.Builder.EnvMode.REPLACE, |
| 224 | ProgramFragment.Builder.Format.RGBA, 0); |
Marco Nelissen | 24c4457 | 2009-11-20 11:30:32 -0800 | [diff] [blame] | 225 | mPfBackgroundNoMip = builder.create(); |
| 226 | mPfBackgroundNoMip.setName("PFBackgroundNoMip"); |
| 227 | mPfBackgroundNoMip.bindSampler(mSamplerNoMip, 0); |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 228 | mScript.set_gPFBackgroundNoMip(mPfBackgroundNoMip); |
Marco Nelissen | 24c4457 | 2009-11-20 11:30:32 -0800 | [diff] [blame] | 229 | } |
Jason Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 230 | |
Marco Nelissen | 24c4457 | 2009-11-20 11:30:32 -0800 | [diff] [blame] | 231 | { |
Jason Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 232 | ProgramFragment.Builder builder = new ProgramFragment.Builder(mRS); |
| 233 | builder.setTexture(ProgramFragment.Builder.EnvMode.REPLACE, |
| 234 | ProgramFragment.Builder.Format.RGBA, 0); |
Marco Nelissen | 24c4457 | 2009-11-20 11:30:32 -0800 | [diff] [blame] | 235 | mPfBackgroundMip = builder.create(); |
| 236 | mPfBackgroundMip.setName("PFBackgroundMip"); |
| 237 | mPfBackgroundMip.bindSampler(mSamplerMip, 0); |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 238 | mScript.set_gPFBackgroundMip(mPfBackgroundMip); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 239 | } |
| 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 Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 250 | |
| 251 | mScript.set_gPFSBackground(mPfsBackground); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 252 | } |
Jason Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 253 | |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 254 | // Start creating the mesh |
| 255 | final SimpleMesh.Builder meshBuilder = new SimpleMesh.Builder(mRS); |
| 256 | |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 257 | mVertexBuffer = new ScriptField_Vertex(mRS, mPointData.length / 4); |
| 258 | |
| 259 | final int vertexSlot = meshBuilder.addVertexType(mVertexBuffer.getType()); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 260 | // 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 Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 268 | |
| 269 | mCubeMesh.bindVertexAllocation(mVertexBuffer.getAllocation(), 0); |
| 270 | |
| 271 | mPointAlloc = mVertexBuffer.getAllocation(); |
| 272 | |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 273 | mPointAlloc = mCubeMesh.createVertexAllocation(vertexSlot); |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 274 | |
| 275 | mScript.bind_gPoints(mPointAlloc); |
| 276 | mScript.set_gPointBuffer(mPointAlloc); |
| 277 | mScript.set_gCubeMesh(mCubeMesh); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 278 | |
| 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 Sams | a811c21 | 2010-05-11 14:00:38 -0700 | [diff] [blame] | 286 | // put the vertex and index data in their respective buffers |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 287 | updateWave(); |
| 288 | for(int i = 0; i < mIndexData.length; i ++) { |
| 289 | mIndexData[i] = (short) i; |
| 290 | } |
| 291 | |
Jason Sams | a811c21 | 2010-05-11 14:00:38 -0700 | [diff] [blame] | 292 | // upload the vertex and index data |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 293 | mPointAlloc.data(mPointData); |
| 294 | mPointAlloc.uploadToBufferObject(); |
| 295 | mLineIdxAlloc.data(mIndexData); |
| 296 | mLineIdxAlloc.uploadToBufferObject(); |
| 297 | |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 298 | mScript.setClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 299 | |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 300 | return mScript; |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 301 | } |
| 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 Nelissen | d760ff3 | 2009-11-10 14:49:08 -0800 | [diff] [blame] | 318 | if (!mVisible) { |
| 319 | return; |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 320 | } |
Marco Nelissen | d760ff3 | 2009-11-10 14:49:08 -0800 | [diff] [blame] | 321 | mHandler.postDelayed(mDrawCube, 20); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 322 | |
| 323 | int len = MediaPlayer.snoop(mVizData, 0); |
Jason Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 324 | |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 325 | // 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 Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 350 | |
| 351 | |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 352 | // 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 Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 377 | |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 378 | // downsample 1024 samples in to 256 |
Jason Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 379 | |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 380 | 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 Sams | 27b89e7 | 2009-12-17 18:46:45 -0800 | [diff] [blame] | 400 | |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 401 | updateWorldState(); |
Marco Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Alex Sakhartchouk | 7eb6bcc | 2010-05-11 14:22:20 -0700 | [diff] [blame^] | 404 | 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 Nelissen | 838dedb | 2009-11-03 17:01:21 -0800 | [diff] [blame] | 412 | } |