How to show/draw the spectrum frequency from line into bar shape (android)? -
i learning how make spectrum analyzer. learn tutorial , here's code
package com.example.prasetyo.spectrumanalyzer; import android.app.activity; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.media.audioformat; import android.media.audiorecord; import android.media.mediarecorder; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.imageview; import ca.uol.aig.fftpack.realdoublefft; /*public class spectrumactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_spectrum); }*/ public class spectrumactivity extends activity implements onclicklistener { public void onclick(view v) { if (started) { started = false; startstopbutton.settext("start"); recordtask.cancel(true); } else { started = true; startstopbutton.settext("stop"); recordtask = new recordaudio(); recordtask.execute(); } } int frequency = 8000; int channelconfiguration = audioformat.channel_in_mono; int audioencoding = audioformat.encoding_pcm_16bit; private realdoublefft transformer; int blocksize = 256; button startstopbutton; boolean started = false; recordaudio recordtask; imageview imageview; bitmap bitmap; canvas canvas; paint paint; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_spectrum); startstopbutton = (button) this.findviewbyid(r.id.startstopbutton); startstopbutton.setonclicklistener(this); transformer = new realdoublefft(blocksize); imageview = (imageview) this.findviewbyid(r.id.imageview01); bitmap = bitmap.createbitmap(256, 100, bitmap.config.argb_8888); canvas = new canvas(bitmap); paint = new paint(); paint.setcolor(color.green); imageview.setimagebitmap(bitmap); } /*public void onclick(view v) { if (started) { started = false; startstopbutton.settext("start"); recordtask.cancel(true); } else { started = true; startstopbutton.settext("stop"); recordtask = new recordaudio(); recordtask.execute(); } }*/ private class recordaudio extends asynctask<void, double[], void> { @override protected void doinbackground(void... params) { if(iscancelled()){ return null; } //try { int buffersize = audiorecord.getminbuffersize(frequency, channelconfiguration, audioencoding); audiorecord audiorecord = new audiorecord(mediarecorder.audiosource.mic, frequency, channelconfiguration, audioencoding, buffersize); short[] buffer = new short[blocksize]; double[] totransform = new double[blocksize]; try { audiorecord.startrecording(); } catch (illegalstateexception e) { log.e("recording failed", e.tostring()); } while (started) { int bufferreadresult = audiorecord.read(buffer, 0, blocksize); (int = 0; < blocksize && < bufferreadresult; i++) { totransform[i] = (double) buffer[i] / 32768.0; // signed 16 bit } transformer.ft(totransform); publishprogress(totransform); //log.e("audiorecord", "recording failed"); //return null; } return null; } protected void onprogressupdate(double[]... totransform) { canvas.drawcolor(color.black); (int = 0; < totransform[0].length; i++) { int x; x = i; int downy = (int) (100 - (totransform[0][i] * 10)); int upy = 100; canvas.drawline(x, downy, x, upy, paint); imageview.invalidate(); } } /*public void onclick(view v) { if (started) { started = false; startstopbutton.settext("start"); recordtask.cancel(true); } else { started = true; startstopbutton.settext("stop"); recordtask = new recordaudio(); recordtask.execute(); } }*/ } }
spectrum show line each frequency. if i'm not mistaken, showed line appear because of drawline method. there way show frequency in other shape (bar)? need :) here's illustration here ilustration https://drive.google.com/open?id=0b3jsxxkra7plclfymddmulk5meu&authuser=0
you can replace canvas.drawline(x, downy, x, upy, paint);
canvas.drawrect(x * 5, downy, x * 5 + 4, upy, paint);
Comments
Post a Comment