Skip to content

Commit

Permalink
Greatly improved javax sound
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenni0451 committed May 8, 2024
1 parent 4d04aad commit 4ee7b1a
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/main/java/net/raphimc/noteblocktool/util/SoundSampleUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,26 @@ public class SoundSampleUtil {

public static int[] mutate(final int[] samples, final float volume, final float pitchChangeFactor) {
final int[] newSamples = new int[(int) (samples.length / pitchChangeFactor)];
for (int i = 0; i < newSamples.length; i++) {
// Long to prevent clipping of the index
final long index = (long) i * samples.length / newSamples.length;
newSamples[i] = (int) (samples[(int) index] * volume);
if (pitchChangeFactor < 1) {
//Interpolate the samples for better quality
for (int i = 0; i < newSamples.length; i++) {
final float index = i * pitchChangeFactor;
final int lowerIndex = (int) index;
final int upperIndex = lowerIndex + 1;
final float fraction = index - lowerIndex;
if (upperIndex < samples.length) {
newSamples[i] = (int) ((1 - fraction) * samples[lowerIndex] + fraction * samples[upperIndex]);
} else {
newSamples[i] = samples[lowerIndex];
}
newSamples[i] = (int) (newSamples[i] * volume);
}
} else {
for (int i = 0; i < newSamples.length; i++) {
// Long to prevent clipping of the index
final long index = (long) i * samples.length / newSamples.length;
newSamples[i] = (int) (samples[(int) index] * volume);
}
}
return newSamples;
}
Expand Down

0 comments on commit 4ee7b1a

Please sign in to comment.