Added progress dialog while exporting #14

This commit is contained in:
2018-09-23 17:51:54 +01:00
parent 3036ef801b
commit c9bada7721
9 changed files with 298 additions and 144 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 896 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 885 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 834 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 839 B

+29
View File
@@ -41,6 +41,9 @@ public class Encoder {
private static final Pattern FORMAT_PATTERN = Pattern private static final Pattern FORMAT_PATTERN = Pattern
.compile("^\\s*([D ])([E ])\\s+([\\w,]+)\\s+.+$"); .compile("^\\s*([D ])([E ])\\s+([\\w,]+)\\s+.+$");
private static final Pattern TIME_PATTERN = Pattern
.compile("^(\\d+):(\\d+):(\\d+)\\.(\\d+)$");
/** /**
* This regexp is used to parse the ffmpeg output about the included * This regexp is used to parse the ffmpeg output about the included
* encoders/decoders. * encoders/decoders.
@@ -895,6 +898,31 @@ public class Encoder {
if (listener != null) { if (listener != null) {
String time = (String) table.get("time"); String time = (String) table.get("time");
if (time != null) { if (time != null) {
Matcher tm = TIME_PATTERN.matcher(time);
if (tm.matches()) {
try {
long hours = Long.parseLong(tm.group(1));
long minutes = Long.parseLong(tm.group(2));
long seconds = Long.parseLong(tm.group(3));
long millis = Long.parseLong(tm.group(4));
progress = (hours * 3600L * 1000L) +
(minutes * 60L * 1000L) +
(seconds * 1000L) +
(millis * 100L);
int perm = (int) Math
.round((double) (progress * 1000L)
/ (double) duration);
if (perm > 1000) {
perm = 1000;
}
listener.progress(perm);
} catch (NumberFormatException e) {
;
}
} else {
int dot = time.indexOf('.'); int dot = time.indexOf('.');
if (dot > 0 && dot == time.length() - 2 if (dot > 0 && dot == time.length() - 2
&& duration > 0) { && duration > 0) {
@@ -918,6 +946,7 @@ public class Encoder {
} }
} }
} }
}
lastWarning = null; lastWarning = null;
} }
} }
@@ -1145,13 +1145,20 @@ public class AudiobookRecorder extends JFrame {
i++; i++;
} }
} }
FileOutputStream fos = null;
try { try {
FileOutputStream fos = new FileOutputStream(config); fos = new FileOutputStream(config);
prefs.storeToXML(fos, "Audiobook Recorder Description"); prefs.storeToXML(fos, "Audiobook Recorder Description");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
}
}
} }
public void loadBookStructure(File f) { public void loadBookStructure(File f) {
@@ -1471,8 +1478,15 @@ public class AudiobookRecorder extends JFrame {
} }
@SuppressWarnings("unchecked") class ExportThread implements Runnable {
public void exportAudio() { ExportDialog exportDialog;
public ExportThread(ExportDialog e) {
super();
exportDialog = e;
}
public void run() {
try { try {
File bookRoot = new File(Options.get("path.storage"), book.getName()); File bookRoot = new File(Options.get("path.storage"), book.getName());
@@ -1484,7 +1498,6 @@ public class AudiobookRecorder extends JFrame {
if (!export.exists()) { if (!export.exists()) {
export.mkdirs(); export.mkdirs();
} }
Encoder encoder; Encoder encoder;
String ffloc = Options.get("path.ffmpeg"); String ffloc = Options.get("path.ffmpeg");
@@ -1504,7 +1517,6 @@ public class AudiobookRecorder extends JFrame {
audioAttributes.setBitRate(Options.getInteger("audio.export.bitrate")); audioAttributes.setBitRate(Options.getInteger("audio.export.bitrate"));
audioAttributes.setSamplingRate(Options.getInteger("audio.export.samplerate")); audioAttributes.setSamplingRate(Options.getInteger("audio.export.samplerate"));
audioAttributes.setChannels(new Integer(2)); audioAttributes.setChannels(new Integer(2));
attributes.setFormat("mp3"); attributes.setFormat("mp3");
attributes.setAudioAttributes(audioAttributes); attributes.setAudioAttributes(audioAttributes);
@@ -1516,20 +1528,30 @@ public class AudiobookRecorder extends JFrame {
int fullLength = 0; int fullLength = 0;
Chapter c = o.nextElement(); Chapter c = o.nextElement();
if (c.getChildCount() == 0) continue; if (c.getChildCount() == 0) continue;
int kids = c.getChildCount();
if (kids == 0) continue;
String name = c.getName(); String name = c.getName();
exportDialog.setMessage("Exporting " + name);
exportDialog.setProgress(0);
File exportFile = new File(export, name + ".wax"); File exportFile = new File(export, name + ".wax");
File wavFile = new File(export, name + ".wav"); File wavFile = new File(export, name + ".wav");
FileOutputStream fos = new FileOutputStream(exportFile); FileOutputStream fos = new FileOutputStream(exportFile);
data = getRoomNoise(s2i(Options.get("catenation.pre-chapter"))); data = getRoomNoise(s2i(Options.get("catenation.pre-chapter")));
fullLength += data.length; fullLength += data.length;
fos.write(data); fos.write(data);
int kidno = 0;
for (Enumeration<Sentence> s = c.children(); s.hasMoreElements();) { for (Enumeration<Sentence> s = c.children(); s.hasMoreElements();) {
kidno++;
exportDialog.setProgress(kidno * 1000 / kids);
Sentence snt = s.nextElement(); Sentence snt = s.nextElement();
data = snt.getRawAudioData(); data = snt.getRawAudioData();
@@ -1545,7 +1567,6 @@ public class AudiobookRecorder extends JFrame {
fos.write(data); fos.write(data);
} }
fos.close(); fos.close();
FileInputStream fis = new FileInputStream(exportFile); FileInputStream fis = new FileInputStream(exportFile);
AudioInputStream ais = new AudioInputStream(fis, format, fullLength); AudioInputStream ais = new AudioInputStream(fis, format, fullLength);
fos = new FileOutputStream(wavFile); fos = new FileOutputStream(wavFile);
@@ -1564,11 +1585,13 @@ public class AudiobookRecorder extends JFrame {
if (c.getChildCount() == 0) continue; if (c.getChildCount() == 0) continue;
String name = c.getName(); String name = c.getName();
exportDialog.setMessage("Converting " + name);
File wavFile = new File(export, name + ".wav"); File wavFile = new File(export, name + ".wav");
File mp3File = new File(export, name + "-untagged.mp3"); File mp3File = new File(export, name + "-untagged.mp3");
File taggedFile = new File(export, name + ".mp3"); File taggedFile = new File(export, name + ".mp3");
encoder.encode(wavFile, mp3File, attributes); encoder.encode(wavFile, mp3File, attributes, exportDialog);
Mp3File id3 = new Mp3File(mp3File); Mp3File id3 = new Mp3File(mp3File);
@@ -1586,18 +1609,29 @@ public class AudiobookRecorder extends JFrame {
tags.setComment(book.getComment()); tags.setComment(book.getComment());
id3.save(taggedFile.getAbsolutePath()); id3.save(taggedFile.getAbsolutePath());
mp3File.delete(); mp3File.delete();
wavFile.delete(); wavFile.delete();
} }
JOptionPane.showMessageDialog(this, "Book exported.", "Done", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
exportDialog.closeDialog();
}
}
@SuppressWarnings("unchecked")
public void exportAudio() {
ExportDialog ed = new ExportDialog("Exporting book...");
ExportThread t = new ExportThread(ed);
Thread nt = new Thread(t);
nt.start();
ed.setVisible(true);
} }
@@ -0,0 +1,84 @@
package uk.co.majenko.audiobookrecorder;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import it.sauronsoftware.jave.*;
public class ExportDialog extends JDialog implements EncoderProgressListener {
JLabel message;
JLabel icon;
JProgressBar progress;
int spin = 0;
public ExportDialog(String m) {
super();
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
message = new JLabel(m);
icon = new JLabel(Icons.spinner0);
progress = new JProgressBar(0, 1000);
setLayout(new BorderLayout());
add(message, BorderLayout.CENTER);
icon.setBorder(new EmptyBorder(10, 10, 10, 10));
add(icon, BorderLayout.WEST);
add(progress, BorderLayout.SOUTH);
setLocationRelativeTo(AudiobookRecorder.window);
setPreferredSize(new Dimension(300, 100));
pack();
setSize(new Dimension(300, 100));
// setVisible(true);
}
public void setMessage(String m) {
message.setText(m);
}
public void closeDialog() {
dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
public void spin() {
spin++;
if (spin == 4) spin = 0;
switch (spin) {
case 0: icon.setIcon(Icons.spinner0); break;
case 1: icon.setIcon(Icons.spinner1); break;
case 2: icon.setIcon(Icons.spinner2); break;
case 3: icon.setIcon(Icons.spinner3); break;
}
}
public void progress(int p) { setProgress(p); }
public void setProgress(int p) {
progress.setValue(p);
progress.setString((p / 10) + "%");
spin();
}
public void message(String m) {
}
public void sourceInfo(MultimediaInfo i) {
}
}
@@ -22,5 +22,9 @@ public class Icons {
static public final ImageIcon appIcon = new ImageIcon(Icons.class.getResource("icons/appIcon.png")); static public final ImageIcon appIcon = new ImageIcon(Icons.class.getResource("icons/appIcon.png"));
static public final ImageIcon star = new ImageIcon(Icons.class.getResource("icons/star.png")); static public final ImageIcon star = new ImageIcon(Icons.class.getResource("icons/star.png"));
static public final ImageIcon important = new ImageIcon(Icons.class.getResource("icons/important.png")); static public final ImageIcon important = new ImageIcon(Icons.class.getResource("icons/important.png"));
static public final ImageIcon spinner0 = new ImageIcon(Icons.class.getResource("icons/spinner0.png"));
static public final ImageIcon spinner1 = new ImageIcon(Icons.class.getResource("icons/spinner1.png"));
static public final ImageIcon spinner2 = new ImageIcon(Icons.class.getResource("icons/spinner2.png"));
static public final ImageIcon spinner3 = new ImageIcon(Icons.class.getResource("icons/spinner3.png"));
} }
@@ -96,6 +96,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
try { try {
AudioSystem.write(inputStream, AudioFileFormat.Type.WAVE, audioFile); AudioSystem.write(inputStream, AudioFileFormat.Type.WAVE, audioFile);
} catch (Exception e) { } catch (Exception e) {
inputStream = null;
e.printStackTrace(); e.printStackTrace();
} }
} }
@@ -112,8 +113,10 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
public void stopRecording() { public void stopRecording() {
try { try {
inputStream.close(); inputStream.close();
inputStream = null;
line.stop(); line.stop();
line.close(); line.close();
line = null;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }