Visible DTMF Piano: Visualizer + ToneGenerator

Last post show a example of Visualizer for MediaPlayer, combine with the DTMF Piano using ToneGenerator, here is a Visible DTMF Piano, using Visualizer + ToneGenerator.

In this example, Visualizer is created with session 0, to make the audio output mix is visualized; permission MODIFY_AUDIO_SETTINGS is needed.



VisualizerView.java, extends View.
package com.example.androidaudiovisualizer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class VisualizerView extends View {

private byte[] mBytes;
private float[] mPoints;
private Rect mRect = new Rect();
private Paint mForePaint = new Paint();

public VisualizerView(Context context) {
super(context);
init();
}

public VisualizerView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public VisualizerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

private void init() {
mBytes = null;
mForePaint.setStrokeWidth(1f);
mForePaint.setAntiAlias(true);
mForePaint.setColor(Color.rgb(0, 128, 255));
}

public void updateVisualizer(byte[] bytes) {
mBytes = bytes;
invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mBytes == null) {
return;
}
if (mPoints == null || mPoints.length < mBytes.length * 4) {
mPoints = new float[mBytes.length * 4];
}
mRect.set(0, 0, getWidth(), getHeight());
for (int i = 0; i < mBytes.length - 1; i++) {
mPoints[i * 4] = mRect.width() * i / (mBytes.length - 1);
mPoints[i * 4 + 1] = mRect.height() / 2
+ ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128;
mPoints[i * 4 + 2] = mRect.width() * (i + 1) / (mBytes.length - 1);
mPoints[i * 4 + 3] = mRect.height() / 2
+ ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2)
/ 128;
}
canvas.drawLines(mPoints, mForePaint);
}

}

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidaudiovisualizer.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<com.example.androidaudiovisualizer.VisualizerView
android:id="@+id/myvisualizerview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<GridView
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:columnWidth="50dp"
android:gravity="center"
android:numColumns="3"
android:stretchMode="columnWidth" >
</GridView>

</LinearLayout>

MainActivity.java
package com.example.androidaudiovisualizer;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.media.audiofx.Visualizer;
import android.os.Bundle;

/*
* needed in AndroidManifest.xml
* android:minSdkVersion="9"
* uses-permission of "android.permission.RECORD_AUDIO"
* and "android.permission.MODIFY_AUDIO_SETTINGS"
*
* reference: Android demo example -
* ApiDemos > Media > AudioTx
*/

public class MainActivity extends ActionBarActivity {

VisualizerView mVisualizerView;
GridView DTMFPianoView;
static final String[] numbers = new String[] { "1", "2", "3", "4", "5",
"6", "7", "8", "9", "*", "0", "#" };

static final int[] toneTypes = new int[] { ToneGenerator.TONE_DTMF_1,
ToneGenerator.TONE_DTMF_2, ToneGenerator.TONE_DTMF_3,
ToneGenerator.TONE_DTMF_4, ToneGenerator.TONE_DTMF_5,
ToneGenerator.TONE_DTMF_6, ToneGenerator.TONE_DTMF_7,
ToneGenerator.TONE_DTMF_8, ToneGenerator.TONE_DTMF_9,
ToneGenerator.TONE_DTMF_S, ToneGenerator.TONE_DTMF_0,
ToneGenerator.TONE_DTMF_P
};

private Visualizer mVisualizer;

int streamType;
int volume;
int durationMs;
ToneGenerator toneGenerator;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVisualizerView = (VisualizerView) findViewById(R.id.myvisualizerview);

initAudio();

DTMFPianoView = (GridView) findViewById(R.id.gridView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, numbers);

DTMFPianoView.setAdapter(adapter);

DTMFPianoView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
toneGenerator.startTone(toneTypes[position], durationMs);

}});

}

@Override
protected void onPause() {
super.onPause();
if (isFinishing()) {
mVisualizer.release();
}
}

private void initAudio() {

streamType = AudioManager.STREAM_MUSIC;
volume = 50;
durationMs = 500;
toneGenerator = new ToneGenerator(streamType, volume);

setupVisualizerFxAndUI();
mVisualizer.setEnabled(true);

}

private void setupVisualizerFxAndUI() {

// Creating a Visualizer on the output mix (audio session 0)
// need permission MODIFY_AUDIO_SETTINGS
mVisualizer = new Visualizer(0);

mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
mVisualizer.setDataCaptureListener(
new Visualizer.OnDataCaptureListener() {
public void onWaveFormDataCapture(Visualizer visualizer,
byte[] bytes, int samplingRate) {
mVisualizerView.updateVisualizer(bytes);
}

public void onFftDataCapture(Visualizer visualizer,
byte[] bytes, int samplingRate) {
}
}, Visualizer.getMaxCaptureRate() / 2, true, false);

}

}

Permission of "android.permission.RECORD_AUDIO" and "android.permission.MODIFY_AUDIO_SETTINGS" are needed in AndroidManifest.xml.


download filesDownload the files.