/
SimpleRecorder.java
266 lines (237 loc) · 10.1 KB
/
SimpleRecorder.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* SimepleRecorder.java
An auto-tune app for Android
Copyright (c) 2010 Ethan Chen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.intervigil.micdroid.recorder;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import net.sourceforge.autotalent.Autotalent;
import net.sourceforge.resample.Resample;
import android.content.Context;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import com.intervigil.micdroid.Constants;
import com.intervigil.micdroid.R;
import com.intervigil.micdroid.helper.AudioHelper;
import com.intervigil.micdroid.helper.DialogHelper;
import com.intervigil.micdroid.helper.PreferenceHelper;
import com.intervigil.micdroid.interfaces.DependentTask;
import com.intervigil.micdroid.interfaces.Recorder;
import com.intervigil.wave.WaveReader;
import com.intervigil.wave.WaveWriter;
import com.intervigil.wave.exception.InvalidWaveException;
public class SimpleRecorder implements Recorder {
private static final int SIMPLE_RECORDER_BUFFER_SIZE = 8192;
private static final int RECORDER_MESSAGE_INVALID_INSTRUMENTAL = 8675309;
private static final int RECORDER_MESSAGE_IO_ERROR = 8675308;
private static final int RECORDER_MESSAGE_RECORD_ERROR = 8675310;
private static final int RECORDER_MESSAGE_FINISHED = 8675307;
private final Context context;
private MicWriter writerThread;
private final boolean isLiveMode;
private final int sampleRate;
private final DependentTask postRecordTask;
public SimpleRecorder(Context context, DependentTask postRecordTask, boolean isLiveMode) {
this.context = context;
this.sampleRate = PreferenceHelper.getSampleRate(context);
this.postRecordTask = postRecordTask;
this.isLiveMode = isLiveMode;
}
@Override
public void start() {
try {
writerThread = new MicWriter();
writerThread.start();
Toast.makeText(context,
R.string.recording_started_toast,
Toast.LENGTH_SHORT).show();
} catch (IllegalArgumentException e) {
DialogHelper.showWarning(context,
R.string.audio_record_exception_title,
R.string.audio_record_exception_warning);
postRecordTask.handleError();
}
}
@Override
public void stop() {
if (isRunning()) {
writerThread.close();
try {
writerThread.join();
} catch (InterruptedException e) {}
writerThread = null;
}
}
@Override
public void cleanup() {
stop();
}
@Override
public boolean isRunning() {
return (writerThread != null
&& writerThread.getState() != Thread.State.NEW && writerThread
.getState() != Thread.State.TERMINATED);
}
private Handler recorderHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case RECORDER_MESSAGE_RECORD_ERROR:
DialogHelper.showWarning(context,
R.string.audio_record_exception_title,
R.string.audio_record_exception_warning);
postRecordTask.handleError();
break;
case RECORDER_MESSAGE_INVALID_INSTRUMENTAL:
DialogHelper.showWarning(context,
R.string.instrumental_not_found_title,
R.string.instrumental_not_found_warning);
postRecordTask.handleError();
break;
case RECORDER_MESSAGE_IO_ERROR:
DialogHelper.showWarning(context,
R.string.recording_io_error_title,
R.string.recording_io_error_warning);
postRecordTask.handleError();
break;
case RECORDER_MESSAGE_FINISHED:
postRecordTask.doTask();
break;
}
}
};
private class MicWriter extends Thread {
private final AudioRecord audioRecord;
private AudioTrack audioTrack;
private final WaveWriter writer;
private WaveReader instrumentalReader;
private boolean running;
public MicWriter() throws IllegalArgumentException {
this.running = false;
this.audioRecord = AudioHelper.getRecorder(context);
this.writer = new WaveWriter(context.getCacheDir().getAbsolutePath(),
context.getString(R.string.default_recording_name), sampleRate,
AudioHelper.getChannelConfig(Constants.DEFAULT_CHANNEL_CONFIG),
AudioHelper.getPcmEncoding(Constants.DEFAULT_PCM_FORMAT));
if (isLiveMode) {
this.audioTrack = AudioHelper.getPlayer(context);
}
String trackName = PreferenceHelper.getInstrumentalTrack(context);
if (!trackName.equals(Constants.EMPTY_STRING)) {
// start reading from instrumental track
this.instrumentalReader = new WaveReader(new File(trackName));
}
}
public synchronized void close() {
running = false;
}
public void initialize() throws FileNotFoundException, InvalidWaveException, IOException {
if (instrumentalReader != null) {
instrumentalReader.openWave();
Resample.initialize(instrumentalReader.getSampleRate(), sampleRate, Resample.DEFAULT_BUFFER_SIZE, instrumentalReader.getChannels());
}
writer.createWaveFile();
if (isLiveMode) {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_NORMAL);
}
}
public void cleanup() {
// stop things
if (audioRecord.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
audioRecord.stop();
}
audioRecord.release();
if (isLiveMode) {
audioTrack.stop();
audioTrack.release();
}
if (instrumentalReader != null) {
try {
Resample.close();
instrumentalReader.closeWaveFile();
} catch (IOException e) {
// no recovery possible here
e.printStackTrace();
}
}
// close file
try {
writer.closeWaveFile();
} catch (IOException e) {
// no recovery possible here
e.printStackTrace();
}
}
public void run() {
Message msg;
int numSamples;
short[] buf = new short[SIMPLE_RECORDER_BUFFER_SIZE];
try {
initialize();
running = true;
audioRecord.startRecording();
if (isLiveMode) {
audioTrack.play();
}
while (running) {
numSamples = audioRecord.read(buf, 0, SIMPLE_RECORDER_BUFFER_SIZE);
if (isLiveMode) {
processLiveAudio(buf, numSamples);
audioTrack.write(buf, 0, numSamples);
}
writer.write(buf, 0, numSamples);
}
msg = recorderHandler.obtainMessage(RECORDER_MESSAGE_FINISHED);
} catch (IllegalStateException e) {
msg = recorderHandler.obtainMessage(RECORDER_MESSAGE_RECORD_ERROR);
} catch (FileNotFoundException e) {
// couldn't find instrumental file
msg = recorderHandler.obtainMessage(RECORDER_MESSAGE_INVALID_INSTRUMENTAL);
} catch (InvalidWaveException e) {
// not a wave file
msg = recorderHandler.obtainMessage(RECORDER_MESSAGE_INVALID_INSTRUMENTAL);
} catch (IOException e) {
// file IO error, no recovery possible?
e.printStackTrace();
msg = recorderHandler.obtainMessage(RECORDER_MESSAGE_IO_ERROR);
}
cleanup();
recorderHandler.sendMessage(msg);
}
private void processLiveAudio(short[] samples, int numSamples) throws IOException {
if (instrumentalReader != null) {
int read, resampled;
int bufferSize = (int) (numSamples / Resample.getFactor());
short[] instrumental = new short[bufferSize];
if (instrumentalReader.getChannels() == 1) {
read = instrumentalReader.read(instrumental, bufferSize);
} else {
short[] instrRight = new short[bufferSize];
read = instrumentalReader.read(instrumental, instrRight, bufferSize);
Resample.downmix(instrumental, instrumental, instrRight, read);
}
resampled = Resample.process(instrumental, instrumental, Resample.CHANNEL_MONO, read != bufferSize);
Autotalent.processSamples(samples, instrumental, numSamples);
} else {
Autotalent.processSamples(samples, numSamples);
}
}
}
}