From ddedb194fdcd38b3d66c4f314f37ea3c0e26f3e8 Mon Sep 17 00:00:00 2001 From: Jim O'Mulloy Date: Fri, 17 May 2024 17:23:55 +0100 Subject: [PATCH] note tracker --- .../cell/AudioSynthesisProcessor.java | 4 +- .../workspace/tonemap/CalibrationMap.java | 1 - .../workspace/tonemap/NoteTracker.java | 137 +++++++++++++++--- .../workspace/tonemap/ToneSynthesiser.java | 20 ++- 4 files changed, 136 insertions(+), 26 deletions(-) diff --git a/instrument-core/src/main/java/jomu/instrument/cognition/cell/AudioSynthesisProcessor.java b/instrument-core/src/main/java/jomu/instrument/cognition/cell/AudioSynthesisProcessor.java index 6b52ebea..1c81cdf0 100644 --- a/instrument-core/src/main/java/jomu/instrument/cognition/cell/AudioSynthesisProcessor.java +++ b/instrument-core/src/main/java/jomu/instrument/cognition/cell/AudioSynthesisProcessor.java @@ -332,7 +332,7 @@ public void accept(List messages) throws InstrumentException { if (tmIndex > 0) { synthesisFrame = synthesisToneMap.getTimeFrame(tmIndex); if (synthesisFrame != null) { - synthesiser.synthesiseChords(synthesisFrame); + synthesiser.synthesiseChords(synthesisFrame, cm); console.getVisor() .updateToneMapView(synthesisToneMap, synthesisFrame, this.cell.getCellType() .toString()); @@ -350,7 +350,7 @@ public void accept(List messages) throws InstrumentException { for (int i = tmIndex + 1; i <= sequence; i++) { synthesisFrame = synthesisToneMap.getTimeFrame(i); if (synthesisFrame != null) { - synthesiser.synthesiseChords(synthesisFrame); + synthesiser.synthesiseChords(synthesisFrame, cm); console.getVisor() .updateToneMapView(synthesisToneMap, synthesisFrame, this.cell.getCellType() .toString()); diff --git a/instrument-core/src/main/java/jomu/instrument/workspace/tonemap/CalibrationMap.java b/instrument-core/src/main/java/jomu/instrument/workspace/tonemap/CalibrationMap.java index fc1afd29..39e8e0c9 100644 --- a/instrument-core/src/main/java/jomu/instrument/workspace/tonemap/CalibrationMap.java +++ b/instrument-core/src/main/java/jomu/instrument/workspace/tonemap/CalibrationMap.java @@ -169,7 +169,6 @@ public void calibrateMetronome(double metronomeStart, double metronomeDistance) double time = metronomeStart; while (time <= lastTime) { beatMap.put(time, -1.0); - LOG.severe(">>CM metro put: " + time); time += metronomeDistance; } } diff --git a/instrument-core/src/main/java/jomu/instrument/workspace/tonemap/NoteTracker.java b/instrument-core/src/main/java/jomu/instrument/workspace/tonemap/NoteTracker.java index 1e9a918c..b54c673f 100644 --- a/instrument-core/src/main/java/jomu/instrument/workspace/tonemap/NoteTracker.java +++ b/instrument-core/src/main/java/jomu/instrument/workspace/tonemap/NoteTracker.java @@ -164,7 +164,9 @@ public class NoteTrack { int number; double salience; + boolean chordPending; List notes = new CopyOnWriteArrayList<>(); + private NoteListElement quantizeNote; public NoteTrack(int number) { this.number = number; @@ -357,6 +359,22 @@ public NoteListElement[] getCurrentNotes(double time) { } return noteList.toArray(new NoteListElement[noteList.size()]); } + + public void setChordPending(boolean chordPending) { + this.chordPending = chordPending; + } + + public boolean isChordPending() { + return this.chordPending; + } + + public NoteListElement getQuantizeNote() { + return this.quantizeNote; + } + + public void setQuantizeNote(NoteListElement quantizeNote) { + this.quantizeNote = quantizeNote; + } } public NoteTracker(ToneMap toneMap) { @@ -893,6 +911,10 @@ private void processChordTrack(int trackNumber, ToneTimeFrame toneTimeFrame, Cho private void addChordNotes(NoteTrack track, NoteListElement quantizeNote, double time, ChordListElement chordListElement, PitchSet pitchSet, SynthChordParameters synthChordParameters) { NoteListElement lastNote = track.getLastNote(); + double lastNoteEndTime = 0; + if (lastNote != null) { + lastNoteEndTime = lastNote.endTime; + } NoteListElement[] currentNotes = track.getCurrentNotes(time * 1000); Set currentNoteSet = new HashSet<>(); for (NoteListElement nle : currentNotes) { @@ -906,18 +928,23 @@ private void addChordNotes(NoteTrack track, NoteListElement quantizeNote, double int note = 0; int octave = 0; - double startTime; - double endTime; + double startTime = 0; + double quantizeStartTime = 0; + double quantizeEndTime = 0; + double endTime = 0; + startTime = chordListElement.getStartTime() * 1000; + endTime = chordListElement.getEndTime() * 1000 + incrementTime; if (quantizeNote != null) { - startTime = quantizeNote.startTime; - endTime = startTime; - double range = quantizeNote.endTime - quantizeNote.startTime; - endTime += range > 0 ? range * synthChordParameters.chordMeasure : synthChordParameters.chordMeasure * 100; - } else { - startTime = chordListElement.getStartTime() * 1000; - endTime = chordListElement.getEndTime() * 1000 + incrementTime; + NoteListElement trackQuantizeNote = track.getQuantizeNote(); + if (trackQuantizeNote == null || trackQuantizeNote.startTime < quantizeNote.startTime) { + track.setQuantizeNote(quantizeNote); + quantizeStartTime = quantizeNote.startTime; + quantizeEndTime = quantizeStartTime; + double range = quantizeNote.endTime - quantizeNote.startTime; + quantizeEndTime += range > 0 ? range * synthChordParameters.chordMeasure + : synthChordParameters.chordMeasure * 100; + } } - double amplitude = 1.0; List camps = new ArrayList<>(); @@ -964,7 +991,8 @@ public int compare(ChordNote c1, ChordNote c2) { } camps.add(amplitude); cnotes.add(note); - if (synthChordParameters.chordPattern == 0 || synthChordParameters.chordPattern == 1) { + if (synthChordParameters.chordPattern == 0 || synthChordParameters.chordPattern == 1 + || synthChordParameters.chordPattern == 4) { NoteListElement cnle = new NoteListElement(note, pitchSet.getIndex(note), startTime, endTime, 0, 0, amplitude, amplitude, amplitude, 0, false, incrementTime); newNotes.add(cnle); @@ -972,39 +1000,113 @@ public int compare(ChordNote c1, ChordNote c2) { } } if (synthChordParameters.chordPattern == 0) { + LOG.severe("--SYNTH0: " + time + ", " + startTime + ", " + track.number + ", " + startTime + ", " + + quantizeNote + ", " + time); if (!newNotes.stream() .allMatch(nle -> currentNoteSet.contains(nle.note))) { + LOG.severe("--SYNTH0 X: " + startTime + ", " + currentNotes.length + ", " + newNotes.size()); for (NoteListElement cnle : currentNotes) { + LOG.severe("--SYNTH0 X1 CURR NOTE: " + startTime + ", " + cnle.note + ", " + cnle.startTime + + ", " + cnle.endTime); if (cnle.endTime + incrementTime >= startTime) { cnle.endTime = startTime - incrementTime; } } for (NoteListElement nnle : newNotes) { + LOG.severe("--SYNTH0 X1 ADD NOTE: " + startTime + ", " + nnle.note); track.addNote(nnle); - if (lastNote != null && lastNote.endTime >= startTime) { - return; - } + // if (lastNote != null && lastNote.endTime >= startTime) { + // return; + // } } } else { - if (quantizeNote != null) { + if (quantizeStartTime > 0) { + LOG.severe("--SYNTH0 Y: " + startTime + ", " + currentNotes.length + ", " + newNotes.size()); for (NoteListElement nnle : newNotes) { for (NoteListElement cnle : currentNotes) { - if (lastNote != null && lastNote.endTime >= startTime) { + LOG.severe("--SYNTH0 Y1 CURR NOTE: " + startTime + ", " + cnle.note + ", " + cnle.startTime + + ", " + cnle.endTime); + if (lastNote != null && lastNoteEndTime >= startTime) { if (nnle.note == cnle.note && (cnle.endTime + incrementTime >= nnle.startTime)) { cnle.endTime = nnle.endTime; + LOG.severe("--SYNTH0 Y2 CURR NOTE: " + startTime + ", " + cnle.note + ", " + + cnle.startTime + + ", " + cnle.endTime); } } else { if (nnle.note == cnle.note && (cnle.endTime + incrementTime >= nnle.startTime)) { cnle.endTime = startTime - incrementTime; + LOG.severe("--SYNTH0 Y3 CURR NOTE: " + startTime + ", " + cnle.note + ", " + + cnle.startTime + + ", " + cnle.endTime); } } } - if (lastNote != null && lastNote.endTime < startTime) { + if (lastNote != null && lastNoteEndTime < startTime) { + LOG.severe("--SYNTH0 Y1 ADD NOTE: " + startTime + ", " + nnle.note); track.addNote(nnle); } } } } + } else if (synthChordParameters.chordPattern == 4) { + LOG.severe(">>SYNTH: " + time + ", " + startTime + ", " + track.number + ", " + startTime + ", " + + quantizeNote + ", " + + track.isChordPending()); + if (track.isChordPending() || !newNotes.stream() + .allMatch(nle -> currentNoteSet.contains(nle.note))) { + + LOG.severe(">>SYNTH X: " + startTime + ", " + currentNotes.length + ", " + newNotes.size()); + if (quantizeStartTime > 0) { + track.setChordPending(false); + for (NoteListElement nnle : newNotes) { + nnle.startTime = quantizeStartTime; + track.addNote(nnle); + LOG.severe(">>SYNTH X1 ADD NOTE: " + startTime + ", " + nnle.note); + // if (lastNote != null && lastNote.endTime >= startTime) { + // return; + // } + } + for (NoteListElement cnle : currentNotes) { + LOG.severe(">>SYNTH X1 CURR NOTE: " + startTime + ", " + cnle.note + ", " + cnle.startTime + + ", " + cnle.endTime); + cnle.endTime = quantizeStartTime - incrementTime; + } + } else { + track.setChordPending(true); + } + } else { + LOG.severe(">>SYNTH Y: " + startTime + ", " + currentNotes.length + ", " + newNotes.size()); + if (quantizeStartTime > 0) { + for (NoteListElement nnle : newNotes) { + for (NoteListElement cnle : currentNotes) { + LOG.severe(">>SYNTH Y1 CURR NOTE: " + startTime + ", " + cnle.note + ", " + cnle.startTime + + ", " + cnle.endTime); + if (lastNote != null && lastNoteEndTime >= startTime) { + if (nnle.note == cnle.note && (cnle.endTime + incrementTime >= nnle.startTime)) { + cnle.endTime = nnle.endTime; + } + } else { + if (nnle.note == cnle.note && (cnle.endTime + incrementTime >= nnle.startTime)) { + cnle.endTime = nnle.startTime - incrementTime; + } + } + } + if (lastNote != null && lastNoteEndTime < startTime) { + LOG.severe(">>SYNTH Y1 ADD NOTE: " + startTime + ", " + nnle.note); + track.addNote(nnle); + } + } + } else { + for (NoteListElement cnle : currentNotes) { + LOG.severe(">>SYNTH Z1 CURR NOTE: " + startTime + ", " + cnle.note + ", " + cnle.startTime + + ", " + cnle.endTime); + cnle.endTime = endTime - incrementTime;// + incrementTime; + LOG.severe(">>SYNTH Z2 CURR NOTE: " + startTime + ", " + cnle.note + ", " + cnle.startTime + + ", " + cnle.endTime + ", " + cnle.avgAmp + ", " + cnle); + } + } + } } else if (synthChordParameters.chordPattern == 1) { for (NoteListElement nnle : newNotes) { if (!currentNoteSet.contains(nnle.note)) { @@ -1017,6 +1119,7 @@ public int compare(ChordNote c1, ChordNote c2) { } } } + } else { lastNote = track.getLastNote(); if (synthChordParameters.chordInvert) { diff --git a/instrument-core/src/main/java/jomu/instrument/workspace/tonemap/ToneSynthesiser.java b/instrument-core/src/main/java/jomu/instrument/workspace/tonemap/ToneSynthesiser.java index 68c511a5..459c2356 100644 --- a/instrument-core/src/main/java/jomu/instrument/workspace/tonemap/ToneSynthesiser.java +++ b/instrument-core/src/main/java/jomu/instrument/workspace/tonemap/ToneSynthesiser.java @@ -91,11 +91,11 @@ public void synthesiseNotes(ToneTimeFrame toneTimeFrame, CalibrationMap calibrat if (toneTimeFrame.getChord() == null) { toneTimeFrame.setChord(chord); } - // if (chord != null) { - // chord = quantizeChord(chord, calibrationMap, quantizeBeatNote, quantizeRange, - // quantizePercent, - // quantizeBeat); - // } + if (chord != null && quantizeSource == 1) { + chord = quantizeChord(chord, calibrationMap, quantizeBeatNote, quantizeRange, + quantizePercent, + quantizeBeat); + } } Set discardedNotes = new HashSet<>(); Set nles = addNotes(toneTimeFrame); @@ -113,7 +113,10 @@ public void synthesiseNotes(ToneTimeFrame toneTimeFrame, CalibrationMap calibrat } } - public void synthesiseChords(ToneTimeFrame toneTimeFrame) { + public void synthesiseChords(ToneTimeFrame toneTimeFrame, CalibrationMap calibrationMap) { + NoteTrack quantizeBeatTrack = toneMap.getNoteTracker() + .getBeatTrack(quantizeSource); + NoteListElement quantizeBeatNote = quantizeBeatTrack.getLastNote(); ChordListElement chord = toneTimeFrame.getChord(); if (!synthChordFirstSwitch) { if (chord != null) { @@ -126,6 +129,11 @@ public void synthesiseChords(ToneTimeFrame toneTimeFrame) { toneTimeFrame.setChord(chord); chord = toneTimeFrame.getChord(); } + if (chord != null && quantizeSource == 1) { + chord = quantizeChord(chord, calibrationMap, quantizeBeatNote, quantizeRange, + quantizePercent, + quantizeBeat); + } } ChordListElement ac = chord;