Added external processors

This commit is contained in:
2019-01-07 15:45:52 +00:00
parent b55f76c855
commit e5b395ffa5
4 changed files with 283 additions and 0 deletions

View File

@@ -903,6 +903,33 @@ public class AudiobookRecorder extends JFrame {
}
});
JMenu external = new JMenu("Run external processor");
for (int i = 0; i < 999; i++) {
String name = Options.get("editor.processor." + i + ".name");
if (name == null) break;
if (name.equals("")) break;
JMenuObject ob = new JMenuObject(name, s, new ActionListener() {
public void actionPerformed(ActionEvent e) {
JMenuObject o = (JMenuObject)e.getSource();
Sentence s = (Sentence)o.getObject();
s.runExternalProcessor(Utils.s2i(o.getActionCommand()));
}
});
ob.setActionCommand(Integer.toString(i));
external.add(ob);
}
JMenuObject undo = new JMenuObject("Undo", s, new ActionListener() {
public void actionPerformed(ActionEvent e) {
JMenuObject o = (JMenuObject)e.getSource();
Sentence s = (Sentence)o.getObject();
s.undo();
}
});
menu.add(undo);
menu.addSeparator();
menu.add(rec);
menu.addSeparator();
menu.add(moveUp);
@@ -910,6 +937,7 @@ public class AudiobookRecorder extends JFrame {
menu.add(moveMenu);
menu.addSeparator();
menu.add(edit);
menu.add(external);
menu.add(ins);
menu.add(del);
menu.addSeparator();
@@ -923,6 +951,17 @@ public class AudiobookRecorder extends JFrame {
JPopupMenu menu = new JPopupMenu();
JMenuObject undo = new JMenuObject("Undo all", c, new ActionListener() {
public void actionPerformed(ActionEvent e) {
JMenuObject o = (JMenuObject)e.getSource();
Chapter c = (Chapter)o.getObject();
for (Enumeration s = c.children(); s.hasMoreElements();) {
Sentence snt = (Sentence)s.nextElement();
snt.undo();
}
}
});
JMenuObject peak = new JMenuObject("Auto-trim all (Peak)", c, new ActionListener() {
public void actionPerformed(ActionEvent e) {
JMenuObject o = (JMenuObject)e.getSource();
@@ -1094,6 +1133,27 @@ public class AudiobookRecorder extends JFrame {
}
});
JMenu external = new JMenu("Run external processor");
for (int i = 0; i < 999; i++) {
String name = Options.get("editor.processor." + i + ".name");
if (name == null) break;
if (name.equals("")) break;
JMenuObject ob = new JMenuObject(name, c, new ActionListener() {
public void actionPerformed(ActionEvent e) {
JMenuObject o = (JMenuObject)e.getSource();
Chapter c = (Chapter)o.getObject();
for (Enumeration s = c.children(); s.hasMoreElements();) {
Sentence snt = (Sentence)s.nextElement();
snt.runExternalProcessor(Utils.s2i(o.getActionCommand()));
}
}
});
ob.setActionCommand(Integer.toString(i));
external.add(ob);
}
menu.add(convertAll);
menu.add(normalizeAll);
menu.addSeparator();
@@ -1111,6 +1171,8 @@ public class AudiobookRecorder extends JFrame {
menu.add(exportChapter);
menu.addSeparator();
menu.add(deleteChapter);
menu.addSeparator();
menu.add(external);
menu.show(bookTree, e.getX(), e.getY());
} else if (node instanceof Book) {
@@ -1212,6 +1274,13 @@ public class AudiobookRecorder extends JFrame {
if (s.isLocked()) return;
try {
s.backup();
} catch (Exception e) {
e.printStackTrace();
return;
}
if (s.startRecording()) {
recording = (Sentence)selectedNode;
centralPanel.setFlash(true);
@@ -2445,4 +2514,10 @@ public class AudiobookRecorder extends JFrame {
}
}
}
public void updateWaveform() {
if (selectedSentence != null) {
sampleWaveform.setData(selectedSentence.getAudioData());
}
}
}

View File

@@ -46,6 +46,8 @@ public class Options extends JDialog {
JTextArea startupScript;
ArrayList<JTextField[]> processorList;
static HashMap<String, String> defaultPrefs;
static Preferences prefs = null;
@@ -84,6 +86,24 @@ public class Options extends JDialog {
return o;
}
void addTwoLabel(JPanel panel, String label1, String label2) {
JLabel l1 = new JLabel(label1);
constraint.gridx = 0;
constraint.gridwidth = 1;
constraint.gridheight = 1;
constraint.anchor = GridBagConstraints.LINE_START;
panel.add(l1, constraint);
JLabel l2 = new JLabel(label2);
constraint.gridx = 1;
constraint.gridwidth = 1;
constraint.gridheight = 1;
constraint.anchor = GridBagConstraints.LINE_START;
panel.add(l2, constraint);
constraint.gridy++;
}
JTextField addTextField(JPanel panel, String label, String def) {
JLabel l = new JLabel(label);
constraint.gridx = 0;
@@ -103,6 +123,22 @@ public class Options extends JDialog {
constraint.gridy++;
return a;
}
JTextField[] addTwoField(JPanel panel, String def1, String def2) {
JTextField a = new JTextField(def1);
constraint.gridx = 0;
constraint.fill = GridBagConstraints.HORIZONTAL;
panel.add(a, constraint);
JTextField b = new JTextField(def2);
constraint.gridx = 1;
panel.add(b, constraint);
constraint.fill = GridBagConstraints.NONE;
constraint.gridy++;
return new JTextField[] { a, b };
}
JTextField addFilePath(JPanel panel, String label, String path, boolean dironly) {
@@ -324,6 +360,39 @@ public class Options extends JDialog {
tabs.add("Effects", effects);
JPanel processors = new JPanel();
processors.setLayout(new BorderLayout());
JPanel processorListPanel = new JPanel();
JScrollPane psp = new JScrollPane(processorListPanel);
processors.add(psp, BorderLayout.CENTER);
processorListPanel.setLayout(new GridBagLayout());
constraint.gridx = 0;
constraint.gridy = 0;
constraint.gridwidth = 1;
constraint.gridheight = 1;
addTwoLabel(processorListPanel, "Name", "Command");
processorList = new ArrayList<JTextField[]>();
for (int i = 0; i < 999; i++) {
String name = get("editor.processor." + i + ".name");
String command = get("editor.processor." + i + ".command");
if (name == null || command == null) break;
if (name.equals("") || command.equals("")) break;
JTextField[] f = addTwoField(processorListPanel, name, command);
processorList.add(f);
}
JTextField[] f = addTwoField(processorListPanel, "", "");
processorList.add(f);
tabs.add("Processors", processors);
@@ -656,6 +725,18 @@ public class Options extends JDialog {
set("scripts.startup", startupScript.getText());
int procNo = 0;
for (JTextField[] proc : processorList) {
String name = proc[0].getText();
String command = proc[1].getText();
if (name.equals("") || command.equals("")) {
continue;
}
set("editor.processor." + procNo + ".name", name);
set("editor.processor." + procNo + ".command", command);
procNo++;
}
savePreferences();
}

View File

@@ -44,6 +44,7 @@ public class ProgressDialog extends JDialog implements EncoderProgressListener {
pack();
setSize(new Dimension(300, 100));
setResizable(false);
// setVisible(true);
}

View File

@@ -1083,6 +1083,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
} catch (Exception e) {
}
clearCache();
AudiobookRecorder.window.updateWaveform();
}
}
@@ -1092,4 +1093,129 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
t.start();
}
public void backup() throws IOException {
File whereto = getFile().getParentFile();
String name = getFile().getName();
int backupNumber = -1;
for (int i = 1; i < 999999; i++) {
String fn = String.format("backup-%08d-%s", i, name);
File testFile = new File(whereto, fn);
if (!testFile.exists()) {
backupNumber = i;
break;
}
}
if (backupNumber == -1) {
System.err.println("Out of backup space!");
return;
}
String fn = String.format("backup-%08d-%s", backupNumber, getFile().getName());
File bak = new File(getFile().getParentFile(), fn);
Files.copy(getFile().toPath(), bak.toPath());
}
class ExternalProcessor implements Runnable {
Sentence sentence;
int number;
public ExternalProcessor(Sentence s, int num) {
sentence = s;
number = num;
}
public void run() {
String command = Options.get("editor.processor." + number + ".command");
if (command == null) return;
if (command.equals("")) return;
String[] parts = command.split("::");
ArrayList<String> args = new ArrayList<String>();
try {
backup();
} catch (Exception e) {
e.printStackTrace();
return;
}
File out = new File(getFile().getParentFile(), "proc-" + getFile().getName());
for (String part : parts) {
if (part.equals("%f")) {
args.add(getFile().getAbsolutePath());
} else if (part.equals("%o")) {
args.add(out.getAbsolutePath());
} else {
args.add(part);
}
}
try {
ProcessBuilder process = new ProcessBuilder(args);
Process proc = process.start();
proc.waitFor();
} catch (Exception e) {
}
if (out.exists()) {
try {
File in = getFile();
in.delete();
Files.copy(out.toPath(), in.toPath());
out.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
clearCache();
AudiobookRecorder.window.updateWaveform();
}
}
public void runExternalProcessor(int num) {
ExternalProcessor ed = new ExternalProcessor(this, num);
Thread t = new Thread(ed);
t.start();
}
public void undo() {
File whereto = getFile().getParentFile();
String name = getFile().getName();
int backupNumber = -1;
for (int i = 1; i < 999999; i++) {
String fn = String.format("backup-%08d-%s", i, name);
File testFile = new File(whereto, fn);
if (testFile.exists()) {
backupNumber = i;
} else {
break;
}
}
if (backupNumber == -1) {
return;
}
String fn = String.format("backup-%08d-%s", backupNumber, getFile().getName());
File bak = new File(getFile().getParentFile(), fn);
try {
File in = getFile();
in.delete();
Files.copy(bak.toPath(), in.toPath());
bak.delete();
} catch (Exception e) {
e.printStackTrace();
}
clearCache();
AudiobookRecorder.window.updateWaveform();
}
}