diff --git a/instrument-core/src/main/java/jomu/instrument/audio/SineGenerator.java b/instrument-core/src/main/java/jomu/instrument/audio/SineGenerator.java index 09eb3700..0145fafd 100644 --- a/instrument-core/src/main/java/jomu/instrument/audio/SineGenerator.java +++ b/instrument-core/src/main/java/jomu/instrument/audio/SineGenerator.java @@ -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; @@ -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; } diff --git a/instrument-core/src/main/java/jomu/instrument/audio/TarsosAudioSynthesizer.java b/instrument-core/src/main/java/jomu/instrument/audio/TarsosAudioSynthesizer.java index bfecbdba..c6571061 100644 --- a/instrument-core/src/main/java/jomu/instrument/audio/TarsosAudioSynthesizer.java +++ b/instrument-core/src/main/java/jomu/instrument/audio/TarsosAudioSynthesizer.java @@ -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; @@ -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(); }