Added waveform zoom and scroll #18
This commit is contained in:
BIN
resources/uk/co/majenko/audiobookrecorder/icons/zoom-in.png
Normal file
BIN
resources/uk/co/majenko/audiobookrecorder/icons/zoom-in.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 996 B |
BIN
resources/uk/co/majenko/audiobookrecorder/icons/zoom-out.png
Normal file
BIN
resources/uk/co/majenko/audiobookrecorder/icons/zoom-out.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 997 B |
@@ -66,6 +66,7 @@ public class AudiobookRecorder extends JFrame {
|
|||||||
|
|
||||||
JPanel sampleControl;
|
JPanel sampleControl;
|
||||||
Waveform sampleWaveform;
|
Waveform sampleWaveform;
|
||||||
|
JScrollBar sampleScroll;
|
||||||
|
|
||||||
JSpinner postSentenceGap;
|
JSpinner postSentenceGap;
|
||||||
JCheckBox locked;
|
JCheckBox locked;
|
||||||
@@ -276,6 +277,18 @@ public class AudiobookRecorder extends JFrame {
|
|||||||
sampleControl.setPreferredSize(new Dimension(400, 150));
|
sampleControl.setPreferredSize(new Dimension(400, 150));
|
||||||
sampleWaveform = new Waveform();
|
sampleWaveform = new Waveform();
|
||||||
|
|
||||||
|
sampleScroll = new JScrollBar(JScrollBar.HORIZONTAL);
|
||||||
|
sampleScroll.setMinimum(0);
|
||||||
|
sampleScroll.setMaximum(1000);
|
||||||
|
sampleScroll.addAdjustmentListener(new AdjustmentListener() {
|
||||||
|
public void adjustmentValueChanged(AdjustmentEvent e) {
|
||||||
|
sampleWaveform.setOffset(sampleScroll.getValue());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
sampleControl.add(sampleScroll, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
|
||||||
sampleWaveform.addMarkerDragListener(new MarkerDragListener() {
|
sampleWaveform.addMarkerDragListener(new MarkerDragListener() {
|
||||||
public void leftMarkerMoved(MarkerDragEvent e) {
|
public void leftMarkerMoved(MarkerDragEvent e) {
|
||||||
if (selectedSentence != null) {
|
if (selectedSentence != null) {
|
||||||
@@ -345,7 +358,7 @@ public class AudiobookRecorder extends JFrame {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
JPanel controlsTop = new JPanel();
|
JToolBar controlsTop = new JToolBar(JToolBar.HORIZONTAL);
|
||||||
JToolBar controlsLeft = new JToolBar(JToolBar.VERTICAL);
|
JToolBar controlsLeft = new JToolBar(JToolBar.VERTICAL);
|
||||||
JToolBar controlsRight = new JToolBar(JToolBar.VERTICAL);
|
JToolBar controlsRight = new JToolBar(JToolBar.VERTICAL);
|
||||||
|
|
||||||
@@ -379,6 +392,24 @@ public class AudiobookRecorder extends JFrame {
|
|||||||
controlsTop.add(postSentenceGap);
|
controlsTop.add(postSentenceGap);
|
||||||
controlsTop.add(new JLabel("ms"));
|
controlsTop.add(new JLabel("ms"));
|
||||||
|
|
||||||
|
JButton zoomIn = new JButton(Icons.zoomIn);
|
||||||
|
zoomIn.setToolTipText("Zoom In");
|
||||||
|
zoomIn.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
sampleWaveform.increaseZoom();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
controlsRight.add(zoomIn);
|
||||||
|
|
||||||
|
JButton zoomOut = new JButton(Icons.zoomOut);
|
||||||
|
zoomOut.setToolTipText("Zoom Out");
|
||||||
|
zoomOut.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
sampleWaveform.decreaseZoom();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
controlsRight.add(zoomOut);
|
||||||
|
|
||||||
sampleControl.add(controlsTop, BorderLayout.NORTH);
|
sampleControl.add(controlsTop, BorderLayout.NORTH);
|
||||||
sampleControl.add(controlsLeft, BorderLayout.WEST);
|
sampleControl.add(controlsLeft, BorderLayout.WEST);
|
||||||
sampleControl.add(controlsRight, BorderLayout.EAST);
|
sampleControl.add(controlsRight, BorderLayout.EAST);
|
||||||
|
|||||||
@@ -28,4 +28,6 @@ public class Icons {
|
|||||||
static public final ImageIcon spinner3 = new ImageIcon(Icons.class.getResource("icons/spinner3.png"));
|
static public final ImageIcon spinner3 = new ImageIcon(Icons.class.getResource("icons/spinner3.png"));
|
||||||
static public final ImageIcon eq = new ImageIcon(Icons.class.getResource("icons/eq.png"));
|
static public final ImageIcon eq = new ImageIcon(Icons.class.getResource("icons/eq.png"));
|
||||||
static public final ImageIcon mic = new ImageIcon(Icons.class.getResource("icons/mic.png"));
|
static public final ImageIcon mic = new ImageIcon(Icons.class.getResource("icons/mic.png"));
|
||||||
|
static public final ImageIcon zoomIn = new ImageIcon(Icons.class.getResource("icons/zoom-in.png"));
|
||||||
|
static public final ImageIcon zoomOut = new ImageIcon(Icons.class.getResource("icons/zoom-out.png"));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,10 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen
|
|||||||
|
|
||||||
int step = 1;
|
int step = 1;
|
||||||
|
|
||||||
|
int zoomFactor = 1;
|
||||||
|
int offsetFactor = 0;
|
||||||
|
int offset = 0;
|
||||||
|
|
||||||
ArrayList<MarkerDragListener> markerDragListeners;
|
ArrayList<MarkerDragListener> markerDragListeners;
|
||||||
|
|
||||||
public Waveform() {
|
public Waveform() {
|
||||||
@@ -66,9 +70,13 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen
|
|||||||
if (samples != null) {
|
if (samples != null) {
|
||||||
|
|
||||||
int num = samples.length;
|
int num = samples.length;
|
||||||
step = num / w;
|
step = num / zoomFactor / w;
|
||||||
if (step == 0) return;
|
if (step == 0) return;
|
||||||
|
|
||||||
|
double off = offsetFactor / 1000d;
|
||||||
|
int ns = (num - (w * step));
|
||||||
|
offset = (int)(ns * off);
|
||||||
|
|
||||||
for (int n = 0; n < w; n++) {
|
for (int n = 0; n < w; n++) {
|
||||||
int hcnt = 0;
|
int hcnt = 0;
|
||||||
long have = 0;
|
long have = 0;
|
||||||
@@ -79,7 +87,7 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen
|
|||||||
int lmax = 0;
|
int lmax = 0;
|
||||||
|
|
||||||
for (int o = 0; o < step; o++) {
|
for (int o = 0; o < step; o++) {
|
||||||
int sample = samples[(n * step) + o];
|
int sample = samples[offset + (n * step) + o];
|
||||||
if (sample >= 0) {
|
if (sample >= 0) {
|
||||||
have += sample;
|
have += sample;
|
||||||
hcnt++;
|
hcnt++;
|
||||||
@@ -100,23 +108,23 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen
|
|||||||
have /= scale;
|
have /= scale;
|
||||||
lave /= scale;
|
lave /= scale;
|
||||||
|
|
||||||
g.setColor(new Color(0, 0, 100));
|
g.setColor(new Color(0, 20, 200));
|
||||||
g.drawLine(n, h/2 + lmax, n, h/2 - hmax);
|
g.drawLine(n, h/2 + lmax, n, h/2 - hmax);
|
||||||
g.setColor(new Color(0, 0, 200));
|
g.setColor(new Color(0, 100, 255));
|
||||||
g.drawLine(n, h/2 + (int)lave, n, h/2 - (int)have);
|
g.drawLine(n, h/2 + (int)lave, n, h/2 - (int)have);
|
||||||
}
|
}
|
||||||
|
|
||||||
g.setColor(new Color(255, 0, 0, 32));
|
g.setColor(new Color(255, 0, 0, 32));
|
||||||
g.fillRect(0, 0, leftAltMarker/step, h);
|
g.fillRect(0, 0, (leftAltMarker - offset)/step, h);
|
||||||
g.fillRect(rightAltMarker/step, 0, w , h);
|
g.fillRect((rightAltMarker - offset)/step, 0, (num - rightAltMarker) / step , h);
|
||||||
|
|
||||||
g.setColor(new Color(255, 0, 0));
|
g.setColor(new Color(255, 0, 0));
|
||||||
g.drawLine(leftAltMarker/step, 0, leftAltMarker/step, h);
|
g.drawLine((leftAltMarker - offset)/step, 0, (leftAltMarker - offset)/step, h);
|
||||||
g.drawLine(rightAltMarker/step, 0, rightAltMarker/step, h);
|
g.drawLine((rightAltMarker - offset)/step, 0, (rightAltMarker - offset)/step, h);
|
||||||
|
|
||||||
g.setColor(new Color(255, 255, 0));
|
g.setColor(new Color(255, 255, 0));
|
||||||
g.drawLine(leftMarker/step, 0, leftMarker/step, h);
|
g.drawLine((leftMarker - offset)/step, 0, (leftMarker - offset)/step, h);
|
||||||
g.drawLine(rightMarker/step, 0, rightMarker/step, h);
|
g.drawLine((rightMarker - offset)/step, 0, (rightMarker - offset)/step, h);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -177,13 +185,13 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen
|
|||||||
|
|
||||||
public void mousePressed(MouseEvent e) {
|
public void mousePressed(MouseEvent e) {
|
||||||
int x = e.getX();
|
int x = e.getX();
|
||||||
if ((x >= (leftMarker/step) - 2) && (x <= (leftMarker/step) + 2)) {
|
if ((x >= ((leftMarker - offset)/step) - 2) && (x <= ((leftMarker - offset)/step) + 2)) {
|
||||||
leftMarkerSaved = leftMarker;
|
leftMarkerSaved = leftMarker;
|
||||||
rightMarkerSaved = rightMarker;
|
rightMarkerSaved = rightMarker;
|
||||||
dragging = 1;
|
dragging = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ((x >= (rightMarker/step) - 2) && (x <= (rightMarker/step) + 2)) {
|
if ((x >= ((rightMarker - offset)/step) - 2) && (x <= ((rightMarker - offset)/step) + 2)) {
|
||||||
rightMarkerSaved = rightMarker;
|
rightMarkerSaved = rightMarker;
|
||||||
leftMarkerSaved = leftMarker;
|
leftMarkerSaved = leftMarker;
|
||||||
dragging = 2;
|
dragging = 2;
|
||||||
@@ -211,11 +219,11 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen
|
|||||||
|
|
||||||
public void mouseMoved(MouseEvent e) {
|
public void mouseMoved(MouseEvent e) {
|
||||||
int x = e.getX();
|
int x = e.getX();
|
||||||
if ((x >= (leftMarker/step) - 2) && (x <= (leftMarker/step) + 2)) {
|
if ((x >= ((leftMarker - offset)/step) - 2) && (x <= ((leftMarker - offset)/step) + 2)) {
|
||||||
setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
|
setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ((x >= (rightMarker/step) - 2) && (x <= (rightMarker/step) + 2)) {
|
if ((x >= ((rightMarker - offset)/step) - 2) && (x <= ((rightMarker - offset)/step) + 2)) {
|
||||||
setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
|
setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -229,12 +237,12 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen
|
|||||||
int x = e.getX();
|
int x = e.getX();
|
||||||
|
|
||||||
if (dragging == 1) {
|
if (dragging == 1) {
|
||||||
leftMarker = x * step;
|
leftMarker = (x * step) + offset;
|
||||||
if (leftMarker > rightMarker) {
|
if (leftMarker > rightMarker) {
|
||||||
leftMarker = rightMarker;
|
leftMarker = rightMarker;
|
||||||
}
|
}
|
||||||
} else if (dragging == 2) {
|
} else if (dragging == 2) {
|
||||||
rightMarker = x * step;
|
rightMarker = (x * step) + offset;
|
||||||
if (rightMarker < leftMarker) {
|
if (rightMarker < leftMarker) {
|
||||||
rightMarker = leftMarker;
|
rightMarker = leftMarker;
|
||||||
}
|
}
|
||||||
@@ -243,4 +251,24 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen
|
|||||||
repaint();
|
repaint();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setOffset(int permil) {
|
||||||
|
offsetFactor = permil;
|
||||||
|
if (offsetFactor < 0) offsetFactor = 0;
|
||||||
|
if (offsetFactor > 1000) offsetFactor = 1000;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void increaseZoom() {
|
||||||
|
zoomFactor *= 2;
|
||||||
|
if (zoomFactor > 64) zoomFactor = 64;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void decreaseZoom() {
|
||||||
|
zoomFactor /= 2;
|
||||||
|
if (zoomFactor < 1) zoomFactor = 1;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user