forked from natario1/Transcoder
-
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.
Add an example of AudioPostProcessor to mix the audio of 2 data sources.
- Loading branch information
Christian Bernier
committed
Apr 13, 2020
1 parent
aebb3c1
commit 68198ce
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...c/main/java/com/otaliastudios/transcoder/postprocessor/MixerSourceAudioPostProcessor.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,28 @@ | ||
package com.otaliastudios.transcoder.postprocessor; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.nio.ShortBuffer; | ||
import java.util.ArrayDeque; | ||
import java.util.Queue; | ||
|
||
public class MixerSourceAudioPostProcessor implements AudioPostProcessor { | ||
|
||
Queue<ShortBuffer> mBuffers = new ArrayDeque<>(); | ||
|
||
@Override | ||
public long calculateNewDurationUs(long durationUs) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public long postProcess(@NonNull ShortBuffer inputBuffer, @NonNull ShortBuffer outputBuffer, long bufferDurationUs) { | ||
if (inputBuffer.remaining() > 0) { | ||
ShortBuffer sourceBuffer = ShortBuffer.allocate(inputBuffer.limit()); | ||
sourceBuffer.put(inputBuffer); | ||
sourceBuffer.rewind(); | ||
mBuffers.add(sourceBuffer); | ||
} | ||
return 0; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...c/main/java/com/otaliastudios/transcoder/postprocessor/MixerTargetAudioPostProcessor.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,54 @@ | ||
package com.otaliastudios.transcoder.postprocessor; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.nio.ShortBuffer; | ||
|
||
import static java.lang.Math.min; | ||
|
||
public class MixerTargetAudioPostProcessor implements AudioPostProcessor { | ||
private MixerSourceAudioPostProcessor mSource; | ||
private float mSourceVolume; | ||
private float mTargetVolume; | ||
|
||
@Override | ||
public long calculateNewDurationUs(long durationUs) { | ||
return durationUs; | ||
} | ||
|
||
public MixerTargetAudioPostProcessor(MixerSourceAudioPostProcessor source, float sourceVolume, float targetVolume) | ||
{ | ||
mSource = source; | ||
mSourceVolume = sourceVolume; | ||
mTargetVolume = targetVolume; | ||
} | ||
|
||
private short mixSamples(short sourceSample, short targetSample) { | ||
float mixedSample = (sourceSample * mSourceVolume) + (targetSample * mTargetVolume); | ||
if (mixedSample < Short.MIN_VALUE) | ||
mixedSample = Short.MIN_VALUE; | ||
else if (mixedSample > Short.MAX_VALUE) | ||
mixedSample = Short.MAX_VALUE; | ||
return (short)mixedSample; | ||
} | ||
|
||
@Override | ||
public long postProcess(@NonNull ShortBuffer inputBuffer, @NonNull ShortBuffer outputBuffer, long bufferDurationUs) { | ||
ShortBuffer sourceBuffer = mSource.mBuffers.peek(); | ||
int inputRemaining = inputBuffer.remaining(); | ||
while (sourceBuffer != null && inputRemaining > 0) { | ||
int sourceRemaining = sourceBuffer.remaining(); | ||
int remaining = min(inputRemaining, sourceRemaining); | ||
for (int i=0; i<remaining; i++) { | ||
outputBuffer.put(mixSamples(sourceBuffer.get(), inputBuffer.get())); | ||
} | ||
inputRemaining -= sourceRemaining; | ||
if (inputRemaining >= 0) { | ||
mSource.mBuffers.remove(); | ||
sourceBuffer = mSource.mBuffers.peek(); | ||
} | ||
} | ||
outputBuffer.put(inputBuffer); | ||
return bufferDurationUs; | ||
} | ||
} |