Skip to content

Commit

Permalink
Cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed May 9, 2024
1 parent b5b9c6c commit a15e1c6
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
public abstract class SoundSystem implements AutoCloseable {

protected final int maxSounds;
protected float masterVolume = 1F;

public SoundSystem(final int maxSounds) {
this.maxSounds = maxSounds;
Expand All @@ -44,8 +43,6 @@ public int getMaxSounds() {

public abstract int getSoundCount();

public void setMasterVolume(final float volume) {
this.masterVolume = volume;
}
public abstract void setMasterVolume(final float volume);

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class JavaxSoundSystem extends SoundSystem {
private final int samplesPerTick;
private final SourceDataLine dataLine;
private final Map<String, int[]> mutationCache;
private float masterVolume = 1F;
private long[] buffer = new long[0];

public JavaxSoundSystem(final float playbackSpeed) {
Expand All @@ -50,7 +51,7 @@ public JavaxSoundSystem(final float playbackSpeed) {
this.dataLine.start();
this.mutationCache = new ConcurrentHashMap<>();
} catch (Throwable e) {
throw new RuntimeException("Could not initialize javax audio system", e);
throw new RuntimeException("Could not initialize javax sound system", e);
}
}

Expand Down Expand Up @@ -87,7 +88,7 @@ public int getSoundCount() {

@Override
public void setMasterVolume(final float volume) {
super.setMasterVolume(volume);
this.masterVolume = volume;
this.mutationCache.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class ExportFrame extends JFrame {
private final List<ListFrame.LoadedSong> loadedSongs;
private final JComboBox<String> format = new JComboBox<>(new String[]{"NBS", "WAV", "AIF"});
private final JLabel soundSystemLabel = new JLabel("Sound System:");
private final JComboBox<String> soundSystem = new JComboBox<>(new String[]{"OpenAL (better sound quality)", "Javax (faster, normalized, mono only)"});
private final JComboBox<String> soundSystem = new JComboBox<>(new String[]{"OpenAL (better sound quality)", "Javax (faster, normalized)"});
private final JLabel sampleRateLabel = new JLabel("Sample Rate:");
private final JSpinner sampleRate = new JSpinner(new SpinnerNumberModel(44_100, 8_000, 192_000, 1_000));
private final JLabel bitDepthLabel = new JLabel("PCM Bit Depth:");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static void close() {
private final ListFrame.LoadedSong song;
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, mono only)"});
private final JComboBox<String> soundSystemComboBox = new JComboBox<>(new String[]{"OpenAL (better sound quality)", "Javax (better system compatibility)"});
private final JSpinner maxSoundsSpinner = new JSpinner(new SpinnerNumberModel(256, 64, 8192, 64));
private final JSlider volumeSlider = new JSlider(0, 100, 50);
private final JButton playStopButton = new JButton("Play");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public static int[] mutate(final AudioFormat format, final int[] samples, final
}

for (int i = 0; i < newLength / format.getChannels(); i++) {
int oldIndex = (int) (i * pitch) * format.getChannels() + channel;
final int oldIndex = (int) (i * pitch) * format.getChannels() + channel;
if (pitch < 1 && oldIndex < samples.length - format.getChannels()) {
// Interpolate between the current sample and the next one
float ratio = (i * pitch) % 1;
final float ratio = (i * pitch) % 1;
newSamples[i * format.getChannels() + channel] = (int) ((samples[oldIndex] * (1 - ratio) + samples[oldIndex + format.getChannels()] * ratio) * channelVolume);
} else if (oldIndex < samples.length) {
newSamples[i * format.getChannels() + channel] = (int) (samples[oldIndex] * channelVolume);
Expand Down

0 comments on commit a15e1c6

Please sign in to comment.