Skip to content

Commit ba2b0cd

Browse files
committed
Updated NoteBlockLib to 3.0.0
1 parent c110edf commit ba2b0cd

24 files changed

+388
-501
lines changed

build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ configurations {
2121

2222
repositories {
2323
mavenCentral()
24+
maven {
25+
name = "Lenni0451"
26+
url = "https://maven.lenni0451.net/snapshots"
27+
}
2428
}
2529

2630
dependencies {
27-
include "net.raphimc:NoteBlockLib:2.1.4"
31+
include "net.raphimc:NoteBlockLib:3.0.0-SNAPSHOT"
2832
include "net.raphimc:audio-mixer:2.0.0"
2933
include "org.jcraft:jorbis:0.0.17"
3034
include "com.formdev:flatlaf:3.5.4"

src/main/java/net/raphimc/noteblocktool/audio/SoundMap.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
*/
1818
package net.raphimc.noteblocktool.audio;
1919

20+
import net.raphimc.noteblocklib.data.MinecraftInstrument;
2021
import net.raphimc.noteblocklib.format.nbs.model.NbsCustomInstrument;
21-
import net.raphimc.noteblocklib.model.SongView;
22-
import net.raphimc.noteblocklib.util.Instrument;
22+
import net.raphimc.noteblocklib.model.Song;
2323
import net.raphimc.noteblocklib.util.SongUtil;
2424
import net.raphimc.noteblocktool.util.IOUtil;
2525

@@ -32,33 +32,33 @@
3232

3333
public class SoundMap {
3434

35-
public static final Map<Instrument, String> INSTRUMENT_SOUNDS = new EnumMap<>(Instrument.class);
35+
public static final Map<MinecraftInstrument, String> INSTRUMENT_SOUNDS = new EnumMap<>(MinecraftInstrument.class);
3636
private static final Map<String, URL> ALL_SOUND_LOCATIONS = new HashMap<>();
3737

3838
static {
39-
INSTRUMENT_SOUNDS.put(Instrument.HARP, "harp.ogg");
40-
INSTRUMENT_SOUNDS.put(Instrument.BASS, "bass.ogg");
41-
INSTRUMENT_SOUNDS.put(Instrument.BASS_DRUM, "bd.ogg");
42-
INSTRUMENT_SOUNDS.put(Instrument.SNARE, "snare.ogg");
43-
INSTRUMENT_SOUNDS.put(Instrument.HAT, "hat.ogg");
44-
INSTRUMENT_SOUNDS.put(Instrument.GUITAR, "guitar.ogg");
45-
INSTRUMENT_SOUNDS.put(Instrument.FLUTE, "flute.ogg");
46-
INSTRUMENT_SOUNDS.put(Instrument.BELL, "bell.ogg");
47-
INSTRUMENT_SOUNDS.put(Instrument.CHIME, "icechime.ogg");
48-
INSTRUMENT_SOUNDS.put(Instrument.XYLOPHONE, "xylobone.ogg");
49-
INSTRUMENT_SOUNDS.put(Instrument.IRON_XYLOPHONE, "iron_xylophone.ogg");
50-
INSTRUMENT_SOUNDS.put(Instrument.COW_BELL, "cow_bell.ogg");
51-
INSTRUMENT_SOUNDS.put(Instrument.DIDGERIDOO, "didgeridoo.ogg");
52-
INSTRUMENT_SOUNDS.put(Instrument.BIT, "bit.ogg");
53-
INSTRUMENT_SOUNDS.put(Instrument.BANJO, "banjo.ogg");
54-
INSTRUMENT_SOUNDS.put(Instrument.PLING, "pling.ogg");
39+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.HARP, "harp.ogg");
40+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.BASS, "bass.ogg");
41+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.BASS_DRUM, "bd.ogg");
42+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.SNARE, "snare.ogg");
43+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.HAT, "hat.ogg");
44+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.GUITAR, "guitar.ogg");
45+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.FLUTE, "flute.ogg");
46+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.BELL, "bell.ogg");
47+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.CHIME, "icechime.ogg");
48+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.XYLOPHONE, "xylobone.ogg");
49+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.IRON_XYLOPHONE, "iron_xylophone.ogg");
50+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.COW_BELL, "cow_bell.ogg");
51+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.DIDGERIDOO, "didgeridoo.ogg");
52+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.BIT, "bit.ogg");
53+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.BANJO, "banjo.ogg");
54+
INSTRUMENT_SOUNDS.put(MinecraftInstrument.PLING, "pling.ogg");
5555

