fix trunk issue

This commit is contained in:
Alexey Kuznetsov 2016-03-29 18:11:09 +03:00
commit 9d7805a0dd
3 changed files with 30 additions and 17 deletions

View file

@ -24,6 +24,7 @@ import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
@ -451,8 +452,9 @@ public class RecordingActivity extends AppCompatActivity {
return;
RawSamples rs = new RawSamples(storage.getTempRecording());
rs.trunk(editSample);
rs.trunk(editSample + samplesUpdate);
rs.close();
edit(false, true);
loadSamples();
pitch.drawCalc();
@ -552,7 +554,6 @@ public class RecordingActivity extends AppCompatActivity {
long start = System.currentTimeMillis();
recorder.startRecording();
int samplesUpdateCount = 0;
int samplesTimeCount = 0;
// how many samples we need to update 'samples'. time clock. every 1000ms.
int samplesTimeUpdate = 1000 / 1000 * sampleRate * (RawSamples.CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_MONO ? 1 : 2);
@ -584,17 +585,13 @@ public class RecordingActivity extends AppCompatActivity {
rs.write(buffer);
samplesUpdateCount += s;
if (samplesUpdateCount >= samplesUpdate) {
final float dB = RawSamples.getdB(buffer, 0, readSize);
handle.post(new Runnable() {
@Override
public void run() {
pitch.add(dB);
}
});
samplesUpdateCount -= samplesUpdate;
}
final float dB = RawSamples.getdB(buffer, 0, readSize);
handle.post(new Runnable() {
@Override
public void run() {
pitch.add(dB);
}
});
samplesTime += s;
samplesTimeCount += s;
@ -647,7 +644,17 @@ public class RecordingActivity extends AppCompatActivity {
void updateBufferSize(boolean pause) {
synchronized (bufferSize) {
if (pause) {
samplesUpdate = (int) (1000 * sampleRate / 1000.0);
// we need make buffer multiply of pitch.getPitchTime() (100 ms).
// to prevent missing blocks from view otherwise:
// file may contain not multiply 'samplesUpdate' count of samples. it is about 100ms.
// we can't show on pitchView sorter then 100ms samples. we can't add partial sample because on
// resumeRecording we have to apply rest of samplesUpdate or reload all samples again
// from file. better then confusing user we cut them on next resumeRecording.
long l = 1000;
l = l / pitch.getPitchTime() * pitch.getPitchTime();
samplesUpdate = (int) (l * sampleRate / 1000.0);
} else {
samplesUpdate = (int) (pitch.getPitchTime() * sampleRate / 1000.0);
}

View file

@ -116,21 +116,26 @@ public class RawSamples {
public void trunk(long pos) {
try {
FileChannel outChan = new FileOutputStream(in, true).getChannel();
outChan.truncate(getBufferLen(pos + 1));
outChan.truncate(getBufferLen(pos));
outChan.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static float getdB(short[] buffer, int offset, int len) {
public static double getAmplitude(short[] buffer, int offset, int len) {
double sum = 0;
for (int i = offset; i < offset + len; i++) {
sum += buffer[i] * buffer[i];
}
return Math.sqrt(sum / len);
}
double amplitude = Math.sqrt(sum / len);
public static float getdB(short[] buffer, int offset, int len) {
return getdB(getAmplitude(buffer, offset, len));
}
public static float getdB(double amplitude) {
// https://en.wikipedia.org/wiki/Sound_pressure
double decibel = 20.0 * Math.log10(amplitude / 32768f);

View file

@ -466,6 +466,7 @@ public class PitchView extends ViewGroup {
public long edit(float offset) {
if (offset < 0)
offset = 0;
editPos = ((int) offset) / pitchSize;
if (editPos >= pitchScreenCount)