Home | History | Annotate | Download | only in effectstest
      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.effectstest;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.os.Bundle;
     23 import android.util.Log;
     24 import android.view.KeyEvent;
     25 import android.view.Menu;
     26 import android.view.View.OnClickListener;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import android.widget.Button;
     30 import android.widget.TextView;
     31 import android.widget.EditText;
     32 import android.widget.SeekBar;
     33 import android.widget.ToggleButton;
     34 import android.widget.CompoundButton;
     35 import android.widget.CompoundButton.OnCheckedChangeListener;
     36 import java.nio.ByteOrder;
     37 import java.nio.ByteBuffer;
     38 import java.util.HashMap;
     39 import java.util.Map;
     40 
     41 import android.media.audiofx.Virtualizer;
     42 import android.media.audiofx.AudioEffect;
     43 
     44 public class VirtualizerTest extends Activity implements OnCheckedChangeListener {
     45 
     46     private final static String TAG = "VirtualizerTest";
     47 
     48     private static int NUM_PARAMS = 1;
     49 
     50     private EffectParameter mStrength;
     51     private Virtualizer mVirtualizer;
     52     ToggleButton mOnOffButton;
     53     ToggleButton mReleaseButton;
     54     EditText mSessionText;
     55     static int sSession = 0;
     56     EffectListner mEffectListener = new EffectListner();
     57     private static HashMap<Integer, Virtualizer> sInstances = new HashMap<Integer, Virtualizer>(10);
     58     String mSettings = "";
     59 
     60     public VirtualizerTest() {
     61         Log.d(TAG, "contructor");
     62     }
     63 
     64     @Override
     65     public void onCreate(Bundle icicle) {
     66         super.onCreate(icicle);
     67 
     68         SeekBar seekBar;
     69         TextView textView;
     70 
     71         setContentView(R.layout.virtualizertest);
     72 
     73         mSessionText = (EditText) findViewById(R.id.sessionEdit);
     74         mSessionText.setOnKeyListener(mSessionKeyListener);
     75         mSessionText.setText(Integer.toString(sSession));
     76 
     77         mReleaseButton = (ToggleButton)findViewById(R.id.virtReleaseButton);
     78         mOnOffButton = (ToggleButton)findViewById(R.id.virtualizerOnOff);
     79 
     80         getEffect(sSession);
     81 
     82         if (mVirtualizer != null) {
     83             mReleaseButton.setOnCheckedChangeListener(this);
     84             mOnOffButton.setOnCheckedChangeListener(this);
     85             textView = (TextView)findViewById(R.id.virtStrengthMin);
     86             textView.setText("0");
     87             textView = (TextView)findViewById(R.id.virtStrengthMax);
     88             textView.setText("1000");
     89             seekBar = (SeekBar)findViewById(R.id.virtStrengthSeekBar);
     90             textView = (TextView)findViewById(R.id.virtStrengthValue);
     91             mStrength = new VirtualizerParam(mVirtualizer, 0, 1000, seekBar, textView);
     92             seekBar.setOnSeekBarChangeListener(mStrength);
     93             mStrength.setEnabled(mVirtualizer.getStrengthSupported());
     94         }
     95     }
     96 
     97     @Override
     98     public void onResume() {
     99         super.onResume();
    100     }
    101 
    102     @Override
    103     public void onPause() {
    104         super.onPause();
    105     }
    106 
    107     private View.OnKeyListener mSessionKeyListener
    108     = new View.OnKeyListener() {
    109         public boolean onKey(View v, int keyCode, KeyEvent event) {
    110             if (event.getAction() == KeyEvent.ACTION_DOWN) {
    111                 switch (keyCode) {
    112                     case KeyEvent.KEYCODE_DPAD_CENTER:
    113                     case KeyEvent.KEYCODE_ENTER:
    114                         try {
    115                             sSession = Integer.parseInt(mSessionText.getText().toString());
    116                             getEffect(sSession);
    117                             if (mVirtualizer != null) {
    118                                 mStrength.setEffect(mVirtualizer);
    119                                 mStrength.setEnabled(mVirtualizer.getStrengthSupported());
    120                             }
    121                         } catch (NumberFormatException e) {
    122                             Log.d(TAG, "Invalid session #: "+mSessionText.getText().toString());
    123                         }
    124                         return true;
    125                 }
    126             }
    127             return false;
    128         }
    129     };
    130 
    131     // OnCheckedChangeListener
    132     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    133         if (buttonView.getId() == R.id.virtualizerOnOff) {
    134             if (mVirtualizer != null) {
    135                 mVirtualizer.setEnabled(isChecked);
    136                 mStrength.updateDisplay();
    137             }
    138         }
    139         if (buttonView.getId() == R.id.virtReleaseButton) {
    140             if (isChecked) {
    141                 if (mVirtualizer == null) {
    142                     getEffect(sSession);
    143                     if (mVirtualizer != null) {
    144                         mStrength.setEffect(mVirtualizer);
    145                         mStrength.setEnabled(mVirtualizer.getStrengthSupported());
    146                     }
    147                 }
    148             } else {
    149                 if (mVirtualizer != null) {
    150                     mStrength.setEnabled(false);
    151                     putEffect(sSession);
    152                 }
    153             }
    154         }
    155     }
    156 
    157     private class VirtualizerParam extends EffectParameter {
    158         private Virtualizer mVirtualizer;
    159 
    160         public VirtualizerParam(Virtualizer virtualizer, int min, int max, SeekBar seekBar, TextView textView) {
    161             super (min, max, seekBar, textView, "o/oo");
    162 
    163             mVirtualizer = virtualizer;
    164             updateDisplay();
    165         }
    166 
    167         @Override
    168         public void setParameter(Integer value) {
    169             if (mVirtualizer != null) {
    170                 mVirtualizer.setStrength(value.shortValue());
    171             }
    172         }
    173 
    174         @Override
    175         public Integer getParameter() {
    176             if (mVirtualizer != null) {
    177                 return new Integer(mVirtualizer.getRoundedStrength());
    178             }
    179             return new Integer(0);
    180         }
    181 
    182         @Override
    183         public void setEffect(Object effect) {
    184             mVirtualizer = (Virtualizer)effect;
    185         }
    186     }
    187 
    188     public class EffectListner implements AudioEffect.OnEnableStatusChangeListener,
    189         AudioEffect.OnControlStatusChangeListener, AudioEffect.OnParameterChangeListener
    190    {
    191         public EffectListner() {
    192         }
    193         public void onEnableStatusChange(AudioEffect effect, boolean enabled) {
    194             Log.d(TAG,"onEnableStatusChange: "+ enabled);
    195         }
    196         public void onControlStatusChange(AudioEffect effect, boolean controlGranted) {
    197             Log.d(TAG,"onControlStatusChange: "+ controlGranted);
    198         }
    199         public void onParameterChange(AudioEffect effect, int status, byte[] param, byte[] value) {
    200             int p = byteArrayToInt(param, 0);
    201             short v = byteArrayToShort(value, 0);
    202 
    203             Log.d(TAG,"onParameterChange, status: "+status+" p: "+p+" v: "+v);
    204         }
    205 
    206         private int byteArrayToInt(byte[] valueBuf, int offset) {
    207             ByteBuffer converter = ByteBuffer.wrap(valueBuf);
    208             converter.order(ByteOrder.nativeOrder());
    209             return converter.getInt(offset);
    210 
    211         }
    212         private short byteArrayToShort(byte[] valueBuf, int offset) {
    213             ByteBuffer converter = ByteBuffer.wrap(valueBuf);
    214             converter.order(ByteOrder.nativeOrder());
    215             return converter.getShort(offset);
    216 
    217         }
    218     }
    219 
    220     private void getEffect(int session) {
    221         synchronized (sInstances) {
    222             if (sInstances.containsKey(session)) {
    223                 mVirtualizer = sInstances.get(session);
    224             } else {
    225                 try{
    226                     mVirtualizer = new Virtualizer(0, session);
    227                 } catch (IllegalArgumentException e) {
    228                     Log.e(TAG,"Virtualizer effect not supported");
    229                 } catch (IllegalStateException e) {
    230                     Log.e(TAG,"Virtualizer cannot get strength supported");
    231                 } catch (UnsupportedOperationException e) {
    232                     Log.e(TAG,"Virtualizer library not loaded");
    233                 } catch (RuntimeException e) {
    234                     Log.e(TAG,"Virtualizer effect not found");
    235                 }
    236                 sInstances.put(session, mVirtualizer);
    237             }
    238         }
    239         mReleaseButton.setEnabled(false);
    240         mOnOffButton.setEnabled(false);
    241 
    242         if (mVirtualizer != null) {
    243             if (mSettings != "") {
    244                 mVirtualizer.setProperties(new Virtualizer.Settings(mSettings));
    245             }
    246             mVirtualizer.setEnableStatusListener(mEffectListener);
    247             mVirtualizer.setControlStatusListener(mEffectListener);
    248             mVirtualizer.setParameterListener(mEffectListener);
    249 
    250             mReleaseButton.setChecked(true);
    251             mReleaseButton.setEnabled(true);
    252 
    253             mOnOffButton.setChecked(mVirtualizer.getEnabled());
    254             mOnOffButton.setEnabled(true);
    255         }
    256     }
    257 
    258     private void putEffect(int session) {
    259         mOnOffButton.setChecked(false);
    260         mOnOffButton.setEnabled(false);
    261         synchronized (sInstances) {
    262             if (mVirtualizer != null) {
    263                 mSettings = mVirtualizer.getProperties().toString();
    264                 mVirtualizer.release();
    265                 Log.d(TAG,"Virtualizer released");
    266                 mVirtualizer = null;
    267                 sInstances.remove(session);
    268             }
    269         }
    270     }
    271 }
    272