|
| 1 | +/* |
| 2 | + * Semitone - tuner, metronome, and piano for Android |
| 3 | + * Copyright (C) 2019 Andy Tockman <[email protected]> |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package mn.tck.semitone; |
| 20 | + |
| 21 | +import android.app.*; |
| 22 | +import android.content.*; |
| 23 | +import android.graphics.*; |
| 24 | +import android.os.*; |
| 25 | +import android.util.*; |
| 26 | +import android.view.*; |
| 27 | +import android.widget.*; |
| 28 | + |
| 29 | +import android.media.AudioFormat; |
| 30 | +import android.media.AudioRecord; |
| 31 | +import android.media.MediaRecorder.AudioSource; |
| 32 | + |
| 33 | +import java.util.Arrays; |
| 34 | + |
| 35 | +public class MainActivity extends Activity { |
| 36 | + |
| 37 | + final static String[] notenames = {"A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"}; |
| 38 | + |
| 39 | + final static int SAMPLE_RATE = 44100; |
| 40 | + final static int HIST_SIZE = 16; |
| 41 | + static int bufsize; |
| 42 | + static AudioRecord ar; |
| 43 | + static DSP dsp; |
| 44 | + |
| 45 | + TextView notename; |
| 46 | + CentErrorView centerror; |
| 47 | + |
| 48 | + @Override protected void onCreate(Bundle state) { |
| 49 | + super.onCreate(state); |
| 50 | + requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 51 | + setContentView(R.layout.activity_main); |
| 52 | + |
| 53 | + notename = (TextView) findViewById(R.id.notename); |
| 54 | + notename.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { |
| 55 | + @Override public void onGlobalLayout() { |
| 56 | + notename.getViewTreeObserver().removeOnGlobalLayoutListener(this); |
| 57 | + notename.setTextSize(TypedValue.COMPLEX_UNIT_PX, |
| 58 | + Util.maxTextSize("G#000", notename.getWidth())); |
| 59 | + } |
| 60 | + }); |
| 61 | + centerror = (CentErrorView) findViewById(R.id.centerror); |
| 62 | + |
| 63 | + bufsize = AudioRecord.getMinBufferSize(SAMPLE_RATE, |
| 64 | + AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); |
| 65 | + ar = new AudioRecord(AudioSource.MIC, SAMPLE_RATE, |
| 66 | + AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, |
| 67 | + bufsize); |
| 68 | + ar.startRecording(); |
| 69 | + |
| 70 | + DSP.init(bufsize); |
| 71 | + |
| 72 | + new Thread(new TunerThread()).start(); |
| 73 | + } |
| 74 | + |
| 75 | + class TunerThread implements Runnable { |
| 76 | + @Override public void run() { |
| 77 | + short[] buf = new short[bufsize]; |
| 78 | + double[] dbuf = new double[DSP.fftlen]; |
| 79 | + double[] hist = new double[HIST_SIZE]; |
| 80 | + double[] sorted = new double[HIST_SIZE]; |
| 81 | + for (;;) { |
| 82 | + // copy data to fft buffer - scale down to avoid huge numbers |
| 83 | + ar.read(buf, 0, bufsize); |
| 84 | + for (int i = 0; i < DSP.fftlen; ++i) dbuf[i] = buf[i] / 1024.0; |
| 85 | + |
| 86 | + // calculate frequency and note |
| 87 | + double freq = DSP.freq(dbuf, SAMPLE_RATE), |
| 88 | + semitone = 12 * Math.log(freq/440)/Math.log(2); |
| 89 | + |
| 90 | + // insert into moving average history |
| 91 | + for (int i = 1; i < HIST_SIZE; ++i) sorted[i-1] = hist[i-1] = hist[i]; |
| 92 | + sorted[HIST_SIZE-1] = hist[HIST_SIZE-1] = semitone; |
| 93 | + |
| 94 | + // find median |
| 95 | + Arrays.sort(sorted); |
| 96 | + final double median = (sorted[HIST_SIZE/2-1]+sorted[HIST_SIZE/2])/2; |
| 97 | + |
| 98 | + final int rounded = (int)Math.round(median); |
| 99 | + final int note = Math.floorMod(rounded, 12); |
| 100 | + |
| 101 | + runOnUiThread(new Runnable() { |
| 102 | + @Override public void run() { |
| 103 | + notename.setText(notenames[note] + |
| 104 | + (Math.floorDiv(rounded, 12) + 5 - (note <= 2 ? 1 : 0))); |
| 105 | + centerror.setError(median - rounded); |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments