Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.iptea.audio22;
- 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.view.MotionEvent;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.SeekBar;
- import com.hanks.htextview.base.HTextView;
- import com.sdsmdg.harjot.crollerTest.Croller;
- import com.skyfishjy.library.RippleBackground;
- public class MainActivity extends AppCompatActivity {
- Thread t; //audio processing thread
- int sr = 44100; //sampling rate
- boolean isRunning = true; //audio on off
- //SeekBar fSlider;
- Croller croller;
- double sliderval;
- public double fr = 440.f;
- public int counter = 0;
- public double max_frequency = 0;
- @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);
- ImageView kulak=(ImageView)findViewById(R.id.centerImage);
- croller = (Croller) findViewById(R.id.croller);
- kulak.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- String both;
- String both2;
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- isRunning = true;
- 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 = counter*0.5 / croller.getMax();
- fr = 440 + 440*sliderval;
- for(int i=0; i < buffsize; i++){
- samples[i] = (short) (amp*Math.sin(ph));
- ph += twopi*fr/sr;
- }
- audioTrack.write(samples, 0, buffsize);
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- if (( counter % 10 ) == 0) {
- //fSlider.setProgress(counter/89); // counter/100
- croller.setProgress(counter/89);
- counter = counter + 100;
- }
- }
- });
- if (fr > 20000) {
- counter = 0;
- }
- }
- counter = 0;
- audioTrack.stop();
- audioTrack.release();
- }
- };
- t.start();
- return true;
- case MotionEvent.ACTION_UP:
- 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;
- counter = 0;
- t = null;
- rippleBackground.stopRippleAnimation();
- return true;
- }
- return false;
- }
- });
- }
- 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
Advertisement