use of com.xenoage.zong.desktop.io.midi.out.JseMidiSequenceWriter in project Zong by Xenoage.
the class WavScoreFileOutput method writeWav.
/**
* Writes the given score as a WAV file into the given stream.
*/
public static void writeWav(Score score, OutputStream stream) throws IOException {
// save WAVE file
try {
// create midi sequence
MidiSequence<Sequence> sequence = MidiConverter.convertToSequence(score, optionsForFileExport, new JseMidiSequenceWriter());
// for all instruments
MidiToWaveRenderer.render(SynthManager.getSoundbank(), sequence.getSequence(), null, new JseOutputStream(stream));
} catch (Exception ex) {
INSTANCE.log(Companion.warning(ex));
throw new IOException(ex);
}
}
use of com.xenoage.zong.desktop.io.midi.out.JseMidiSequenceWriter in project Zong by Xenoage.
the class FileToMidiConvert method execute.
@Override
public void execute() {
FileChooser fileChooser = new FileChooser();
// use last document directory
File initDir = FileSettings.getLastDir();
if (initDir != null)
fileChooser.setInitialDirectory(initDir);
// add filters
SupportedFormats<?> supportedFormats = app().getSupportedFormats();
for (FileFormat<?> fileFormat : supportedFormats.getReadFormats()) {
addFilter(fileChooser, fileFormat);
}
// show the dialog
File file = fileChooser.showOpenDialog(ownerWindow);
if (file != null) {
INSTANCE.log(Companion.remark("Dialog closed (OK), converting file \"" + file.getName() + "\""));
// save document directory
FileSettings.rememberDir(file);
// convert - TODO: show progress
String lastPath = file.getAbsolutePath();
List<Score> scores = pApp().loadMxlScores(lastPath, new AllFilter<>());
boolean useNumber = scores.size() > 1;
It<Score> scoresIt = new It<>(scores);
for (Score score : scoresIt) {
Sequence seq = MidiConverter.convertToSequence(score, optionsForFileExport, new JseMidiSequenceWriter()).getSequence();
String newPath = lastPath;
String number = (useNumber ? ("-" + (scoresIt.getIndex() + 1)) : "");
if (// TIDY: share code: FilenameUtils.numberFiles
newPath.toLowerCase().endsWith(".xml") || newPath.toLowerCase().endsWith(".mxl")) {
newPath = newPath.substring(0, newPath.length() - 4);
}
newPath += (number + ".mid");
try {
MidiSystem.write(seq, 1, new File(newPath));
} catch (Exception ex) {
handle(warning(Voc.ErrorSavingFile));
}
}
} else {
INSTANCE.log(Companion.remark("Dialog closed (Cancel)"));
}
}
use of com.xenoage.zong.desktop.io.midi.out.JseMidiSequenceWriter in project Zong by Xenoage.
the class DirToMidiConvert method execute.
@Override
public void execute() {
DirectoryChooser dc = new DirectoryChooser();
// use last document directory
File initDir = FileSettings.getLastDir();
if (initDir != null)
dc.setInitialDirectory(initDir);
/* TODO - how to extend the DirectoryChooser in JavaFX?
//see http://stackoverflow.com/questions/25982945/javafx-filechooser-and-directorychooser-accessory-component
JCheckBox chkSubdir = new JCheckBox(Lang.get(Voc.IncludeSubdirectories), true);
JCheckBox chkCancel = new JCheckBox(Lang.get(Voc.CancelAtFirstError), false);
JPanel pnlOptions = new JPanel();
pnlOptions.setLayout(new BoxLayout(pnlOptions, BoxLayout.Y_AXIS));
pnlOptions.add(chkSubdir);
pnlOptions.add(chkCancel);
fc.setAccessory(pnlOptions); */
// TODO
boolean subDirs = true;
// TODO
boolean cancelOnFirstError = false;
File dir = dc.showDialog(ownerWindow);
if (dir != null) {
// remember directory
FileSettings.rememberDir(dir);
// start conversion - TODO: show progress
List<File> files = listFiles(dir, subDirs);
int countOK = 0;
int countFailed = 0;
for (File file : files) {
try {
// only process MusicXML files
FileType fileType = FileTypeReader.getFileType(new JseInputStream(file));
if (fileType != null) {
String filePath = file.getAbsolutePath();
List<Score> scores = pApp().loadMxlScores(filePath, new AllFilter<>());
if ((scores.size() == 0)) /* TODO && chkCancel.isSelected() */
{
countFailed++;
break;
}
boolean useNumber = scores.size() > 1;
It<Score> scoresIt = new It<>(scores);
for (Score score : scoresIt) {
Sequence seq = MidiConverter.convertToSequence(score, optionsForFileExport, new JseMidiSequenceWriter()).getSequence();
String number = (useNumber ? ("-" + (scoresIt.getIndex() + 1)) : "");
String newPath = filePath;
if (filePath.toLowerCase().endsWith(".xml") || filePath.toLowerCase().endsWith(".mxl")) {
newPath = newPath.substring(0, filePath.length() - 4);
}
newPath += (number + ".mid");
MidiSystem.write(seq, 1, new File(newPath));
countOK++;
}
}
} catch (IOException ex) {
countFailed++;
if (cancelOnFirstError) {
break;
}
}
}
app().showMessageDialog(Lang.get(Voc.DirectoryConversionResult, "" + countOK, "" + countFailed));
}
}
use of com.xenoage.zong.desktop.io.midi.out.JseMidiSequenceWriter in project Zong by Xenoage.
the class MidiConverterTest method testSampleFiles.
@Test
public void testSampleFiles() {
boolean writeFiles = true;
for (String file : MusicXmlScoreFileInputTest.getSampleFiles()) {
try {
val score = new MusicXmlScoreFileInput().read(jsePlatformUtils().openFile(file), file);
val sequence = MidiConverter.convertToSequence(score, optionsForPlayback, new JseMidiSequenceWriter());
if (writeFiles) {
// try: write to file
new File("temp").mkdirs();
System.out.println("Converting: " + file);
MidiSystem.write(sequence.getSequence(), 1, new File("temp/" + file.replaceAll("/", "_") + ".mid"));
}
} catch (Exception ex) {
ex.printStackTrace();
fail("Failed to load file: " + file);
}
}
}
use of com.xenoage.zong.desktop.io.midi.out.JseMidiSequenceWriter in project Zong by Xenoage.
the class CursorOutput method write.
public JsonObject write(ScoreDoc doc) {
JsonObject ret = new JsonObject();
// create midi sequence and mp mappings
Score score = doc.getScore();
val seq = MidiConverter.convertToSequence(score, optionsForFileExport, new JseMidiSequenceWriter());
// save time map
JsonArray jsonMPs = new JsonArray();
val timeMap = seq.getTimeMap();
for (int iRep : range(timeMap.getRepetitionsCount())) {
for (val time : timeMap.getTimesSorted(iRep)) {
val midiTime = timeMap.getByRepTime(iRep, time);
JsonObject jsonMP = new JsonObject();
jsonMP.addProperty("measure", time.measure);
jsonMP.addProperty("beat", "" + time.beat);
jsonMP.addProperty("ms", midiTime.ms);
jsonMPs.add(jsonMP);
}
}
ret.add("mps", jsonMPs);
// collect data
int measuresCount = score.getMeasuresCount();
ArrayList<System> systems = new ArrayList<>();
ArrayList<Measure> measures = new ArrayList<>();
for (int i = 0; i < measuresCount; i++) {
measures.add(new Measure());
}
int systemCount = 0;
Layout layout = doc.getLayout();
for (int iPage : range(layout.getPages())) {
Page page = layout.getPages().get(iPage);
Size2f pageSize = page.getFormat().getSize();
for (Frame frame : page.getFrames()) {
if (frame instanceof ScoreFrame) {
Point2f absPos = frame.getAbsolutePosition();
float offsetX = absPos.x - frame.getSize().width / 2;
float offsetY = absPos.y - frame.getSize().height / 2;
ScoreFrameLayout sfl = ((ScoreFrame) frame).getScoreFrameLayout();
for (SystemSpacing systemSpacing : sfl.getFrameSpacing().getSystems()) {
// read system data
int systemIndex = systemCount + systemSpacing.getSystemIndexInFrame();
while (systems.size() - 1 < systemIndex) systems.add(new System());
System system = systems.get(systemIndex);
system.page = iPage;
system.top = (offsetY + systemSpacing.offsetYMm) / pageSize.height;
system.bottom = (offsetY + systemSpacing.offsetYMm + systemSpacing.getHeightMm()) / pageSize.height;
// read measure beats
float systemOffsetX = systemSpacing.marginLeftMm;
for (int iMeasure : systemSpacing.getMeasures()) {
Measure measure = measures.get(iMeasure);
measure.system = systemIndex;
measure.left = (offsetX + systemOffsetX + systemSpacing.getMeasureStartMm(iMeasure)) / pageSize.width;
measure.right = (offsetX + systemOffsetX + systemSpacing.getMeasureEndMm(iMeasure)) / pageSize.width;
for (BeatOffset bo : systemSpacing.getColumn(iMeasure).getBeatOffsets()) {
measure.beats.put(bo.getBeat(), (offsetX + systemOffsetX + bo.getOffsetMm()) / pageSize.width);
}
}
}
systemCount += sfl.getFrameSpacing().getSystems().size();
}
}
}
// save systems
JsonArray jsonSystems = new JsonArray();
for (int i = 0; i < systems.size(); i++) {
System system = systems.get(i);
JsonObject jsonSystem = new JsonObject();
jsonSystem.addProperty("number", i);
jsonSystem.addProperty("page", system.page);
jsonSystem.addProperty("top", system.top);
jsonSystem.addProperty("bottom", system.bottom);
jsonSystems.add(jsonSystem);
}
ret.add("systems", jsonSystems);
// save measures
JsonArray jsonMeasures = new JsonArray();
for (int i = 0; i < measuresCount; i++) {
Measure measure = measures.get(i);
JsonObject jsonMeasure = new JsonObject();
jsonMeasure.addProperty("number", i);
jsonMeasure.addProperty("system", measure.system);
jsonMeasure.addProperty("left", measure.left);
jsonMeasure.addProperty("right", measure.right);
// beats
JsonArray jsonBeats = new JsonArray();
ArrayList<Fraction> sortedBeats = new ArrayList<>(measure.beats.keySet());
Collections.sort(sortedBeats);
for (Fraction beat : sortedBeats) {
JsonObject jsonBeat = new JsonObject();
jsonBeat.addProperty("at", "" + beat);
jsonBeat.addProperty("x", measure.beats.get(beat));
jsonBeats.add(jsonBeat);
}
jsonMeasure.add("beats", jsonBeats);
jsonMeasures.add(jsonMeasure);
}
ret.add("measures", jsonMeasures);
// save time cursors
JsonArray jsonTCs = new JsonArray();
for (int iRep : range(timeMap.getRepetitionsCount())) {
for (val time : timeMap.getTimesSorted(iRep)) {
val midiTime = timeMap.getByRepTime(iRep, time);
JsonObject jsonTC = new JsonObject();
jsonTC.addProperty("time", midiTime.ms);
Measure measure = measures.get(time.measure);
System system = systems.get(measure.system);
jsonTC.addProperty("page", system.page);
jsonTC.addProperty("top", system.top);
jsonTC.addProperty("left", measure.beats.get(time.beat));
jsonTC.addProperty("bottom", system.bottom);
jsonTCs.add(jsonTC);
}
}
ret.add("timecursors", jsonTCs);
return ret;
}
Aggregations