diff options
Diffstat (limited to 'source/Android-lib/src/net/surina/ExampleActivity.java')
-rw-r--r-- | source/Android-lib/src/net/surina/ExampleActivity.java | 201 |
1 files changed, 178 insertions, 23 deletions
diff --git a/source/Android-lib/src/net/surina/ExampleActivity.java b/source/Android-lib/src/net/surina/ExampleActivity.java index d1f8e1c..465cb9d 100644 --- a/source/Android-lib/src/net/surina/ExampleActivity.java +++ b/source/Android-lib/src/net/surina/ExampleActivity.java @@ -1,21 +1,50 @@ +///////////////////////////////////////////////////////////////////////////// +/// +/// Example Android Application/Activity that allows processing WAV +/// audio files with SoundTouch library +/// +/// Copyright (c) Olli Parviainen +/// +//////////////////////////////////////////////////////////////////////////////// +// +// $Id: SoundTouch.java 210 2015-05-14 20:03:56Z oparviai $ +// +//////////////////////////////////////////////////////////////////////////////// + + package net.surina; +import java.io.File; + import net.surina.soundtouch.SoundTouch; import net.surina.soundtouchexample.R; -import net.surina.soundtouchexample.R.id; -import net.surina.soundtouchexample.R.layout; import android.app.Activity; +import android.content.Intent; +import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; +import android.util.Log; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.EditText; import android.widget.TextView; +import android.widget.Toast; -public class ExampleActivity extends Activity +public class ExampleActivity extends Activity implements OnClickListener { - TextView textViewConsole; + TextView textViewConsole = null; + EditText editSourceFile = null; + EditText editOutputFile = null; + EditText editTempo = null; + EditText editPitch = null; + CheckBox checkBoxPlay = null; + StringBuilder consoleText = new StringBuilder(); + /// Called when the activity is created @Override protected void onCreate(Bundle savedInstanceState) { @@ -23,38 +52,164 @@ public class ExampleActivity extends Activity setContentView(R.layout.activity_example); textViewConsole = (TextView)findViewById(R.id.textViewResult); + editSourceFile = (EditText)findViewById(R.id.editTextSrcFileName); + editOutputFile = (EditText)findViewById(R.id.editTextOutFileName); + + editTempo = (EditText)findViewById(R.id.editTextTempo); + editPitch = (EditText)findViewById(R.id.editTextPitch); - // Check soundtouch + Button buttonFileSrc = (Button)findViewById(R.id.buttonSelectSrcFile); + Button buttonFileOutput = (Button)findViewById(R.id.buttonSelectOutFile); + Button buttonProcess = (Button)findViewById(R.id.buttonProcess); + buttonFileSrc.setOnClickListener(this); + buttonFileOutput.setOnClickListener(this); + buttonProcess.setOnClickListener(this); + + checkBoxPlay = (CheckBox)findViewById(R.id.checkBoxPlay); + + // Check soundtouch library presence & version checkLibVersion(); } - - @Override - protected void onDestroy() + + /// Function to append status text onto "console box" on the Activity + public void appendToConsole(final String text) { - textViewConsole = null; + // run on UI thread to avoid conflicts + runOnUiThread(new Runnable() + { + public void run() + { + consoleText.append(text); + consoleText.append("\n"); + textViewConsole.setText(consoleText); + } + }); } + - /// Append text to console on the activity - public void appendToConsole(String text) + /// print SoundTouch native library version onto console + protected void checkLibVersion() { - if (textViewConsole == null) return; - consoleText.append(text); - consoleText.append("\n"); - textViewConsole.setText(consoleText); + String ver = SoundTouch.getVersionString(); + appendToConsole("SoundTouch native library version = " + ver); + } + + + + /// Button click handler + @Override + public void onClick(View arg0) + { + switch (arg0.getId()) + { + case R.id.buttonSelectSrcFile: + case R.id.buttonSelectOutFile: + // one of the file select buttons clicked ... we've not just implemented them ;-) + Toast.makeText(this, "File selector not implemented, sorry! Enter the file path manually ;-)", Toast.LENGTH_LONG).show(); + break; + + case R.id.buttonProcess: + // button "process" pushed + process(); + break; + } + } + /// Play audio file + protected void playWavFile(String fileName) + { + File file2play = new File(fileName); + Intent i = new Intent(); + i.setAction(android.content.Intent.ACTION_VIEW); + i.setDataAndType(Uri.fromFile(file2play), "audio/wav"); + startActivity(i); + } - /// print SoundTouch native library version onto console - protected void checkLibVersion() + + + /// Helper class that will execute the SoundTouch processing. As the processing may take + /// some time, run it in background thread to avoid hanging of the UI. + protected class ProcessTask extends AsyncTask<ProcessTask.Parameters, Integer, Long> { - SoundTouch st = new SoundTouch(); + /// Helper class to store the SoundTouch file processing parameters + public final class Parameters + { + String inFileName; + String outFileName; + float tempo; + float pitch; + } + + + /// Processing routine + @Override + protected Long doInBackground(Parameters... aparams) + { + Parameters params = aparams[0]; + + SoundTouch st = new SoundTouch(); + st.setTempo(params.tempo); + st.setPitchSemiTones(params.pitch); + Log.i("SoundTouch", "process file " + params.inFileName); + long startTime = System.currentTimeMillis(); + int res = st.processFile(params.inFileName, params.outFileName); + long endTime = System.currentTimeMillis(); + float duration = (endTime - startTime) * 0.001f; + + Log.i("SoundTouch", "process file done, duration = " + duration); + appendToConsole("Processing done, duration " + duration + " sec."); + if (res != 0) + { + String err = SoundTouch.getErrorString(); + appendToConsole("Failure: " + err); + return -1L; + } + + // Play file if so is desirable + if (checkBoxPlay.isChecked()) + { + playWavFile(params.outFileName); + } + return 0L; + } - String ver = st.getVersionString(); - appendToConsole("SoundTouch native library version = " + ver); } -} + + /// process a file with SoundTouch. Do the processing using a background processing + /// task to avoid hanging of the UI + protected void process() + { + try + { + ProcessTask task = new ProcessTask(); + ProcessTask.Parameters params = task.new Parameters(); + // parse processing parameters + params.inFileName = editSourceFile.getText().toString(); + params.outFileName = editOutputFile.getText().toString(); + params.tempo = 0.01f * Float.parseFloat(editTempo.getText().toString()); + params.pitch = Float.parseFloat(editPitch.getText().toString()); + + // update UI about status + appendToConsole("Process audio file :" + params.inFileName +" => " + params.outFileName); + appendToConsole("Tempo = " + params.tempo); + appendToConsole("Pitch adjust = " + params.pitch); + + Toast.makeText(this, "Starting to process file " + params.inFileName + "...", Toast.LENGTH_SHORT).show(); + + // start processing task in background + task.execute(params); + + } + catch (Exception exp) + { + exp.printStackTrace(); + } + + } +}
\ No newline at end of file |