Skip to content

Commit a852454

Browse files
committed
Reduced memory allocations
1 parent fc625d6 commit a852454

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

src/main/java/net/raphimc/noteblocktool/audio/soundsystem/impl/JavaxSoundSystem.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ public synchronized void playSound(final String sound, final float pitch, final
7171
@Override
7272
public synchronized void writeSamples() {
7373
final long[] samples = new long[this.samplesPerTick];
74+
final int[] outputBuffer = new int[this.samplesPerTick];
75+
final int[] mutationBuffer = new int[this.samplesPerTick * 2];
7476
for (SoundInstance playingSound : this.playingSounds) {
75-
playingSound.render();
76-
playingSound.write(samples);
77+
playingSound.render(mutationBuffer);
78+
playingSound.write(samples, outputBuffer);
7779
}
7880
this.dataLine.write(this.writeNormalized(samples), 0, samples.length * 2);
7981
this.playingSounds.removeIf(SoundInstance::isFinished);
@@ -130,8 +132,6 @@ protected class SoundInstance {
130132
private final float volume;
131133
private final float panning;
132134
private final int sliceLength;
133-
private final int[] mutationBuffer;
134-
private final int[] outputBuffer;
135135
private final CircularBuffer mutatedSamplesBuffer;
136136
private int cursor = 0;
137137

@@ -140,20 +140,18 @@ public SoundInstance(final int[] samples, final float pitch, final float volume,
140140
this.pitch = pitch;
141141
this.volume = volume;
142142
this.panning = panning;
143-
this.sliceLength = (int) (JavaxSoundSystem.this.samplesPerTick / FORMAT.getChannels() * pitch) * FORMAT.getChannels() + FORMAT.getChannels();
144-
this.mutationBuffer = new int[JavaxSoundSystem.this.samplesPerTick * 2];
145-
this.outputBuffer = new int[JavaxSoundSystem.this.samplesPerTick];
143+
this.sliceLength = (int) (JavaxSoundSystem.this.samplesPerTick * pitch / FORMAT.getChannels()) * FORMAT.getChannels() + FORMAT.getChannels();
146144
this.mutatedSamplesBuffer = new CircularBuffer(JavaxSoundSystem.this.samplesPerTick * 3);
147145
}
148146

149-
public void render() {
147+
public void render(final int[] mutationBuffer) {
150148
if (!this.hasDataToRender()) return;
151149

152150
final int sliceLength = Math.min(this.samples.length - this.cursor, this.sliceLength);
153-
final long result = SoundSampleUtil.mutate(FORMAT, this.samples, this.cursor, sliceLength, this.pitch, this.volume, this.panning, this.mutationBuffer);
151+
final long result = SoundSampleUtil.mutate(FORMAT, this.samples, this.cursor, sliceLength, this.pitch, this.volume, this.panning, mutationBuffer);
154152
final int mutationBufferAvailable = (int) (result >> 32);
155153
if (this.mutatedSamplesBuffer.hasSpaceFor(mutationBufferAvailable)) {
156-
this.mutatedSamplesBuffer.addAll(this.mutationBuffer, mutationBufferAvailable);
154+
this.mutatedSamplesBuffer.addAll(mutationBuffer, mutationBufferAvailable);
157155
if ((int) result > 0) {
158156
this.cursor += (int) result;
159157
} else {
@@ -162,13 +160,13 @@ public void render() {
162160
}
163161
}
164162

165-
public void write(final long[] buffer) {
166-
if (buffer.length < this.outputBuffer.length) {
163+
public void write(final long[] samples, final int[] outputBuffer) {
164+
if (samples.length < outputBuffer.length) {
167165
throw new IllegalArgumentException("Buffer is too small");
168166
}
169-
this.mutatedSamplesBuffer.takeAllSafe(this.outputBuffer);
170-
for (int i = 0; i < buffer.length; i++) {
171-
buffer[i] += this.outputBuffer[i];
167+
this.mutatedSamplesBuffer.takeAllSafe(outputBuffer);
168+
for (int i = 0; i < samples.length; i++) {
169+
samples[i] += outputBuffer[i];
172170
}
173171
}
174172

src/main/java/net/raphimc/noteblocktool/audio/soundsystem/impl/MultithreadedJavaxSoundSystem.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class MultithreadedJavaxSoundSystem extends JavaxSoundSystem {
3131
private final Queue<SoundInstance> soundsToMerge = new ConcurrentLinkedQueue<>();
3232
private final AtomicInteger syncLock = new AtomicInteger(0);
3333
private final long[][] threadSamples;
34+
private final int[][] threadOutputBuffers;
35+
private final int[][] threadMutationBuffers;
3436

3537
public MultithreadedJavaxSoundSystem(final int maxSounds, final float playbackSpeed) {
3638
super(maxSounds, playbackSpeed);
@@ -41,8 +43,17 @@ public MultithreadedJavaxSoundSystem(final int maxSounds, final float playbackSp
4143
for (int i = 0; i < mergingThreads; i++) {
4244
this.threadSamples[i] = new long[this.samplesPerTick];
4345
}
46+
this.threadOutputBuffers = new int[mergingThreads][];
47+
for (int i = 0; i < mergingThreads; i++) {
48+
this.threadOutputBuffers[i] = new int[this.samplesPerTick];
49+
}
50+
this.threadMutationBuffers = new int[renderingThreads][];
51+
for (int i = 0; i < renderingThreads; i++) {
52+
this.threadMutationBuffers[i] = new int[this.samplesPerTick * 2];
53+
}
4454

4555
for (int i = 0; i < renderingThreads; i++) {
56+
final int finalI = i;
4657
this.threadPool.submit(() -> {
4758
try {
4859
while (!Thread.currentThread().isInterrupted()) {
@@ -51,7 +62,7 @@ public MultithreadedJavaxSoundSystem(final int maxSounds, final float playbackSp
5162
Thread.sleep(1);
5263
continue;
5364
}
54-
soundInstance.render();
65+
soundInstance.render(this.threadMutationBuffers[finalI]);
5566
this.soundsToMerge.add(soundInstance);
5667
}
5768
} catch (InterruptedException ignored) {
@@ -68,7 +79,7 @@ public MultithreadedJavaxSoundSystem(final int maxSounds, final float playbackSp
6879
Thread.sleep(1);
6980
continue;
7081
}
71-
soundInstance.write(this.threadSamples[finalI]);
82+
soundInstance.write(this.threadSamples[finalI], this.threadOutputBuffers[finalI]);
7283
this.syncLock.decrementAndGet();
7384
}
7485
} catch (InterruptedException ignored) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static void close() {
8888
private final MonitoringSongPlayer songPlayer;
8989
private final Timer updateTimer;
9090
private final JComboBox<String> soundSystemComboBox = new JComboBox<>(new String[]{"OpenAL (better sound quality)", "Un4seen BASS", "Javax (better system compatibility)", "Javax multithreaded (experimental)"});
91-
private final JSpinner maxSoundsSpinner = new JSpinner(new SpinnerNumberModel(256, 64, 8192, 64));
91+
private final JSpinner maxSoundsSpinner = new JSpinner(new SpinnerNumberModel(256, 64, 10240, 64));
9292
private final JSlider volumeSlider = new JSlider(0, 100, 50);
9393
private final JButton playStopButton = new JButton("Play");
9494
private final JButton pauseResumeButton = new JButton("Pause");

0 commit comments

Comments
 (0)