| 1 | package net.minecraft.client.sounds;
|
| 2 |
|
| 3 | import com.jcraft.jogg.Packet;
|
| 4 | import com.jcraft.jogg.Page;
|
| 5 | import com.jcraft.jogg.StreamState;
|
| 6 | import com.jcraft.jogg.SyncState;
|
| 7 | import com.jcraft.jorbis.Block;
|
| 8 | import com.jcraft.jorbis.Comment;
|
| 9 | import com.jcraft.jorbis.DspState;
|
| 10 | import com.jcraft.jorbis.Info;
|
| 11 | import it.unimi.dsi.fastutil.floats.FloatConsumer;
|
| 12 | import java.io.IOException;
|
| 13 | import java.io.InputStream;
|
| 14 | import javax.sound.sampled.AudioFormat;
|
| 15 | import net.fabricmc.api.EnvType;
|
| 16 | import net.fabricmc.api.Environment;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class JOrbisAudioStream implements FloatSampleSource {
|
| 21 | private static final int BUFSIZE = 8192;
|
| 22 | private static final int PAGEOUT_RECAPTURE = -1;
|
| 23 | private static final int PAGEOUT_NEED_MORE_DATA = 0;
|
| 24 | private static final int PAGEOUT_OK = 1;
|
| 25 | private static final int PACKETOUT_ERROR = -1;
|
| 26 | private static final int PACKETOUT_NEED_MORE_DATA = 0;
|
| 27 | private static final int PACKETOUT_OK = 1;
|
| 28 | private final SyncState syncState = new SyncState();
|
| 29 | private final Page page = new Page();
|
| 30 | private final StreamState streamState = new StreamState();
|
| 31 | private final Packet packet = new Packet();
|
| 32 | private final Info info = new Info();
|
| 33 | private final DspState dspState = new DspState();
|
| 34 | private final Block block = new Block(this.dspState);
|
| 35 | private final AudioFormat audioFormat;
|
| 36 | private final InputStream input;
|
| 37 | private long samplesWritten;
|
| 38 | private long totalSamplesInStream = Long.MAX_VALUE;
|
| 39 |
|
| 40 | public JOrbisAudioStream(final InputStream input) throws IOException {
|
| 41 | this.input = input;
|
| 42 | Comment comment = new Comment();
|
| 43 | Page firstPage = this.readPage();
|
| 44 | if (firstPage == null) {
|
| 45 | throw new IOException("Invalid Ogg file - can't find first page");
|
| 46 | }
|
| 47 |
|
| 48 | Packet firstPacket = this.readIdentificationPacket(firstPage);
|
| 49 | if (isError(this.info.synthesis_headerin(comment, firstPacket))) {
|
| 50 | throw new IOException("Invalid Ogg identification packet");
|
| 51 | }
|
| 52 |
|
| 53 | for (int headerPacketCount = 0; headerPacketCount < 2; headerPacketCount++) {
|
| 54 | firstPacket = this.readPacket();
|
| 55 | if (firstPacket == null) {
|
| 56 | throw new IOException("Unexpected end of Ogg stream");
|
| 57 | }
|
| 58 |
|
| 59 | if (isError(this.info.synthesis_headerin(comment, firstPacket))) {
|
| 60 | throw new IOException("Invalid Ogg header packet " + headerPacketCount);
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | this.dspState.synthesis_init(this.info);
|
| 65 | this.block.init(this.dspState);
|
| 66 | this.audioFormat = new AudioFormat(this.info.rate, 16, this.info.channels, true, false);
|
| 67 | }
|
| 68 |
|
| 69 | private static boolean isError(final int value) {
|
| 70 | return value < 0;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public AudioFormat getFormat() {
|
| 75 | return this.audioFormat;
|
| 76 | }
|
| 77 |
|
| 78 | private boolean readToBuffer() throws IOException {
|
| 79 | int offset = this.syncState.buffer(8192);
|
| 80 | byte[] buffer = this.syncState.data;
|
| 81 | int bytes = this.input.read(buffer, offset, 8192);
|
| 82 | if (bytes == -1) {
|
| 83 | return false;
|
| 84 | }
|
| 85 |
|
| 86 | this.syncState.wrote(bytes);
|
| 87 | return true;
|
| 88 | }
|
| 89 |
|
| 90 | private @Nullable Page readPage() throws IOException {
|
| 91 | while (true) {
|
| 92 | int pageOutResult = this.syncState.pageout(this.page);
|
| 93 | switch (pageOutResult) {
|
| 94 | case -1:
|
| 95 | throw new IOException("Corrupt or missing data in bitstream");
|
| 96 | case 0:
|
| 97 | if (this.readToBuffer()) {
|
| 98 | break;
|
| 99 | }
|
| 100 |
|
| 101 | return null;
|
| 102 | case 1:
|
| 103 | if (this.page.eos() != 0) {
|
| 104 | this.totalSamplesInStream = this.page.granulepos();
|
| 105 | }
|
| 106 |
|
| 107 | return this.page;
|
| 108 | default:
|
| 109 | throw new IllegalStateException("Unknown page decode result: " + pageOutResult);
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | private Packet readIdentificationPacket(final Page firstPage) throws IOException {
|
| 115 | this.streamState.init(firstPage.serialno());
|
| 116 | if (isError(this.streamState.pagein(firstPage))) {
|
| 117 | throw new IOException("Failed to parse page");
|
| 118 | } else {
|
| 119 | int result = this.streamState.packetout(this.packet);
|
| 120 | if (result != 1) {
|
| 121 | throw new IOException("Failed to read identification packet: " + result);
|
| 122 | } else {
|
| 123 | return this.packet;
|
| 124 | }
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | private @Nullable Packet readPacket() throws IOException {
|
| 129 | while (true) {
|
| 130 | int packetOutResult = this.streamState.packetout(this.packet);
|
| 131 | switch (packetOutResult) {
|
| 132 | case -1:
|
| 133 | throw new IOException("Failed to parse packet");
|
| 134 | case 0:
|
| 135 | Page page = this.readPage();
|
| 136 | if (page == null) {
|
| 137 | return null;
|
| 138 | }
|
| 139 |
|
| 140 | if (!isError(this.streamState.pagein(page))) {
|
| 141 | break;
|
| 142 | }
|
| 143 |
|
| 144 | throw new IOException("Failed to parse page");
|
| 145 | case 1:
|
| 146 | return this.packet;
|
| 147 | default:
|
| 148 | throw new IllegalStateException("Unknown packet decode result: " + packetOutResult);
|
| 149 | }
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | private long getSamplesToWrite(final int samples) {
|
| 154 | long samplesAfterWrite = this.samplesWritten + samples;
|
| 155 | long samplesToWrite;
|
| 156 | if (samplesAfterWrite > this.totalSamplesInStream) {
|
| 157 | samplesToWrite = this.totalSamplesInStream - this.samplesWritten;
|
| 158 | this.samplesWritten = this.totalSamplesInStream;
|
| 159 | } else {
|
| 160 | this.samplesWritten = samplesAfterWrite;
|
| 161 | samplesToWrite = samples;
|
| 162 | }
|
| 163 |
|
| 164 | return samplesToWrite;
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public boolean readChunk(final FloatConsumer consumer) throws IOException {
|
| 169 | float[][][] pcmSampleOutput = new float[1][][];
|
| 170 | int[] pcmOffsetOutput = new int[this.info.channels];
|
| 171 | Packet packet = this.readPacket();
|
| 172 | if (packet == null) {
|
| 173 | return false;
|
| 174 | }
|
| 175 |
|
| 176 | if (isError(this.block.synthesis(packet))) {
|
| 177 | throw new IOException("Can't decode audio packet");
|
| 178 | }
|
| 179 |
|
| 180 | this.dspState.synthesis_blockin(this.block);
|
| 181 |
|
| 182 | int samples;
|
| 183 | while ((samples = this.dspState.synthesis_pcmout(pcmSampleOutput, pcmOffsetOutput)) > 0) {
|
| 184 | float[][] channelSamples = pcmSampleOutput[0];
|
| 185 | long samplesToWrite = this.getSamplesToWrite(samples);
|
| 186 | switch (this.info.channels) {
|
| 187 | case 1:
|
| 188 | copyMono(channelSamples[0], pcmOffsetOutput[0], samplesToWrite, consumer);
|
| 189 | break;
|
| 190 | case 2:
|
| 191 | copyStereo(channelSamples[0], pcmOffsetOutput[0], channelSamples[1], pcmOffsetOutput[1], samplesToWrite, consumer);
|
| 192 | break;
|
| 193 | default:
|
| 194 | copyAnyChannels(channelSamples, this.info.channels, pcmOffsetOutput, samplesToWrite, consumer);
|
| 195 | }
|
| 196 |
|
| 197 | this.dspState.synthesis_read(samples);
|
| 198 | }
|
| 199 |
|
| 200 | return true;
|
| 201 | }
|
| 202 |
|
| 203 | private static void copyAnyChannels(final float[][] samples, final int channelCount, final int[] offsets, final long count, final FloatConsumer output) {
|
| 204 | for (int j = 0; j < count; j++) {
|
| 205 | for (int channel = 0; channel < channelCount; channel++) {
|
| 206 | int offset = offsets[channel];
|
| 207 | float val = samples[channel][offset + j];
|
| 208 | output.accept(val);
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | private static void copyMono(final float[] samples, final int offset, final long count, final FloatConsumer output) {
|
| 214 | for (int i = offset; i < offset + count; i++) {
|
| 215 | output.accept(samples[i]);
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | private static void copyStereo(
|
| 220 | final float[] samples1, final int offset1, final float[] samples2, final int offset2, final long count, final FloatConsumer output
|
| 221 | ) {
|
| 222 | for (int i = 0; i < count; i++) {
|
| 223 | output.accept(samples1[offset1 + i]);
|
| 224 | output.accept(samples2[offset2 + i]);
|
| 225 | }
|
| 226 | }
|
| 227 |
|
| 228 | @Override
|
| 229 | public void close() throws IOException {
|
| 230 | this.input.close();
|
| 231 | }
|
| 232 | }
|
| 233 |
|