6

我正在使用 TarsosDSP 实时计算音高频率。它使用实现 Runnable 的 AudioDispatcher 并通过 handlePitch 方法发布结果以在主线程中使用。

我正在使用 SurfaceView 在更新时绘制此值。SurfaceView 还需要另一个线程在画布上绘制。所以我有 2 个可运行的对象。我无法管理如何通过一个线程更新表面视图,同时从另一个线程(audiodispatcher)获取音高值。

我只想使用我在handlePitch() 方法中获得的美分值来更新我在surfaceview 上的绘图。但是我的应用程序冻结了。任何想法?

在 MainAcitivity.java (onCreate(...))

   myView = (MySurfaceView) findViewById(R.id.myview);

    int sr = 44100;//The sample rate
    int bs = 2048;
    AudioDispatcher d = AudioDispatcherFactory.fromDefaultMicrophone(sr,bs,0);
    PitchDetectionHandler printPitch = new PitchDetectionHandler() {
        @Override
        public void handlePitch(final PitchDetectionResult pitchDetectionResult, AudioEvent audioEvent) {
            final float p = pitchDetectionResult.getPitch();

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    if (p != -1){
                        float cent = (float) (1200*Math.log(p/8.176)/Math.log(2)) % 12;
                        System.out.println(cent);
                        myView.setCent(cent);
                    }
                }
            });
        }
    };

    PitchProcessor.PitchEstimationAlgorithm algo = PitchProcessor.PitchEstimationAlgorithm.YIN; //use YIN
    AudioProcessor pitchEstimator = new PitchProcessor(algo, sr,bs,printPitch);
    d.addAudioProcessor(pitchEstimator);
    d.run();//starts the dispatching process
    AudioProcessor p = new PitchProcessor(algo, sr, bs, printPitch);
    d.addAudioProcessor(p);
    new Thread(d,"Audio Dispatcher").start();

在 SurfaceView.java 中(下面的代码由构造函数触发)

    myThread = new MyThread(this);
    surfaceHolder = getHolder();
    bmpIcon = BitmapFactory.decodeResource(getResources(),
            R.mipmap.ic_launcher);

    iconWidth = bmpIcon.getWidth();
    iconHeight = bmpIcon.getHeight();
    density = getResources().getDisplayMetrics().scaledDensity;
    setLabelTextSize(Math.round(DEFAULT_LABEL_TEXT_SIZE_DP * density));

    surfaceHolder.addCallback(new SurfaceHolder.Callback(){

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            myThread.setRunning(true);
            myThread.start();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder,
                                   int format, int width, int height) {
            // TODO Auto-generated method stub

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            myThread.setRunning(false);
            while (retry) {
                try {
                    myThread.join();
                    retry = false;
                } catch (InterruptedException e) {
                }
            }
        }});


      protected void drawSomething(Canvas canvas) {

         updateCanvas(canvas, this.cent); //draws some lines depending on the cent value
      }


    public void setCent(double cent) {

        if (this.cent > maxCent)
            this.cent = maxCent;
        this.cent = cent;
    }

更新:

MyThread.java

public class MyThread extends Thread {

    MySurfaceView myView;
    private boolean running = false;

    public MyThread(MySurfaceView view) {
        myView = view;
    }

    public void setRunning(boolean run) {
        running = run;
    }

