英文:
How can I save pattern changes to a loaded midi?
问题
我正在进行一个学校编程项目,涉及在Java中使用JFugue库。然而,我在将加载和编辑后的模式保存到文件方面遇到了很大困难。到目前为止,我已经尝试了player.saveMidi(未被识别)和MidiFileManager.savePatternToMidi函数,但编辑内容并未保存。
以下是上述代码的片段:
Pattern pattern = new Pattern(MidiFileManager.loadPatternFromMidi(new File(filePath.getText())));
TextField midiData = new TextField(pattern.toString());
midiData.setFont(Font.font(14));
pattern = new Pattern(midiData.getText());
Button save = new Button("Save Edits");
Pattern finalPattern = pattern;
save.setOnAction(e -> {
try {
MidiFileManager.savePatternToMidi(finalPattern, new File(filePath.getText()));
Load.playAndEdit(filePath);
} catch (Exception ex) {
ex.printStackTrace();
}
});
英文:
I am working on a school coding project, which involves using the JFugue library in Java. However, I am having significant difficulty saving loaded and edited patterns to a file. So far, I have tried both the player.saveMidi (which is not recognized) and MidiFileManager.savePatternToMidi functions, but the edits do not save.
Here is a snippet of the aforementioned code:
Pattern pattern = new Pattern(MidiFileManager.loadPatternFromMidi(new File(filePath.getText())));
TextField midiData = new TextField(pattern.toString());
midiData.setFont(Font.font(14));
pattern = new Pattern(midiData.getText());
Button save = new Button("Save Edits");
Pattern finalPattern = pattern;
save.setOnAction(e -> {
try {
MidiFileManager.savePatternToMidi(finalPattern, new File(filePath.getText()));
Load.playAndEdit(filePath);
} catch (Exception ex) {
ex.printStackTrace();
}
});
Any help would certainly be appreciated!
答案1
得分: 2
尽管你怀疑问题出在 JFugue 上,但根本问题在于代码中涉及 JavaFX 部分。
主要问题在于代码的编写方式,看起来期望是 TextField 将被显示,用户会进行更改,然后代码会在 "pattern = new Pattern(midiData.getText())" 中记住那次更改。然而,JavaFX 的 TextField 并不是这样工作的。UI 元素并不是像这样连续的;相反,它们与动作一起工作(或者在 JavaFX 中,你可以将 UI 组件的值与数据元素 "绑定" 起来)。如果你在用户按下保存按钮时触发的动作中从 TextField 获取文本,你将得到来自文本框的最新数据。按照代码的编写方式,用户的更改从未存储到变量中。
另外还有两点:
-
在你的第一行中,MidiFileManager.loadPatternFromMidi 返回一个 Pattern,因此不需要将其放入 "new Pattern()" 中。
-
没必要写 "Pattern finalPattern = pattern"。这只会创建一个指向与现有变量相同数据的新变量。你只需保存 "pattern",如果它具有你从文本框中期望的信息(但再次强调,这不是适当的位置,所以它不会包含你期望的信息)。
测试这不是 JFugue 问题的一种方法是创建一个简单的程序,例如:
Pattern pattern = MidiFileManager.loadPatternFromMidi(new File(filePath.getText()));
pattern.add(" C D E");
MidiFileManager.savePatternToMidi(pattern, new File(filePath.getText()));
然后你会看到生成的模式在末尾添加了三个新音符。
英文:
Although your suspicion is that this is an issue with JFugue, the underlying issue is in the JavaFX pieces of the code.
The main issue is that the way the code is written, it looks like the expectation is that the TextField will be displayed, the user will make a change, and the code will remember that change in "pattern = new Pattern(midiData.getText())". That's not how the JavaFX TextField works. UI elements are not sequential like this; instead, they work with actions (or in JavaFX, you can "bind" the value of a UI component with a data element). If you get the text from the TextField in the action that triggers when the user presses the Save button, you will have the most recent data from the text field. As the code is written, the user's change is never placed into a variable.
Two other notes:
-
In your first line, MidiFileManager.loadPatternFromMidi returns a Pattern, so you don't need to put it into a "new Pattern()".
-
There is no need to say "Pattern finalPattern = pattern". That just creates a new variable that points to the same data as the existing variable. You can just save "pattern", if it has the information you expect from the text field (but again, this isn't the right place to say it, so it won't have the information that you're expecting).
One way to test that this is not a JFugue issue is to create a simple program that does, for example:
Pattern pattern = MidiFileManager.loadPatternFromMidi(new File(filePath.getText()));
pattern.add(" C D E");
MidiFileManager.savePatternToMidi(pattern, new File(filePath.getText()));
Then you would see that the resulting pattern has three new notes added to the end.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论