From 0e9ae7cc91402a594e9f6ad2ede9219aa9cef05d Mon Sep 17 00:00:00 2001 From: Matt Jenkins Date: Mon, 18 May 2020 14:11:36 +0100 Subject: [PATCH] Improved gain curve editing --- .../audiobookrecorder/AudiobookRecorder.java | 19 ++++---- .../majenko/audiobookrecorder/Sentence.java | 35 +++++++++++--- .../majenko/audiobookrecorder/Waveform.java | 47 ++++++++++++++++++- 3 files changed, 84 insertions(+), 17 deletions(-) diff --git a/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java b/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java index e3969dc..432958e 100644 --- a/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java +++ b/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java @@ -398,15 +398,15 @@ public class AudiobookRecorder extends JFrame implements DocumentListener { } }); - sampleWaveform.addMouseWheelListener(new MouseWheelListener() { - public void mouseWheelMoved(MouseWheelEvent e) { - if (selectedSentence == null) return; - if (selectedSentence.isLocked()) return; - int val = ((int)gainPercent.getValue()) - e.getWheelRotation(); - if (val < 1) val = 1; - gainPercent.setValue(val); - } - }); +// sampleWaveform.addMouseWheelListener(new MouseWheelListener() { +// public void mouseWheelMoved(MouseWheelEvent e) { +// if (selectedSentence == null) return; +// if (selectedSentence.isLocked()) return; +// int val = ((int)gainPercent.getValue()) - e.getWheelRotation(); +// if (val < 1) val = 1; +// gainPercent.setValue(val); +// } +// }); sampleControl.add(sampleWaveform, BorderLayout.CENTER); @@ -3473,6 +3473,7 @@ public class AudiobookRecorder extends JFrame implements DocumentListener { try { b.save(); } catch (Exception e) { + e.printStackTrace(); JOptionPane.showMessageDialog(AudiobookRecorder.this, "There was an error saving the book: " + e.getMessage(), "Save Error", JOptionPane.ERROR_MESSAGE); } } diff --git a/src/uk/co/majenko/audiobookrecorder/Sentence.java b/src/uk/co/majenko/audiobookrecorder/Sentence.java index 080a95e..081963d 100644 --- a/src/uk/co/majenko/audiobookrecorder/Sentence.java +++ b/src/uk/co/majenko/audiobookrecorder/Sentence.java @@ -1686,14 +1686,16 @@ public class Sentence extends BookTreeNode implements Cacheable { sentenceNode.appendChild(Book.makeTextNode(doc, "peak", getPeak())); sentenceNode.appendChild(Book.makeTextNode(doc, "detected", beenDetected())); Element gp = doc.createElement("gainpoints"); - for (Integer loc : gainPoints.keySet()) { - Double g = gainPoints.get(loc); - Element p = doc.createElement("gainpoint"); - p.setAttribute("location", String.format("%d", loc)); - p.setAttribute("gain", String.format("%.3g", g)); - gp.appendChild(p); + if (gainPoints != null) { + for (Integer loc : gainPoints.keySet()) { + Double g = gainPoints.get(loc); + Element p = doc.createElement("gainpoint"); + p.setAttribute("location", String.format("%d", loc)); + p.setAttribute("gain", String.format("%.3g", g)); + gp.appendChild(p); + } + sentenceNode.appendChild(gp); } - sentenceNode.appendChild(gp); return sentenceNode; } @@ -1835,10 +1837,17 @@ public class Sentence extends BookTreeNode implements Cacheable { } public TreeMap getGainPoints() { + Debug.trace(); + if (gainPoints == null) { + gainPoints = new TreeMap(); + } return gainPoints; } public void addGainPoint(Integer loc, Double g) { + if (gainPoints == null) { + gainPoints = new TreeMap(); + } gainPoints.put(loc, g); CacheManager.removeFromCache(this); } @@ -1848,6 +1857,18 @@ public class Sentence extends BookTreeNode implements Cacheable { CacheManager.removeFromCache(this); } + public void adjustGainPoint(Integer loc, Double adj) { + if (gainPoints == null) { + gainPoints = new TreeMap(); + return; + } + Double gp = gainPoints.get(loc); + if (gp == null) return; + gp += adj; + gainPoints.put(loc, gp); + CacheManager.removeFromCache(this); + } + public double[] calculateGains() { double[] gains = new double[sampleSize]; diff --git a/src/uk/co/majenko/audiobookrecorder/Waveform.java b/src/uk/co/majenko/audiobookrecorder/Waveform.java index fb1995e..c6ba163 100644 --- a/src/uk/co/majenko/audiobookrecorder/Waveform.java +++ b/src/uk/co/majenko/audiobookrecorder/Waveform.java @@ -2,16 +2,18 @@ package uk.co.majenko.audiobookrecorder; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; +import java.awt.event.MouseWheelListener; import javax.swing.JPanel; import java.util.TreeMap; import java.util.ArrayList; import java.awt.Graphics; import java.awt.event.MouseEvent; +import java.awt.event.MouseWheelEvent; import java.awt.Dimension; import java.awt.Color; import java.awt.Cursor; -public class Waveform extends JPanel implements MouseListener, MouseMotionListener { +public class Waveform extends JPanel implements MouseListener, MouseMotionListener, MouseWheelListener { Sentence sentence = null; @@ -46,6 +48,7 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen super(); addMouseListener(this); addMouseMotionListener(this); + addMouseWheelListener(this); } public void setSentence(Sentence s) { @@ -267,6 +270,37 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen dragging = 0; } + public void mouseWheelMoved(MouseWheelEvent e) { + if (sentence == null) return; + if (sentence.isLocked()) return; + + if (displayGainCurve) { + int x = e.getX() * step + offset; + int f = -1; + int diff = Integer.MAX_VALUE; + + TreeMap gc = sentence.getGainPoints(); + for (Integer loc : gc.keySet()) { + int d = Math.abs(loc - x); + if (d < diff) { + diff = d; + f = loc; + } + } + + if (diff / step < 5) { + sentence.adjustGainPoint(f, (0 - e.getWheelRotation()) / 100d); + repaint(); + return; + } + } + + int val = ((int)AudiobookRecorder.window.gainPercent.getValue()) - e.getWheelRotation(); + if (val < 1) val = 1; + AudiobookRecorder.window.gainPercent.setValue(val); + } + + public void mouseClicked(MouseEvent e) { if (displayGainCurve) { if (e.getButton() == MouseEvent.BUTTON1) { @@ -278,6 +312,17 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen int x = e.getX() * step + offset; double y = (double)(h - e.getY()) / (double)h * 2.0; + sentence.addGainPoint(x, y); + repaint(); + } else if (e.getButton() == MouseEvent.BUTTON2) { + Dimension size = getSize(); + + int w = size.width; + int h = size.height; + + int x = e.getX() * step + offset; + double y = 1.0d; + sentence.addGainPoint(x, y); repaint(); } else if (e.getButton() == MouseEvent.BUTTON3) {