This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
WaveformActivity.java
145 lines (119 loc) · 4.75 KB
/
WaveformActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (C) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.glass.sample.waveform;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder.AudioSource;
import android.os.Bundle;
import android.widget.TextView;
/**
* Receives audio input from the microphone and displays a visualization of that data as a waveform
* on the screen.
*/
public class WaveformActivity extends Activity {
// The sampling rate for the audio recorder.
private static final int SAMPLING_RATE = 44100;
private WaveformView mWaveformView;
private TextView mDecibelView;
private RecordingThread mRecordingThread;
private int mBufferSize;
private short[] mAudioBuffer;
private String mDecibelFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_waveform);
mWaveformView = (WaveformView) findViewById(R.id.waveform_view);
mDecibelView = (TextView) findViewById(R.id.decibel_view);
// Compute the minimum required audio buffer size and allocate the buffer.
mBufferSize = AudioRecord.getMinBufferSize(SAMPLING_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
mAudioBuffer = new short[mBufferSize / 2];
mDecibelFormat = getResources().getString(R.string.decibel_format);
}
@Override
protected void onResume() {
super.onResume();
mRecordingThread = new RecordingThread();
mRecordingThread.start();
}
@Override
protected void onPause() {
super.onPause();
if (mRecordingThread != null) {
mRecordingThread.stopRunning();
mRecordingThread = null;
}
}
/**
* A background thread that receives audio from the microphone and sends it to the waveform
* visualizing view.
*/
private class RecordingThread extends Thread {
private boolean mShouldContinue = true;
@Override
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);
AudioRecord record = new AudioRecord(AudioSource.MIC, SAMPLING_RATE,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, mBufferSize);
record.startRecording();
while (shouldContinue()) {
record.read(mAudioBuffer, 0, mBufferSize / 2);
mWaveformView.updateAudioData(mAudioBuffer);
updateDecibelLevel();
}
record.stop();
record.release();
}
/**
* Gets a value indicating whether the thread should continue running.
*
* @return true if the thread should continue running or false if it should stop
*/
private synchronized boolean shouldContinue() {
return mShouldContinue;
}
/** Notifies the thread that it should stop running at the next opportunity. */
public synchronized void stopRunning() {
mShouldContinue = false;
}
/**
* Computes the decibel level of the current sound buffer and updates the appropriate text
* view.
*/
private void updateDecibelLevel() {
// Compute the root-mean-squared of the sound buffer and then apply the formula for
// computing the decibel level, 20 * log_10(rms). This is an uncalibrated calculation
// that assumes no noise in the samples; with 16-bit recording, it can range from
// -90 dB to 0 dB.
double sum = 0;
for (short rawSample : mAudioBuffer) {
double sample = rawSample / 32768.0;
sum += sample * sample;
}
double rms = Math.sqrt(sum / mAudioBuffer.length);
final double db = 20 * Math.log10(rms);
// Update the text view on the main thread.
mDecibelView.post(new Runnable() {
@Override
public void run() {
mDecibelView.setText(String.format(mDecibelFormat, db));
}
});
}
}
}