Skip to content

Commit

Permalink
Added 3 second trailer to exported songs
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Jun 16, 2024
1 parent f3ec709 commit 58ecb10
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,28 @@

import net.raphimc.noteblocktool.util.SoundSampleUtil;

public class AudioMerger {
public class AudioBuffer {

private final long[] samples;
private long[] samples;
private int sampleIndex;

public AudioMerger(final int sampleCount) {
this.samples = new long[sampleCount];
public AudioBuffer(final int initialSize) {
this.samples = new long[initialSize];
}

public void addSamples(final int[] samples) {
public void pushSamples(final int[] samples) {
if (this.sampleIndex + samples.length >= this.samples.length) {
final long[] newSamples = new long[this.sampleIndex + samples.length];
System.arraycopy(this.samples, 0, newSamples, 0, this.samples.length);
this.samples = newSamples;
}

for (int i = 0; i < samples.length; i++) {
final int index = this.sampleIndex + i;
if (index >= this.samples.length) break;
final int sample = samples[i];
this.samples[index] += sample;
this.samples[this.sampleIndex + i] += samples[i];
}
}

public void pushSamples(final int samples) {
public void advanceIndex(final int samples) {
this.sampleIndex += samples;
}

Expand Down Expand Up @@ -69,8 +72,8 @@ public int[] normalizeInts() {
}

private void normalize(final long maxValue) {
long max = SoundSampleUtil.getMax(this.samples);
float factor = (float) maxValue / max;
final long max = SoundSampleUtil.getMax(this.samples);
final float factor = (float) maxValue / max;
for (int i = 0; i < this.samples.length; i++) {
this.samples[i] = (long) (this.samples[i] * factor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public void render() throws InterruptedException {
this.progressConsumer.accept((float) this.processedNotes / this.noteCount);
if (Thread.currentThread().isInterrupted()) throw new InterruptedException();
}

final int threeSeconds = Math.round(this.songView.getSpeed() * 3);
for (int i = 0; i < threeSeconds; i++) {
this.writeSamples();
}

this.finish();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import net.raphimc.noteblocklib.model.SongView;
import net.raphimc.noteblocktool.audio.SoundMap;
import net.raphimc.noteblocktool.audio.export.AudioBuffer;
import net.raphimc.noteblocktool.audio.export.AudioExporter;
import net.raphimc.noteblocktool.audio.export.AudioMerger;
import net.raphimc.noteblocktool.util.SoundSampleUtil;

import javax.sound.sampled.AudioFormat;
Expand All @@ -30,24 +30,24 @@
public class JavaxAudioExporter extends AudioExporter {

private final Map<String, int[]> sounds;
private final AudioMerger merger;
private final AudioBuffer merger;

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

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

this.merger.addSamples(SoundSampleUtil.mutate(this.format, this.sounds.get(sound), pitch, volume, panning));
this.merger.pushSamples(SoundSampleUtil.mutate(this.format, this.sounds.get(sound), pitch, volume, panning));
}

@Override
protected void writeSamples() {
this.merger.pushSamples(this.samplesPerTick * this.format.getChannels());
this.merger.advanceIndex(this.samplesPerTick * this.format.getChannels());
}

@Override
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 (multithreaded, normalized)"});
private final JComboBox<String> soundSystem = new JComboBox<>(new String[]{"OpenAL (better sound quality)", "Javax (parallel capable, 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

0 comments on commit 58ecb10

Please sign in to comment.