Skip to content

Commit 58ecb10

Browse files
committed
Added 3 second trailer to exported songs
1 parent f3ec709 commit 58ecb10

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

src/main/java/net/raphimc/noteblocktool/audio/export/AudioMerger.java renamed to src/main/java/net/raphimc/noteblocktool/audio/export/AudioBuffer.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,28 @@
1919

2020
import net.raphimc.noteblocktool.util.SoundSampleUtil;
2121

22-
public class AudioMerger {
22+
public class AudioBuffer {
2323

24-
private final long[] samples;
24+
private long[] samples;
2525
private int sampleIndex;
2626

27-
public AudioMerger(final int sampleCount) {
28-
this.samples = new long[sampleCount];
27+
public AudioBuffer(final int initialSize) {
28+
this.samples = new long[initialSize];
2929
}
3030

31-
public void addSamples(final int[] samples) {
31+
public void pushSamples(final int[] samples) {
32+
if (this.sampleIndex + samples.length >= this.samples.length) {
33+
final long[] newSamples = new long[this.sampleIndex + samples.length];
34+
System.arraycopy(this.samples, 0, newSamples, 0, this.samples.length);
35+
this.samples = newSamples;
36+
}
37+
3238
for (int i = 0; i < samples.length; i++) {
33-
final int index = this.sampleIndex + i;
34-
if (index >= this.samples.length) break;
35-
final int sample = samples[i];
36-
this.samples[index] += sample;
39+
this.samples[this.sampleIndex + i] += samples[i];
3740
}
3841
}
3942

40-
public void pushSamples(final int samples) {
43+
public void advanceIndex(final int samples) {
4144
this.sampleIndex += samples;
4245
}
4346

@@ -69,8 +72,8 @@ public int[] normalizeInts() {
6972
}
7073

7174
private void normalize(final long maxValue) {
72-
long max = SoundSampleUtil.getMax(this.samples);
73-
float factor = (float) maxValue / max;
75+
final long max = SoundSampleUtil.getMax(this.samples);
76+
final float factor = (float) maxValue / max;
7477
for (int i = 0; i < this.samples.length; i++) {
7578
this.samples[i] = (long) (this.samples[i] * factor);
7679
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ public void render() throws InterruptedException {
6969
this.progressConsumer.accept((float) this.processedNotes / this.noteCount);
7070
if (Thread.currentThread().isInterrupted()) throw new InterruptedException();
7171
}
72+
73+
final int threeSeconds = Math.round(this.songView.getSpeed() * 3);
74+
for (int i = 0; i < threeSeconds; i++) {
75+
this.writeSamples();
76+
}
77+
7278
this.finish();
7379
}
7480

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
import net.raphimc.noteblocklib.model.SongView;
2121
import net.raphimc.noteblocktool.audio.SoundMap;
22+
import net.raphimc.noteblocktool.audio.export.AudioBuffer;
2223
import net.raphimc.noteblocktool.audio.export.AudioExporter;
23-
import net.raphimc.noteblocktool.audio.export.AudioMerger;
2424
import net.raphimc.noteblocktool.util.SoundSampleUtil;
2525

2626
import javax.sound.sampled.AudioFormat;
@@ -30,24 +30,24 @@
3030
public class JavaxAudioExporter extends AudioExporter {
3131

3232
private final Map<String, int[]> sounds;
33-
private final AudioMerger merger;
33+
private final AudioBuffer merger;
3434

3535
public JavaxAudioExporter(final SongView<?> songView, final AudioFormat format, final Consumer<Float> progressConsumer) {
3636
super(songView, format, progressConsumer);
3737
this.sounds = SoundMap.loadInstrumentSamples(format);
38-
this.merger = new AudioMerger(this.samplesPerTick * format.getChannels() * songView.getLength());
38+
this.merger = new AudioBuffer(this.samplesPerTick * format.getChannels() * songView.getLength());
3939
}
4040

4141
@Override
4242
protected void processSound(final String sound, final float pitch, final float volume, final float panning) {
4343
if (!this.sounds.containsKey(sound)) return;
4444

45-
this.merger.addSamples(SoundSampleUtil.mutate(this.format, this.sounds.get(sound), pitch, volume, panning));
45+
this.merger.pushSamples(SoundSampleUtil.mutate(this.format, this.sounds.get(sound), pitch, volume, panning));
4646
}
4747

4848
@Override
4949
protected void writeSamples() {
50-
this.merger.pushSamples(this.samplesPerTick * this.format.getChannels());
50+
this.merger.advanceIndex(this.samplesPerTick * this.format.getChannels());
5151
}
5252

5353
@Override

src/main/java/net/raphimc/noteblocktool/frames/ExportFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class ExportFrame extends JFrame {
5959
private final List<ListFrame.LoadedSong> loadedSongs;
6060
private final JComboBox<String> format = new JComboBox<>(new String[]{"NBS", "WAV", "AIF"});
6161
private final JLabel soundSystemLabel = new JLabel("Sound System:");
62-
private final JComboBox<String> soundSystem = new JComboBox<>(new String[]{"OpenAL (better sound quality)", "Javax (multithreaded, normalized)"});
62+
private final JComboBox<String> soundSystem = new JComboBox<>(new String[]{"OpenAL (better sound quality)", "Javax (parallel capable, normalized)"});
6363
private final JLabel sampleRateLabel = new JLabel("Sample Rate:");
6464
private final JSpinner sampleRate = new JSpinner(new SpinnerNumberModel(44_100, 8_000, 192_000, 1_000));
6565
private final JLabel bitDepthLabel = new JLabel("PCM Bit Depth:");

0 commit comments

Comments
 (0)