-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add AdaptiveOggAudioStream - Add OggAudioStreamMixin
- Loading branch information
1 parent
f5f49fb
commit d518d47
Showing
6 changed files
with
182 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...n/java/com/mmodding/mmodding_lib/library/sounds/client/stream/AdaptiveOggAudioStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.mmodding.mmodding_lib.library.sounds.client.stream; | ||
|
||
import com.mmodding.mmodding_lib.mixin.injectors.client.OggAudioStreamMixin; | ||
import net.minecraft.client.sound.OggAudioStream; | ||
import net.minecraft.util.math.MathHelper; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.lwjgl.BufferUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.FloatBuffer; | ||
|
||
/** | ||
* This class is combined with {@link OggAudioStreamMixin} | ||
*/ | ||
public class AdaptiveOggAudioStream extends OggAudioStream { | ||
|
||
private float sourceSampleRate; | ||
private float targetSampleRate; | ||
|
||
public AdaptiveOggAudioStream(InputStream inputStream) throws IOException { | ||
super(inputStream); | ||
} | ||
|
||
public FloatBuffer resample(FloatBuffer source, float sourceRate, float targetRate) { | ||
// Create Target Buffer | ||
float ratio = targetRate / sourceRate; | ||
int newLength = Math.round(source.remaining() * ratio); | ||
FloatBuffer target = BufferUtils.createFloatBuffer(newLength); | ||
|
||
// Use Linear Interpolation | ||
for (int i = 0; i < newLength; i++) { | ||
float srcIndex = i / ratio; | ||
int index = (int) Math.floor(srcIndex); | ||
float delta = srcIndex - index; | ||
float sourceSample = source.get(index); | ||
float targetSample = index + 1 < source.limit() ? source.get(index + 1) : sourceSample; | ||
float interpolated = MathHelper.lerp(delta, sourceSample, targetSample); | ||
target.put(interpolated); | ||
} | ||
|
||
target.flip(); | ||
return target; | ||
} | ||
|
||
@Override | ||
protected void readChannels(FloatBuffer floatBuffer, OggAudioStream.ChannelList channels) { | ||
if (this.sourceSampleRate != this.targetSampleRate) { | ||
floatBuffer = this.resample(floatBuffer, this.sourceSampleRate, this.targetSampleRate); | ||
} | ||
if (this.getTrackType().equals(TrackType.STEREO)) { | ||
while (floatBuffer.hasRemaining()) { | ||
float f = floatBuffer.get(); | ||
channels.addChannel(f); | ||
channels.addChannel(f); | ||
} | ||
} | ||
else { | ||
super.readChannels(floatBuffer, channels); | ||
} | ||
} | ||
|
||
@Override | ||
protected void readChannels(FloatBuffer leftFloatBuffer, FloatBuffer rightFloatBuffer, ChannelList channels) { | ||
if (this.sourceSampleRate != this.targetSampleRate) { | ||
leftFloatBuffer = this.resample(leftFloatBuffer, this.sourceSampleRate, this.targetSampleRate); | ||
rightFloatBuffer = this.resample(rightFloatBuffer, this.sourceSampleRate, this.targetSampleRate); | ||
} | ||
if (this.getTrackType().equals(TrackType.MONO)) { | ||
while (leftFloatBuffer.hasRemaining()) { | ||
channels.addChannel((leftFloatBuffer.get() + rightFloatBuffer.get()) / 2.0f); | ||
} | ||
} | ||
else { | ||
super.readChannels(leftFloatBuffer, rightFloatBuffer, channels); | ||
} | ||
} | ||
|
||
// Return Value must be a Constant | ||
public float getForcedSampleRate() { | ||
return 44100.0f; | ||
} | ||
|
||
// Return Value must be a Constant | ||
public TrackType getTrackType() { | ||
return TrackType.STEREO; | ||
} | ||
|
||
@ApiStatus.Internal | ||
public void setSampleRates(float sourceSampleRate, float targetSampleRate) { | ||
this.sourceSampleRate = sourceSampleRate; | ||
this.targetSampleRate = targetSampleRate; | ||
} | ||
|
||
public enum TrackType { | ||
MONO, | ||
STEREO | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/mmodding/mmodding_lib/mixin/injectors/client/OggAudioStreamMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.mmodding.mmodding_lib.mixin.injectors.client; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import com.mmodding.mmodding_lib.library.sounds.client.stream.AdaptiveOggAudioStream; | ||
import com.mmodding.mmodding_lib.library.utils.Self; | ||
import net.minecraft.client.sound.OggAudioStream; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
import javax.sound.sampled.AudioFormat; | ||
|
||
@Mixin(OggAudioStream.class) | ||
public class OggAudioStreamMixin implements Self<OggAudioStream> { | ||
|
||
@WrapOperation(method = "<init>", at = @At(value = "NEW", target = "(FIIZZ)Ljavax/sound/sampled/AudioFormat;")) | ||
private AudioFormat forceAdaptivity(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian, Operation<AudioFormat> original) { | ||
if (this.getObject() instanceof AdaptiveOggAudioStream adaptive) { // If Adaptive | ||
float sourceSampleRate = sampleRate; | ||
if (adaptive.getTrackType().equals(AdaptiveOggAudioStream.TrackType.MONO) && channels == 2) { // Force Mono if Stereo | ||
channels = 1; | ||
} | ||
else if (adaptive.getTrackType().equals(AdaptiveOggAudioStream.TrackType.STEREO) && channels == 1) { // Force Stereo if Mono | ||
channels = 2; | ||
} | ||
if (adaptive.getForcedSampleRate() != sampleRate) { // Force New Sample Rate if Source Sample Rate is not Equal | ||
sampleRate = adaptive.getForcedSampleRate(); | ||
} | ||
adaptive.setSampleRates(sourceSampleRate, sampleRate); // Provide Original & New Sample Rate Information | ||
} | ||
return original.call(sampleRate, sampleSizeInBits, channels, signed, bigEndian); | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
src/main/java/com/mmodding/mmodding_lib/mixin/injectors/client/SourceMixin.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters