我如何更改我的代码以应用此自定义库?

时间:2015-02-17 13:06:52

标签: java android audio-recording audioeffect tarsosdsp

我实时制作一个关于录音的分支,除了添加音频效果(延迟(回声),混响......),以及我对音频编程世界的新意外

我遇到了一些麻烦,如下面

我得到了joren的图书馆(be.tarsos.dsp)   我想对我的应用程序应用延迟效果

在我从库中添加延迟效果的新行之前默认情况下运行良好,但很难申请我,所以我需要你的帮助

这是主要的活动,我想在下面应用延迟效果

MainActivity.java

package com.example.makeechomike;

import android.app.Activity;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;



public class MainActivity extends Activity {
    private static final String MEDIA_ID = "88550";
    private static final String PUBLISHER_ID = "26505";
    private static final String SPOT_ID = "190377";
    AudioManager am;
    private AudioRecord audioRec;
    private boolean bIsRecording;
    private int bufSize;
    private ImageView mike;
    private boolean mikeActionFLG = true;
    private AudioTrack track;
    private SeekBar volSeekBar;

    public void MikePlay() {
        this.bufSize = (2 * AudioRecord.getMinBufferSize(44100, 2, 2));
        this.audioRec = new AudioRecord(1, 44100, 2, 2, this.bufSize);
        this.track = new AudioTrack(3, 44100, 2, 1, 44100, 1);
        this.track.play();
        this.audioRec.startRecording();
        this.bIsRecording = true;
        new Thread(new Runnable() {
            public void run() {
                byte[] arrayOfByte = new byte[MainActivity.this.bufSize];
                for (;;) {
                    if (!MainActivity.this.bIsRecording) {
                        Log.v("AudioRecord", "stop");
                        MainActivity.this.audioRec.stop();
                        MainActivity.this.track.stop();
                        MainActivity.this.audioRec.release();
                        return;
                    }
                    MainActivity.this.audioRec.read(arrayOfByte, 0,
                            arrayOfByte.length);
                    Log.v("AudioRecord", "read " + arrayOfByte.length
                            + " bytes");
                    MainActivity.this.track.write(arrayOfByte, 0,
                            arrayOfByte.length);
                }
            }
        }).start();
    }

    public void MikeStop() {
        this.track.stop();
        this.audioRec.stop();
        this.bIsRecording = false;
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_main);
        this.mike = ((ImageView) findViewById(R.id.mikeoff_imageView));
        this.volSeekBar = ((SeekBar) findViewById(R.id.volume_seekBar));
        this.am = ((AudioManager) getSystemService("audio"));
        int i = this.am.getStreamMaxVolume(3);
        this.volSeekBar.setMax(i);
        int j = this.am.getStreamVolume(3);
        this.am.setStreamVolume(1, j, 0);
        this.volSeekBar.setProgress(j);
        this.mike.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramAnonymousView) {
                if (MainActivity.this.mikeActionFLG) {
                    MainActivity.this.mike.setImageResource(R.drawable.mic_on);
                    MainActivity.this.mikeActionFLG = false;
                    MainActivity.this.MikePlay();
                    return;
                } else {
                    MainActivity.this.mikeActionFLG = true;
                    MainActivity.this.mike.setImageResource(R.drawable.mic_off);
                    MainActivity.this.MikeStop();
                    return;
                }
            }
        });
        this.volSeekBar
                .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    public void onProgressChanged(
                            SeekBar paramAnonymousSeekBar,
                            int paramAnonymousInt, boolean paramAnonymousBoolean) {
                        MainActivity.this.am.setStreamVolume(3,
                                paramAnonymousInt, 0);
                    }

                    public void onStartTrackingTouch(
                            SeekBar paramAnonymousSeekBar) {
                    }

                    public void onStopTrackingTouch(
                            SeekBar paramAnonymousSeekBar) {
                    }
                });
    }

    protected void onDestroy() {
        super.onDestroy();
    }

    public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
        if (paramInt == 4) {
            Log.v("test", "뒤로 가기 버튼 누름");
            return true;
        }
        return super.onKeyDown(paramInt, paramKeyEvent);
    }

    protected void onPause() {
        super.onPause();
        if (!this.mikeActionFLG) {
            MikeStop();
        }
        finish();
    }
}

