Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed May 7, 2024
2 parents af387bf + 086b99b commit 54c6363
Show file tree
Hide file tree
Showing 7 changed files with 463 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ public AudioExporter(final SongView<?> songView, final AudioFormat format, final
this.samplesPerTick = (int) (format.getSampleRate() / songView.getSpeed());
}

public void render() {
public void render() throws InterruptedException {
for (int tick = 0; tick <= this.songView.getLength(); tick++) {
List<? extends Note> notes = this.songView.getNotesAtTick(tick);
this.processNotes(notes);
this.writeSamples();

this.progressConsumer.accept((float) this.processedNotes / this.noteCount);
if (Thread.currentThread().isInterrupted()) throw new InterruptedException();
}
this.finish();
}
Expand All @@ -74,8 +75,9 @@ public void write(final File file) throws IOException {
audioInputStream.close();
}

private void processNotes(final List<? extends Note> notes) {
private void processNotes(final List<? extends Note> notes) throws InterruptedException {
for (Note note : notes) {
if (Thread.currentThread().isInterrupted()) throw new InterruptedException();
if (note.getInstrument() >= Instrument.values().length) continue;
final float volume;
if (note instanceof NoteWithVolume) {
Expand Down Expand Up @@ -104,8 +106,8 @@ private void processNotes(final List<? extends Note> notes) {
final float playerPanning = panning / 100F;

this.processNote(instrument, playerVolume, pitch, playerPanning);
this.processedNotes++;
}
this.processedNotes += notes.size();
}

protected abstract void processNote(final Instrument instrument, final float volume, final float pitch, final float panning);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected void writeSamples() {

@Override
protected void finish() {
OpenALSoundSystem.stopAllSources();
}

}
401 changes: 401 additions & 0 deletions src/main/java/net/raphimc/noteblocktool/frames/ExportFrame.java

Large diffs are not rendered by default.

84 changes: 8 additions & 76 deletions src/main/java/net/raphimc/noteblocktool/frames/ListFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@
import net.raphimc.noteblocklib.NoteBlockLib;
import net.raphimc.noteblocklib.format.SongFormat;
import net.raphimc.noteblocklib.format.mcsp.McSpSong;
import net.raphimc.noteblocklib.format.mcsp.model.McSpHeader;
import net.raphimc.noteblocklib.format.nbs.NbsSong;
import net.raphimc.noteblocklib.format.nbs.model.NbsHeader;
import net.raphimc.noteblocklib.model.Song;
import net.raphimc.noteblocklib.model.SongView;
import net.raphimc.noteblocktool.elements.FastScrollPane;
import net.raphimc.noteblocktool.elements.NoteBlockFileFilter;
import net.raphimc.noteblocktool.elements.TextOverlayPanel;
import net.raphimc.noteblocktool.elements.drag.DragTable;
import net.raphimc.noteblocktool.elements.drag.DragTableDropTargetListener;
import net.raphimc.noteblocktool.elements.drag.DragTableModel;
import net.raphimc.noteblocktool.util.filefilter.NoteBlockFileFilter;

import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
Expand All @@ -55,7 +53,7 @@ public class ListFrame extends JFrame {
private final JButton removeButton = new JButton("Remove");
private final JButton editButton = new JButton("Edit");
private final JButton playButton = new JButton("Play");
private final JButton exportButton = new JButton("Export NBS");
private final JButton exportButton = new JButton("Export");
private DropTarget dropTarget;
private TextOverlayPanel textOverlayPanel;

Expand Down Expand Up @@ -136,34 +134,12 @@ public void keyPressed(KeyEvent e) {
});
GBC.create(buttonPanel).gridx(5).insets(5, 5, 5, 5).anchor(GBC.LINE_START).add(this.exportButton, () -> {
this.exportButton.addActionListener(e -> {
SongPlayerFrame.close();
this.setEnabled(false);
final int[] rows = this.table.getSelectedRows();
if (rows.length == 1) {
final LoadedSong song = (LoadedSong) this.table.getValueAt(rows[0], 0);
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Export Song");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.setFileFilter(new NoteBlockFileFilter(SongFormat.NBS));
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if (!file.getName().toLowerCase().endsWith(".nbs")) file = new File(file.getParentFile(), file.getName() + ".nbs");
this.exportSong(song, file);
}
} else if (rows.length > 1) {
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Export Songs");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setMultiSelectionEnabled(false);
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
final File directory = fileChooser.getSelectedFile();
for (int row : rows) {
final LoadedSong song = (LoadedSong) this.table.getValueAt(row, 0);
final File file = new File(directory, song.getFile().getName().substring(0, song.getFile().getName().lastIndexOf('.')) + ".nbs");
this.exportSong(song, file);
}
}
}
List<LoadedSong> songs = new ArrayList<>();
for (int row : rows) songs.add((LoadedSong) this.table.getValueAt(row, 0));
new ExportFrame(this, songs);
});
});
root.add(buttonPanel, BorderLayout.SOUTH);
Expand Down Expand Up @@ -203,7 +179,7 @@ public void popupMenuCanceled(PopupMenuEvent e) {
contextMenuPlay.addActionListener(e -> this.playButton.doClick());
contextMenu.add(contextMenuPlay);
this.table.getSelectionModel().addListSelectionListener(e -> contextMenuPlay.setEnabled(this.table.getSelectedRows().length == 1));
JMenuItem contextMenuExport = new JMenuItem("Export NBS");
JMenuItem contextMenuExport = new JMenuItem("Export");
contextMenuExport.addActionListener(e -> this.exportButton.doClick());
contextMenu.add(contextMenuExport);
this.table.setComponentPopupMenu(contextMenu);
Expand Down Expand Up @@ -285,50 +261,6 @@ private void runSync(final Runnable task) {
}
}

private void exportSong(final LoadedSong song, final File file) {
try {
final Song<?, ?, ?> exportSong = NoteBlockLib.createSongFromView(song.getSong().getView(), SongFormat.NBS);
final NbsSong exportNbsSong = (NbsSong) exportSong;
final NbsHeader exportNbsHeader = exportNbsSong.getHeader();
if (song.getSong() instanceof NbsSong) {
final NbsSong nbsSong = (NbsSong) song.getSong();
final NbsHeader nbsHeader = ((NbsSong) song.getSong()).getHeader();
exportNbsHeader.setVersion((byte) Math.max(nbsHeader.getVersion(), exportNbsHeader.getVersion()));
exportNbsHeader.setAuthor(nbsHeader.getAuthor());
exportNbsHeader.setOriginalAuthor(nbsHeader.getOriginalAuthor());
exportNbsHeader.setDescription(nbsHeader.getDescription());
exportNbsHeader.setAutoSave(nbsHeader.isAutoSave());
exportNbsHeader.setAutoSaveInterval(nbsHeader.getAutoSaveInterval());
exportNbsHeader.setTimeSignature(nbsHeader.getTimeSignature());
exportNbsHeader.setMinutesSpent(nbsHeader.getMinutesSpent());
exportNbsHeader.setLeftClicks(nbsHeader.getLeftClicks());
exportNbsHeader.setRightClicks(nbsHeader.getRightClicks());
exportNbsHeader.setNoteBlocksAdded(nbsHeader.getNoteBlocksAdded());
exportNbsHeader.setNoteBlocksRemoved(nbsHeader.getNoteBlocksRemoved());
exportNbsHeader.setSourceFileName(nbsHeader.getSourceFileName());
exportNbsHeader.setLoop(nbsHeader.isLoop());
exportNbsHeader.setMaxLoopCount(nbsHeader.getMaxLoopCount());
exportNbsHeader.setLoopStartTick(nbsHeader.getLoopStartTick());
exportNbsSong.getData().setCustomInstruments(nbsSong.getData().getCustomInstruments());
} else if (song.getSong() instanceof McSpSong) {
final McSpHeader mcSpHeader = ((McSpSong) song.getSong()).getHeader();
exportNbsHeader.setAuthor(mcSpHeader.getAuthor());
exportNbsHeader.setOriginalAuthor(mcSpHeader.getOriginalAuthor());
exportNbsHeader.setAutoSave(mcSpHeader.getAutoSaveInterval() != 0);
exportNbsHeader.setAutoSaveInterval((byte) mcSpHeader.getAutoSaveInterval());
exportNbsHeader.setMinutesSpent(mcSpHeader.getMinutesSpent());
exportNbsHeader.setLeftClicks(mcSpHeader.getLeftClicks());
exportNbsHeader.setRightClicks(mcSpHeader.getRightClicks());
exportNbsHeader.setNoteBlocksAdded(mcSpHeader.getNoteBlocksAdded());
exportNbsHeader.setNoteBlocksRemoved(mcSpHeader.getNoteBlocksRemoved());
}
NoteBlockLib.writeSong(exportSong, file);
} catch (Throwable t) {
t.printStackTrace();
JOptionPane.showMessageDialog(this, "Failed to export song:\n" + song.getFile().getAbsolutePath() + "\n" + t.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}


public static class LoadedSong {
private final File file;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public static void open(final ListFrame.LoadedSong song, final SongView<?> view)
instance.setVisible(true);
}

public static void close() {
if (instance != null) instance.dispose();
}


private final ListFrame.LoadedSong song;
private final SongPlayer songPlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.noteblocktool.elements;
package net.raphimc.noteblocktool.util.filefilter;

import net.raphimc.noteblocklib.format.SongFormat;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This file is part of NoteBlockTool - https://github.com/RaphiMC/NoteBlockTool
* Copyright (C) 2022-2024 RK_01/RaphiMC and contributors
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.noteblocktool.util.filefilter;

import javax.swing.filechooser.FileFilter;
import java.io.File;

public class SingleFileFilter extends FileFilter {

private final String extension;

public SingleFileFilter(final String extension) {
this.extension = extension.toLowerCase();
}

@Override
public boolean accept(File f) {
if (f.isDirectory()) return true;
if (!f.isFile()) return false;
return f.getName().toLowerCase().endsWith("." + this.extension);
}

@Override
public String getDescription() {
return this.extension + " File";
}

}

0 comments on commit 54c6363

Please sign in to comment.