Advertisement
TurtyWurty

SoundboardSound

Apr 28th, 2023
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. public class SoundboardSound extends AbstractSoundInstance {
  2.     public static final ResourceLocation SOUND_LOC = new ResourceLocation(SoundboardMod.MODID, "soundboard_sound");
  3.     private final Path soundPath;
  4.  
  5.     public SoundboardSound(Path soundPath, Vec3 position, float volume, float pitch) {
  6.         super(SOUND_LOC, SoundSource.MASTER, SoundInstance.createUnseededRandom());
  7.  
  8.         this.soundPath = soundPath;
  9.  
  10.         this.x = position.x;
  11.         this.y = position.y;
  12.         this.z = position.z;
  13.  
  14.         this.volume = volume;
  15.         this.pitch = pitch;
  16.     }
  17.  
  18.     @Override
  19.     public CompletableFuture<AudioStream> getStream(SoundBufferLibrary soundBuffers, Sound sound, boolean looping) {
  20.         try {
  21.             return CompletableFuture.completedFuture(new SoundboardStream(this.soundPath));
  22.         } catch (IOException exception) {
  23.             return CompletableFuture.failedFuture(exception);
  24.         }
  25.     }
  26.  
  27.     public static class SoundboardStream implements AudioStream {
  28.         private static final AudioFormat AUDIO_FORMAT = new AudioFormat(44100, 16, 2, true, false);
  29.  
  30.         private final byte[] bytes;
  31.  
  32.         public SoundboardStream(Path soundPath) throws IOException {
  33.             this.bytes = Files.readAllBytes(soundPath);
  34.         }
  35.  
  36.         @Override
  37.         public AudioFormat getFormat() {
  38.             return AUDIO_FORMAT;
  39.         }
  40.  
  41.         @Override
  42.         public ByteBuffer read(int pSize) {
  43.             ByteBuffer buffer = ByteBuffer.allocate(pSize);
  44.  
  45.             for (int index = 0; index < pSize; index++) {
  46.                 buffer.put(this.bytes[index % this.bytes.length]);
  47.             }
  48.  
  49.             return buffer;
  50.         }
  51.  
  52.         @Override
  53.         public void close() {}
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement