Skip to content

Commit

Permalink
Reduced javax audio export memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Sep 8, 2024
1 parent 922766f commit de54996
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@

public class AudioBuffer {

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

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

public void pushSamples(final int[] samples) {
if (this.sampleIndex + samples.length >= this.samples.length) {
final long[] newSamples = new long[this.sampleIndex + samples.length];
final int[] newSamples = new int[this.sampleIndex + samples.length];
System.arraycopy(this.samples, 0, newSamples, 0, this.samples.length);
this.samples = newSamples;
}
Expand Down Expand Up @@ -65,17 +65,15 @@ public short[] normalizeShorts() {
public int[] normalizeInts() {
this.normalize(Integer.MAX_VALUE);
final int[] ints = new int[this.samples.length];
for (int i = 0; i < this.samples.length; i++) {
ints[i] = (int) this.samples[i];
}
System.arraycopy(this.samples, 0, ints, 0, this.samples.length);
return ints;
}

private void normalize(final long maxValue) {
final long max = SoundSampleUtil.getMax(this.samples);
private void normalize(final int maxValue) {
final int 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);
this.samples[i] = (int) (this.samples[i] * factor);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public synchronized void preTick() {

@Override
public synchronized void postTick() {
final long[] samples = this.render();
final int[] samples = this.render();
if (this.dataLine.available() < samples.length * 2) {
// In case of buffer overrun, flush the queued samples
this.dataLine.flush();
Expand Down Expand Up @@ -107,8 +107,8 @@ public synchronized void setMasterVolume(final float volume) {
this.masterVolume = volume;
}

protected long[] render() {
final long[] samples = new long[this.samplesPerTick];
protected int[] render() {
final int[] samples = new int[this.samplesPerTick];
final int[] outputBuffer = new int[this.samplesPerTick];
final int[] mutationBuffer = new int[this.samplesPerTick * 2];
for (SoundInstance playingSound : this.playingSounds) {
Expand All @@ -118,9 +118,9 @@ protected long[] render() {
return samples;
}

private byte[] normalize(final long[] samples) {
private byte[] normalize(final int[] samples) {
final byte[] out = new byte[samples.length * 2];
final long max = SoundSampleUtil.getMax(samples);
final int max = SoundSampleUtil.getMax(samples);
float div = Math.max(1, (float) max / Short.MAX_VALUE);
if (this.volumeDividers == null) {
this.volumeDividers = new float[this.volumeDividersLength];
Expand Down Expand Up @@ -176,7 +176,7 @@ public void render(final int[] mutationBuffer) {
}
}

public void write(final long[] samples, final int[] outputBuffer) {
public void write(final int[] samples, final int[] outputBuffer) {
if (samples.length < outputBuffer.length) {
throw new IllegalArgumentException("Buffer is too small");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class MultithreadedJavaxSoundSystem extends JavaxSoundSystem {
private final Queue<SoundInstance> soundsToRender = new ConcurrentLinkedQueue<>();
private final Queue<SoundInstance> soundsToMerge = new ConcurrentLinkedQueue<>();
private final AtomicInteger syncLock = new AtomicInteger(0);
private final long[][] threadSamples;
private final int[][] threadSamples;
private final int[][] threadOutputBuffers;
private final int[][] threadMutationBuffers;

Expand All @@ -40,9 +40,9 @@ public MultithreadedJavaxSoundSystem(final Map<String, byte[]> soundData, final

final int mergingThreads = Math.max(1, this.threadPool.getCorePoolSize() / 3);
final int renderingThreads = this.threadPool.getCorePoolSize() - mergingThreads;
this.threadSamples = new long[mergingThreads][];
this.threadSamples = new int[mergingThreads][];
for (int i = 0; i < mergingThreads; i++) {
this.threadSamples[i] = new long[this.samplesPerTick];
this.threadSamples[i] = new int[this.samplesPerTick];
}
this.threadOutputBuffers = new int[mergingThreads][];
for (int i = 0; i < mergingThreads; i++) {
Expand Down Expand Up @@ -101,15 +101,15 @@ public synchronized String getStatusLine() {
}

@Override
protected long[] render() {
protected int[] render() {
this.soundsToRender.addAll(this.playingSounds);
this.syncLock.set(this.playingSounds.size());
while (this.syncLock.get() != 0 && !Thread.currentThread().isInterrupted()) {
// Wait for all sounds to be rendered and merged
}

final long[] samples = new long[this.samplesPerTick];
for (long[] threadSamples : this.threadSamples) {
final int[] samples = new int[this.samplesPerTick];
for (int[] threadSamples : this.threadSamples) {
for (int i = 0; i < samples.length; i++) {
samples[i] += threadSamples[i];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ public static long mutate(final AudioFormat format, final int[] samples, final i
return ((long) mutatedSamplesLength << 32) | ((int) (mutatedLength * pitch) * channels);
}

public static long getMax(final long[] samples) {
long max = 1;
for (long sample : samples) max = Math.max(max, Math.abs(sample));
public static int getMax(final int[] samples) {
int max = 1;
for (int sample : samples) max = Math.max(max, Math.abs(sample));
return max;
}

Expand Down

0 comments on commit de54996

Please sign in to comment.