如何改变声音可视化输出? [英] How to change sound visualiser output?

查看:67
本文介绍了如何改变声音可视化输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在开发一个基于java的Android应用程序来识别独特的声音模式。我想在下面的URL中显示它。







我试过更改代码位,但无法获得预期的显示。希望任何人都能找到问题或建议解决这个问题。



我的代码如下。



XML:activity_monitor.xml



Hi,
I’m developing an android application based on java to identify unique sound pattern. I want to display it as in the image in the following URL.



I tried changing the code bit, but couldn’t get the expected display. Hope any one could find the problem or suggest a solution for this problem.

My codes are as follows.

XML: activity_monitor.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:paddingBottom="@dimen/activity_vertical_margin"

    tools:context=".Monitor">
    <ImageView

        android:id="@+id/ImageView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />
    <Button

        android:text="@string/start"

        android:id="@+id/start"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_centerHorizontal="true" />
</RelativeLayout>



活动类:Monitor.java




Activity class: Monitor.java

package com.ase.visualiser;

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.widget.Button;
import android.widget.ImageView;

public class Monitor extends Activity implements View.OnClickListener {

    int frequency = 8000;
    int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
    int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
    public RealDoubleFFT transformer;
    int blockSize = 512;

    Button start;

    boolean started = false;

    RecordAudio recordTask;

    ImageView imageView;
    Bitmap bitmap;
    Canvas canvas;
    Paint paint;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from activity_monitor.xml
        setContentView(R.layout.activity_monitor);

        start = (Button) this.findViewById(R.id.start);
        start.setOnClickListener(this);

        transformer = new RealDoubleFFT();

        imageView = (ImageView) this.findViewById(R.id.ImageView);
        bitmap = Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitmap);
        paint = new Paint();
        paint.setColor(Color.WHITE);
        imageView.setImageBitmap(bitmap);

    }

    class RecordAudio extends AsyncTask<Void, double[], Void> {

        @Override
        protected Void doInBackground(Void... arg0) {

            try {
                int bufferSize = AudioRecord.getMinBufferSize(frequency, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);

                AudioRecord audioRecord = new AudioRecord(
                        MediaRecorder.AudioSource.MIC, frequency,
                        channelConfiguration, audioEncoding, bufferSize);

                short[] buffer = new short[blockSize];
                double[] toTransform = new double[blockSize];

                audioRecord.startRecording();

                started = true;
                // This should true before calling following while loop

                while (started) {
                    int bufferReadResult = audioRecord.read(buffer, 0, blockSize);

                    for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
                        toTransform[i] = (double) buffer[i] / 1024.0;
                        // signed 16 bit 32768.0
                    }
                    transformer.ft();
                    publishProgress(toTransform);


                }

                audioRecord.stop();

            } catch (Throwable t) {
                t.printStackTrace();
                Log.e("AudioRecord", "Recording Failed");
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(double[]... toTransform) {

            canvas.drawColor(Color.BLACK);

            for (int i = 0; i < 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();

            super.onProgressUpdate(toTransform);
        }

    }
    public void onClick(View arg0) {

        if (started) {
            started = false;
            start.setText("Start");
            recordTask.cancel(true);
        } else {
            started = true;
            start.setText("Stop");
            recordTask = new RecordAudio();
            recordTask.execute();
        }
    }

    public class RealDoubleFFT {
        public RealDoubleFFT() {
        }

        public void ft() {

        }
    }
}





谢谢。



Thanks.

推荐答案

抱歉,我忘了粘贴网址。



http://stackoverflow.com/questions/2606985 / generate-sound-wave-image-from-mp3-wav-file [ ^ ]
Sorry I forgot to paste the URL.

http://stackoverflow.com/questions/2606985/generate-sound-wave-image-from-mp3-wav-file[^]


这篇关于如何改变声音可视化输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