5656
reload(null);
5757
}
5858

5959
public static void reload(final File customSoundsFolder) {
6060
ALL_SOUND_LOCATIONS.clear();
61-
for (Map.Entry<Instrument, String> entry : INSTRUMENT_SOUNDS.entrySet()) {
61+
for (Map.Entry<MinecraftInstrument, String> entry : INSTRUMENT_SOUNDS.entrySet()) {
6262
ALL_SOUND_LOCATIONS.put(entry.getValue(), SoundMap.class.getResource("/noteblock_sounds/" + entry.getValue()));
6363
}
6464

@@ -82,17 +82,17 @@ public static void reload(final File customSoundsFolder) {
8282
}
8383
}
8484

85-
public static Map<String, byte[]> loadSoundData(final SongView<?> songView) {
85+
public static Map<String, byte[]> loadSoundData(final Song song) {
8686
try {
8787
final Map<String, byte[]> soundData = new HashMap<>();
88-
for (Instrument instrument : SongUtil.getUsedVanillaInstruments(songView)) {
88+
for (MinecraftInstrument instrument : SongUtil.getUsedVanillaInstruments(song)) {
8989
final String sound = INSTRUMENT_SOUNDS.get(instrument);
9090
if (sound != null && ALL_SOUND_LOCATIONS.containsKey(sound)) {
9191
soundData.put(sound, IOUtil.readFully(ALL_SOUND_LOCATIONS.get(sound).openStream()));
9292
}
9393
}
94-
for (NbsCustomInstrument customInstrument : SongUtil.getUsedCustomInstruments(songView)) {
95-
final String fileName = customInstrument.getSoundFileName().replace(File.separatorChar, '/');
94+
for (NbsCustomInstrument customInstrument : SongUtil.getUsedNbsCustomInstruments(song)) {
95+
final String fileName = customInstrument.getSoundFilePathOr("").replace(File.separatorChar, '/');
9696
if (ALL_SOUND_LOCATIONS.containsKey(fileName)) {
9797
soundData.put(fileName, IOUtil.readFully(ALL_SOUND_LOCATIONS.get(fileName).openStream()));
9898
}

src/main/java/net/raphimc/noteblocktool/audio/export/AudioExporter.java

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,83 +18,82 @@
1818
package net.raphimc.noteblocktool.audio.export;
1919

2020
import net.raphimc.audiomixer.util.GrowableArray;
21+
import net.raphimc.noteblocklib.data.MinecraftInstrument;
2122
import net.raphimc.noteblocklib.format.nbs.model.NbsCustomInstrument;
2223
import net.raphimc.noteblocklib.model.Note;
23-
import net.raphimc.noteblocklib.model.SongView;
24-
import net.raphimc.noteblocklib.player.FullNoteConsumer;
25-
import net.raphimc.noteblocklib.util.Instrument;
26-
import net.raphimc.noteblocklib.util.SongUtil;
24+
import net.raphimc.noteblocklib.model.Song;
25+
import net.raphimc.noteblocklib.player.SongPlayer;
2726
import net.raphimc.noteblocktool.audio.SoundMap;
2827

2928
import javax.sound.sampled.AudioFormat;
3029
import java.io.File;
3130
import java.util.List;
3231
import java.util.function.Consumer;
3332

34-
public abstract class AudioExporter implements FullNoteConsumer {
33+
public abstract class AudioExporter extends SongPlayer {
3534

36-
private final SongView<?> songView;
3735
protected final AudioFormat format;
3836
private final float masterVolume;
3937
private final Consumer<Float> progressConsumer;
4038
protected GrowableArray samples;
4139
private final long noteCount;
42-
protected final int samplesPerTick;
4340
private int processedNotes;
4441

45-
public AudioExporter(final SongView<?> songView, final AudioFormat format, final float masterVolume, final Consumer<Float> progressConsumer) {
46-
this.songView = songView;
42+
public AudioExporter(final Song song, final AudioFormat format, final float masterVolume, final Consumer<Float> progressConsumer) {
43+
super(song);
4744
this.format = format;
4845
this.progressConsumer = progressConsumer;
4946
this.masterVolume = masterVolume;
5047

51-
this.noteCount = SongUtil.getNoteCount(songView);
52-
this.samplesPerTick = (int) Math.ceil(format.getSampleRate() / songView.getSpeed());
53-
this.samples = new GrowableArray(this.samplesPerTick * format.getChannels() * (songView.getLength() + Math.round(this.songView.getSpeed() * 3)));
48+
this.noteCount = song.getNotes().getNoteCount();
49+
this.samples = new GrowableArray((song.getLengthInSeconds() + 5) * format.getChannels());
5450
}
5551

5652
public void render() throws InterruptedException {
57-
for (int tick = 0; tick < this.songView.getLength(); tick++) {
58-
if (Thread.currentThread().isInterrupted()) throw new InterruptedException();
59-
final List<? extends Note> notes = this.songView.getNotesAtTick(tick);
60-
this.preTick();
61-
for (Note note : notes) this.playNote(note);
62-
this.postTick();
63-
this.processedNotes += notes.size();
64-
65-
this.progressConsumer.accept((float) this.processedNotes / this.noteCount);
53+
this.start();
54+
while (this.isRunning()) {
55+
this.tick();
6656
if (Thread.currentThread().isInterrupted()) throw new InterruptedException();
6757
}
6858

69-
final int threeSeconds = Math.round(this.songView.getSpeed() * 3);
59+
final int threeSeconds = Math.round(this.getCurrentTicksPerSecond() * 3);
7060
for (int i = 0; i < threeSeconds; i++) {
7161
this.postTick();
7262
}
73-
74-
this.finish();
7563
}
7664

7765
public int[] getSamples() {
7866
return this.samples.getArray();
7967
}
8068

8169
@Override
82-
public void playNote(final Instrument instrument, final float pitch, final float volume, final float panning) {
83-
this.processSound(SoundMap.INSTRUMENT_SOUNDS.get(instrument), pitch, volume * this.masterVolume, panning);
70+
protected void playNotes(final List<Note> notes) {
71+
for (Note note : notes) {
72+
if (note.getInstrument() instanceof MinecraftInstrument instrument) {
73+
this.processSound(SoundMap.INSTRUMENT_SOUNDS.get(instrument), note.getPitch(), note.getVolume() * this.masterVolume, note.getPanning());
74+
} else if (note.getInstrument() instanceof NbsCustomInstrument instrument) {
75+
this.processSound(instrument.getSoundFilePathOr("").replace(File.separatorChar, '/'), note.getPitch(), note.getVolume() * this.masterVolume, note.getPanning());
76+
} else {
77+
throw new IllegalArgumentException("Unsupported instrument type: " + note.getInstrument().getClass().getName());
78+
}
79+
}
80+
81+
this.processedNotes += notes.size();
82+
this.progressConsumer.accept((float) this.processedNotes / this.noteCount);
8483
}
8584

8685
@Override
87-
public void playCustomNote(final NbsCustomInstrument customInstrument, final float pitch, final float volume, final float panning) {
88-
this.processSound(customInstrument.getSoundFileName().replace(File.separatorChar, '/'), pitch, volume * this.masterVolume, panning);
86+
protected void postTick() {
87+
final int samplesPerTick = (int) Math.ceil(this.format.getSampleRate() / this.getCurrentTicksPerSecond());
88+
this.mix(samplesPerTick);
8989
}
9090

91-
protected abstract void processSound(final String sound, final float pitch, final float volume, final float panning);
92-
93-
protected void preTick() {
91+
@Override
92+
protected void createTickTask() {
9493
}
9594

96-
protected abstract void postTick();
95+
protected abstract void processSound(final String sound, final float pitch, final float volume, final float panning);
9796

98-
protected abstract void finish();
97+
protected abstract void mix(final int samplesPerTick);
9998

10099
}

src/main/java/net/raphimc/noteblocktool/audio/export/impl/AudioMixerAudioExporter.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import net.raphimc.audiomixer.util.AudioFormats;
2525
import net.raphimc.audiomixer.util.SoundSampleUtil;
2626
import net.raphimc.audiomixer.util.io.SoundIO;
27-
import net.raphimc.noteblocklib.model.SongView;
27+
import net.raphimc.noteblocklib.model.Song;
2828
import net.raphimc.noteblocktool.audio.SoundMap;
2929
import net.raphimc.noteblocktool.audio.export.AudioExporter;
3030
import net.raphimc.noteblocktool.util.SoundFileUtil;
@@ -41,13 +41,13 @@ public class AudioMixerAudioExporter extends AudioExporter {
4141
protected final Map<String, int[]> sounds;
4242
protected final AudioMixer audioMixer;
4343

44-
public AudioMixerAudioExporter(final SongView<?> songView, final AudioFormat format, final float masterVolume, final boolean globalNormalization, final Consumer<Float> progressConsumer) {
45-
super(songView, format, masterVolume, progressConsumer);
44+
public AudioMixerAudioExporter(final Song song, final AudioFormat format, final float masterVolume, final boolean globalNormalization, final Consumer<Float> progressConsumer) {
45+
super(song, format, masterVolume, progressConsumer);
4646
this.globalNormalization = globalNormalization;
4747

4848
try {
4949
this.sounds = new HashMap<>();
50-
for (Map.Entry<String, byte[]> entry : SoundMap.loadSoundData(songView).entrySet()) {
50+
for (Map.Entry<String, byte[]> entry : SoundMap.loadSoundData(song).entrySet()) {
5151
this.sounds.put(entry.getKey(), SoundIO.readSamples(SoundFileUtil.readAudioFile(new ByteArrayInputStream(entry.getValue())), AudioFormats.withChannels(format, 1)));
5252
}
5353
this.audioMixer = new AudioMixer(format);
@@ -60,6 +60,14 @@ public AudioMixerAudioExporter(final SongView<?> songView, final AudioFormat for
6060
}
6161
}
6262

63+
@Override
64+
public void render() throws InterruptedException {
65+
super.render();
66+
if (this.globalNormalization) {
67+
SoundSampleUtil.normalize(this.samples.getArrayDirect(), (int) Math.pow(2, this.format.getSampleSizeInBits() - 1) - 1);
68+
}
69+
}
70+
6371
@Override
6472
protected void processSound(final String sound, final float pitch, final float volume, final float panning) {
6573
if (!this.sounds.containsKey(sound)) return;
@@ -68,15 +76,8 @@ protected void processSound(final String sound, final float pitch, final float v
6876
}
6977

7078
@Override
71-
protected void postTick() {
72-
this.samples.add(this.audioMixer.mix(this.samplesPerTick * this.format.getChannels()));
73-
}
74-
75-
@Override
76-
protected void finish() {
77-
if (this.globalNormalization) {
78-
SoundSampleUtil.normalize(this.samples.getArrayDirect(), (int) Math.pow(2, this.format.getSampleSizeInBits() - 1) - 1);
79-
}
79+
protected void mix(final int samplesPerTick) {
80+
this.samples.add(this.audioMixer.mix(samplesPerTick * this.format.getChannels()));
8081
}
8182

8283
}

src/main/java/net/raphimc/noteblocktool/audio/export/impl/BassAudioExporter.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
package net.raphimc.noteblocktool.audio.export.impl;
1919

20-
import net.raphimc.noteblocklib.model.SongView;
20+
import net.raphimc.noteblocklib.model.Song;
2121
import net.raphimc.noteblocktool.audio.SoundMap;
2222
import net.raphimc.noteblocktool.audio.export.AudioExporter;
2323
import net.raphimc.noteblocktool.audio.soundsystem.impl.BassSoundSystem;
@@ -29,9 +29,18 @@ public class BassAudioExporter extends AudioExporter {
2929

3030
private final BassSoundSystem soundSystem;
3131

32-
public BassAudioExporter(final SongView<?> songView, final AudioFormat format, final float masterVolume, final Consumer<Float> progressConsumer) {
33-
super(songView, format, masterVolume, progressConsumer);
34-
this.soundSystem = BassSoundSystem.createCapture(SoundMap.loadSoundData(songView), 8192, format);
32+
public BassAudioExporter(final Song song, final AudioFormat format, final float masterVolume, final Consumer<Float> progressConsumer) {
33+
super(song, format, masterVolume, progressConsumer);
34+
this.soundSystem = BassSoundSystem.createCapture(SoundMap.loadSoundData(song), 8192, format);
35+
}
36+
37+
@Override
38+
public void render() throws InterruptedException {
39+
try {
40+
super.render();
41+
} finally {
42+
this.soundSystem.close();
43+
}
3544
}
3645

3746
@Override
@@ -40,13 +49,13 @@ protected void processSound(final String sound, final float pitch, final float v
4049
}
4150

4251
@Override
43-
protected void postTick() {
44-
this.soundSystem.renderSamples(this.samples, this.samplesPerTick);
52+
protected void preTick() {
53+
this.soundSystem.preTick();
4554
}
4655

4756
@Override
48-
protected void finish() {
49-
this.soundSystem.close();
57+
protected void mix(final int samplesPerTick) {
58+
this.soundSystem.renderSamples(this.samples, samplesPerTick);
5059
}
5160

5261
}

src/main/java/net/raphimc/noteblocktool/audio/export/impl/OpenALAudioExporter.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
package net.raphimc.noteblocktool.audio.export.impl;
1919

20-
import net.raphimc.noteblocklib.model.SongView;
20+
import net.raphimc.noteblocklib.model.Song;
2121
import net.raphimc.noteblocktool.audio.SoundMap;
2222
import net.raphimc.noteblocktool.audio.export.AudioExporter;
2323
import net.raphimc.noteblocktool.audio.soundsystem.impl.OpenALSoundSystem;
@@ -29,9 +29,18 @@ public class OpenALAudioExporter extends AudioExporter {
2929

3030
private final OpenALSoundSystem soundSystem;
3131

32-
public OpenALAudioExporter(final SongView<?> songView, final AudioFormat format, final float masterVolume, final Consumer<Float> progressConsumer) {
33-
super(songView, format, masterVolume, progressConsumer);
34-
this.soundSystem = OpenALSoundSystem.createCapture(SoundMap.loadSoundData(songView), 8192, format);
32+
public OpenALAudioExporter(final Song song, final AudioFormat format, final float masterVolume, final Consumer<Float> progressConsumer) {
33+
super(song, format, masterVolume, progressConsumer);
34+
this.soundSystem = OpenALSoundSystem.createCapture(SoundMap.loadSoundData(song), 8192, format);
35+
}
36+
37+
@Override
38+
public void render() throws InterruptedException {
39+
try {
40+
super.render();
41+
} finally {
42+
this.soundSystem.close();
43+
}
3544
}
3645

3746
@Override
@@ -45,14 +54,8 @@ protected void preTick() {
4554
}
4655

4756
@Override
48-
protected void postTick() {
49-
this.soundSystem.renderSamples(this.samples, this.samplesPerTick);
50-
this.soundSystem.postTick();
51-
}
52-
53-
@Override
54-
protected void finish() {
55-
this.soundSystem.close();
57+
protected void mix(final int samplesPerTick) {
58+
this.soundSystem.renderSamples(this.samples, samplesPerTick);
5659
}
5760

5861
}

src/main/java/net/raphimc/noteblocktool/audio/soundsystem/SoundSystem.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ public SoundSystem(final int maxSounds) {
3030
public void preTick() {
3131
}
3232

33-
public void postTick() {
34-
}
35-
3633
public abstract void stopSounds();
3734

3835
@Override

0 commit comments

Comments
 (0)