use of com.xenoage.zong.documents.ScoreDoc in project Zong by Xenoage.
the class Content method saveAs.
/**
* Saves the current score using the given format.
* pdf, png, mid and ogg is supported by this demo.
*/
public void saveAs(String format) {
FileOutput<ScoreDoc> out = null;
switch(format) {
case "pdf":
out = new PdfScoreDocFileOutput();
break;
case "png":
out = new PngScoreDocFileOutput();
break;
case "mid":
out = new MidiScoreDocFileOutput();
break;
case "ogg":
out = new OggScoreDocFileOutput();
break;
default:
return;
}
String filePath = "demo." + format;
try {
DocumentIO.write(scoreDoc, new File(filePath), out);
mainWindow.showMessageDialog(filePath + " saved.");
} catch (Exception ex) {
Err.handle(Report.Companion.error(ex));
}
}
use of com.xenoage.zong.documents.ScoreDoc in project Zong by Xenoage.
the class VisualHtmlReport method renderScore.
/**
* Renders the given score into an image file.
*/
private void renderScore(File scoreFile) throws IOException {
ScoreDoc doc = DocumentIO.read(scoreFile, new MusicXmlScoreDocFileInput());
File pngFile = new File(new File(dirReport, appDir), scoreFile.getName() + ".png");
DocumentIO.write(doc, pngFile, new PngScoreDocFileOutput());
cropScoreImage(pngFile);
}
use of com.xenoage.zong.documents.ScoreDoc in project Zong by Xenoage.
the class MusicXmlScoreDocFileReader method read.
public Promise<ScoreDoc> read() {
return new Promise<>(ret -> {
MusicXmlFileReader reader = new MusicXmlFileReader(stream, filePath, new AllFilter<>());
reader.produce(new AsyncResult<List<Score>>() {
@Override
public void onSuccess(List<Score> scores) {
if (scores.size() == 0) {
// no score was opened
ret.resolve(null);
} else {
// open first selected score
Score score = scores.get(0);
ScoreDoc scoreDoc;
try {
scoreDoc = new ScoreDocFactory().read(score);
// add credit elements - TIDY
Object o = score.getMetaData().get("mxldoc");
if (o != null && o instanceof MxlScorePartwise) {
MxlScorePartwise doc = (MxlScorePartwise) o;
CreditsReader.read(doc, scoreDoc.getLayout(), score.getFormat());
}
ret.resolve(scoreDoc);
} catch (Exception ex) {
ret.reject(ex);
}
}
}
@Override
public void onFailure(Exception ex) {
ret.reject(ex);
}
});
});
}
use of com.xenoage.zong.documents.ScoreDoc in project Zong by Xenoage.
the class OpenAction method perform.
@Override
public void perform(Request request, Webserver server, HttpServletResponse response) throws SQLException, IOException {
OpenRequest openRequest = getAs(OpenRequest.class, request);
INSTANCE.log(Companion.remark("OpenAction started for URL " + openRequest.url));
final Connection db = server.getDBConnection();
// cleanup: delete documents which were not used during a defined period
PreparedStatement stmtDelete = stmt(db, "SELECT id FROM docs WHERE last_access < ?", unixTime() - Integer.parseInt(server.getSetting("cachetime")));
ResultSet resDelete = stmtDelete.executeQuery();
while (resDelete.next()) {
// TODO: automatically cascade
int deleteID = resDelete.getInt(1);
delete(db, "audio", "doc_id = ?", deleteID);
delete(db, "cursors", "doc_id = ?", deleteID);
delete(db, "pages", "doc_id = ?", deleteID);
delete(db, "pageinfos", "doc_id = ?", deleteID);
delete(db, "scaledpageinfos", "doc_id = ?", deleteID);
delete(db, "docs", "id = ?", deleteID);
}
stmtDelete.close();
// test
// long startTime = System.currentTimeMillis();
// see if document is already in database. if not, load it from the URL.
ScoreDoc scoreDoc = null;
Doc doc = null;
PreparedStatement stmtDoc = stmt(db, "SELECT id FROM docs WHERE url = ?", openRequest.url);
ResultSet resDoc = stmtDoc.executeQuery();
if (resDoc.next()) {
// the document already exists in the database
INSTANCE.log(Companion.remark("Requested document is still in cache. Using it."));
doc = Doc.fromDB(db, openRequest.url);
} else {
// the document is unknown. load it.
INSTANCE.log(Companion.remark("Requested document is not in cache. Loading it."));
Tuple2<ScoreDoc, Doc> t = loadDocument(openRequest.url, openRequest.requestedID);
scoreDoc = t.get1();
doc = t.get2();
}
stmtDoc.close();
// load size of first page
PreparedStatement stmtFirstPageSize = stmt(db, "SELECT width, height FROM pageinfos WHERE doc_id = ? AND page = 0", doc.id);
ResultSet resFirstPageSize = stmtFirstPageSize.executeQuery();
resFirstPageSize.next();
Size2f firstPageSize = new Size2f(resFirstPageSize.getFloat(1), resFirstPageSize.getFloat(2));
stmtFirstPageSize.close();
// scalings are saved as value*72dpi/10000 in the database.
// convert all requested scalings to this format
final LinkedList<Integer> requestedScalings = llist();
for (Scaling scaling : openRequest.scalings) {
requestedScalings.add(scaling.convertTo10000(firstPageSize));
}
// find all requested scalings, that are not already available in the database
final LinkedList<Integer> scalingsToRender = llist();
for (int scaling : requestedScalings) {
boolean scalingExists = (ScaledPage.fromDB(db, doc.id, 0, scaling) != null);
if (!scalingExists) {
scalingsToRender.add(scaling);
// if ScoreDoc was not loaded yet, load it now
if (scoreDoc == null)
scoreDoc = loadDocument(openRequest.url, openRequest.requestedID).get1();
// save information about scaled pages
List<com.xenoage.zong.layout.Page> pages = scoreDoc.getLayout().getPages();
for (int iPage : range(pages)) {
com.xenoage.zong.layout.Page page = pages.get(iPage);
Size2f pageSize = page.getFormat().getSize();
ScaledPage scaledPage = new ScaledPage(doc.id, iPage, scaling, Units.mmToPxInt(pageSize.width, scaling / 10000f), Units.mmToPxInt(pageSize.height, scaling / 10000f));
scaledPage.insertIntoDB(db);
}
}
}
// decide, if we have something to do
final boolean renderPages = (scalingsToRender.size() > 0);
final boolean renderAudio = !exists(db, "audio", "doc_id = ?", doc.id);
final boolean renderCursor = !exists(db, "cursors", "doc_id = ?", doc.id);
if (renderPages || renderAudio || renderCursor) {
// if ScoreDoc was not loaded yet, load it now
if (scoreDoc == null)
scoreDoc = loadDocument(openRequest.url, openRequest.requestedID).get1();
}
// from here on, try to do things in parallel
// first thread: render pages and save them in the database
final ScoreDoc scoreDocFinal = scoreDoc;
final Doc docFinal = doc;
final ArrayList<Page> pages = alist();
final ArrayList<ArrayList<ScaledPage>> scaledPages = alist();
Thread threadPages = new WorkerThread() {
@Override
public void runTry() throws Exception {
if (renderPages) {
// render the pages
for (int scaling : scalingsToRender) {
List<BufferedImage> pages = renderTiles(scoreDocFinal.getLayout(), scaling / 10000f);
for (int iPage : range(pages)) {
BufferedImage page = pages.get(iPage);
// write page
ByteArrayOutputStream imageData = new ByteArrayOutputStream();
ImageIO.write(page, "png", imageData);
insert(db, "pages", "doc_id, page, scaling, image", docFinal.id, iPage, scaling, imageData.toByteArray());
}
}
INSTANCE.log(Companion.remark("Rendered " + scoreDocFinal.getLayout().getPages().size() + " pages at " + scalingsToRender + " scalings"));
}
// collect pages for response
for (int iPage : range(docFinal.pages)) {
pages.add(Page.fromDB(db, docFinal.id, iPage));
}
// collect scaled pages for response
for (int iPage : range(docFinal.pages)) {
ArrayList<ScaledPage> sp = alist();
for (int scaling : requestedScalings) {
sp.add(ScaledPage.fromDB(db, docFinal.id, iPage, scaling));
}
scaledPages.add(sp);
}
// TEST
System.out.println("pages finished");
}
};
threadPages.start();
// second and third thread: render audio files, if not already in the cache
Thread threadOgg = new WorkerThread() {
@Override
public void runTry() throws Exception {
// render OGG file
if (renderAudio)
renderAndSaveAudioFile(db, docFinal, scoreDocFinal, "OGG", new OggScoreFileOutput());
// TEST
System.out.println("ogg finished");
}
};
threadOgg.start();
Thread threadMp3 = new WorkerThread() {
@Override
public void runTry() throws Exception {
// render MP3 file
if (renderAudio)
renderAndSaveAudioFile(db, docFinal, scoreDocFinal, "MP3", new Mp3ScoreFileOutput());
// TEST
System.out.println("mp3 finished");
}
};
threadMp3.start();
// fourth thread: render cursor file, if not already in the cache
Thread threadCursor = new WorkerThread() {
@Override
public void runTry() throws Exception {
// create cursor data
if (renderCursor) {
INSTANCE.log(Companion.remark("Creating cursor data"));
JsonObject jsonCursor = new CursorOutput().write(scoreDocFinal);
insert(db, "cursors", "doc_id, cursors", docFinal.id, jsonCursor.toString());
// TEST
System.out.println("cursors finished");
}
}
};
threadCursor.start();
// wait until all threads are finished
try {
threadPages.join();
threadOgg.join();
threadMp3.join();
threadCursor.join();
} catch (InterruptedException e) {
throw new RuntimeException("interrupted");
}
// create response message
JsonObject jsonResponse = new JsonObject();
jsonResponse.addProperty("id", "" + doc.publicID);
JsonArray jsonPages = new JsonArray();
for (int iPage : range(pages)) {
Page page = pages.get(iPage);
JsonObject jsonPage = new JsonObject();
jsonPage.addProperty("width", page.width);
jsonPage.addProperty("height", page.height);
JsonArray jsonScalesPages = new JsonArray();
for (ScaledPage sp : scaledPages.get(iPage)) jsonScalesPages.add(Webserver.instance.getGson().toJsonTree(sp));
jsonPage.add("scaledPages", jsonScalesPages);
jsonPages.add(jsonPage);
}
jsonResponse.add("pages", jsonPages);
// test
// long endTime = System.currentTimeMillis();
// System.out.println("total time: " + (endTime - startTime));
// send success response
writeSuccess(response, jsonResponse);
}
use of com.xenoage.zong.documents.ScoreDoc in project Zong by Xenoage.
the class WebApp method openFile.
private void openFile(String filePath) {
// show loading, hide score
if (loadingPanel != null)
loadingPanel.getStyle().setDisplay(BLOCK);
canvas.setVisible(false);
// load score
platformUtils().openFileAsync(filePath).thenAsync(scoreStream -> new MusicXmlScoreDocFileReader(scoreStream, null).read()).thenAsync(scoreDoc -> {
WebApp.this.scoreDoc = scoreDoc;
return platformUtils().openFileAsync("data/layout/default.xml");
}).thenDo(testXmlStream -> {
LayoutSettings layoutSettings;
try {
layoutSettings = LayoutSettingsReader.read(testXmlStream);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
Size2f areaSize = new Size2f(150, 10000);
SymbolPool symbolPool = zongPlatformUtils().getSymbolPool();
Context context = new Context(scoreDoc.getScore(), symbolPool, layoutSettings);
Target target = Target.completeLayoutTarget(new ScoreLayoutArea(areaSize));
paintLayout();
// show score, hide loading
canvas.setVisible(true);
if (loadingPanel != null)
loadingPanel.getStyle().setDisplay(NONE);
}).onError(ex -> consoleLog("Error: " + ex.toString()));
}
Aggregations