diff options
author | Andrew Comminos <andrew@morlunk.com> | 2015-06-20 05:43:41 +0300 |
---|---|---|
committer | Andrew Comminos <andrew@morlunk.com> | 2015-06-20 05:43:41 +0300 |
commit | 47d4f0c31ad55a2f438353e4dd767842a26c3309 (patch) | |
tree | 3a209f0a6387d53d9474af40a66a9161fa858806 | |
parent | a8803b14e30b46fb38007976b1a6af800f84bea0 (diff) | |
parent | 5488e0ab23ceac0b6ad28e05e0be725bac984d5a (diff) |
Merge pull request #5 from BuddyButterfly/mixfix
Fixed clipping when mixing.
-rw-r--r-- | src/main/java/com/morlunk/jumble/audio/AudioOutput.java | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/src/main/java/com/morlunk/jumble/audio/AudioOutput.java b/src/main/java/com/morlunk/jumble/audio/AudioOutput.java index 169c3f4..3037188 100644 --- a/src/main/java/com/morlunk/jumble/audio/AudioOutput.java +++ b/src/main/java/com/morlunk/jumble/audio/AudioOutput.java @@ -189,15 +189,28 @@ public class AudioOutput implements Runnable, AudioOutputSpeech.TalkStateListene } if(!mMixBuffer.isEmpty()) { + + /* Declare temp vars here to avoid creation/destructions within loops */ + short pcm = 0; + int pcm_tmp = 0; + for(AudioOutputSpeech.Result result : mMixBuffer) { float[] buffer = result.getSamples(); for(int i = 0; i < BUFFER_SIZE; i++) { - short pcm = (short) (buffer[i]*Short.MAX_VALUE); // Convert float to short - pcm = pcm <= Short.MAX_VALUE ? (pcm >= Short.MIN_VALUE ? pcm : Short.MIN_VALUE) : Short.MIN_VALUE; // Clip audio - outBuffer[i] += pcm; + + /* @TODO replace with higher quality mixing algorithm (better quality and maintaining loudness) */ + + /* Convert float to short. float pcm values are in range [-1.0,1.0] */ + pcm = (short) (buffer[i]*Short.MAX_VALUE); + + pcm_tmp = outBuffer[i] + pcm; + + /* Clip audio */ + outBuffer[i] = pcm_tmp <= Short.MAX_VALUE ? (pcm_tmp >= Short.MIN_VALUE ? (short) pcm_tmp : Short.MIN_VALUE) : Short.MAX_VALUE; } } } + for(AudioOutputSpeech.Result result : mDelBuffer) { AudioOutputSpeech speech = result.getSpeechOutput(); Log.v(Constants.TAG, "Deleted audio user "+speech.getUser().getName()); |