Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.iptea.audio22;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Color;
- import android.media.AudioFormat;
- import android.media.AudioManager;
- import android.media.AudioTrack;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.SeekBar;
- import android.widget.TextView;
- import com.hanks.htextview.base.HTextView;
- import com.sdsmdg.harjot.crollerTest.Croller;
- import com.skyfishjy.library.RippleBackground;
- import com.xw.repo.BubbleSeekBar;
- import static java.lang.Math.abs;
- public class MainActivity extends AppCompatActivity {
- Thread t; //audio processing thread
- int sr = 44100; //sampling rate
- boolean isRunning = true; //audio on off
- //private SeekBar volumeSeekbar = null;
- private SeekBar volumeSeekbar = null;
- private AudioManager audioManager = null;
- Croller croller;
- int sliderval = 0;
- int crolprogress = 0;
- int trackButPress = 0;
- /*public double fr = 440.f;*/
- public double fr;
- public int frHigher;
- public int frLower;
- public int counter = 0;
- public int highCount = 0;
- public int max_frequency = 0;
- public int min_frequency = 0;
- ImageView finishBut;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.contents);
- final HTextView animText = (HTextView)findViewById(R.id.animatedText);
- final HTextView animText2 = (HTextView)findViewById(R.id.animatedText2);
- finishBut = (ImageView) findViewById(R.id.finishButton);
- ImageView kulak=(ImageView)findViewById(R.id.centerImage);
- final TextView instructionText = (TextView)findViewById(R.id.instruction_text);
- initControls();
- croller = (Croller) findViewById(R.id.croller);
- croller.setProgress(0);
- kulak.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- String both;
- String both2;
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- if ((trackButPress % 2) == 0) {
- isRunning = true;
- counter = 0;
- /*croller.setProgressPrimaryColor(R.color.checkbox_active);*/
- croller.setProgressPrimaryColor(Color.parseColor("#42f4d4"));
- croller.setStartOffset(counter*36/20);
- croller.setProgress(counter/2);
- rippleBackground.startRippleAnimation();
- t = new Thread() {
- public void run() {
- int buffsize = AudioTrack.getMinBufferSize(sr,
- AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
- AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
- sr, AudioFormat.CHANNEL_OUT_MONO,
- AudioFormat.ENCODING_PCM_16BIT, buffsize,
- AudioTrack.MODE_STREAM);
- short samples[] = new short[buffsize]; //audio synthesis
- int amp = 10000;
- double twopi = 8.*Math.atan(1.);
- double ph = 0.0;
- audioTrack.play(); //audio running
- while(isRunning){ //synthesis loop
- frHigher = 100*counter;
- //fr = 100*counter;
- for(int i=0; i < buffsize; i++){
- samples[i] = (short) (amp*Math.sin(ph));
- //ph += twopi*fr/sr;
- ph += twopi*frHigher/sr;
- }
- audioTrack.write(samples, 0, buffsize);
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- //croller.setProgressPrimaryColor(R.color.cyan);
- croller.setProgress(counter/2);
- }
- });
- counter++;
- if (frHigher >= 20000) {
- //counter = 0;
- isRunning = false;
- //rippleBackground.stopRippleAnimation();
- return;
- }
- }
- highCount = counter;
- audioTrack.stop();
- audioTrack.release();
- }
- };
- t.start();
- trackButPress++;
- return true;
- }
- else {
- isRunning = true;
- Log.d("ADebugTag", "highCount/2: " + Double.toString(highCount/2));
- highCount = counter;
- Log.d("ADebugTag", "highCount: " + Double.toString(highCount));
- counter = 0;
- crolprogress = counter/2;
- //croller.setProgressPrimaryColor(Color.parseColor("#e0b3ff"));
- croller.setProgressPrimaryColor(Color.parseColor("#ffb3ff"));
- croller.setProgress(crolprogress);
- croller.setStartOffset(highCount*36/20);
- rippleBackground.startRippleAnimation();
- t = new Thread() {
- public void run() {
- int buffsize = AudioTrack.getMinBufferSize(sr,
- AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
- AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
- sr, AudioFormat.CHANNEL_OUT_MONO,
- AudioFormat.ENCODING_PCM_16BIT, buffsize,
- AudioTrack.MODE_STREAM);
- short samples[] = new short[buffsize]; //audio synthesis
- int amp = 10000;
- double twopi = 8.*Math.atan(1.);
- double ph = 0.0;
- audioTrack.play(); //audio running
- while(isRunning){ //synthesis loop
- sliderval = highCount;
- frLower = 100*sliderval;
- //fr = 100*sliderval;
- for(int i=0; i < buffsize; i++){
- samples[i] = (short) (amp*Math.sin(ph));
- ph += twopi*frLower/sr;
- }
- audioTrack.write(samples, 0, buffsize);
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- crolprogress = counter/2;
- croller.setProgress(crolprogress);
- }
- });
- counter--;
- highCount--;
- if (frLower <= 0) {
- //counter = 0;
- isRunning = false;
- //rippleBackground.stopRippleAnimation();
- return;
- }
- }
- audioTrack.stop();
- audioTrack.release();
- }
- };
- t.start();
- trackButPress++;
- return true;
- }
- //m, k
- case MotionEvent.ACTION_UP:
- rippleBackground.stopRippleAnimation();
- if ((trackButPress % 2) == 0) {
- instructionText.setText(R.string.evenPress);
- }
- else {
- instructionText.setText(R.string.oddPress);
- }
- max_frequency = frHigher;
- min_frequency = frLower;
- if (trackButPress%2 == 0) {
- both = "Max: " + String.valueOf(max_frequency) + " Hz";
- both2 = "Min: " + String.valueOf(min_frequency) + " Hz";
- animText.animateText(both);
- animText2.animateText(both2);
- }
- //both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
- //both2 = "You can hear down to: " + String.valueOf(min_frequency) + " Hz";
- /* if (fr > max_frequency) {
- max_frequency = fr;
- if (max_frequency < 10000) {
- both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
- both2 = "Try again :)";
- }
- else if (max_frequency > 10000 && max_frequency <15000) {
- both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
- both2 = "Time to see a doctor :)";
- }
- else {
- both = "You can hear up to: " + String.valueOf(max_frequency) + " Hz";
- both2 = "Not bad :)";
- }
- animText.animateText(both);
- animText2.animateText(both2);
- }*/
- isRunning = false;
- t = null;
- return true;
- }
- return false;
- }
- });
- }
- private void initControls()
- {
- try
- {
- volumeSeekbar = (SeekBar)findViewById(R.id.volumeControl);
- audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
- volumeSeekbar.setMax(audioManager
- .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
- volumeSeekbar.setProgress(audioManager
- .getStreamVolume(AudioManager.STREAM_MUSIC));
- volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
- {
- @Override
- public void onStopTrackingTouch(SeekBar arg0)
- {
- }
- @Override
- public void onStartTrackingTouch(SeekBar arg0)
- {
- }
- @Override
- public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
- {
- audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
- progress, 0);
- }
- });
- /* {
- volumeSeekbar = (BubbleSeekBar) findViewById(R.id.volumeControl);
- audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
- volumeSeekbar.setOnProgressChangedListener(new BubbleSeekBar.OnProgressChangedListener() {
- @Override
- public void onProgressChanged(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {
- audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
- progress, 0);
- }
- @Override
- public void getProgressOnActionUp(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {
- }
- @Override
- public void getProgressOnFinally(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {
- }
- });*/
- /* volumeSeekbar.se
- volumeSeekbar.setMax(audioManager
- .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
- volumeSeekbar.setProgress(audioManager
- .getStreamVolume(AudioManager.STREAM_MUSIC));
- volumeSeekbar.setOnScrollChangeListener(new );
- volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
- {
- @Override
- public void onStopTrackingTouch(SeekBar arg0)
- {
- }
- @Override
- public void onStartTrackingTouch(SeekBar arg0)
- {
- }
- @Override
- public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
- {
- audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
- progress, 0);
- }
- });*/
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public void lastpage(View v) {
- //String strname = urname.getText().toString();
- //String stremail = uremail.getText().toString();
- /* if (strname.equals("")||stremail.equals("")) {
- Toast.makeText(getBaseContext(), "Enter both name and email", Toast.LENGTH_SHORT).show();
- }else {
- Intent in = new Intent(LoginActivity.this, VoiceActivity.class);
- Bundle bundle = new Bundle();
- bundle.putString("urname",strname);
- bundle.putString("uremail",stremail);
- in.putExtras(bundle);
- startActivity(in);
- }*/
- Intent initi = new Intent(MainActivity.this, ResultActivity.class);
- /* Bundle bundle = new Bundle();
- bundle.putString("urname",strname);
- bundle.putString("uremail",stremail);
- in.putExtras(bundle);*/
- startActivity(initi);
- }
- /* public void onDestroy(){
- super.onDestroy();
- isRunning = false;
- try {
- t.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- t = null;
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment