问题描述
我试图创建的Android谱图可视化。我用的是Android设备来记录声音.PCM格式,然后将它转换为.WAV,以便它可以分析使用musicg库的
使用musicg我可以创建记录.WAV的谱图和从该谱图我可以提取频率的时域数据作为双[] []。
什么我想下一个工作了是如何在可视化的Android这个数据。任何帮助将是AP preciated。
随着user3161880答案,我曾尝试以下,但我没有得到任何东西吸引到屏幕上。我可以画一个圆,如图user3161880答案。任何想法,为什么这是行不通的?
私有静态类SpectrogramView扩展视图{
民营涂料粉刷=新的油漆();
私人双[] []的数据;
公共SpectrogramView(上下文的背景下,双[] []数据){
超(上下文);
this.data =数据;
}
@覆盖
保护无效的OnDraw(帆布油画){
super.onDraw(画布);
如果(数据!= NULL){
paint.setStrokeWidth(1);
canvas.drawColor(Color.WHITE);
INT宽度= data.length;
INT高=数据[0] .length;
的for(int i = 0; I<宽度;我++){
对于(INT J = 0; J<高度; J ++){
int值;
值= 255 - (INT)(数据[I] [J] * 255);
paint.setColor(值小于;< 16 |值小于;< 8 |值| 255<< 24);
canvas.drawPoint(ⅰ,高度-1-j中,油漆);
}
}
} 其他 {
通信System.err.println(数据损坏);
}
//画出圆
/*paint.setColor(Color.RED);
canvas.drawColor(Color.BLUE);
canvas.drawCircle(100.0f,100.0f,50.0f,油漆); * /
}
}
比我原来的职位的改进的答案。
私有静态类SpectrogramView扩展视图{
民营涂料粉刷=新的油漆();
私人BMP位图;
公共SpectrogramView(上下文的背景下,双[] []数据){
超(上下文);
如果(数据!= NULL){
paint.setStrokeWidth(1);
INT宽度= data.length;
INT高=数据[0] .length;
INT [] arrayCol =新INT [宽*高]。
INT计数器= 0;
的for(int i = 0; I<高度;我++){
对于(INT J = 0; J<宽度; J ++){
int值;
INT的颜色;
值= 255 - (int)的(数据[j]的[I] * 255);
颜色=(值小于;< 16 |值小于;< 8 |值| 255<< 24);
arrayCol [计数器] =颜色;
反++;
}
}
BMP = Bitmap.createBitmap(arrayCol,宽度,高度,Config.ARGB_8888);
} 其他 {
通信System.err.println(数据损坏);
}
}
@覆盖
保护无效的OnDraw(帆布油画){
super.onDraw(画布);
canvas.drawBitmap(BMP,0,100,漆);
}
}
这将通过绘制到画布上之前创建一个位图来提高性能。
I am attempting to create a spectrogram visualisation in Android. I use the android device to record sound in .PCM format and then convert this to .WAV so that it can be analysed using the musicg library https://code.google.com/p/musicg/.
Using musicg I can create a spectrogram of the recorded .WAV and from this spectrogram I can extract the frequency time domain data as a double[][].
What I am trying to work out next is how to visualise this data in Android. Any help would be appreciated.
Following user3161880 answer, I have tried the following but I am not getting anything drawn to screen. I can draw a circle as shown in user3161880 answer. Any ideas why this wouldn't work?
private static class SpectrogramView extends View {
private Paint paint = new Paint();
private double [][] data;
public SpectrogramView(Context context, double [][] data) {
super(context);
this.data = data;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (data != null) {
paint.setStrokeWidth(1);
canvas.drawColor(Color.WHITE);
int width = data.length;
int height = data[0].length;
for(int i = 0; i < width; i++) {
for(int j = 0; j < height; j++) {
int value;
value = 255 - (int)(data[i][j] * 255);
paint.setColor(value<<16|value<<8|value|255<<24);
canvas.drawPoint(i, height-1-j, paint);
}
}
} else {
System.err.println("Data Corrupt");
}
//draw circle
/*paint.setColor(Color.RED);
canvas.drawColor(Color.BLUE);
canvas.drawCircle(100.0f, 100.0f, 50.0f, paint);*/
}
}
An improved answer than in my original post.
private static class SpectrogramView extends View {
private Paint paint = new Paint();
private Bitmap bmp;
public SpectrogramView(Context context, double [][] data) {
super(context);
if (data != null) {
paint.setStrokeWidth(1);
int width = data.length;
int height = data[0].length;
int[] arrayCol = new int[width*height];
int counter = 0;
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
int value;
int color;
value = 255 - (int)(data[j][i] * 255);
color = (value<<16|value<<8|value|255<<24);
arrayCol[counter] = color;
counter ++;
}
}
bmp = Bitmap.createBitmap(arrayCol, width, height, Config.ARGB_8888);
} else {
System.err.println("Data Corrupt");
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bmp, 0, 100, paint);
}
}
This will improve performance by creating a bitmap before drawing to canvas.
这篇关于使用musicg库创建谱图的曲线图的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!