Commit 6d9ef299 authored by CallisteHanriat's avatar CallisteHanriat

adding undo feature

parent 7bca1f98
......@@ -92,7 +92,8 @@
android:layout_height="fill_parent"
android:text="@string/undo"
android:layout_weight="0.5"
android:id="@+id/undoButton"
android:id="@+id/undoButton"
android:enabled="false"
/>
<ImageButton
......
......@@ -27,7 +27,7 @@
android:id="@+id/respeak_total_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/0s" />
android:text="/0s" />
</LinearLayout>
......@@ -90,9 +90,10 @@
<Button
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="undo"
android:text="@string/undo"
android:layout_weight="0.5"
android:id="@+id/undoButton"
android:id="@+id/undoButton"
android:enabled="false"
/>
<ImageButton
......
......@@ -117,6 +117,16 @@ public class ThumbRespeaker {
recorder.listen();
}
/**
* called when we need to going on the previous sample.
*/
public void goToPreviousSample() {
if(previousEndSample > 0l) {
player.seekToSample(previousEndSample);
} else {
player.seekToSample(0);
}
}
/**
* Pauses the respeaking process.
*
......
......@@ -55,12 +55,45 @@ public class ListenFragment extends Fragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.listen_fragment, container, false);
playPauseButton = (ImageButton) v.findViewById(R.id.PlayPauseButton);
inflatedView = inflater.inflate(R.layout.listen_fragment, container, false);
playPauseButton = (ImageButton) inflatedView.findViewById(R.id.PlayPauseButton);
playPauseButton.setOnClickListener(this);
playPauseButton.setEnabled(false);
seekBar = (InterleavedSeekBar) v.findViewById(R.id.InterleavedSeekBar);
disabledSeekBar();
return inflatedView;
}
/**
* called by constructor onCreate() because we need to
* disabled seekBar touch event if we havn't set a player object
*/
private void disabledSeekBar() {
seekBar = (InterleavedSeekBar) inflatedView.findViewById(R.id.InterleavedSeekBar);
seekBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
int originalProgress;
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
if (fromUser) {
seekBar.setProgress(originalProgress);
}
}
public void onStopTrackingTouch(SeekBar _seekBar) {};
public void onStartTrackingTouch(SeekBar _seekBar) {
originalProgress = seekBar.getProgress();
};
});
seekBar.invalidate();
}
/**
* called after we have set a player object
*/
private void enabledSeekBar() {
seekBar = (InterleavedSeekBar) inflatedView.findViewById(R.id.InterleavedSeekBar);
seekBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
int prog;
......@@ -88,7 +121,6 @@ public class ListenFragment extends Fragment implements OnClickListener {
public void onStartTrackingTouch(SeekBar _seekBar) {};
});
seekBar.invalidate();
return v;
}
/**
......@@ -212,6 +244,7 @@ public class ListenFragment extends Fragment implements OnClickListener {
this.player = simplePlayer;
player.setOnCompletionListener(onCompletionListener);
playPauseButton.setEnabled(true);
enabledSeekBar();
}
/**
......@@ -223,6 +256,7 @@ public class ListenFragment extends Fragment implements OnClickListener {
this.player = markedPlayer;
player.setOnCompletionListener(onCompletionListener);
playPauseButton.setEnabled(true);
enabledSeekBar();
}
/**
......@@ -234,6 +268,7 @@ public class ListenFragment extends Fragment implements OnClickListener {
this.player = transcriptPlayer;
player.setOnCompletionListener(onCompletionListener);
playPauseButton.setEnabled(true);
enabledSeekBar();
}
/**
......@@ -256,6 +291,7 @@ public class ListenFragment extends Fragment implements OnClickListener {
}
player.setOnCompletionListener(onCompletionListener);
playPauseButton.setEnabled(true);
enabledSeekBar();
}
public void releasePlayer() {
......@@ -310,6 +346,6 @@ public class ListenFragment extends Fragment implements OnClickListener {
private ImageButton playPauseButton;
private InterleavedSeekBar seekBar;
private Thread seekBarThread;
private View inflatedView;
private ListenFragment otherPlayer;
}
......@@ -15,6 +15,7 @@ import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
......@@ -42,6 +43,7 @@ import org.lp20.aikuma.audio.record.ThumbRespeaker;
import org.lp20.aikuma.audio.record.Microphone.MicException;
import org.lp20.aikuma2.R;
import com.google.android.gms.wallet.fragment.BuyButtonText;
import com.musicg.experiment.math.cluster.Segment;
import com.musicg.wave.Wave;
......@@ -59,6 +61,16 @@ public class ThumbRespeakFragment extends Fragment {
installButtonBehaviour(v);
textTimeProgression = (TextView) v.findViewById(R.id.respeak_current_time);
totalTime = (TextView) v.findViewById(R.id.respeak_total_time);
undo = (Button) v.findViewById(R.id.undoButton);
undo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//if undo is enabled, we disabled because we want to undo one time.
onUndiAction();
}
});
seekBar = (InterleavedSeekBar) v.findViewById(R.id.InterleavedSeekBar);
seekBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
......@@ -85,6 +97,15 @@ public class ThumbRespeakFragment extends Fragment {
return v;
}
private void setProgressBarAtPreviousState() {
respeaker.playOriginal(1);
int currentPosition =
respeaker.getSimplePlayer().getCurrentMsec();
seekBar.setProgress(
(int)(((float)currentPosition/(float)
respeaker.getSimplePlayer().getDurationMsec())*100));
}
/**
* Called when the fragment is destroyed; ensures resources are
* appropriately released.
......@@ -99,13 +120,18 @@ public class ThumbRespeakFragment extends Fragment {
}
String filename = FileIO.getOwnerPath().getAbsolutePath() +
"/recordings/" + "temp.rspk.wav";
"/recordings/" + "temp"+count+".rspk.wav";
if (listenFragment.getPlayer() != null) {
listenFragment.getPlayer().release();
}
try {
FileIO.delete(new File(filename));
Log.i(TAG, "Deleting " + filename);
while (count>0) {
FileIO.delete(new File(filename));
Log.i(TAG, "Deleting " + filename);
count--;
filename = FileIO.getOwnerPath().getAbsolutePath() +
"/recordings/" + "temp"+count+".rspk.wav";
}
} catch (IOException e) {
Log.e(TAG, "failed to delete " + filename + " - maybe the file didn't exist");
}
......@@ -137,7 +163,7 @@ public class ThumbRespeakFragment extends Fragment {
long previousGestureTime = gestureTime;
gestureTime = System.currentTimeMillis();
gestureTimeUpToDown = System.currentTimeMillis() - gestureTimeUpToDown;
if(isCommented){ // After commentary is recorded(or At the start)
//Rewind and Store the start-point
respeaker.playOriginal(1);
......@@ -180,7 +206,7 @@ public class ThumbRespeakFragment extends Fragment {
count++;
String filename = FileIO.getOwnerPath().getAbsolutePath() +
"/recordings/" + "temp.rspk.wav";
"/recordings/" + "temp"+count+".rspk.wav";
if (listenFragment.getPlayer() != null) {
listenFragment.getPlayer().release();
}
......@@ -213,10 +239,11 @@ public class ThumbRespeakFragment extends Fragment {
ThumbRespeakFragment.this.getActivity().finish();
}
undo.setEnabled(true);
// save the latest segment in a temporary file
String filename = FileIO.getOwnerPath().getAbsolutePath() +
"/recordings/" + "temp.rspk.wav";
"/recordings/" + "temp"+count+".rspk.wav";
int rate = respeaker.getRecorder().getMicrophone().getSampleRate();
PCMWriter file = PCMWriter.getInstance(rate,
respeaker.getRecorder().getMicrophone().getChannelConfiguration(),
......@@ -277,7 +304,6 @@ public class ThumbRespeakFragment extends Fragment {
seekBar.setProgress(
(int)(((float)currentPosition/(float)
respeaker.getSimplePlayer().getDurationMsec())*100));
Log.d("changement barre", "barre principale");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
......@@ -288,7 +314,52 @@ public class ThumbRespeakFragment extends Fragment {
});
seekBarThread.start();
}
/**
* called when we need to set the previous
* recorded segment to the graphical listen fragment
*/
private void setPreviousPlayerToListenFragment() {
int rate = respeaker.getRecorder().getMicrophone().getSampleRate();
String filename = FileIO.getOwnerPath().getAbsolutePath() +
"/recordings/" + "temp"+(count-1)+".rspk.wav";
if (count-1 == 0){
FragmentTransaction ft = getFragmentManager().beginTransaction();
listenFragment = new ListenFragment();
ft.replace(R.id.respeak_latest_segment_player, listenFragment);
ft.commit();
}else {
try {
SimplePlayer player = new SimplePlayer(new File(filename),
rate, true);
Log.i(TAG, "New SimplePlater of" + filename + " (rate: " + rate + ")");
listenFragment.setPlayer(player);
Log.d("passage", "fin setPreviousPLayer");
} catch (IOException e) {
Log.e(TAG, e.toString());
} catch (NoSuchElementException e) {
Log.e(TAG, e.toString());
}
}
}
/**
* called when we need to delete last temp file and
* decrements the count attribute
*/
private void deleteLastTempFile() {
String filename = FileIO.getOwnerPath().getAbsolutePath() +
"/recordings/" + "temp"+count+".rspk.wav";
count--;
try {
FileIO.delete(new File(filename));
Log.i(TAG, "Deleting " + filename);
} catch (IOException e) {
Log.e(TAG, "failed to delete " + filename + " - maybe the file didn't exist");
}
}
/**
* Recording mutator.
*
......@@ -298,6 +369,32 @@ public class ThumbRespeakFragment extends Fragment {
this.recording = recording;
}
/**
* called when we need to return one state before.
*/
private void onUndiAction() {
if (count == 1 || undo.isEnabled()) {
undo.setEnabled(false);
}
setPreviousPlayerToListenFragment();
deleteLastTempFile();
respeaker.goToPreviousSample();
int currentPosition =
respeaker.getSimplePlayer().getCurrentMsec();
Toast.makeText(getActivity(), "currentPos="+currentPosition, Toast.LENGTH_SHORT).show();
seekBar.setProgress(
(int)(((float)currentPosition/(float)
respeaker.getSimplePlayer().getDurationMsec())*100));
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
textTimeProgression.setText(respeaker.getSimplePlayer().getCurrentMsec()/1000 + "s");
}
});
}
/**
* sample rate mutator.
*
......@@ -383,10 +480,20 @@ public class ThumbRespeakFragment extends Fragment {
private TextView totalTime;
private final int VALID_GESTURE_TIME = 250; //0.25sec
/**
* time we have kept in touch on the button
*/
private long gestureTime = VALID_GESTURE_TIME;
/**
* time we have kept hand apart the button
*/
private long gestureTimeUpToDown = VALID_GESTURE_TIME;
private boolean isCommented = true;
private int count = 0;
private Button undo;
public static final String TAG = "ThumbRespeakFragment";
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment