kopia lustrzana https://github.com/jameshball/osci-render
Save and load rendered files to osci project file
rodzic
8b41aeb73a
commit
f087635e31
|
@ -13,15 +13,13 @@ import javafx.stage.DirectoryChooser;
|
||||||
import javafx.util.Duration;
|
import javafx.util.Duration;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
import org.w3c.dom.Node;
|
|
||||||
import org.w3c.dom.NodeList;
|
import org.w3c.dom.NodeList;
|
||||||
import sh.ball.audio.*;
|
import sh.ball.audio.*;
|
||||||
import sh.ball.audio.effect.*;
|
import sh.ball.audio.effect.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
@ -89,6 +87,7 @@ public class Controller implements Initializable, FrequencyListener, MidiListene
|
||||||
// frames
|
// frames
|
||||||
private static final InputStream DEFAULT_OBJ = Controller.class.getResourceAsStream("/models/cube.obj");
|
private static final InputStream DEFAULT_OBJ = Controller.class.getResourceAsStream("/models/cube.obj");
|
||||||
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
|
private final List<byte[]> openFiles = new ArrayList<>();
|
||||||
private final List<String> frameSourcePaths = new ArrayList<>();
|
private final List<String> frameSourcePaths = new ArrayList<>();
|
||||||
private List<FrameSource<List<Shape>>> frameSources = new ArrayList<>();
|
private List<FrameSource<List<Shape>>> frameSources = new ArrayList<>();
|
||||||
private FrameProducer<List<Shape>> producer;
|
private FrameProducer<List<Shape>> producer;
|
||||||
|
@ -216,7 +215,15 @@ public class Controller implements Initializable, FrequencyListener, MidiListene
|
||||||
new Thread(midiCommunicator).start();
|
new Thread(midiCommunicator).start();
|
||||||
|
|
||||||
this.audioPlayer = new ShapeAudioPlayer(ConglomerateAudioEngine::new, midiCommunicator);
|
this.audioPlayer = new ShapeAudioPlayer(ConglomerateAudioEngine::new, midiCommunicator);
|
||||||
FrameSource<List<Shape>> frames = new ObjParser(DEFAULT_OBJ).parse();
|
|
||||||
|
// Clone DEFAULT_OBJ InputStream using a ByteArrayOutputStream
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
assert DEFAULT_OBJ != null;
|
||||||
|
DEFAULT_OBJ.transferTo(baos);
|
||||||
|
InputStream objClone = new ByteArrayInputStream(baos.toByteArray());
|
||||||
|
FrameSource<List<Shape>> frames = new ObjParser(objClone).parse();
|
||||||
|
|
||||||
|
openFiles.add(baos.toByteArray());
|
||||||
frameSources.add(frames);
|
frameSources.add(frames);
|
||||||
frameSourcePaths.add("cube.obj");
|
frameSourcePaths.add("cube.obj");
|
||||||
currentFrameSource = 0;
|
currentFrameSource = 0;
|
||||||
|
@ -620,32 +627,43 @@ public class Controller implements Initializable, FrequencyListener, MidiListene
|
||||||
objTitledPane.setDisable(!ObjParser.isObjFile(frameSourcePaths.get(index)));
|
objTitledPane.setDisable(!ObjParser.isObjFile(frameSourcePaths.get(index)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// selects a new file or folder for files to be rendered from
|
private void updateFiles(List<byte[]> files, List<String> names) throws IOException, ParserConfigurationException, SAXException {
|
||||||
private void chooseFile(File chosenFile) {
|
|
||||||
try {
|
|
||||||
if (chosenFile.exists()) {
|
|
||||||
List<FrameSource<List<Shape>>> oldFrameSources = frameSources;
|
List<FrameSource<List<Shape>>> oldFrameSources = frameSources;
|
||||||
frameSources = new ArrayList<>();
|
frameSources = new ArrayList<>();
|
||||||
frameSourcePaths.clear();
|
frameSourcePaths.clear();
|
||||||
|
openFiles.clear();
|
||||||
|
jkLabel.setVisible(files.size() > 1);
|
||||||
|
|
||||||
if (chosenFile.isDirectory()) {
|
for (int i = 0; i < files.size(); i++) {
|
||||||
jkLabel.setVisible(true);
|
|
||||||
for (File file : Objects.requireNonNull(chosenFile.listFiles())) {
|
|
||||||
try {
|
try {
|
||||||
frameSources.add(ParserFactory.getParser(file.getAbsolutePath()).parse());
|
frameSources.add(ParserFactory.getParser(names.get(i), files.get(i)).parse());
|
||||||
frameSourcePaths.add(file.getName());
|
frameSourcePaths.add(names.get(i));
|
||||||
} catch (IOException ignored) {
|
openFiles.add(files.get(i));
|
||||||
}
|
} catch (IOException ignored) {}
|
||||||
}
|
|
||||||
} else {
|
|
||||||
jkLabel.setVisible(false);
|
|
||||||
frameSources.add(ParserFactory.getParser(chosenFile.getAbsolutePath()).parse());
|
|
||||||
frameSourcePaths.add(chosenFile.getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
oldFrameSources.forEach(FrameSource::disable);
|
oldFrameSources.forEach(FrameSource::disable);
|
||||||
changeFrameSource(0);
|
changeFrameSource(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// selects a new file or folder for files to be rendered from
|
||||||
|
private void chooseFile(File chosenFile) {
|
||||||
|
try {
|
||||||
|
if (chosenFile.exists()) {
|
||||||
|
List<byte[]> files = new ArrayList<>();
|
||||||
|
List<String> names = new ArrayList<>();
|
||||||
|
if (chosenFile.isDirectory()) {
|
||||||
|
for (File file : Objects.requireNonNull(chosenFile.listFiles())) {
|
||||||
|
files.add(Files.readAllBytes(file.toPath()));
|
||||||
|
names.add(file.getName());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
files.add(Files.readAllBytes(chosenFile.toPath()));
|
||||||
|
names.add(chosenFile.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
updateFiles(files, names);
|
||||||
|
}
|
||||||
} catch (IOException | ParserConfigurationException | SAXException ioException) {
|
} catch (IOException | ParserConfigurationException | SAXException ioException) {
|
||||||
ioException.printStackTrace();
|
ioException.printStackTrace();
|
||||||
|
|
||||||
|
@ -668,6 +686,9 @@ public class Controller implements Initializable, FrequencyListener, MidiListene
|
||||||
|
|
||||||
// increments and changes the frameSource after pressing 'j'
|
// increments and changes the frameSource after pressing 'j'
|
||||||
public void nextFrameSet() {
|
public void nextFrameSet() {
|
||||||
|
if (frameSources.size() == 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
int index = currentFrameSource + 1;
|
int index = currentFrameSource + 1;
|
||||||
if (index >= frameSources.size()) {
|
if (index >= frameSources.size()) {
|
||||||
index = 0;
|
index = 0;
|
||||||
|
@ -677,6 +698,9 @@ public class Controller implements Initializable, FrequencyListener, MidiListene
|
||||||
|
|
||||||
// decrements and changes the frameSource after pressing 'k'
|
// decrements and changes the frameSource after pressing 'k'
|
||||||
public void previousFrameSource() {
|
public void previousFrameSource() {
|
||||||
|
if (frameSources.size() == 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
int index = currentFrameSource - 1;
|
int index = currentFrameSource - 1;
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
index = frameSources.size() - 1;
|
index = frameSources.size() - 1;
|
||||||
|
@ -880,6 +904,20 @@ public class Controller implements Initializable, FrequencyListener, MidiListene
|
||||||
objectRotationElement.appendChild(objectRotationZElement);
|
objectRotationElement.appendChild(objectRotationZElement);
|
||||||
root.appendChild(objectRotationElement);
|
root.appendChild(objectRotationElement);
|
||||||
|
|
||||||
|
Element filesElement = document.createElement("files");
|
||||||
|
for (int i = 0; i < openFiles.size(); i++) {
|
||||||
|
Element fileElement = document.createElement("file");
|
||||||
|
Element fileNameElement = document.createElement("name");
|
||||||
|
fileNameElement.appendChild(document.createTextNode(frameSourcePaths.get(i)));
|
||||||
|
Element dataElement = document.createElement("data");
|
||||||
|
String encodedData = Base64.getEncoder().encodeToString(openFiles.get(i));
|
||||||
|
dataElement.appendChild(document.createTextNode(encodedData));
|
||||||
|
fileElement.appendChild(fileNameElement);
|
||||||
|
fileElement.appendChild(dataElement);
|
||||||
|
filesElement.appendChild(fileElement);
|
||||||
|
}
|
||||||
|
root.appendChild(filesElement);
|
||||||
|
|
||||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||||
Transformer transformer = transformerFactory.newTransformer();
|
Transformer transformer = transformerFactory.newTransformer();
|
||||||
DOMSource domSource = new DOMSource(document);
|
DOMSource domSource = new DOMSource(document);
|
||||||
|
@ -958,6 +996,19 @@ public class Controller implements Initializable, FrequencyListener, MidiListene
|
||||||
Double.parseDouble(objectRotationZElement.getTextContent())
|
Double.parseDouble(objectRotationZElement.getTextContent())
|
||||||
);
|
);
|
||||||
setObjRotate(rotation);
|
setObjRotate(rotation);
|
||||||
|
|
||||||
|
Element filesElement = (Element) root.getElementsByTagName("files").item(0);
|
||||||
|
List<byte[]> files = new ArrayList<>();
|
||||||
|
List<String> fileNames = new ArrayList<>();
|
||||||
|
NodeList fileElements = filesElement.getElementsByTagName("file");
|
||||||
|
for (int i = 0; i < fileElements.getLength(); i++) {
|
||||||
|
Element fileElement = (Element) fileElements.item(i);
|
||||||
|
String fileData = fileElement.getElementsByTagName("data").item(0).getTextContent();
|
||||||
|
files.add(Base64.getDecoder().decode(fileData));
|
||||||
|
String fileName = fileElement.getElementsByTagName("name").item(0).getTextContent();
|
||||||
|
fileNames.add(fileName);
|
||||||
|
}
|
||||||
|
updateFiles(files, fileNames);
|
||||||
} catch (ParserConfigurationException | SAXException | IOException e) {
|
} catch (ParserConfigurationException | SAXException | IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,26 @@
|
||||||
package sh.ball.parser;
|
package sh.ball.parser;
|
||||||
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
import sh.ball.audio.FrameSource;
|
import sh.ball.audio.FrameSource;
|
||||||
import sh.ball.parser.obj.ObjParser;
|
import sh.ball.parser.obj.ObjParser;
|
||||||
import sh.ball.parser.svg.SvgParser;
|
import sh.ball.parser.svg.SvgParser;
|
||||||
import sh.ball.parser.txt.TextParser;
|
import sh.ball.parser.txt.TextParser;
|
||||||
import sh.ball.shapes.Shape;
|
import sh.ball.shapes.Shape;
|
||||||
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ParserFactory {
|
public class ParserFactory {
|
||||||
|
|
||||||
public static FileParser<FrameSource<List<Shape>>> getParser(String filePath) throws IOException, ParserConfigurationException, SAXException {
|
public static FileParser<FrameSource<List<Shape>>> getParser(String filePath, byte[] fileData) throws IOException {
|
||||||
|
ByteArrayInputStream bais = new ByteArrayInputStream(fileData);
|
||||||
if (ObjParser.isObjFile(filePath)) {
|
if (ObjParser.isObjFile(filePath)) {
|
||||||
return new ObjParser(filePath);
|
return new ObjParser(bais);
|
||||||
} else if (SvgParser.isSvgFile(filePath)) {
|
} else if (SvgParser.isSvgFile(filePath)) {
|
||||||
return new SvgParser(filePath);
|
return new SvgParser(bais);
|
||||||
} else if (TextParser.isTxtFile(filePath)) {
|
} else if (TextParser.isTxtFile(filePath)) {
|
||||||
return new TextParser(filePath);
|
return new TextParser(bais);
|
||||||
}
|
}
|
||||||
throw new IOException("No known parser that can parse " + new File(filePath).getName());
|
throw new IOException("No known parser that can parse " + new File(filePath).getName());
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,10 @@ public class TextParser extends FileParser<FrameSource<List<Shape>>> {
|
||||||
this.charToShape = new HashMap<>();
|
this.charToShape = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TextParser(InputStream input) {
|
||||||
|
this(input, TextParser.class.getResourceAsStream(DEFAULT_FONT));
|
||||||
|
}
|
||||||
|
|
||||||
public TextParser(String path) throws FileNotFoundException {
|
public TextParser(String path) throws FileNotFoundException {
|
||||||
this(new FileInputStream(path), TextParser.class.getResourceAsStream(DEFAULT_FONT));
|
this(new FileInputStream(path), TextParser.class.getResourceAsStream(DEFAULT_FONT));
|
||||||
checkFileExtension(path);
|
checkFileExtension(path);
|
||||||
|
|
Ładowanie…
Reference in New Issue