リアルタイムでのオーディオ録音についてフォークを作成し、オーディオエフェクト(遅延(エコー)、リバーブなど)を追加することに加えて、オーディオプログラミングの世界では初めてです
以下のようなトラブルがありました
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 を変更するにはどうすればよいですか?