=============================================== ============

这是名为DelayEffect.java的Java文件(来自Joren的be.tarsos.dsp库)

DelayEffect.java

/*
*      _______                       _____   _____ _____  
*     |__   __|                     |  __ \ / ____|  __ \ 
*        | | __ _ _ __ ___  ___  ___| |  | | (___ | |__) |
*        | |/ _` | '__/ __|/ _ \/ __| |  | |\___ \|  ___/ 
*        | | (_| | |  \__ \ (_) \__ \ |__| |____) | |     
*        |_|\__,_|_|  |___/\___/|___/_____/|_____/|_|     
*                                                         
* -------------------------------------------------------------
*
* TarsosDSP is developed by Joren Six at IPEM, University Ghent
*  
* -------------------------------------------------------------
*
*  Info: http://0110.be/tag/TarsosDSP
*  Github: https://github.com/JorenSix/TarsosDSP
*  Releases: http://0110.be/releases/TarsosDSP/
*  
*  TarsosDSP includes modified source code by various authors,
*  for credits and info, see README.
* 
*/


package be.tarsos.dsp.effects;

import be.tarsos.dsp.AudioEvent;
import be.tarsos.dsp.AudioProcessor;


/**
 * <p>
 * Adds an echo effect to the signal.
 * </p>
 * 
 * @author Joren Six
 */
public class DelayEffect implements AudioProcessor {

    private double sampleRate;
    private float[] echoBuffer;//in seconds
    private int position;
    private float decay;

    private double newEchoLength;

    /**
     * @param echoLength in seconds
     * @param sampleRate the sample rate in Hz.
     * @param decay The decay of the echo, a value between 0 and 1. 1 meaning no decay, 0 means immediate decay (not echo effect).
     */
    public DelayEffect(double echoLength,double decay,double sampleRate) {
        this.sampleRate = sampleRate;
        setDecay(decay);
        setEchoLength(echoLength);
        applyNewEchoLength();   
    }

    /**
     * @param newEchoLength A new echo buffer length in seconds.
     */
    public void setEchoLength(double newEchoLength){
        this.newEchoLength = newEchoLength;
    }

    private void applyNewEchoLength(){
        if(newEchoLength != -1){

            //create a new buffer with the information of the previous buffer
            float[] newEchoBuffer = new float[(int) (sampleRate * newEchoLength)];
            if(echoBuffer != null){
                for(int i = 0 ; i < newEchoBuffer.length; i++){
                    if(position >= echoBuffer.length){
                        position = 0;
                    }
                    newEchoBuffer[i] = echoBuffer[position];
                    position++;
                }
            }
            this.echoBuffer = newEchoBuffer;
            newEchoLength = -1;
        }
    }

    /**
     * A decay, should be a value between zero and one.
     * @param newDecay the new decay (preferably between zero and one).
     */
    public void setDecay(double newDecay){
        this.decay = (float) newDecay;
    }

    @Override
    public boolean process(AudioEvent audioEvent) {
        float[] audioFloatBuffer = audioEvent.getFloatBuffer();
        int overlap = audioEvent.getOverlap();

        for(int i = overlap ; i < audioFloatBuffer.length ; i++){
            if(position >= echoBuffer.length){
                position = 0;
            }

            //output is the input added with the decayed echo       
            audioFloatBuffer[i] = audioFloatBuffer[i] + echoBuffer[position] * decay;
            //store the sample in the buffer;
            echoBuffer[position] = audioFloatBuffer[i];

            position++;
        }

        applyNewEchoLength();

        return true;
    }

    @Override
    public void processingFinished() {      
    }
}

q1)我想知道该库是否可以应用于Android应用程序     有可能吗?

q2)如何更改要从DelayEffect.Java应用的MainActivity?

0 个答案:

没有答案