Professional Documents
Culture Documents
imscope;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
//import android.util.Log;
import android.view.View;
import android.widget.Toast;
import android.widget.EditText;
import android.view.View.OnFocusChangeListener;
import android.content.Context;
import com.cuboid.cuboidcirclebutton.CuboidButton;
import com.jjoe64.graphview.DefaultLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GridLabelRenderer;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
//import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.NumberFormat;
import in.ac.iitb.www.imscope.CircularSeekBar.OnCircularSeekBarChangeListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_im_scope);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
Freq_seekBar = (CircularSeekBar) findViewById(R.id.Frequency_circularSeek);
Time_seekBar = (CircularSeekBar) findViewById(R.id.Duration_circularSeek);
Amp_seekBar = (CircularSeekBar) findViewById(R.id.Amplitude_circularSeek);
if (!hasFocus){
try {
time_progress =
Integer.parseInt(Dura_text.getText().toString());
if (time_progress > 100 | time_progress < 0 ){
Toast.makeText(getApplicationContext(),"Maximum limit
is 100 Sec",
Toast.LENGTH_SHORT).show();
Dura_text.setText("");
}
else{
Time_seekBar.setProgress(time_progress);
time_progress = time_progress*44100;
//Toast.makeText(getApplicationContext(),time_progress
+ " Sec",
// Toast.LENGTH_SHORT).show();
}
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
}
}
});
if (!hasFocus){
try {
amp_progress =
Integer.parseInt(Amp_text.getText().toString());
if (amp_progress > 1000 | amp_progress < 0 ) {
Toast.makeText(getApplicationContext(), "Maximum limit
is 1000 mV",
Toast.LENGTH_SHORT).show();
Amp_text.setText("");
audioManager = (AudioManager)
getSystemService(Context.AUDIO_SERVICE);
int max_vol =
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float point = (Amp_seekBar.getMax())/max_vol;
int value = Math.round(amp_progress/point);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
value, 0);
}
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
}
}
});
if (!hasFocus){
try {
Lfreq_progress =
Integer.parseInt(Start_text.getText().toString());
if (Lfreq_progress > 20000 | Lfreq_progress < 0 ) {
Toast.makeText(getApplicationContext(), "Maximum limit
is 20000 Hz",
Toast.LENGTH_SHORT).show();
Start_text.setText("");
}
});
}
});
Freq_seekBar.setOnSeekBarChangeListener( new
OnCircularSeekBarChangeListener() {
@Override
public void onProgressChanged(CircularSeekBar circularSeekBar, int
progress, boolean fromUser) {
freq_progress = progress;
}
@Override
public void onStopTrackingTouch(CircularSeekBar seekBar) {
if (freq_progress == 0){
Start_text.setText("");
End_text.setText("");
}
else {
Toast.makeText(getApplicationContext(),freq_progress+" Hz",
Toast.LENGTH_SHORT).show();
Start_text.setText(String.format("%d",freq_progress));
End_text.setText(String.format("%d",freq_progress));
Lfreq_progress =
Integer.parseInt(End_text.getText().toString());
Hfreq_progress =
Integer.parseInt(End_text.getText().toString());
}
}
@Override
public void onStartTrackingTouch(CircularSeekBar seekBar) {
Start_text.setText("");
End_text.setText("");
}
});
Time_seekBar.setOnSeekBarChangeListener( new
OnCircularSeekBarChangeListener() {
@Override
public void onProgressChanged(CircularSeekBar circularSeekBar, int
progress, boolean fromUser) {
time_progress = progress;
}
@Override
public void onStopTrackingTouch(CircularSeekBar seekBar) {
if (time_progress == 0){
Dura_text.setText("");
}
else {
Toast.makeText(getApplicationContext(),time_progress+" Sec",
Toast.LENGTH_SHORT).show();
Dura_text.setText(String.format("%d",time_progress));
time_progress = time_progress*44100;
}
}
@Override
public void onStartTrackingTouch(CircularSeekBar seekBar) {
Dura_text.setText("");
}
});
Amp_seekBar.setOnSeekBarChangeListener( new
OnCircularSeekBarChangeListener() {
@Override
public void onProgressChanged(CircularSeekBar circularSeekBar, int
progress, boolean fromUser) {
amp_progress = progress;
}
@Override
public void onStopTrackingTouch(CircularSeekBar seekBar) {
if (amp_progress == 0){
Amp_text.setText("");
//amp_progress = 1;
}
else if (0 < amp_progress & amp_progress< 1000) {
Toast.makeText(getApplicationContext(), amp_progress + " mV",
Toast.LENGTH_SHORT).show();
Amp_text.setText(String.format("%d",amp_progress));
audioManager = (AudioManager)
getSystemService(Context.AUDIO_SERVICE);
int max_vol =
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float point = (Amp_seekBar.getMax())/max_vol;
int value = Math.round(amp_progress/point);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, value,
0);
}
@Override
public void onStartTrackingTouch(CircularSeekBar seekBar) {
Amp_text.setText("");
}
});
strt_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getApplicationContext(), freq_progress + " Hz"+ "
" + Lfreq_progress + " Hz"+ " " + Hfreq_progress + " Hz",
// Toast.LENGTH_SHORT).show();
new
record_Sound().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new
play_Sound_Single_Tone().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
else if (Lfreq_progress < Hfreq_progress & time_progress != 0){
new
record_Sound().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new
play_Sound_Multi_Tone().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
else {
if (Lfreq_progress == Hfreq_progress & Hfreq_progress == 0){
Toast.makeText(getApplicationContext(), "Set the
Frequency",
Toast.LENGTH_SHORT).show();
}
else if (time_progress == 0){
Toast.makeText(getApplicationContext(), "Set the Duration",
Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "Start frequency
can't be greater than End frequency",
Toast.LENGTH_SHORT).show();
Start_text.setText("");
End_text.setText("");
}
});
cyc_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
try {
File in_file = new File(Environment.getExternalStorageDirectory(),
freq_progress + " Hz.pcm");
in_file.createNewFile();
// AudioTrack definition
int mBufferSize = AudioTrack.getMinBufferSize(44100,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_8BIT);
// Sine wave
double[] mSound = new double[duration];
short[] mBuffer = new short[duration];
for (int i = 0; i < mSound.length; i++) {
mSound[i] = Math.sin((2.0*Math.PI * i/(44100/frequency)));
mBuffer[i] = (short) (mSound[i]*Short.MAX_VALUE);
in_dataOutputStream.writeShort(mBuffer[i]);
}
in_dataOutputStream.close();
//noinspection deprecation
mAudioTrack.setStereoVolume(AudioTrack.getMaxVolume(),
AudioTrack.getMaxVolume());
mAudioTrack.play();
mAudioTrack.write(mBuffer, 0, mSound.length);
mAudioTrack.stop();
mAudioTrack.release();
} catch (IOException e) {
e.printStackTrace();
}
try {
file.createNewFile();
audioRecord.startRecording();
while(recording){
int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
for(int i = 0; i < numberOfShort; i++){
dataOutputStream.writeShort(audioData[i]);
}
}
audioRecord.stop();
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
@Override
protected Integer doInBackground(String... params) {
playSound(freq_progress, time_progress);
return null;
}
@Override
protected void onPostExecute(Integer result) {
//Toast.makeText(getApplicationContext(),"Thread
over",Toast.LENGTH_SHORT).show();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
recording = false;
int i = 0, o = 0;
int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);
short[] audioData = new short[bufferSizeInBytes];
double[] data = new double[audioData.length/2];
double[] fft_data_x = new double[1024];
double[] fft_data_y = new double[1024];
InputStream inputStream;
try {
inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new
BufferedInputStream(inputStream);
DataInputStream dataInputStream = new
DataInputStream(bufferedInputStream);
dataInputStream.close();
o++;
graph.removeAllSeries();
NumberFormat nf = NumberFormat.getNumberInstance();
graph.addSeries(series);
//nf.setMinimumFractionDigits(3);
nf.setMinimumIntegerDigits(3);
nf.setMaximumIntegerDigits(4);
//nf.setMaximumFractionDigits(0);
graph.getViewport().setMaxX(2000);
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setYAxisBoundsManual(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class play_Sound_Multi_Tone extends AsyncTask<String, Integer, Integer>
{
@Override
protected void onPreExecute(){
@Override
protected Integer doInBackground(String... params) {
x = min_dec;
k = (int) Math.pow(10,x);
serie = ((dec*10)-(dec-1));
do {
for (int i = 0; i <= 8; i++){
serie--;
number[serie] = (i+1)*k;
}
x++;
k = (int) Math.pow(10,x);
number[0] = max;
lenth = number.length;
if (Hfreq_progress == 20000){
playSound(Hfreq_progress, time_progress);
}
freq_progress = number[j];
playSound(number[j],time_progress);
}
return null;
}
@Override
protected void onPostExecute(Integer result) {
Toast.makeText(getApplicationContext(),"Thread
over",Toast.LENGTH_SHORT).show();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
recording = false;
}
}
@Override
protected void onPreExecute(){
recording = true;
}
@Override
protected Integer doInBackground(String... params) {
//recording = true;
startRecord();
return null;
}
@Override
protected void onPostExecute(Integer result) {
//Toast.makeText(getApplicationContext(), "Recording Thread over",
Toast.LENGTH_SHORT).show();
}
}
int n, m;
FFT(int n) {
this.n = n;
this.m = (int) (Math.log(n) / Math.log(2));
// precompute tables
cos = new double[n / 2];
sin = new double[n / 2];
// Bit-reverse
j = 0;
n2 = n / 2;
for (i = 1; i < n - 1; i++) {
n1 = n2;
while (j >= n1) {
j = j - n1;
n1 = n1 / 2;
}
j = j + n1;
if (i < j) {
t1 = x[i];
x[i] = x[j];
x[j] = t1;
t1 = y[i];
y[i] = y[j];
y[j] = t1;
}
}
// FFT
//n1 = 0;
n2 = 1;