Skip to content

Commit

Permalink
Cleaned up code style
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed May 8, 2024
1 parent fc6debf commit 883bb9c
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/raphimc/noteblocktool/audio/SoundMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

import net.raphimc.noteblocklib.util.Instrument;

import java.util.HashMap;
import java.util.EnumMap;
import java.util.Map;

public class SoundMap {

public static final Map<Instrument, String> SOUNDS = new HashMap<>();
public static final Map<Instrument, String> SOUNDS = new EnumMap<>(Instrument.class);

static {
SOUNDS.put(Instrument.HARP, "/noteblock_sounds/harp.wav");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.raphimc.noteblocklib.model.Note;
import net.raphimc.noteblocklib.model.SongView;
import net.raphimc.noteblocklib.util.Instrument;
import net.raphimc.noteblocklib.util.SongUtil;
import net.raphimc.noteblocktool.util.DefaultSongPlayerCallback;

import javax.sound.sampled.AudioFileFormat;
Expand All @@ -34,7 +35,7 @@

public abstract class AudioExporter {

private final DefaultSongPlayerCallback callback = this::processNote;
private final DefaultSongPlayerCallback noteConsumer = this::processNote;
private final SongView<?> songView;
protected final AudioFormat format;
private final Consumer<Float> progressConsumer;
Expand All @@ -49,16 +50,16 @@ public AudioExporter(final SongView<?> songView, final AudioFormat format, final
this.progressConsumer = progressConsumer;
this.sampleOutputStream = new SampleOutputStream(format);

this.noteCount = songView.getNotes().values().stream().mapToLong(List::size).sum();
this.noteCount = SongUtil.getNoteCount(songView);
this.samplesPerTick = (int) (format.getSampleRate() / songView.getSpeed());
}

public void render() throws InterruptedException {
for (int tick = 0; tick <= this.songView.getLength(); tick++) {
List<? extends Note> notes = this.songView.getNotesAtTick(tick);
final List<? extends Note> notes = this.songView.getNotesAtTick(tick);
for (Note note : notes) {
if (Thread.currentThread().isInterrupted()) throw new InterruptedException();
this.callback.playNote(note);
this.noteConsumer.playNote(note);
}
this.processedNotes += notes.size();
this.writeSamples();
Expand All @@ -69,10 +70,10 @@ public void render() throws InterruptedException {
this.finish();
}

public void write(final File file) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(this.sampleOutputStream.getBytes());
AudioInputStream audioInputStream = new AudioInputStream(bais, this.format, bais.available());
AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, file);
public void write(final AudioFileFormat.Type format, final File file) throws IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream(this.sampleOutputStream.getBytes());
final AudioInputStream audioInputStream = new AudioInputStream(bais, this.format, bais.available());
AudioSystem.write(audioInputStream, format, file);
audioInputStream.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public AudioMerger(final int sampleCount) {

public void addSamples(final int[] samples) {
for (int i = 0; i < samples.length; i++) {
int index = this.sampleIndex + i;
final int index = this.sampleIndex + i;
if (index >= this.samples.length) break;
int sample = samples[i];
final int sample = samples[i];
this.samples[index] += sample;
}
}
Expand All @@ -40,26 +40,26 @@ public void pushSamples(final int samples) {
}

public byte[] normalizeBytes() {
byte[] bytes = new byte[this.samples.length];
long max = this.getMax();
final byte[] bytes = new byte[this.samples.length];
final long max = this.getMax();
for (int i = 0; i < this.samples.length; i++) {
bytes[i] = (byte) (this.samples[i] * Byte.MAX_VALUE / max);
}
return bytes;
}

public short[] normalizeShorts() {
short[] shorts = new short[this.samples.length];
long max = this.getMax();
final short[] shorts = new short[this.samples.length];
final long max = this.getMax();
for (int i = 0; i < this.samples.length; i++) {
shorts[i] = (short) (this.samples[i] * Short.MAX_VALUE / max);
}
return shorts;
}

public int[] normalizeInts() {
int[] ints = new int[this.samples.length];
long max = this.getMax();
final int[] ints = new int[this.samples.length];
final long max = this.getMax();
for (int i = 0; i < this.samples.length; i++) {
ints[i] = (int) (this.samples[i] * Integer.MAX_VALUE / max);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public JavaxAudioExporter(final SongView<?> songView, final AudioFormat format,
}

@Override
protected void processNote(Instrument instrument, float volume, float pitch, float panning) {
protected void processNote(final Instrument instrument, final float volume, final float pitch, final float panning) {
String key = instrument + "\0" + volume + "\0" + pitch;
this.merger.addSamples(this.mutationCache.computeIfAbsent(key, k -> SoundSampleUtil.mutate(this.sounds.get(instrument), volume, pitch)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public OpenALAudioExporter(final OpenALSoundSystem soundSystem, final SongView<?
}

@Override
protected void processNote(Instrument instrument, float volume, float pitch, float panning) {
protected void processNote(final Instrument instrument, final float volume, final float pitch, final float panning) {
this.soundSystem.playNote(instrument, volume, pitch, panning);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public SoundSystem(final int maxSounds) {
this.maxSounds = maxSounds;
}

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

public abstract void playNote(final Instrument instrument, final float volume, final float pitch, final float panning);

public void writeSamples() {
Expand All @@ -48,4 +44,8 @@ public int getMaxSounds() {

public abstract int getSoundCount();

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class JavaxSoundSystem extends SoundSystem {
private final Map<String, int[]> mutationCache;
private long[] buffer = new long[0];

public JavaxSoundSystem(final int maxSounds, final float playbackSpeed) {
super(maxSounds);
public JavaxSoundSystem(final float playbackSpeed) {
super(0);

try {
this.sounds = this.loadSounds();
Expand All @@ -62,16 +62,16 @@ public JavaxSoundSystem(final int maxSounds, final float playbackSpeed) {
}

@Override
public void playNote(Instrument instrument, float volume, float pitch, float panning) {
String key = instrument.ordinal() + "\0" + volume + "\0" + pitch;
int[] samples = this.mutationCache.computeIfAbsent(key, k -> SoundSampleUtil.mutate(this.sounds.get(instrument), volume * this.masterVolume, pitch));
public void playNote(final Instrument instrument, final float volume, final float pitch, final float panning) {
final String key = instrument.ordinal() + "\0" + volume + "\0" + pitch;
final int[] samples = this.mutationCache.computeIfAbsent(key, k -> SoundSampleUtil.mutate(this.sounds.get(instrument), volume * this.masterVolume, pitch));
if (this.buffer.length < samples.length) this.buffer = Arrays.copyOf(this.buffer, samples.length);
for (int i = 0; i < samples.length; i++) this.buffer[i] += samples[i];
}

@Override
public void writeSamples() {
long[] samples = Arrays.copyOfRange(this.buffer, 0, this.samplesPerTick);
final long[] samples = Arrays.copyOfRange(this.buffer, 0, this.samplesPerTick);
this.dataLine.write(this.write(samples), 0, samples.length * 2);
if (this.buffer.length > this.samplesPerTick) this.buffer = Arrays.copyOfRange(this.buffer, this.samplesPerTick, this.buffer.length);
else if (this.buffer.length != 0) this.buffer = new long[0];
Expand All @@ -88,24 +88,19 @@ public void close() {
}

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

@Override
public int getMaxSounds() {
public int getSoundCount() {
return 0;
}

@Override
public int getSoundCount() {
return 0;
public void setMasterVolume(final float volume) {
super.setMasterVolume(volume);
this.mutationCache.clear();
}

private Map<Instrument, int[]> loadSounds() {
try {
Map<Instrument, int[]> sounds = new EnumMap<>(Instrument.class);
final Map<Instrument, int[]> sounds = new EnumMap<>(Instrument.class);
for (Map.Entry<Instrument, String> entry : SoundMap.SOUNDS.entrySet()) {
sounds.put(entry.getKey(), this.readSound(JavaxSoundSystem.class.getResourceAsStream(entry.getValue())));
}
Expand Down Expand Up @@ -134,13 +129,13 @@ private int[] readSound(final InputStream is) {
}

private byte[] write(final long[] samples) {
byte[] out = new byte[samples.length * 2];
final byte[] out = new byte[samples.length * 2];
for (int i = 0; i < samples.length; i++) {
long sample = samples[i];
if (sample > Short.MAX_VALUE) sample = Short.MAX_VALUE;
else if (sample < Short.MIN_VALUE) sample = Short.MIN_VALUE;

short conv = (short) sample;
final short conv = (short) sample;
out[i * 2] = (byte) (conv & 0xFF);
out[i * 2 + 1] = (byte) ((conv >> 8) & 0xFF);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public static OpenALSoundSystem createCapture(final int maxSounds, final AudioFo
private Thread shutdownHook;
private ByteBuffer captureBuffer;

private OpenALSoundSystem(final int maxSounds) { //Playback
private OpenALSoundSystem(final int maxSounds) { // Playback
this(maxSounds, null);
}

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

this.audioFormat = captureAudioFormat;
Expand Down Expand Up @@ -146,7 +146,7 @@ private OpenALSoundSystem(final int maxSounds, final AudioFormat captureAudioFor
}

@Override
public void playNote(Instrument instrument, float volume, float pitch, float panning) {
public void playNote(final Instrument instrument, final float volume, final float pitch, final float panning) {
if (this.playingSources.size() >= this.maxSounds) {
AL10.alDeleteSources(this.playingSources.remove(0));
this.checkError("Could not delete audio source");
Expand Down Expand Up @@ -229,14 +229,14 @@ public void close() {
}

@Override
public void setMasterVolume(float volume) {
AL10.alListenerf(AL10.AL_GAIN, volume);
this.checkError("Could not set listener gain");
public int getSoundCount() {
return this.playingSources.size();
}

@Override
public int getSoundCount() {
return this.playingSources.size();
public void setMasterVolume(final float volume) {
AL10.alListenerf(AL10.AL_GAIN, volume);
this.checkError("Could not set listener gain");
}

private void tick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.raphimc.noteblocktool.audio.soundsystem.impl.OpenALSoundSystem;
import net.raphimc.noteblocktool.util.filefilter.SingleFileFilter;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.swing.*;
import java.awt.*;
Expand All @@ -58,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, multithreaded, mono only)"});
private final JComboBox<String> soundSystem = new JComboBox<>(new String[]{"OpenAL (better sound quality)", "Javax (faster, normalized, mono only)"});
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 Expand Up @@ -390,7 +391,7 @@ private void exportSong(final ListFrame.LoadedSong song, final OpenALSoundSystem
else exporter = new JavaxAudioExporter(songView, format, progressConsumer);

exporter.render();
exporter.write(file);
exporter.write(this.format.getSelectedIndex() == 1 ? AudioFileFormat.Type.WAVE : AudioFileFormat.Type.AIFF, file);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.raphimc.noteblocklib.format.nbs.NbsSong;
import net.raphimc.noteblocklib.model.Song;
import net.raphimc.noteblocklib.model.SongView;
import net.raphimc.noteblocklib.util.SongUtil;
import net.raphimc.noteblocktool.elements.FastScrollPane;
import net.raphimc.noteblocktool.elements.TextOverlayPanel;
import net.raphimc.noteblocktool.elements.drag.DragTable;
Expand Down Expand Up @@ -329,7 +330,7 @@ public int getNoteCount() {
}

public int getNoteCount(final SongView<?> view) {
return (int) view.getNotes().values().stream().mapToLong(List::size).sum();
return (int) SongUtil.getNoteCount(view);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private boolean initSoundSystem() {
if (this.soundSystemComboBox.getSelectedIndex() == 0) {
this.soundSystem = OpenALSoundSystem.createPlayback(((Number) this.maxSoundsSpinner.getValue()).intValue());
} else if (this.soundSystemComboBox.getSelectedIndex() == 1) {
this.soundSystem = new JavaxSoundSystem(((Number) this.maxSoundsSpinner.getValue()).intValue(), this.songPlayer.getSongView().getSpeed());
this.soundSystem = new JavaxSoundSystem(this.songPlayer.getSongView().getSpeed());
} else {
throw new UnsupportedOperationException(UNAVAILABLE_MESSAGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public interface DefaultSongPlayerCallback extends ISongPlayerCallback {

@Override
default void playNote(Note note) {
default void playNote(final Note note) {
if (note.getInstrument() >= Instrument.values().length) return;
final float volume;
if (note instanceof NoteWithVolume) {
Expand Down Expand Up @@ -61,6 +61,6 @@ default void playNote(Note note) {
this.playNote(instrument, playerVolume, pitch, playerPanning);
}

void playNote(Instrument instrument, float volume, float pitch, float panning);
void playNote(final Instrument instrument, final float volume, final float pitch, final float panning);

}

0 comments on commit 883bb9c

Please sign in to comment.