Skip to content

Instantly share code, notes, and snippets.

@burnix
Created March 21, 2016 08:30
Show Gist options
  • Save burnix/525d0751f31ef44cd2d8 to your computer and use it in GitHub Desktop.
Save burnix/525d0751f31ef44cd2d8 to your computer and use it in GitHub Desktop.
public class Alnome {
private double bpm = 180;
private int beat;
private int noteValue;
private int silence;
private double beatSound;
private double sound;
private final int tick = 500; // samples of tick
private boolean play = true;
private AudioGenerator audioGenerator = new AudioGenerator(8000);
public Alnome(double beatSound, double sound) {
this.beatSound = beatSound;
this.sound = sound;
audioGenerator.createPlayer();
}
public void calcSilence() {
silence = (int) (((60 / bpm) * 8000) - (tick - 150));
}
public void play() {
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 stop() {
play = false;
audioGenerator.destroyAudioTrack();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment