Skip to content

Commit

Permalink
gainer
Browse files Browse the repository at this point in the history
  • Loading branch information
jimomulloy committed Jul 20, 2024
1 parent 8139a5c commit d7d16ee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@

package jomu.instrument.audio;

import java.util.logging.Logger;

import be.tarsos.dsp.AudioEvent;
import be.tarsos.dsp.AudioProcessor;

public class SineGenerator implements AudioProcessor {

private static final Logger LOG = Logger.getLogger(SineGenerator.class.getName());

private double gain;
private double frequency;
private double phase;
Expand All @@ -49,19 +53,21 @@ public boolean process(AudioEvent audioEvent) {
double sampleRate = audioEvent.getSampleRate();
double twoPiF = 2 * Math.PI * frequency;
double time = 0;
double useGain = gain;
if (gain == lastGain) {
useGain = gain;
}
double useGain = lastGain;
boolean factorPositive = Math.sin(phase) > 0;
for (int i = 0; i < buffer.length; i++) {
time = i / sampleRate;
double factor = Math.sin(twoPiF * time + phase);
if (factor == 0) {
if (factorPositive) {
if (factor <= 0) {
useGain = gain;
}
} else if (factor >= 0) {
useGain = gain;
lastGain = gain;
}
buffer[i] += (float) (useGain * factor);
}
lastGain = useGain;
phase = twoPiF * buffer.length / sampleRate + phase;
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,14 @@ public void run() {
}
gain = amplitude;
// if (noteStatusElement.state != OFF) {
audioStream.getSineGenerators()[toneMapElement.getIndex()].setGain(gain); // GAIN
totalGain += gain;
lastAmps[toneMapElement.getIndex()] = gain;
double newGain = lastAmps[toneMapElement.getIndex()]
+ (gain - lastAmps[toneMapElement.getIndex()]) / 2.0;
if (newGain < 0) {
newGain = 0;
}
totalGain += newGain;
lastAmps[toneMapElement.getIndex()] = newGain;
audioStream.getSineGenerators()[toneMapElement.getIndex()].setGain(newGain);
// } else {
// audioStream.getSineGenerators()[toneMapElement.getIndex()].setGain(0.0);
// lastAmps[toneMapElement.getIndex()] = 0F;
Expand All @@ -423,13 +428,13 @@ public void run() {
this.audioStream.getGenerator()
.process();
if (count > 0) {
// LOG.severe(
// ">>Audio gen process: " + time + ", " + count + ", " +
// audioEvent.getTimeStamp());
LOG.severe(
">>Audio gen process COUNT: " + time + ", " + count + ", " +
audioEvent.getTimeStamp());
}
count++;
// LOG.severe(">>Audio gen process: " + time + ", " +
// audioEvent.getTimeStamp());
LOG.severe(">>Audio gen process: " + time + ", " +
audioEvent.getTimeStamp());
audioEvent = this.audioStream.getGenerator()
.getAudioEvent();
}
Expand Down

0 comments on commit d7d16ee

Please sign in to comment.