Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.thomas.android_ss1;
- /**
- * Created by thomas on 21.06.18.
- * Übernommen und angepasst von http://masterex.github.io/archive/2012/05/28/android-audio-synthesis.html
- *
- */
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- public class Metronome extends Thread {
- public double bpm = 120;
- private int beat;
- private int noteValue;
- private int silence;
- private double beatSound = 800;
- private double sound = 400;
- private final int tick = 1000; // samples of tick
- public boolean play = false;
- private AudioGenerator audioGenerator = new AudioGenerator(8000);
- public Metronome() {
- audioGenerator.createPlayer();
- }
- public void calcSilence() {
- silence = (int) (((60/bpm)*8000)-tick);
- }
- public void startPlayingMetronome() {
- play = true;
- calcSilence();
- double[] tick =
- audioGenerator.getSineWave(this.tick, 8000, beatSound);
- double[] tock =
- audioGenerator.getSineWave(this.tick, 8000, sound);
- double silence = 0;
- double[] sound = new double[8000];
- int t = 0,s = 0,b = 0;
- do {
- for(int i=0;i<sound.length&&play;i++) {
- if(t<this.tick) {
- if(b == 0)
- sound[i] = tock[t];
- else
- sound[i] = tick[t];
- t++;
- } else {
- sound[i] = silence;
- s++;
- if(s >= this.silence) {
- t = 0;
- s = 0;
- b++;
- if(b > (this.beat-1))
- b = 0;
- }
- }
- }
- audioGenerator.writeSound(sound);
- } while(play);
- }
- public void stopPlayingMetronome() {
- play = false;
- audioGenerator.destroyAudioTrack();
- }
- public void destroyMetronome(){
- }
- /* Getters and Setters ... */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement