Search in sources :

Example 1 with AudioSink

use of pl.asie.charset.api.audio.AudioSink in project Charset by CharsetMC.

the class AudioUtils method send.

public static void send(int id, AudioPacket audio) {
    if (audio.getVolume() <= 0.0f) {
        return;
    }
    PacketAudioData packet = new PacketAudioData(id, audio);
    Map<WorldServer, Set<AudioSink>> worlds = new HashMap<>();
    for (AudioSink sink : audio.getSinks()) {
        if (sink.getVolume() <= 0.0f || sink.getDistance() <= 0.0f) {
            continue;
        }
        if (worlds.containsKey(sink.getWorld())) {
            worlds.get(sink.getWorld()).add(sink);
        } else {
            HashSet<AudioSink> sinkLocal = new HashSet<>();
            sinkLocal.add(sink);
            worlds.put((WorldServer) sink.getWorld(), sinkLocal);
        }
    }
    for (WorldServer world : worlds.keySet()) {
        for (EntityPlayerMP player : world.getMinecraftServer().getPlayerList().getPlayers()) {
            if (player.world.provider.getDimension() == world.provider.getDimension()) {
                for (AudioSink sink : worlds.get(world)) {
                    BlockPos pos = new BlockPos(sink.getPos());
                    if (world.getPlayerChunkMap().isPlayerWatchingChunk(player, pos.getX() >> 4, pos.getZ() >> 4)) {
                        CharsetLib.packet.sendTo(packet, player);
                        break;
                    }
                }
            }
        }
    }
}
Also used : AudioSink(pl.asie.charset.api.audio.AudioSink) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) WorldServer(net.minecraft.world.WorldServer) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) BlockPos(net.minecraft.util.math.BlockPos) HashSet(java.util.HashSet)

Example 2 with AudioSink

use of pl.asie.charset.api.audio.AudioSink in project Charset by CharsetMC.

the class PacketAudioData method apply.

@Override
public void apply(INetHandler handler) {
    AudioData audioData = packet.getData();
    if (audioData instanceof IDataGameSound) {
        IDataGameSound sound = (IDataGameSound) audioData;
        playSoundNote(packet, sound);
        return;
    }
    if (!(audioData instanceof IAudioDataPCM) || ((IAudioDataPCM) audioData).getSampleSize() != 1) {
        // Nope!
        return;
    }
    IAudioDataPCM pcmPacket = (IAudioDataPCM) audioData;
    byte[] data = pcmPacket.getSamplePCMData();
    if (pcmPacket.isSampleSigned()) {
        byte[] data2 = new byte[data.length];
        for (int i = 0; i < data.length; i++) {
            data2[i] = (byte) (data[i] ^ 0x80);
        }
        data = data2;
    }
    IAudioStream stream = AudioStreamManagerClient.INSTANCE.get(id);
    if (stream == null) {
        stream = new AudioStreamOpenAL(false, false, 8);
        AudioStreamManagerClient.INSTANCE.put(id, stream);
    }
    stream.setSampleRate(pcmPacket.getSampleRate());
    stream.push(data);
    for (AudioSink sink : packet.getSinks()) {
        try {
            stream.play((float) sink.getPos().x, (float) sink.getPos().y, (float) sink.getPos().z, sink.getDistance(), sink.getVolume() * packet.getVolume());
        } catch (Exception e) {
        }
    }
}
Also used : AudioSink(pl.asie.charset.api.audio.AudioSink) AudioData(pl.asie.charset.api.audio.AudioData) IAudioStream(pl.asie.charset.lib.audio.manager.IAudioStream) AudioStreamOpenAL(pl.asie.charset.lib.audio.manager.AudioStreamOpenAL) IAudioDataPCM(pl.asie.charset.api.audio.IAudioDataPCM) IDataGameSound(pl.asie.charset.lib.audio.types.IDataGameSound)

Aggregations

AudioSink (pl.asie.charset.api.audio.AudioSink)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 BlockPos (net.minecraft.util.math.BlockPos)1 WorldServer (net.minecraft.world.WorldServer)1 AudioData (pl.asie.charset.api.audio.AudioData)1 IAudioDataPCM (pl.asie.charset.api.audio.IAudioDataPCM)1 AudioStreamOpenAL (pl.asie.charset.lib.audio.manager.AudioStreamOpenAL)1 IAudioStream (pl.asie.charset.lib.audio.manager.IAudioStream)1 IDataGameSound (pl.asie.charset.lib.audio.types.IDataGameSound)1