Skip to content

Commit

Permalink
resynth
Browse files Browse the repository at this point in the history
  • Loading branch information
jimomulloy committed Jul 21, 2024
1 parent 2c28f25 commit 9d71aa4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,17 @@ public void run() {
}
gain = amplitude;
// if (noteStatusElement.state != OFF) {
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);
// 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);
totalGain += gain;
lastAmps[toneMapElement.getIndex()] = gain;
audioStream.getSineGenerators()[toneMapElement.getIndex()].setGain(gain);
// } else {
// audioStream.getSineGenerators()[toneMapElement.getIndex()].setGain(0.0);
// lastAmps[toneMapElement.getIndex()] = 0F;
Expand All @@ -413,7 +416,7 @@ public void run() {
// if (totalGain > 1.0) {
for (ToneMapElement toneMapElement : ttfElements) {
double gain = audioStream.getSineGenerators()[toneMapElement.getIndex()].getGain();
if (gain > 0.01) {
if (gain > 0.1) {
audioStream.getSineGenerators()[toneMapElement.getIndex()].setGain(gain / totalGain); // GAIN
} else {
audioStream.getSineGenerators()[toneMapElement.getIndex()].setGain(0.0); // GAIN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public ResynthSource(AudioDispatcher dispatcher) {
.getParameterManager();
this.windowSize = parameterManager.getIntParameter(InstrumentParameterNames.PERCEPTION_HEARING_DEFAULT_WINDOW);
envelopeFollower = new EnvelopeFollower(samplerate, 0.005, 0.01);
this.followEnvelope = true;
this.usePureSine = true;
this.followEnvelope = false;
this.usePureSine = false;
previousFrequencies = new double[5];
previousFrequencyIndex = 0;
}
Expand Down Expand Up @@ -113,7 +113,6 @@ public float getSampleRate() {
}

void initialise() {
LOG.severe(">>!!RS init: " + this.windowSize);
PitchEstimationAlgorithm algo = PitchEstimationAlgorithm.FFT_YIN;

binStartingPointsInCents = new float[windowSize];
Expand All @@ -140,7 +139,6 @@ public boolean process(AudioEvent audioEvent) {
.clone();
ResynthInfo ri = new ResynthInfo(audioFloatBuffer, envelopeAudioBuffer);
putFeature(audioEvent.getTimeStamp(), ri);
LOG.severe(">>!!RS process");
return true;
}

Expand All @@ -156,7 +154,6 @@ public void processingFinished() {
@Override
public void handlePitch(PitchDetectionResult pitchDetectionResult, AudioEvent audioEvent) {
double frequency = pitchDetectionResult.getPitch();
LOG.severe(">>!!RS handlePitch: " + audioEvent.getTimeStamp());
if (frequency == -1) {
frequency = prevFrequency;
} else {
Expand All @@ -177,29 +174,26 @@ public void handlePitch(PitchDetectionResult pitchDetectionResult, AudioEvent au
}

final double twoPiF = 2 * Math.PI * frequency;
envelopeAudioBuffer = audioEvent.getFloatBuffer()
.clone(); // !!TODO CLONED
float[] envelope = null;
LOG.severe(">>!!RS handlePitch ENVELOP: " + audioEvent.getTimeStamp());
float[] audioBuffer = audioEvent.getFloatBuffer();
envelopeAudioBuffer = audioBuffer.clone();
if (followEnvelope) {
envelope = envelopeAudioBuffer.clone();
envelopeFollower.calculateEnvelope(envelope);
envelopeFollower.calculateEnvelope(envelopeAudioBuffer);
}

for (int sample = 0; sample < envelopeAudioBuffer.length; sample++) {
for (int sample = 0; sample < audioBuffer.length; sample++) {
double time = sample / samplerate;
double wave = Math.sin(twoPiF * time + phase);
if (!usePureSine) {
wave += 0.05 * Math.sin(twoPiF * 4 * time + phaseFirst);
wave += 0.01 * Math.sin(twoPiF * 8 * time + phaseSecond);
}
envelopeAudioBuffer[sample] = (float) wave;
audioBuffer[sample] = (float) wave;
if (followEnvelope) {
envelopeAudioBuffer[sample] = envelopeAudioBuffer[sample] * envelope[sample];
audioBuffer[sample] = audioBuffer[sample] * envelopeAudioBuffer[sample];
}
}

double timefactor = twoPiF * envelopeAudioBuffer.length / samplerate;
double timefactor = twoPiF * audioBuffer.length / samplerate;
phase = timefactor + phase;
if (!usePureSine) {
phaseFirst = 4 * timefactor + phaseFirst;
Expand Down

0 comments on commit 9d71aa4

Please sign in to comment.