7 changed files with 338 additions and 53 deletions
-
2.idea/misc.xml
-
146app/src/main/java/de/mal_richtig/toney/AudioManager.java
-
60app/src/main/java/de/mal_richtig/toney/LevelMeter.java
-
78app/src/main/java/de/mal_richtig/toney/MainActivity.java
-
2app/src/main/java/de/mal_richtig/toney/SplashActivity.java
-
99app/src/main/java/de/mal_richtig/toney/TuneDataModel.java
-
2app/src/main/res/layout/activity_main.xml
@ -0,0 +1,146 @@ |
|||
package de.mal_richtig.toney; |
|||
|
|||
import android.content.Context; |
|||
import android.content.Intent; |
|||
import android.media.AudioRecord; |
|||
import android.util.Log; |
|||
|
|||
import be.tarsos.dsp.AudioDispatcher; |
|||
import be.tarsos.dsp.AudioEvent; |
|||
import be.tarsos.dsp.AudioProcessor; |
|||
import be.tarsos.dsp.SilenceDetector; |
|||
import be.tarsos.dsp.filters.HighPass; |
|||
import be.tarsos.dsp.filters.LowPassSP; |
|||
import be.tarsos.dsp.io.android.AudioDispatcherFactory; |
|||
import be.tarsos.dsp.pitch.PitchDetectionHandler; |
|||
import be.tarsos.dsp.pitch.PitchDetectionResult; |
|||
import be.tarsos.dsp.pitch.PitchProcessor; |
|||
|
|||
/** |
|||
* Created by ermo on 09.06.17. |
|||
*/ |
|||
|
|||
public class AudioManager implements PitchDetectionHandler { |
|||
|
|||
private Context mContext; |
|||
|
|||
private PitchProcessor.PitchEstimationAlgorithm mPitchAlgorithm; |
|||
private int mSampleRate; |
|||
private int mBufferSize; |
|||
private int mSilenceThreshold; |
|||
|
|||
private int mLowPassFrequency; |
|||
private int mHighPassFrequency; |
|||
|
|||
private AudioDispatcher mDispatcher; |
|||
|
|||
public AudioManager(Context context, PitchProcessor.PitchEstimationAlgorithm algorithm, int sampleRate, |
|||
int bufferSize, int silenceThreshold, int lowPassFreq, int highPassFreq) { |
|||
|
|||
mContext = context; |
|||
|
|||
mPitchAlgorithm = algorithm; |
|||
mSampleRate = sampleRate; |
|||
mBufferSize = bufferSize; |
|||
mSilenceThreshold = silenceThreshold; |
|||
|
|||
mLowPassFrequency = lowPassFreq; |
|||
mHighPassFrequency = highPassFreq; |
|||
|
|||
buildChain(); |
|||
} |
|||
|
|||
@Override |
|||
public void handlePitch(PitchDetectionResult pitchResult, AudioEvent audioEvent) { |
|||
final float pitchInHz = pitchResult.getPitch(); |
|||
TuneDataModel tuneData; |
|||
|
|||
try { |
|||
tuneData = convertPitch(pitchInHz); |
|||
} |
|||
catch (IllegalArgumentException e) { |
|||
Intent intent = new Intent(); |
|||
intent.setAction("de.mal_richtig.toney.AUDIO_RESULT"); |
|||
intent.putExtra("success", false); |
|||
mContext.sendBroadcast(intent); |
|||
return; |
|||
} |
|||
|
|||
Intent intent = new Intent(); |
|||
intent.setAction("de.mal_richtig.toney.AUDIO_RESULT"); |
|||
intent.putExtra("success", true); |
|||
intent.putExtra("tune_data",tuneData); |
|||
mContext.sendBroadcast(intent); |
|||
} |
|||
|
|||
private void buildChain() { |
|||
|
|||
if (mDispatcher != null) { |
|||
mDispatcher.stop(); |
|||
} |
|||
|
|||
try { |
|||
mDispatcher = AudioDispatcherFactory.fromDefaultMicrophone(mSampleRate, mBufferSize, mBufferSize/2); |
|||
} |
|||
catch (IllegalArgumentException e) { |
|||
mBufferSize = AudioRecord.getMinBufferSize(mSampleRate, 16, 2) * 4; |
|||
Log.w("AudioManager", "audio buffer too small - increasing buffer size to " + mBufferSize); |
|||
mDispatcher = AudioDispatcherFactory.fromDefaultMicrophone(mSampleRate, mBufferSize, mBufferSize/2); |
|||
} |
|||
catch (Exception e) { |
|||
Log.e("AudioManager", "Microphone could not be accessed - returning"); |
|||
return; |
|||
} |
|||
|
|||
SilenceDetector mAmbientFilter = new SilenceDetector(mSilenceThreshold, true); |
|||
mDispatcher.addAudioProcessor(mAmbientFilter); |
|||
|
|||
LowPassSP mLowPass = new LowPassSP(mLowPassFrequency, mSampleRate); |
|||
mDispatcher.addAudioProcessor(mLowPass); |
|||
|
|||
HighPass mHighPass = new HighPass(mHighPassFrequency, mSampleRate); |
|||
mDispatcher.addAudioProcessor(mHighPass); |
|||
|
|||
AudioProcessor mPitch = new PitchProcessor(mPitchAlgorithm, mSampleRate, mBufferSize, this); |
|||
mDispatcher.addAudioProcessor(mPitch); |
|||
|
|||
new Thread(mDispatcher,"Audio dispatching").start(); |
|||
} |
|||
|
|||
private TuneDataModel convertPitch(float pitch) { |
|||
|
|||
String keys[] = {"C", "C", "D", "D", "E", "E", "F", "G", "G", "A", "A", "B"}; |
|||
boolean sharps[] = {false, true, false, true, false, true, false, false, true, false, true, false}; |
|||
|
|||
|
|||
if (pitch <= 0.0) { |
|||
throw new IllegalArgumentException("can't convert a pitch lower 0Hz"); |
|||
} |
|||
|
|||
TuneDataModel tuneData = new TuneDataModel(); |
|||
|
|||
tuneData.setPitch(pitch); |
|||
|
|||
// Formula taken from https://en.wikipedia.org/wiki/Musical_note |
|||
// Calculates a number for a given pitch / key, with the integer part being an index for a semitone |
|||
// e.g. C0 -> 12; C#0 -> 13; A4 -> 69 |
|||
double convertedPitch = 69.0 + 12.0 * (Math.log(pitch / 440.0) / Math.log(2)); |
|||
|
|||
int nearestNote = (int) Math.round(convertedPitch) - 12; |
|||
|
|||
if (nearestNote < 0 || nearestNote > 120) { |
|||
throw new IllegalArgumentException("the pitch is too low/high to be converted"); |
|||
} |
|||
|
|||
int keyInOctave = nearestNote % 12; |
|||
tuneData.setKey(keys[keyInOctave]); |
|||
tuneData.setSharp(sharps[keyInOctave]); |
|||
|
|||
tuneData.setOctave(nearestNote / 12); |
|||
|
|||
double centOutOfTune = (convertedPitch - (double) nearestNote) * 100; |
|||
tuneData.setCentOutOfTune((int) Math.round(centOutOfTune)); |
|||
|
|||
return tuneData; |
|||
} |
|||
} |
|||
@ -0,0 +1,60 @@ |
|||
package de.mal_richtig.toney; |
|||
|
|||
import android.media.AudioRecord; |
|||
import android.os.Build; |
|||
import android.util.Log; |
|||
import android.widget.ProgressBar; |
|||
import android.widget.Toast; |
|||
|
|||
import be.tarsos.dsp.AudioDispatcher; |
|||
import be.tarsos.dsp.AudioEvent; |
|||
import be.tarsos.dsp.AudioProcessor; |
|||
import be.tarsos.dsp.SilenceDetector; |
|||
import be.tarsos.dsp.io.android.AudioDispatcherFactory; |
|||
|
|||
/** |
|||
* Created by ermo on 12.06.17. |
|||
*/ |
|||
|
|||
class LevelMeter extends MainActivity implements AudioProcessor |
|||
{ |
|||
public LevelMeter() |
|||
{ |
|||
AudioDispatcher dispatcher; |
|||
|
|||
try { |
|||
int bufferSize = AudioRecord.getMinBufferSize(22050, 16, 2) * 4; |
|||
dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050, bufferSize, bufferSize/2); |
|||
} |
|||
catch (Exception e) { |
|||
Log.e("AudioManager", "Microphone could not be accessed - returning"); |
|||
Toast.makeText(this, "ERROR: Microphone could not be accessed!", |
|||
Toast.LENGTH_SHORT).show(); |
|||
return; |
|||
} |
|||
|
|||
final SilenceDetector meter = new SilenceDetector(0, false); |
|||
|
|||
dispatcher.addAudioProcessor(meter); |
|||
|
|||
// run the dispatcher (on a new thread). |
|||
new Thread(dispatcher,"Audio dispatching").start(); |
|||
} |
|||
|
|||
@Override |
|||
public boolean process(AudioEvent audioEvent) { |
|||
ProgressBar levelMeter = (ProgressBar) findViewById(R.id.progressBar3); |
|||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
|||
levelMeter.setProgress(100 + (int)audioEvent.getdBSPL(), true); |
|||
} |
|||
else { |
|||
levelMeter.setProgress(100 + (int)audioEvent.getdBSPL()); |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
@Override |
|||
public void processingFinished() { |
|||
|
|||
} |
|||
} |
|||
@ -1,77 +1,59 @@ |
|||
package de.mal_richtig.toney; |
|||
|
|||
import android.content.BroadcastReceiver; |
|||
import android.content.Context; |
|||
import android.content.Intent; |
|||
import android.media.AudioRecord; |
|||
import android.os.Build; |
|||
import android.support.v7.app.AppCompatActivity; |
|||
import android.os.Bundle; |
|||
import android.util.Log; |
|||
import android.widget.ProgressBar; |
|||
import android.widget.SeekBar; |
|||
import android.widget.TextView; |
|||
import android.widget.Toast; |
|||
|
|||
import be.tarsos.dsp.AudioDispatcher; |
|||
import be.tarsos.dsp.AudioEvent; |
|||
import be.tarsos.dsp.AudioProcessor; |
|||
import be.tarsos.dsp.SilenceDetector; |
|||
import be.tarsos.dsp.io.android.AudioDispatcherFactory; |
|||
import be.tarsos.dsp.pitch.PitchDetectionHandler; |
|||
import be.tarsos.dsp.pitch.PitchDetectionResult; |
|||
import be.tarsos.dsp.pitch.PitchProcessor; |
|||
|
|||
public class MainActivity extends AppCompatActivity { |
|||
public class MainActivity extends AppCompatActivity implements BroadcastReceiver{ |
|||
|
|||
private SeekBar volumeControl; |
|||
|
|||
@Override |
|||
protected void onCreate(Bundle savedInstanceState) { |
|||
super.onCreate(savedInstanceState); |
|||
setContentView(R.layout.activity_main); |
|||
|
|||
AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(44100,512*8,0); |
|||
volumeControl = (SeekBar) findViewById(R.id.volume_control); |
|||
|
|||
PitchDetectionHandler pdh = new PitchDetectionHandler() { |
|||
@Override |
|||
public void handlePitch(PitchDetectionResult result,AudioEvent e) { |
|||
final float pitchInHz = result.getPitch(); |
|||
runOnUiThread(new Runnable() { |
|||
@Override |
|||
public void run() { |
|||
TextView text = (TextView) findViewById(R.id.key_major); |
|||
text.setText("" + pitchInHz); |
|||
} |
|||
}); |
|||
} |
|||
}; |
|||
AudioProcessor p = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.DYNAMIC_WAVELET, 44100, 512*8, pdh); |
|||
dispatcher.addAudioProcessor(p); |
|||
new Thread(dispatcher,"Audio Dispatcher").start(); |
|||
} |
|||
volumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { |
|||
int progressChanged = 0; |
|||
|
|||
public void processPitch(float pitchInHz) { |
|||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
|||
progressChanged = progress; |
|||
} |
|||
|
|||
TextView noteText = (TextView) findViewById(R.id.key_major); |
|||
public void onStartTrackingTouch(SeekBar seekBar) { |
|||
// TODO Auto-generated method stub |
|||
} |
|||
|
|||
//pitchText.setText("" + pitchInHz); |
|||
public void onStopTrackingTouch(SeekBar seekBar) { |
|||
|
|||
if(pitchInHz >= 110 && pitchInHz < 123.47) { |
|||
//A |
|||
noteText.setText("A"); |
|||
} |
|||
else if(pitchInHz >= 123.47 && pitchInHz < 130.81) { |
|||
//B |
|||
noteText.setText("B"); |
|||
} |
|||
else if(pitchInHz >= 130.81 && pitchInHz < 146.83) { |
|||
//C |
|||
noteText.setText("C"); |
|||
} |
|||
else if(pitchInHz >= 146.83 && pitchInHz < 164.81) { |
|||
//D |
|||
noteText.setText("D"); |
|||
} |
|||
else if(pitchInHz >= 164.81 && pitchInHz <= 174.61) { |
|||
//E |
|||
noteText.setText("E"); |
|||
} |
|||
else if(pitchInHz >= 174.61 && pitchInHz < 185) { |
|||
//F |
|||
noteText.setText("F"); |
|||
} |
|||
else if(pitchInHz >= 185 && pitchInHz < 196) { |
|||
//G |
|||
noteText.setText("G"); |
|||
}); |
|||
|
|||
//TODO: implement LOCAL broadcasts and register the receivers ... |
|||
} |
|||
|
|||
@Override |
|||
public void onReceive(Context context, Intent intent) { |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,99 @@ |
|||
package de.mal_richtig.toney; |
|||
|
|||
import android.os.Parcel; |
|||
import android.os.Parcelable; |
|||
|
|||
/** |
|||
* Created by ermo on 11.06.17. |
|||
*/ |
|||
|
|||
public class TuneDataModel implements Parcelable { |
|||
|
|||
private float mPitch; |
|||
private String mKey; |
|||
private boolean mSharp; |
|||
private int mOctave; |
|||
private int mCentOutOfTune; |
|||
|
|||
|
|||
protected TuneDataModel(Parcel in) { |
|||
mPitch = in.readFloat(); |
|||
mKey = in.readString(); |
|||
mSharp = (boolean) in.readValue(null); |
|||
mOctave = in.readInt(); |
|||
mCentOutOfTune = in.readInt(); |
|||
} |
|||
|
|||
public static final Creator<TuneDataModel> CREATOR = new Creator<TuneDataModel>() { |
|||
@Override |
|||
public TuneDataModel createFromParcel(Parcel in) { |
|||
return new TuneDataModel(in); |
|||
} |
|||
|
|||
@Override |
|||
public TuneDataModel[] newArray(int size) { |
|||
return new TuneDataModel[size]; |
|||
} |
|||
}; |
|||
|
|||
public TuneDataModel() { |
|||
} |
|||
|
|||
public void setPitch(float Pitch) { |
|||
this.mPitch = Pitch; |
|||
} |
|||
|
|||
public void setKey(String Key) { |
|||
this.mKey = Key; |
|||
} |
|||
|
|||
public void setSharp(boolean sharp) { |
|||
this.mSharp = sharp; |
|||
} |
|||
|
|||
public void setOctave(int mOctave) { |
|||
this.mOctave = mOctave; |
|||
} |
|||
|
|||
public void setCentOutOfTune(int CentOutOfTune) { |
|||
this.mCentOutOfTune = CentOutOfTune; |
|||
} |
|||
|
|||
|
|||
|
|||
public float getPitch() { |
|||
return mPitch; |
|||
} |
|||
|
|||
public String getKey() { |
|||
return mKey; |
|||
} |
|||
|
|||
public boolean isSharp() { |
|||
return mSharp; |
|||
} |
|||
|
|||
public int getOctave() { |
|||
return mOctave; |
|||
} |
|||
|
|||
public int getCentOutOfTune() { |
|||
return mCentOutOfTune; |
|||
} |
|||
|
|||
@Override |
|||
public int describeContents() { |
|||
return 0; |
|||
} |
|||
|
|||
@Override |
|||
public void writeToParcel(Parcel dest, int flags) { |
|||
dest.writeFloat(mPitch); |
|||
dest.writeString(mKey); |
|||
dest.writeValue(mSharp); |
|||
dest.writeInt(mOctave); |
|||
dest.writeInt(mCentOutOfTune); |
|||
} |
|||
|
|||
|
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue