Reimplemented book merge

This commit is contained in:
2020-02-08 19:19:10 +00:00
parent fc3365af62
commit c9b65cb315
2 changed files with 73 additions and 98 deletions
@@ -1196,9 +1196,9 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
newbook.setComment(info.getComment().trim()); newbook.setComment(info.getComment().trim());
newbook.setACX(info.getACX().trim()); newbook.setACX(info.getACX().trim());
Chapter caud = new Chapter(UUID.randomUUID().toString(), "Audition"); Chapter caud = new Chapter("audition", "Audition");
Chapter copen = new Chapter(UUID.randomUUID().toString(), "Opening Credits"); Chapter copen = new Chapter("open", "Opening Credits");
Chapter cclose = new Chapter(UUID.randomUUID().toString(), "Closing Credits"); Chapter cclose = new Chapter("close", "Closing Credits");
Chapter cone = new Chapter(UUID.randomUUID().toString(), "Chapter 1"); Chapter cone = new Chapter(UUID.randomUUID().toString(), "Chapter 1");
newbook.add(caud); newbook.add(caud);
@@ -1674,13 +1674,9 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
JMenuObject2 ob = (JMenuObject2)e.getSource(); JMenuObject2 ob = (JMenuObject2)e.getSource();
Chapter source = (Chapter)ob.getObject1(); Chapter source = (Chapter)ob.getObject1();
Chapter target = (Chapter)ob.getObject2(); Chapter target = (Chapter)ob.getObject2();
moveSentences(source, target);
DefaultMutableTreeNode n = source.getFirstLeaf(); bookTreeModel.reload(source);
while (n instanceof Sentence) { bookTreeModel.reload(target);
bookTreeModel.removeNodeFromParent(n);
bookTreeModel.insertNodeInto(n, target, target.getChildCount());
n = source.getFirstLeaf();
}
} }
}); });
mergeWith.add(m); mergeWith.add(m);
@@ -2191,9 +2187,8 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
name = chapname + (chapnum + 1); name = chapname + (chapnum + 1);
} }
} }
Chapter c = getBook().addChapter(); Chapter c = getBook().addChapter(name);
c.setName(name); bookTreeModel.reload(c);
bookTreeModel.insertNodeInto(c, getBook(), getBook().getChildCount());
bookTree.scrollPathToVisible(new TreePath(c.getPath())); bookTree.scrollPathToVisible(new TreePath(c.getPath()));
} }
@@ -2854,89 +2849,45 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
} }
try { try {
Properties prefs = new Properties(); Book toBook = getBook();
FileInputStream fis = new FileInputStream(f); Book fromBook = new Book(f);
prefs.loadFromXML(fis); mergeAllChapters(fromBook, toBook);
bookTreeModel.reload(toBook);
// Merge the opening credits if they exist
if (prefs.getProperty("chapter.open.name") != null) {
mergeChapter(prefs, "open");
}
// Merge the audition if it exists
if (prefs.getProperty("chapter.audition.name") != null) {
mergeChapter(prefs, "audition");
}
for (int i = 0; i < 9999; i++) {
String chid = String.format("%04d", i);
if (prefs.getProperty("chapter." + chid + ".name") != null) {
mergeChapter(prefs, chid);
}
}
// Merge the opening credits if they exist
if (prefs.getProperty("chapter.open.name") != null) {
mergeChapter(prefs, "open");
}
// Merge the closing credits if they exist
if (prefs.getProperty("chapter.close.name") != null) {
mergeChapter(prefs, "close");
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
public void mergeChapter(Properties prefs, String chid) { public void moveSentences(Chapter from, Chapter to) {
Debug.trace(); while (from.getChildCount() > 0) {
Chapter c = getBook().addChapter(); DefaultMutableTreeNode n = (DefaultMutableTreeNode)from.getFirstChild();
c.setName("Merged-" + prefs.getProperty("chapter." + chid + ".name")); from.remove(n);
c.setPostGap(Utils.s2i(prefs.getProperty("chapter." + chid + ".post-gap"))); to.add(n);
c.setPreGap(Utils.s2i(prefs.getProperty("chapter." + chid + ".pre-gap")));
Chapter lc = getBook().getLastChapter();
int idx = bookTreeModel.getIndexOfChild(getBook(), lc);
bookTreeModel.insertNodeInto(c, getBook(), idx+1);
File srcBook = new File(Options.get("path.storage"), prefs.getProperty("book.name"));
File srcFolder = new File(srcBook, "files");
File dstBook = new File(Options.get("path.storage"), getBook().getName());
File dstFolder = new File(dstBook, "files");
for (int i = 0; i < 100000000; i++) {
String id = prefs.getProperty(String.format("chapter." + chid + ".sentence.%08d.id", i));
String text = prefs.getProperty(String.format("chapter." + chid + ".sentence.%08d.text", i));
int gap = Utils.s2i(prefs.getProperty(String.format("chapter." + chid + ".sentence.%08d.post-gap", i)));
if (id == null) break;
Sentence s = new Sentence(id, text);
s.setPostGap(gap);
s.setStartOffset(Utils.s2i(prefs.getProperty(String.format("chapter." + chid + ".sentence.%08d.start-offset", i))));
s.setEndOffset(Utils.s2i(prefs.getProperty(String.format("chapter." + chid + ".sentence.%08d.end-offset", i))));
s.setLocked(Utils.s2b(prefs.getProperty(String.format("chapter." + chid + ".sentence.%08d.locked", i))));
bookTreeModel.insertNodeInto(s, c, c.getChildCount());
File srcFile = new File(srcFolder, s.getId() + ".wav");
File dstFile = new File(dstFolder, s.getId() + ".wav");
if (srcFile.exists()) {
try {
Files.copy(srcFile.toPath(), dstFile.toPath());
} catch (Exception e) {
e.printStackTrace();
}
}
} }
}
public Chapter findChapter(Book from, String id, String name) {
Chapter c = from.getChapterById(id);
if (c != null) return c;
c = from.getChapterByName(name);
if (c != null) return c;
return null;
}
public void mergeAllChapters(Book from, Book to) {
if (from.getChildCount() == 0) return;
while (from.getChildCount() > 0) {
Chapter fc = (Chapter)from.getFirstChild();
Chapter tc = findChapter(to, fc.getId(), fc.getName());
if (tc != null) {
moveSentences(fc, tc);
from.remove(fc);
} else {
from.remove(fc);
to.add(fc);
}
}
} }
ArrayList<File> gatherFiles(File root) { ArrayList<File> gatherFiles(File root) {
+34 -10
View File
@@ -62,6 +62,8 @@ public class Book extends BookTreeNode {
location = new File(Options.get("path.storage"), sanitize(name)); location = new File(Options.get("path.storage"), sanitize(name));
AudiobookRecorder.window.setTitle("AudioBook Recorder :: " + name); // This should be in the load routine!!!! AudiobookRecorder.window.setTitle("AudioBook Recorder :: " + name); // This should be in the load routine!!!!
setIcon(Icons.book); setIcon(Icons.book);
roomNoise = new Sentence("room-noise", "Room Noise");
roomNoise.setParentBook(this);
} }
/* /*
public Book(Element root) { public Book(Element root) {
@@ -203,16 +205,6 @@ public class Book extends BookTreeNode {
public String getComment() { Debug.trace(); return comment; } public String getComment() { Debug.trace(); return comment; }
public String getACX() { Debug.trace(); if (ACX == null) return ""; return ACX; } public String getACX() { Debug.trace(); if (ACX == null) return ""; return ACX; }
public Chapter getClosingCredits() {
Debug.trace();
return getChapterById("close");
}
public Chapter getOpeningCredits() {
Debug.trace();
return getChapterById("open");
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Chapter getChapterById(String id) { public Chapter getChapterById(String id) {
Debug.trace(); Debug.trace();
@@ -228,6 +220,20 @@ public class Book extends BookTreeNode {
return null; return null;
} }
public Chapter getChapterByName(String name) {
Debug.trace();
for (Enumeration o = children(); o.hasMoreElements();) {
Object ob = (Object)o.nextElement();
if (ob instanceof Chapter) {
Chapter c = (Chapter)ob;
if (c.getName().equals(name)) {
return c;
}
}
}
return null;
}
public Chapter getLastChapter() { public Chapter getLastChapter() {
Debug.trace(); Debug.trace();
DefaultMutableTreeNode leaf = getLastLeaf(); DefaultMutableTreeNode leaf = getLastLeaf();
@@ -251,6 +257,24 @@ public class Book extends BookTreeNode {
Debug.trace(); Debug.trace();
String uuid = UUID.randomUUID().toString(); String uuid = UUID.randomUUID().toString();
Chapter c = new Chapter(uuid, uuid); Chapter c = new Chapter(uuid, uuid);
add(c);
c.setParentBook(this);
return c;
}
public Chapter addChapter(String name) {
Debug.trace();
String uuid = UUID.randomUUID().toString();
Chapter c = new Chapter(uuid, name);
add(c);
c.setParentBook(this);
return c;
}
public Chapter addChapter(String id, String name) {
Debug.trace();
Chapter c = new Chapter(id, name);
add(c);
c.setParentBook(this); c.setParentBook(this);
return c; return c;
} }