Skip to content

Commit

Permalink
Improved sound sample mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenni0451 committed May 9, 2024
1 parent b2ccea2 commit b5b9c6c
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/main/java/net/raphimc/noteblocktool/util/SoundSampleUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,33 +90,28 @@ public static int[] readSamples(final InputStream inputStream, final AudioFormat
}

public static int[] mutate(final AudioFormat format, final int[] samples, final float volume, final float pitch, final float panning) {
final int[] newSamples = new int[(int) ((float) samples.length / format.getChannels() / pitch * format.getChannels())];
final int newLength = (int) ((float) samples.length / format.getChannels() / pitch * format.getChannels());
final int[] newSamples = new int[newLength];

for (int channel = 0; channel < format.getChannels(); channel++) {
//Interpolate the samples for better quality
for (int i = channel; i < newSamples.length; i += format.getChannels()) {
float index = (float) i / format.getChannels() * pitch;
int lowerIndex = (int) index;
int upperIndex = (lowerIndex + 1) * (channel + 1);
float fraction = index - lowerIndex;
int lowerSample = samples[lowerIndex * (channel + 1)];
if (upperIndex < samples.length) {
newSamples[i] = (int) ((1 - fraction) * lowerSample + fraction * samples[upperIndex]);
} else {
newSamples[i] = lowerSample;
}
float channelVolume = volume;
if (format.getChannels() == 2) {
if (channel == 0) channelVolume *= 1 - panning;
else channelVolume *= 1 + panning;
}

float channelVolume = volume;
if (format.getChannels() > 1) {
if (channel == 0) { //left channel
channelVolume *= 1 - Math.max(0, panning);
} else {
channelVolume *= 1 - Math.max(0, -panning);
}
for (int i = 0; i < newLength / format.getChannels(); i++) {
int oldIndex = (int) (i * pitch) * format.getChannels() + channel;
if (pitch < 1 && oldIndex < samples.length - format.getChannels()) {
// Interpolate between the current sample and the next one
float ratio = (i * pitch) % 1;
newSamples[i * format.getChannels() + channel] = (int) ((samples[oldIndex] * (1 - ratio) + samples[oldIndex + format.getChannels()] * ratio) * channelVolume);
} else if (oldIndex < samples.length) {
newSamples[i * format.getChannels() + channel] = (int) (samples[oldIndex] * channelVolume);
}
newSamples[i] = (int) (newSamples[i] * channelVolume);
}
}

return newSamples;
}

Expand Down

0 comments on commit b5b9c6c

Please sign in to comment.