Skip to content

Commit

Permalink
Improved song player status line
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed May 12, 2024
1 parent c93ba2d commit 5886d9b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@

public abstract class SoundSystem implements AutoCloseable {

protected final int maxSounds;

public SoundSystem(final int maxSounds) {
this.maxSounds = maxSounds;
}

public abstract void playSound(final String sound, final float pitch, final float volume, final float panning);

public void writeSamples() {
Expand All @@ -35,11 +29,7 @@ public void writeSamples() {
@Override
public abstract void close();

public int getMaxSounds() {
return this.maxSounds;
}

public abstract int getSoundCount();
public abstract String getStatusLine();

public abstract void setMasterVolume(final float volume);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public class JavaxSoundSystem extends SoundSystem {
private long[] buffer = new long[0];

public JavaxSoundSystem(final float playbackSpeed) {
super(0);

try {
this.sounds = SoundMap.loadInstrumentSamples(FORMAT);
this.mutationCache = CacheBuilder.newBuilder().maximumSize(1000).build();
Expand Down Expand Up @@ -84,8 +82,8 @@ public void close() {
}

@Override
public int getSoundCount() {
return 0;
public String getStatusLine() {
return " ";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static OpenALSoundSystem createCapture(final int maxSounds, final AudioFo
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setNameFormat("OpenAL Sound System").setDaemon(true).build());
private final Map<String, Integer> soundBuffers = new HashMap<>();
private final List<Integer> playingSources = new CopyOnWriteArrayList<>();
private final int maxSounds;
private final AudioFormat captureAudioFormat;
private long device;
private long context;
Expand All @@ -74,8 +75,7 @@ private OpenALSoundSystem(final int maxSounds) {
}

private OpenALSoundSystem(final int maxSounds, final AudioFormat captureAudioFormat) {
super(maxSounds);

this.maxSounds = maxSounds;
this.captureAudioFormat = captureAudioFormat;
int[] attributes;
if (captureAudioFormat == null) {
Expand Down Expand Up @@ -232,8 +232,12 @@ public void close() {
}

@Override
public int getSoundCount() {
return this.playingSources.size();
public String getStatusLine() {
return "Sounds: " + this.playingSources.size() + " / " + this.maxSounds;
}

public int getMaxSounds() {
return this.maxSounds;
}

@Override
Expand Down
53 changes: 34 additions & 19 deletions src/main/java/net/raphimc/noteblocktool/frames/SongPlayerFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class SongPlayerFrame extends JFrame implements SongPlayerCallback, FullN
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##");
private static SongPlayerFrame instance;
private static Point lastPosition;
private static int lastMaxSounds = 256;
private static int lastOpenAlMaxSounds = 256;
private static int lastVolume = 50;

public static void open(final ListFrame.LoadedSong song) {
Expand All @@ -60,13 +60,13 @@ public static void open(final ListFrame.LoadedSong song) {
public static void open(final ListFrame.LoadedSong song, final SongView<?> view) {
if (instance != null && instance.isVisible()) {
lastPosition = instance.getLocation();
lastMaxSounds = (int) instance.maxSoundsSpinner.getValue();
lastOpenAlMaxSounds = (int) instance.openAlMaxSoundsSpinner.getValue();
lastVolume = instance.volumeSlider.getValue();
instance.dispose();
}
instance = new SongPlayerFrame(song, view);
if (lastPosition != null) instance.setLocation(lastPosition);
instance.maxSoundsSpinner.setValue(lastMaxSounds);
instance.openAlMaxSoundsSpinner.setValue(lastOpenAlMaxSounds);
instance.volumeSlider.setValue(lastVolume);
instance.playStopButton.doClick();
instance.setVisible(true);
Expand All @@ -81,12 +81,12 @@ public static void close() {
private final SongPlayer songPlayer;
private final Timer updateTimer;
private final JComboBox<String> soundSystemComboBox = new JComboBox<>(new String[]{"OpenAL (better sound quality)", "Javax (better system compatibility, laggier)"});
private final JSpinner maxSoundsSpinner = new JSpinner(new SpinnerNumberModel(256, 64, 8192, 64));
private final JSpinner openAlMaxSoundsSpinner = new JSpinner(new SpinnerNumberModel(256, 64, 8192, 64));
private final JSlider volumeSlider = new JSlider(0, 100, 50);
private final JButton playStopButton = new JButton("Play");
private final JButton pauseResumeButton = new JButton("Pause");
private final JSlider progressSlider = new JSlider(0, 100, 0);
private final JLabel soundCount = new JLabel("Sounds: 0/" + DECIMAL_FORMAT.format(this.maxSoundsSpinner.getValue()));
private final JLabel statusLine = new JLabel("");
private final JLabel progressLabel = new JLabel("Current Position: 00:00:00");
private SoundSystem soundSystem;

Expand Down Expand Up @@ -126,16 +126,25 @@ private void initComponents() {
root.add(northPanel, BorderLayout.NORTH);

int gridy = 0;
final JLabel maxSoundsLabel = new JLabel("Max Sounds:");
GBC.create(northPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.LINE_START).add(new JLabel("Sound System:"));
GBC.create(northPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(this.soundSystemComboBox);

GBC.create(northPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.LINE_START).add(new JLabel("Max Sounds:"));
GBC.create(northPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(this.maxSoundsSpinner, () -> {
this.maxSoundsSpinner.addChangeListener(e -> {
lastMaxSounds = (int) this.maxSoundsSpinner.getValue();
GBC.create(northPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(this.soundSystemComboBox, () -> {
this.soundSystemComboBox.addActionListener(e -> {
if (this.soundSystemComboBox.getSelectedIndex() == 0) {
this.openAlMaxSoundsSpinner.setVisible(true);
maxSoundsLabel.setVisible(true);
} else {
this.openAlMaxSoundsSpinner.setVisible(false);
maxSoundsLabel.setVisible(false);
}
});
});

GBC.create(northPanel).grid(0, gridy).insets(5, 5, 0, 5).anchor(GBC.LINE_START).add(maxSoundsLabel);
GBC.create(northPanel).grid(1, gridy++).insets(5, 0, 0, 5).weightx(1).fill(GBC.HORIZONTAL).add(this.openAlMaxSoundsSpinner, () -> {
this.openAlMaxSoundsSpinner.addChangeListener(e -> lastOpenAlMaxSounds = (int) this.openAlMaxSoundsSpinner.getValue());
});

GBC.create(northPanel).grid(0, gridy).insets(5, 5, 5, 5).anchor(GBC.LINE_START).add(new JLabel("Volume:"));
GBC.create(northPanel).grid(1, gridy++).insets(5, 0, 5, 5).weightx(1).fill(GBC.HORIZONTAL).add(this.volumeSlider, () -> {
this.volumeSlider.setPaintLabels(true);
Expand Down Expand Up @@ -230,22 +239,28 @@ private void initComponents() {
final JPanel statusBar = new JPanel();
statusBar.setBorder(BorderFactory.createEtchedBorder());
statusBar.setLayout(new GridLayout(1, 1));
statusBar.add(this.soundCount);
statusBar.add(this.statusLine);
GBC.create(southPanel).grid(0, gridy++).weightx(1).fill(GBC.HORIZONTAL).add(statusBar);
}
}

private boolean initSoundSystem() {
int currentIndex = -1;
if (this.soundSystem instanceof OpenALSoundSystem) currentIndex = 0;
else if (this.soundSystem instanceof JavaxSoundSystem) currentIndex = 1;
boolean soundSystemPropertiesChanged = false;
if (this.soundSystem instanceof OpenALSoundSystem) {
final OpenALSoundSystem openALSoundSystem = (OpenALSoundSystem) this.soundSystem;
soundSystemPropertiesChanged = openALSoundSystem.getMaxSounds() != (int) this.openAlMaxSoundsSpinner.getValue();
currentIndex = 0;
} else if (this.soundSystem instanceof JavaxSoundSystem) {
currentIndex = 1;
}

try {
if (this.soundSystem == null || this.soundSystemComboBox.getSelectedIndex() != currentIndex || this.soundSystem.getMaxSounds() != (int) this.maxSoundsSpinner.getValue()) {
if (this.soundSystem == null || this.soundSystemComboBox.getSelectedIndex() != currentIndex || soundSystemPropertiesChanged) {
if (this.soundSystem != null) this.soundSystem.close();

if (this.soundSystemComboBox.getSelectedIndex() == 0) {
this.soundSystem = OpenALSoundSystem.createPlayback(((Number) this.maxSoundsSpinner.getValue()).intValue());
this.soundSystem = OpenALSoundSystem.createPlayback(((Number) this.openAlMaxSoundsSpinner.getValue()).intValue());
} else if (this.soundSystemComboBox.getSelectedIndex() == 1) {
this.soundSystem = new JavaxSoundSystem(this.songPlayer.getSongView().getSpeed());
} else {
Expand Down Expand Up @@ -281,7 +296,7 @@ public void windowClosed(WindowEvent e) {
private void tick() {
if (this.songPlayer.isRunning()) {
this.soundSystemComboBox.setEnabled(false);
this.maxSoundsSpinner.setEnabled(false);
this.openAlMaxSoundsSpinner.setEnabled(false);
this.playStopButton.setText("Stop");
this.pauseResumeButton.setEnabled(true);
if (this.songPlayer.isPaused()) this.pauseResumeButton.setText("Resume");
Expand All @@ -292,13 +307,13 @@ private void tick() {
this.progressSlider.setValue(this.songPlayer.getTick());
} else {
this.soundSystemComboBox.setEnabled(true);
this.maxSoundsSpinner.setEnabled(true);
this.openAlMaxSoundsSpinner.setEnabled(true);
this.playStopButton.setText("Play");
this.pauseResumeButton.setText("Pause");
this.pauseResumeButton.setEnabled(false);
this.progressSlider.setValue(0);
}
this.soundCount.setText("Sounds: " + DECIMAL_FORMAT.format(this.soundSystem.getSoundCount()) + "/" + DECIMAL_FORMAT.format(this.soundSystem.getMaxSounds()));
this.statusLine.setText(this.soundSystem.getStatusLine());

int msLength = (int) (this.songPlayer.getTick() / this.songPlayer.getSongView().getSpeed());
this.progressLabel.setText("Current Position: " + String.format("%02d:%02d:%02d", msLength / 3600, (msLength / 60) % 60, msLength % 60));
Expand Down

0 comments on commit 5886d9b

Please sign in to comment.