/* * Decompiled with CFR 0.152. */ package de.maxhenkel.voicechat.plugins.impl.opus; import de.maxhenkel.voicechat.concentus.OpusApplication; import de.maxhenkel.voicechat.concentus.OpusEncoder; public class JavaOpusEncoderImpl implements de.maxhenkel.voicechat.api.opus.OpusEncoder { protected OpusEncoder opusEncoder; protected byte[] buffer; protected int sampleRate; protected int frameSize; protected int maxPayloadSize; protected OpusApplication application; public JavaOpusEncoderImpl(int sampleRate, int frameSize, int maxPayloadSize, OpusApplication application) { this.sampleRate = sampleRate; this.frameSize = frameSize; this.maxPayloadSize = maxPayloadSize; this.application = application; this.buffer = new byte[maxPayloadSize]; this.open(); } private void open() { if (this.opusEncoder != null) { return; } try { this.opusEncoder = new OpusEncoder(this.sampleRate, 1, this.application); this.opusEncoder.setUseInbandFEC(true); this.opusEncoder.setPacketLossPercent(5); } catch (Exception e) { throw new IllegalStateException("Opus encoder error " + e.getMessage()); } } @Override public byte[] encode(short[] rawAudio) { int result; if (this.isClosed()) { throw new IllegalStateException("Encoder is closed"); } try { result = this.opusEncoder.encode(rawAudio, 0, this.frameSize, this.buffer, 0, this.buffer.length); } catch (Exception e) { throw new RuntimeException("Failed to encode audio data: " + e.getMessage()); } if (result < 0) { throw new RuntimeException("Failed to encode audio data"); } byte[] audio = new byte[result]; System.arraycopy(this.buffer, 0, audio, 0, result); return audio; } @Override public void resetState() { if (this.isClosed()) { throw new IllegalStateException("Encoder is closed"); } this.opusEncoder.resetState(); } @Override public boolean isClosed() { return this.opusEncoder == null; } @Override public void close() { if (this.isClosed()) { return; } this.opusEncoder = null; } }