    @Override
    public void run() {
        while(running){

            Canvas canvas = myView.getHolder().lockCanvas();

            if(canvas != null){
                synchronized (myView.getHolder()) {
                    myView.drawSomething(canvas);
                }
                myView.getHolder().unlockCanvasAndPost(canvas);
            }

            try {
                sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
4

1 回答 1

2

如果我正确理解了您的问题,那么您有一个独立的事件源在其自己的线程(PitchDetectionHandler)上工作,并且SurfaceView当来自源的事件发生时,您希望在自己的线程上重新绘制。如果是这种情况,那么我认为整个想法sleep(1000)都是错误的。您应该跟踪实际事件并对它们做出反应,而不是等待它们睡觉。似乎在 Android 上最简单的解决方案是使用HandlerThread//基础设施LooperHandler如下所示:

注意以下代码中的错误;我不仅没有尝试过,而且我什至没有编译它。

import android.graphics.Canvas;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.view.SurfaceHolder;


public class SurfacePitchDrawingHelper implements Handler.Callback, SurfaceHolder.Callback2 {

    private static final int MSG_DRAW = 100;
    private static final int MSG_FORCE_REDRAW = 101;

    private final Object _lock = new Object();
    private SurfaceHolder _surfaceHolder;
    private HandlerThread _drawingThread;
    private Handler _handler;

    private float _lastDrawnCent;
    private volatile float _lastCent;

    private final boolean _processOnlyLast = true;

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        synchronized (_lock) {
            _surfaceHolder = holder;

            _drawingThread = new HandlerThread("SurfaceDrawingThread") {
                @Override
                protected void onLooperPrepared() {
                    super.onLooperPrepared();
                }
            };

            _drawingThread.start();
            _handler = new Handler(_drawingThread.getLooper(), this); // <-- this is where bug was
            _lastDrawnCent = Float.NaN;
            //postForceRedraw(); // if needed
        }
    }


    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        synchronized (_lock) {
            // clean queue and kill looper
            _handler.removeCallbacksAndMessages(null);
            _drawingThread.getLooper().quit();

            while (true) {
                try {
                    _drawingThread.join();
                    break;
                } catch (InterruptedException e) {
                }
            }

            _handler = null;
            _drawingThread = null;
            _surfaceHolder = null;
        }
    }

    @Override
    public void surfaceRedrawNeeded(SurfaceHolder holder) {
        postForceRedraw();
    }


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        synchronized (_lock) {
            _surfaceHolder = holder;
        }
        postForceRedraw();
    }

    private void postForceRedraw() {
        _handler.sendEmptyMessage(MSG_FORCE_REDRAW);
    }

    public void postRedraw(float cent) {
        if (_processOnlyLast) {
            _lastCent = cent;
            _handler.sendEmptyMessage(MSG_DRAW);
        } else {
            Message message = _handler.obtainMessage(MSG_DRAW);
            message.obj = Float.valueOf(cent);
            _handler.sendMessage(message);
        }
    }


    private void doRedraw(Canvas canvas, float cent) {
        // put actual painting logic here
    }

    @Override
    public boolean handleMessage(Message msg) {
        float lastCent = _processOnlyLast ? _lastCent : ((Float) msg.obj).floatValue();
        boolean shouldRedraw = (MSG_FORCE_REDRAW == msg.what)
                || ((MSG_DRAW == msg.what) && (_lastDrawnCent != lastCent));

        if (shouldRedraw) {
            Canvas canvas = null;
            synchronized (_lock) {
                if (_surfaceHolder != null)
                    canvas =_surfaceHolder.lockCanvas();
            }
            if (canvas != null) {
                doRedraw(canvas, lastCent);
                _surfaceHolder.unlockCanvasAndPost(canvas);
                _lastDrawnCent = lastCent;
            }

            return true;
        }

        return false;
    }
}

然后在你的活动课上你做类似的事情

private SurfaceView surfaceView;
private SurfacePitchDrawingHelper surfacePitchDrawingHelper = new SurfacePitchDrawingHelper();

...

@Override
protected void onCreate(Bundle savedInstanceState) {

    ...

    surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
    surfaceView.getHolder().addCallback(surfacePitchDrawingHelper);

    int sr = 44100;//The sample rate
    int bs = 2048;
    AudioDispatcher d = AudioDispatcherFactory.fromDefaultMicrophone(sr, bs, 0);
    PitchDetectionHandler printPitch = new PitchDetectionHandler() {
        @Override
        public void handlePitch(final PitchDetectionResult pitchDetectionResult, AudioEvent audioEvent) {
            final float p = pitchDetectionResult.getPitch();
            float cent = (float) (1200 * Math.log(p / 8.176) / Math.log(2)) % 12;
            System.out.println(cent);
            surfacePitchDrawingHelper.postRedraw(cent);
        }
    };

    PitchProcessor.PitchEstimationAlgorithm algo = PitchProcessor.PitchEstimationAlgorithm.YIN; //use YIN
    AudioProcessor pitchEstimator = new PitchProcessor(algo, sr, bs, printPitch);
    d.addAudioProcessor(pitchEstimator);
    // d.run();//starts the dispatching process <-- this was another bug in the original code (see update)!
    AudioProcessor p = new PitchProcessor(algo, sr, bs, printPitch);
    d.addAudioProcessor(p);
    new Thread(d, "Audio Dispatcher").start();

    ...

}

请注意,它SurfacePitchDrawingHelper封装了与绘图相关的大部分逻辑,并且在您的子类中不需要MySurfaceView(无论如何我认为这是一个坏主意)。

主要思想是,当创建新的时,SurfacePitchDrawingHelper创建是专用的。+ +提供了一个有用的基础设施,在一个单独的线程上运行(以一种有效的方式)一个无限循环,该线程等待传入的消息并一一处理它们。因此,它的有效公共 API 除了包含可用于要求绘图线程进行另一次重绘的单一方法外,这正是 custom 使用的方法。“询问”是通过将消息放入队列中以由绘图线程(更具体地说是我们的自定义HandlerThreadSurfaceHandlerThreadLooperHandlerSurfaceHolder.Callback2postRedrawPitchDetectionHandlerHandler在那个线程上)。我没有费心将真正的公共 API 简化为“有效”的 API,因为它使代码更复杂,而且我太懒了。但是当然这两个“实现”都可以移动到内部类。

您需要做出一个重要的决定:绘图线程是否应该cent按顺序生成每个入站消息(所有值),或者只是在绘图发生的那一刻产生最新消息。这可能变得特别重要,以防万一PitchDetectionHandler产生事件比“绘图线程”可以更新快得多Surface。我相信在大多数情况下,只处理最后一个值是可以的,PitchDetectionHandler但我将这两个版本都留在了代码中以进行说明。这种区别目前在代码中按_processOnlyLast字段实现。很可能您应该做出这个决定,然后摆脱这个几乎恒定的字段和不相关分支中的代码。

当然不要忘记将您的实际绘图逻辑放入其中doRedraw


更新(为什么后退按钮不起作用)

TLDR 版本

违规行是

 d.run();//starts the dispatching process

把它评论出来!

更长的版本

查看您的示例,我们可以看到which和因此方法是要在新线程上调用的方法d。您可能会注意到这很重要,因为在此方法内部会执行一些 IO 并阻塞它运行的线程。因此,在您的情况下,它阻塞了主 UI 线程。几行下来你做AudioDispatcherimplements Runnablerun

new Thread(d, "Audio Dispatcher").start();

这似乎是正确的使用方式AudioDispatcher

这可以从我在评论中询问的堆栈跟踪中轻松看出。

主线程的堆栈跟踪

于 2017-03-10T01:41:43.313 回答