| 1 | package com.mojang.blaze3d.audio;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import javax.sound.sampled.AudioFormat;
|
| 5 | import javax.sound.sampled.AudioFormat.Encoding;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import org.lwjgl.openal.AL10;
|
| 9 | import org.lwjgl.openal.ALC10;
|
| 10 | import org.slf4j.Logger;
|
| 11 |
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public class OpenAlUtil {
|
| 14 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 15 |
|
| 16 | private static String alErrorToString(final int error) {
|
| 17 | return switch (error) {
|
| 18 | case 40961 -> "Invalid name parameter.";
|
| 19 | case 40962 -> "Invalid enumerated parameter value.";
|
| 20 | case 40963 -> "Invalid parameter parameter value.";
|
| 21 | case 40964 -> "Invalid operation.";
|
| 22 | case 40965 -> "Unable to allocate memory.";
|
| 23 | default -> "An unrecognized error occurred.";
|
| 24 | };
|
| 25 | }
|
| 26 |
|
| 27 | public static boolean checkALError(final String location) {
|
| 28 | int error = AL10.alGetError();
|
| 29 | if (error != 0) {
|
| 30 | LOGGER.error("{}: {}", location, alErrorToString(error));
|
| 31 | return true;
|
| 32 | } else {
|
| 33 | return false;
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | private static String alcErrorToString(final int error) {
|
| 38 | return switch (error) {
|
| 39 | case 40961 -> "Invalid device.";
|
| 40 | case 40962 -> "Invalid context.";
|
| 41 | case 40963 -> "Illegal enum.";
|
| 42 | case 40964 -> "Invalid value.";
|
| 43 | case 40965 -> "Unable to allocate memory.";
|
| 44 | default -> "An unrecognized error occurred.";
|
| 45 | };
|
| 46 | }
|
| 47 |
|
| 48 | public static boolean checkALCError(final long device, final String location) {
|
| 49 | int error = ALC10.alcGetError(device);
|
| 50 | if (error != 0) {
|
| 51 | LOGGER.error("{} ({}): {}", location, device, alcErrorToString(error));
|
| 52 | return true;
|
| 53 | } else {
|
| 54 | return false;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | public static int audioFormatToOpenAl(final AudioFormat audioFormat) {
|
| 59 | Encoding encoding = audioFormat.getEncoding();
|
| 60 | int channels = audioFormat.getChannels();
|
| 61 | int sampleSizeInBits = audioFormat.getSampleSizeInBits();
|
| 62 | if (encoding.equals(Encoding.PCM_UNSIGNED) || encoding.equals(Encoding.PCM_SIGNED)) {
|
| 63 | if (channels == 1) {
|
| 64 | if (sampleSizeInBits == 8) {
|
| 65 | return 4352;
|
| 66 | }
|
| 67 |
|
| 68 | if (sampleSizeInBits == 16) {
|
| 69 | return 4353;
|
| 70 | }
|
| 71 | } else if (channels == 2) {
|
| 72 | if (sampleSizeInBits == 8) {
|
| 73 | return 4354;
|
| 74 | }
|
| 75 |
|
| 76 | if (sampleSizeInBits == 16) {
|
| 77 | return 4355;
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | throw new IllegalArgumentException("Invalid audio format: " + audioFormat);
|
| 83 | }
|
| 84 | }
|
| 85 |
|