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.vis2;
18 
19 import com.android.musicvis.GenericWaveRS;
20 import com.android.musicvis.R;
21 import com.android.musicvis.AudioCapture;
22 import android.util.Log;
23 
24 import android.media.MediaPlayer;
25 
26 class Visualization2RS extends GenericWaveRS {
27 
Visualization2RS(int width, int height)28     Visualization2RS(int width, int height) {
29         super(width, height, R.drawable.fire);
30     }
31 
32     @Override
start()33     public void start() {
34         if (mAudioCapture == null) {
35             mAudioCapture = new AudioCapture(AudioCapture.TYPE_PCM, 1024);
36         }
37         super.start();
38     }
39 
40     @Override
stop()41     public void stop() {
42         super.stop();
43         if (mAudioCapture != null) {
44             mAudioCapture.release();
45             mAudioCapture = null;
46         }
47     }
48 
49     @Override
update()50     public void update() {
51         int len = 0;
52         if (mAudioCapture != null) {
53             mVizData = mAudioCapture.getFormattedData(1,1);
54             len = mVizData.length;
55         }
56 
57         if (len == 0) {
58             if (mWorldState.idle == 0) {
59                 mWorldState.idle = 1;
60                 //mState.data(mWorldState);
61                 updateWorldState();
62             }
63             return;
64         }
65 
66         int outlen = mPointData.length / 8;
67         if (len > outlen) len = outlen;
68 
69         if (mWorldState.idle != 0) {
70             mWorldState.idle = 0;
71             //mState.data(mWorldState);
72             updateWorldState();
73         }
74         // TODO: might be more efficient to push this in to renderscript
75         for(int i = 0; i < len; i++) {
76             int amp = mVizData[i];
77             mPointData[i*8+1] = amp;
78             mPointData[i*8+5] = -amp;
79         }
80         mPointAlloc.copyFromUnchecked(mPointData);
81     }
82 
83 }
84