private static final SimpleDateFormat AMBIENT_DATE_FORMAT =
new SimpleDateFormat("HH:mm", Locale.US);
+ private static final int AUDIO_RECORDER_BUFFER_SIZE = 2048;
+ private static final int AUDIO_SAMPLES = 512;
+
private BoxInsetLayout mOuterContainer;
private LinearLayout mInnerContainer;
private TextView mTextView, mClockView, mDebugView;
final AudioRecord ar = new AudioRecord(
MediaRecorder.AudioSource.MIC,
- 11025, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_FLOAT, 2048);
+ 11025, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_FLOAT,
+ AUDIO_RECORDER_BUFFER_SIZE);
Log.d("shc", "Audio session ID:" + ar.getAudioSessionId());
cycler = new Thread() {
public void run() {
- float[] samples = new float[512];
- FloatFFT_1D fft = new FloatFFT_1D(samples.length);
+ // Raw audio samples
+ float[] samples = new float[AUDIO_SAMPLES];
+
+ // FFT data and engine
+ float[] fft = new float[AUDIO_SAMPLES];
+ FloatFFT_1D fftc = new FloatFFT_1D(AUDIO_SAMPLES);
ar.startRecording();
while (!Thread.interrupted()) {
final RenderCB rcb;
ar.read(samples, 0, samples.length, AudioRecord.READ_BLOCKING);
+ System.arraycopy(samples,0,fft,0,AUDIO_SAMPLES);
/*
* Debug: triangle wave
*/
if (i % 32 >= 16) { samples[i] *= -1; }
}
*/
- fft.realForward(samples);
+
+ fftc.realForward(fft);
synchronized(this) {
rcb = cyclercb;
break;
}
cv.drawColor(Color.BLACK);
- rcb.render(cv,samples);
+ rcb.render(cv,samples,fft);
h.unlockCanvasAndPost(cv);
// try { Thread.sleep(100); } catch (InterruptedException e) { break; }
}
@Override
- public void render(Canvas cv, float[] samples) {
+ public void render(Canvas cv, float[] au, float[] fft) {
int c = Color.HSVToColor(hsv);
hsv[0] = hsv[0] >= 359 ? 0.0f : hsv[0] + 1.0f;
p.setColor(c);
+ final float scale = 32;
+
int rxs = cv.getWidth() / 8;
int rys = cv.getHeight() / 8;
for (int rx = 0; rx < 8; rx++) {
for (int ry = 0; ry < 8; ry++) {
int ix = (rx * 8 + ry) * 4;
- float x = (Math.abs(samples[ix]) + Math.abs(samples[ix+2])) * 32;
+ float x = (Math.abs(fft[ix]) + Math.abs(fft[ix+2])) * scale;
int b = x > 255 ? 255 : (int)x;
p.setAlpha(b > 223 ? 255 : b + 32);
cv.drawRect(rx * rxs, ry * rys, (rx + 1) * rxs - 1, (ry + 1) * rys - 1, p);
}
@Override
- public void render(Canvas cv, float[] samples) {
+ public void render(Canvas cv, float[] audio, float[] fft) {
float msamp = 0.0f;
int mix = -1;
/* Restrict search to lowest half in agreement with Grid */
- for (int i = 0; i < samples.length/2; i += 2) {
- if (samples[i] > msamp) {
- msamp = samples[i];
+ for (int i = 0; i < fft.length/2; i += 2) {
+ if (fft[i] > msamp) {
+ msamp = fft[i];
mix = i;
}
}