Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed May 9, 2024
2 parents fb8c20d + 5002f4a commit 679a078
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package net.raphimc.noteblocktool.audio.export;

import net.raphimc.noteblocktool.util.SoundSampleUtil;

public class AudioMerger {

private final long[] samples;
Expand All @@ -40,36 +42,38 @@ public void pushSamples(final int samples) {
}

public byte[] normalizeBytes() {
this.normalize(Byte.MAX_VALUE);
final byte[] bytes = new byte[this.samples.length];
final long max = this.getMax();
for (int i = 0; i < this.samples.length; i++) {
bytes[i] = (byte) (this.samples[i] * Byte.MAX_VALUE / max);
bytes[i] = (byte) this.samples[i];
}
return bytes;
}

public short[] normalizeShorts() {
this.normalize(Short.MAX_VALUE);
final short[] shorts = new short[this.samples.length];
final long max = this.getMax();
for (int i = 0; i < this.samples.length; i++) {
shorts[i] = (short) (this.samples[i] * Short.MAX_VALUE / max);
shorts[i] = (short) this.samples[i];
}
return shorts;
}

public int[] normalizeInts() {
this.normalize(Integer.MAX_VALUE);
final int[] ints = new int[this.samples.length];
final long max = this.getMax();
for (int i = 0; i < this.samples.length; i++) {
ints[i] = (int) (this.samples[i] * Integer.MAX_VALUE / max);
ints[i] = (int) this.samples[i];
}
return ints;
}

private long getMax() {
long max = 1;
for (long sample : this.samples) max = Math.max(max, Math.abs(sample));
return max;
private void normalize(final long maxValue) {
long max = SoundSampleUtil.getMax(this.samples);
float factor = (float) maxValue / max;
for (int i = 0; i < this.samples.length; i++) {
this.samples[i] = (long) (this.samples[i] * factor);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,10 @@ public void setMasterVolume(final float volume) {

private byte[] write(final long[] samples) {
final byte[] out = new byte[samples.length * 2];
final long max = SoundSampleUtil.getMax(samples);
final float div = Math.max(1, (float) max / Short.MAX_VALUE);
for (int i = 0; i < samples.length; i++) {
long sample = samples[i];
if (sample > Short.MAX_VALUE) sample = Short.MAX_VALUE;
else if (sample < Short.MIN_VALUE) sample = Short.MIN_VALUE;

final short conv = (short) sample;
final short conv = (short) (samples[i] / div);
out[i * 2] = (byte) (conv & 0xFF);
out[i * 2 + 1] = (byte) ((conv >> 8) & 0xFF);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,10 @@ public static int[] mutate(final int[] samples, final float volume, final float
return newSamples;
}

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

}

0 comments on commit 679a078

Please sign in to comment.