stable editing

This commit is contained in:
Alexey Kuznetsov 2016-03-26 16:32:03 +03:00
commit 8d9cc6dd37
5 changed files with 156 additions and 168 deletions

View file

@ -54,8 +54,6 @@ import com.github.axet.audiorecorder.widgets.PitchView;
import java.io.File;
public class RecordingActivity extends AppCompatActivity {
public static int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
public static int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO;
public static int MAXIMUM_ALTITUDE = 5000;
public static final String TAG = RecordingActivity.class.getSimpleName();
@ -81,7 +79,7 @@ public class RecordingActivity extends AppCompatActivity {
Integer bufferSize = 0;
// variable from settings. how may samples per second.
int sampleRate;
// how many samples count need to update view. 4410 for 100ms update.
// pitch size in samples. how many samples count need to update view. 4410 for 100ms update.
int samplesUpdate;
// output target file 2016-01-01 01.01.01.wav
File targetFile;
@ -125,6 +123,7 @@ public class RecordingActivity extends AppCompatActivity {
class PhoneStateChangeListener extends PhoneStateListener {
public boolean wasRinging;
public boolean pausedByCall;
@Override
public void onCallStateChanged(int s, String incomingNumber) {
@ -133,14 +132,18 @@ public class RecordingActivity extends AppCompatActivity {
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stopRecording("playerPause (hold by call)");
wasRinging = true;
if (thread != null) {
stopRecording("playerPause (hold by call)");
pausedByCall = true;
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (wasRinging && permitted()) {
resumeRecording();
if (pausedByCall) {
startRecording();
}
wasRinging = false;
pausedByCall = false;
break;
}
}
@ -212,7 +215,6 @@ public class RecordingActivity extends AppCompatActivity {
public void run() {
stopRecording();
storage.delete(storage.getTempRecording());
//storage.delete(targetFile);
finish();
}
});
@ -250,8 +252,10 @@ public class RecordingActivity extends AppCompatActivity {
}
void loadSamples() {
if (!storage.getTempRecording().exists())
if (!storage.getTempRecording().exists()) {
updateSamples(0);
return;
}
RawSamples rs = new RawSamples(storage.getTempRecording());
samplesTime = rs.getSamples();
@ -270,11 +274,13 @@ public class RecordingActivity extends AppCompatActivity {
rs.open(cut, buf.length);
int len = rs.read(buf);
rs.close();
pitch.clear(cut / samplesUpdate);
for (int i = 0; i < len; i += samplesUpdate) {
pitch.add(getPa(buf, i, samplesUpdate));
}
rs.close();
pitch.drawCalc();
updateSamples(samplesTime);
}
@ -297,16 +303,9 @@ public class RecordingActivity extends AppCompatActivity {
if (thread != null) {
stopRecording("pause");
} else {
if (editSample != -1) {
RawSamples rs = new RawSamples(storage.getTempRecording());
rs.trunk(editSample);
loadSamples();
edit(false);
}
editCut();
if (permitted(PERMISSIONS)) {
resumeRecording();
}
startRecording();
}
}
@ -315,18 +314,18 @@ public class RecordingActivity extends AppCompatActivity {
super.onResume();
Log.d(TAG, "onResume");
updateBufferSize(false);
// start once
if (start) {
start = false;
if (permitted()) {
record();
startRecording();
}
}
updateBufferSize(false);
if (thread != null)
pitch.resume();
pitch.record();
}
@Override
@ -335,7 +334,7 @@ public class RecordingActivity extends AppCompatActivity {
Log.d(TAG, "onPause");
updateBufferSize(true);
edit(false);
pitch.pause();
pitch.stop();
}
void stopRecording(String status) {
@ -361,7 +360,7 @@ public class RecordingActivity extends AppCompatActivity {
thread.interrupt();
thread = null;
}
pitch.pause();
pitch.stop();
sound.unsilent();
}
@ -377,10 +376,7 @@ public class RecordingActivity extends AppCompatActivity {
cut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RawSamples rs = new RawSamples(storage.getTempRecording());
rs.trunk(editSample);
loadSamples();
edit(false);
editCut();
}
});
@ -407,7 +403,7 @@ public class RecordingActivity extends AppCompatActivity {
editSample = -1;
state.setText("pause");
editPlay(false);
pitch.pause();
pitch.stop();
View box = findViewById(R.id.recording_edit_box);
box.setVisibility(View.GONE);
}
@ -429,12 +425,7 @@ public class RecordingActivity extends AppCompatActivity {
AudioTrack.OnPlaybackPositionUpdateListener listener = new AudioTrack.OnPlaybackPositionUpdateListener() {
@Override
public void onMarkerReached(AudioTrack track) {
handler.post(new Runnable() {
@Override
public void run() {
editPlay(false);
}
});
editPlay(false);
}
@Override
@ -466,6 +457,17 @@ public class RecordingActivity extends AppCompatActivity {
}
}
void editCut() {
if (editSample == -1)
return;
RawSamples rs = new RawSamples(storage.getTempRecording());
rs.trunk(editSample);
rs.close();
edit(false);
loadSamples();
}
@Override
public void onBackPressed() {
cancelDialog(new Runnable() {
@ -495,13 +497,6 @@ public class RecordingActivity extends AppCompatActivity {
builder.show();
}
void resumeRecording() {
if (thread == null) {
record();
}
pitch.resume();
}
@Override
protected void onDestroy() {
super.onDestroy();
@ -523,7 +518,7 @@ public class RecordingActivity extends AppCompatActivity {
showNotificationAlarm(false);
}
void record() {
void startRecording() {
edit(false);
pitch.setOnTouchListener(null);
@ -547,10 +542,7 @@ public class RecordingActivity extends AppCompatActivity {
Log.e(TAG, "Unable to set Thread Priority " + android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
}
long startTime = System.currentTimeMillis();
// start recording after 1 sec or stableRefresh
long goTime = startTime + 1000;
long start = System.currentTimeMillis();
RawSamples rs = null;
AudioRecord recorder = null;
try {
@ -558,17 +550,12 @@ public class RecordingActivity extends AppCompatActivity {
rs.open(samplesTime);
int min = AudioRecord.getMinBufferSize(sampleRate, CHANNEL_CONFIG, AUDIO_FORMAT);
int min = AudioRecord.getMinBufferSize(sampleRate, RawSamples.CHANNEL_CONFIG, RawSamples.AUDIO_FORMAT);
if (min <= 0) {
throw new RuntimeException("Unable to initialize AudioRecord: Bad audio values");
}
// make it 5 seconds buffer
int min2 = 5 * sampleRate
* (AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 2 : 1)
* (CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_STEREO ? 2 : 1);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, CHANNEL_CONFIG, AUDIO_FORMAT, Math.min(min2, min));
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, RawSamples.CHANNEL_CONFIG, RawSamples.AUDIO_FORMAT, min);
if (recorder.getState() != AudioRecord.STATE_INITIALIZED) {
throw new RuntimeException("Unable to initialize AudioRecord");
}
@ -578,13 +565,11 @@ public class RecordingActivity extends AppCompatActivity {
int samplesUpdateCount = 0;
int samplesTimeCount = 0;
// how many samples we need to update 'samples'. time clock. every 1000ms.
int samplesTimeUpdate = 1000 / 1000 * sampleRate * (CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_MONO ? 1 : 2);
int samplesTimeUpdate = 1000 / 1000 * sampleRate * (RawSamples.CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_MONO ? 1 : 2);
short[] buffer = null;
while (!Thread.currentThread().isInterrupted()) {
long cur = System.currentTimeMillis();
synchronized (bufferSize) {
if (buffer == null || buffer.length != bufferSize)
buffer = new short[bufferSize];
@ -595,10 +580,10 @@ public class RecordingActivity extends AppCompatActivity {
break;
}
if (cur > goTime || pitch.stableRefresh()) {
rs.write(buffer);
int s = RawSamples.CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_MONO ? readSize : readSize / 2;
int s = CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_MONO ? readSize : readSize / 2;
if (pitch.stableRefresh()) {
rs.write(buffer);
samplesUpdateCount += s;
if (samplesUpdateCount >= samplesUpdate) {
@ -630,9 +615,13 @@ public class RecordingActivity extends AppCompatActivity {
}
});
} finally {
if (rs != null) {
// redraw view, we may add one last pich which is not been drawen because draw tread already interrupted.
// to prevent resume recording jump - draw last added pitch here.
pitch.drawEnd();
if (rs != null)
rs.close();
}
if (recorder != null)
recorder.release();
}
@ -640,6 +629,8 @@ public class RecordingActivity extends AppCompatActivity {
}, "RecordingThread");
thread.start();
pitch.record();
showNotificationAlarm(true);
}
@ -653,7 +644,7 @@ public class RecordingActivity extends AppCompatActivity {
samplesUpdate = (int) (pitch.getPitchTime() * sampleRate / 1000.0);
}
bufferSize = CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_MONO ? samplesUpdate : samplesUpdate * 2;
bufferSize = RawSamples.CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_MONO ? samplesUpdate : samplesUpdate * 2;
}
}
@ -670,7 +661,7 @@ public class RecordingActivity extends AppCompatActivity {
}
int amplitude = (int) (Math.sqrt(sum / len));
float pa = amplitude / (float) MAXIMUM_ALTITUDE + 0.01f;
float pa = amplitude / (float) MAXIMUM_ALTITUDE;
return pa;
}
@ -716,7 +707,7 @@ public class RecordingActivity extends AppCompatActivity {
switch (requestCode) {
case 1:
if (permitted(permissions)) {
record();
startRecording();
} else {
Toast.makeText(this, "Not permitted", Toast.LENGTH_SHORT).show();
finish();
@ -748,8 +739,8 @@ public class RecordingActivity extends AppCompatActivity {
}
EncoderInfo getInfo() {
final int channels = CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_STEREO ? 2 : 1;
final int bps = AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 16 : 8;
final int channels = RawSamples.CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_STEREO ? 2 : 1;
final int bps = RawSamples.AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 16 : 8;
return new EncoderInfo(channels, sampleRate, bps);
}

View file

@ -16,6 +16,8 @@ import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
public class RawSamples {
public static int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
public static int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO;
File in;
@ -42,7 +44,7 @@ public class RawSamples {
// bufReadSize - samples count
public void open(int bufReadSize) {
try {
readBuffer = new byte[getBufferLen(bufReadSize)];
readBuffer = new byte[(int) getBufferLen(bufReadSize)];
is = new FileInputStream(in);
} catch (IOException e) {
throw new RuntimeException(e);
@ -55,9 +57,9 @@ public class RawSamples {
// bufReadSize - samples size
public void open(long offset, int bufReadSize) {
try {
readBuffer = new byte[getBufferLen(bufReadSize)];
readBuffer = new byte[(int) getBufferLen(bufReadSize)];
is = new FileInputStream(in);
is.skip(offset * (RecordingActivity.AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 2 : 1));
is.skip(offset * (AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 2 : 1));
} catch (IOException e) {
throw new RuntimeException(e);
}
@ -96,18 +98,18 @@ public class RawSamples {
return getSamples(in.length());
}
public long getSamples(long len) {
return len / (RecordingActivity.AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 2 : 1);
public static long getSamples(long len) {
return len / (AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 2 : 1);
}
public int getBufferLen(int samples) {
return samples * (RecordingActivity.AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 2 : 1);
public static long getBufferLen(long samples) {
return samples * (AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 2 : 1);
}
public void trunk(long pos) {
try {
FileChannel outChan = new FileOutputStream(in, true).getChannel();
outChan.truncate(pos * (RecordingActivity.AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 2 : 1));
outChan.truncate(getBufferLen(pos));
outChan.close();
} catch (IOException e) {
throw new RuntimeException(e);

View file

@ -56,10 +56,10 @@ public class Sound {
int c = 0;
if (RecordingActivity.CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_MONO)
if (RawSamples.CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_MONO)
c = AudioFormat.CHANNEL_OUT_MONO;
if (RecordingActivity.CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_STEREO)
if (RawSamples.CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_STEREO)
c = AudioFormat.CHANNEL_OUT_STEREO;
// old phones bug.
@ -67,7 +67,7 @@ public class Sound {
//
// with MODE_STATIC setNotificationMarkerPosition not called
AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
c, RecordingActivity.AUDIO_FORMAT,
c, RawSamples.AUDIO_FORMAT,
len * (Short.SIZE / 8), AudioTrack.MODE_STREAM);
track.write(buf, 0, len);
if (track.setNotificationMarkerPosition(end) != AudioTrack.SUCCESS)

View file

@ -4,20 +4,13 @@ import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import java.util.LinkedList;
@ -68,12 +61,14 @@ public class PitchView extends ViewGroup {
Runnable edit;
// index
int editPos = 0;
int editCount = 0;
boolean editFlash = false;
// current playing position in samples
float playPos = -1;
Runnable play;
Runnable draw;
float offset = 0;
Thread thread;
int pitchColor = 0xff0433AE;
@ -123,13 +118,8 @@ public class PitchView extends ViewGroup {
super.onLayout(changed, left, top, right, bottom);
}
public void draw() {
float offset = 0;
public void calc() {
if (data.size() >= pitchMemCount) {
if (time == 0)
time = System.currentTimeMillis();
long cur = System.currentTimeMillis();
float tick = (cur - time) / (float) pitchTime;
@ -157,11 +147,9 @@ public class PitchView extends ViewGroup {
offset = pitchSize * tick;
}
draw(offset);
}
void draw(float offset) {
void draw() {
Canvas canvas = holder.lockCanvas(null);
int m = Math.min(pitchMemCount, data.size());
@ -186,12 +174,12 @@ public class PitchView extends ViewGroup {
p = cutColor;
// left channel pitch
canvas.drawLine(x, mid, x, mid - mid * left, p);
canvas.drawLine(x, mid, x, mid - mid * left - 1, p);
// right channel pitch
canvas.drawLine(x, mid, x, mid + mid * right, p);
canvas.drawLine(x, mid, x, mid + mid * right + 1, p);
}
if (edit != null && editCount == 0) {
if (edit != null && editFlash) {
float x = editPos * pitchSize + pitchSize / 2f;
canvas.drawLine(x, 0, x, getHeight(), editPaint);
}
@ -208,8 +196,8 @@ public class PitchView extends ViewGroup {
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
synchronized (PitchView.this) {
this.holder = holder;
fit();
draw(0);
fit(pitchScreenCount);
draw();
}
}
@ -217,8 +205,8 @@ public class PitchView extends ViewGroup {
public void surfaceCreated(final SurfaceHolder holder) {
synchronized (PitchView.this) {
this.holder = holder;
fit();
draw(0);
fit(pitchScreenCount);
draw();
}
}
@ -275,6 +263,9 @@ public class PitchView extends ViewGroup {
if (edit != null) {
end = editPos;
}
if (play != null) {
end = (int) playPos;
}
float left = data.get(end);
float right = data.get(end);
@ -369,41 +360,50 @@ public class PitchView extends ViewGroup {
public void clear(long s) {
data.clear();
samples = s;
offset = 0;
edit = null;
draw = null;
play = null;
}
public void fit() {
if (data.size() > pitchMemCount) {
int cut = data.size() - pitchMemCount;
public void fit(int max) {
if (data.size() > max) {
int cut = data.size() - max;
data.subList(0, cut).clear();
samples += cut;
}
}
public void add(float a) {
data.add(a);
// after pause, we still may get one last sample. force view redraw.
if (thread == null) {
draw();
synchronized (this) {
data.add(a);
}
}
public void draw() {
public void drawCalc() {
synchronized (this) {
if (graph.holder != null)
if (graph.holder != null) {
graph.calc();
graph.draw();
}
if (current.holder != null)
current.draw();
}
}
public void drawEnd() {
synchronized (this) {
fit(pitchMemCount);
offset = 0;
draw();
}
}
// draw in edit mode
public void drawEdit() {
public void draw() {
synchronized (this) {
if (graph.holder != null)
graph.draw(0);
graph.draw();
if (current.holder != null)
current.draw();
}
@ -452,20 +452,17 @@ public class PitchView extends ViewGroup {
current.draw(canvas);
}
public void pause() {
public void stop() {
if (thread != null) {
thread.interrupt();
thread = null;
}
if (edit != null)
edit = null;
if (draw != null)
draw = null;
if (play != null)
play = null;
edit = null;
draw = null;
play = null;
drawEdit();
draw();
}
public long edit(float offset) {
@ -480,8 +477,8 @@ public class PitchView extends ViewGroup {
if (editPos >= data.size())
editPos = data.size() - 1;
editCount = 0;
drawEdit();
editFlash = true;
draw();
}
if (draw != null) {
@ -504,11 +501,9 @@ public class PitchView extends ViewGroup {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
long time = System.currentTimeMillis();
drawEdit();
draw();
editCount++;
if (editCount > 1)
editCount = 0;
editFlash = !editFlash;
long cur = System.currentTimeMillis();
@ -530,7 +525,7 @@ public class PitchView extends ViewGroup {
}
}
public void resume() {
public void record() {
if (edit != null) {
edit = null;
if (thread != null) {
@ -552,19 +547,15 @@ public class PitchView extends ViewGroup {
@Override
public void run() {
time = System.currentTimeMillis();
int count = 0;
while (!Thread.currentThread().isInterrupted()) {
long time = System.currentTimeMillis();
draw();
drawCalc();
long cur = System.currentTimeMillis();
long delay = UPDATE_SPEED - (cur - time);
if (delay > 0) {
count++;
if (count > 5) {
stableRefresh = true;
}
stableRefresh = true;
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
@ -585,9 +576,9 @@ public class PitchView extends ViewGroup {
synchronized (this) {
playPos = pos - samples;
editCount = 0;
editFlash = true;
if (playPos < 0)
if (playPos < 0 || playPos > data.size())
playPos = -1;
if (playPos < 0) {
@ -617,7 +608,7 @@ public class PitchView extends ViewGroup {
time = System.currentTimeMillis();
while (!Thread.currentThread().isInterrupted()) {
long time = System.currentTimeMillis();
drawEdit();
draw();
long cur = System.currentTimeMillis();
long delay = UPDATE_SPEED - (cur - time);

View file

@ -40,38 +40,42 @@
android:layout_height="120dp"
android:layout_centerInParent="true" />
<com.github.axet.audiorecorder.widgets.EqualLinearLayout
android:id="@+id/recording_edit_box"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/recording_pitch"
android:paddingBottom="5dp">
android:layout_above="@id/recording_pitch">
<ImageButton
android:id="@+id/recording_cut"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/round"
android:gravity="center"
android:src="@drawable/ic_content_cut_24dp"
android:text="Cancel" />
<com.github.axet.audiorecorder.widgets.EqualLinearLayout
android:id="@+id/recording_edit_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp">
<ImageButton
android:id="@+id/recording_play"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/round"
android:src="@drawable/play" />
<ImageButton
android:id="@+id/recording_cut"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/round"
android:gravity="center"
android:src="@drawable/ic_content_cut_24dp"
android:text="Cancel" />
<ImageButton
android:id="@+id/recording_play"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/round"
android:src="@drawable/play" />
<ImageButton
android:id="@+id/recording_edit_done"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/round"
android:src="@drawable/ic_close_24dp" />
</com.github.axet.audiorecorder.widgets.EqualLinearLayout>
<ImageButton
android:id="@+id/recording_edit_done"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/round"
android:src="@drawable/ic_close_24dp" />
</com.github.axet.audiorecorder.widgets.EqualLinearLayout>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"