-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create multiple encoders for each sound.
- Loading branch information
Showing
9 changed files
with
124 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 56 additions & 44 deletions
100
src/main/java/com/ubivashka/plasmovoice/audio/codecs/OpusCodecHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,65 @@ | ||
package com.ubivashka.plasmovoice.audio.codecs; | ||
|
||
import javax.sound.sampled.AudioFormat; | ||
|
||
import com.ubivashka.plasmovoice.function.MemoizingSupplier; | ||
import com.ubivashka.plasmovoice.opus.OpusDecoder; | ||
import com.ubivashka.plasmovoice.opus.OpusEncoder; | ||
|
||
import de.maxhenkel.opus4j.Opus; | ||
|
||
import javax.sound.sampled.AudioFormat; | ||
import java.util.function.Supplier; | ||
|
||
public class OpusCodecHolder extends AbstractCodecHolder { | ||
private static final int MTU_SIZE = 1024; | ||
private static final int JOPUS_MODE = Opus.OPUS_APPLICATION_VOIP; | ||
|
||
private OpusEncoder encoder = new OpusEncoder(sampleRate, frameSize, MTU_SIZE, JOPUS_MODE); | ||
private OpusDecoder decoder = new OpusDecoder(sampleRate, frameSize, MTU_SIZE); | ||
|
||
public OpusEncoder getEncoder() { | ||
return encoder; | ||
} | ||
|
||
public void setEncoder(OpusEncoder encoder) { | ||
this.encoder = encoder; | ||
} | ||
|
||
public OpusDecoder getDecoder() { | ||
return decoder; | ||
} | ||
|
||
public void setDecoder(OpusDecoder decoder) { | ||
this.decoder = decoder; | ||
} | ||
|
||
@Override | ||
public byte[] encode(byte[] data) { | ||
return encoder.encode(data); | ||
} | ||
|
||
@Override | ||
public byte[] decode(byte[] data) { | ||
return decoder.decode(data); | ||
} | ||
|
||
@Override | ||
public void setSampleRate(int sampleRate) { | ||
this.sampleRate = sampleRate; | ||
this.audioFormat = new AudioFormat(sampleRate, 16, 1, true, false); | ||
this.frameSize = (sampleRate / 1000) * 2 * 20; | ||
|
||
this.encoder = new OpusEncoder(sampleRate, frameSize, MTU_SIZE, JOPUS_MODE); | ||
this.decoder = new OpusDecoder(sampleRate, frameSize, MTU_SIZE); | ||
} | ||
private static final int MTU_SIZE = 1024; | ||
private static final int JOPUS_MODE = Opus.OPUS_APPLICATION_VOIP; | ||
|
||
private Supplier<OpusEncoder> lazyEncoder = MemoizingSupplier.memoize(() -> new OpusEncoder(sampleRate, frameSize, MTU_SIZE, JOPUS_MODE)); | ||
private Supplier<OpusDecoder> lazyDecoder = MemoizingSupplier.memoize(() -> new OpusDecoder(sampleRate, frameSize, MTU_SIZE)); | ||
|
||
public OpusEncoder getEncoder() { | ||
return lazyEncoder.get(); | ||
} | ||
|
||
public OpusDecoder getDecoder() { | ||
return lazyDecoder.get(); | ||
} | ||
|
||
@Override | ||
public byte[] encode(byte[] data) { | ||
return getEncoder().encode(data); | ||
} | ||
|
||
@Override | ||
public byte[] decode(byte[] data) { | ||
return getDecoder().decode(data); | ||
} | ||
|
||
@Override | ||
public void resetEncoder() { | ||
getEncoder().reset(); | ||
} | ||
|
||
@Override | ||
public void closeEncoder() { | ||
getEncoder().close(); | ||
} | ||
|
||
public void resetDecoder() { | ||
getDecoder().reset(); | ||
} | ||
|
||
@Override | ||
public void closeDecoder() { | ||
getDecoder().close(); | ||
} | ||
|
||
@Override | ||
public void setSampleRate(int sampleRate) { | ||
this.sampleRate = sampleRate; | ||
this.audioFormat = new AudioFormat(sampleRate, 16, 1, true, false); | ||
this.frameSize = (sampleRate / 1000) * 2 * 20; | ||
|
||
lazyEncoder = MemoizingSupplier.memoize(() -> new OpusEncoder(sampleRate, frameSize, MTU_SIZE, JOPUS_MODE)); | ||
lazyDecoder = MemoizingSupplier.memoize(() -> new OpusDecoder(sampleRate, frameSize, MTU_SIZE)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/ubivashka/plasmovoice/function/MemoizingSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.ubivashka.plasmovoice.function; | ||
|
||
import java.io.Serializable; | ||
import java.util.function.Supplier; | ||
|
||
public final class MemoizingSupplier<T> implements Supplier<T>, Serializable { | ||
|
||
final Supplier<T> delegate; | ||
transient volatile boolean initialized; | ||
// "value" does not need to be volatile; visibility piggy-backs | ||
// on volatile read of "initialized". | ||
transient T value; | ||
|
||
MemoizingSupplier(Supplier<T> delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public T get() { | ||
// A 2-field variant of Double Checked Locking. | ||
if (!initialized) { | ||
synchronized (this) { | ||
if (!initialized) { | ||
T t = delegate.get(); | ||
value = t; | ||
initialized = true; | ||
return t; | ||
} | ||
} | ||
} | ||
return value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Suppliers.memoize(" + delegate + ")"; | ||
} | ||
|
||
private static final long serialVersionUID = 0; | ||
|
||
public static <T> Supplier<T> memoize(Supplier<T> supplier) { | ||
return new MemoizingSupplier<>(supplier); | ||
} | ||
|
||
} |
9 changes: 4 additions & 5 deletions
9
src/main/java/com/ubivashka/plasmovoice/opus/OpusEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters