-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix an issue in Audio transcoding where target-sample-rate is less than the source-sample-rate #111
Merged
izzytwosheds
merged 4 commits into
linkedin:main
from
Ma7moudHatem:fix-audio-transcoding-issue
Apr 2, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
55 changes: 55 additions & 0 deletions
55
litr/src/main/java/com/linkedin/android/litr/resample/AudioResampler.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,55 @@ | ||
/* | ||
* Copyright (C) 2021 natario1 Transcoder | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
// from: https://github.com/natario1/Transcoder/blob/main/lib/src/main/java/com/otaliastudios/transcoder/resample/AudioResampler.java | ||
// modified: removed all constant fields from this interface | ||
// modified: changed the signature of resample() method | ||
// modified: added two other default methods, getSampleRate() and getChannels() | ||
package com.linkedin.android.litr.resample; | ||
|
||
import android.media.MediaFormat; | ||
import androidx.annotation.NonNull; | ||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* Resamples audio data. See {@link DownsampleAudioResampler} for concrete implementations. | ||
*/ | ||
public interface AudioResampler { | ||
|
||
/** | ||
* Resamples input audio from input buffer into the output buffer. | ||
* | ||
* @param inputBuffer the input buffer | ||
* @param outputBuffer the output buffer | ||
* @param sourceMediaFormat the source media format | ||
* @param targetMediaFormat the target media format | ||
*/ | ||
void resample(@NonNull final ByteBuffer inputBuffer, @NonNull final ByteBuffer outputBuffer, | ||
@NonNull MediaFormat sourceMediaFormat, @NonNull MediaFormat targetMediaFormat); | ||
|
||
/** | ||
* Return the sample rate of an audio format | ||
*/ | ||
default int getSampleRate(MediaFormat format) { | ||
return format.getInteger(MediaFormat.KEY_SAMPLE_RATE); | ||
} | ||
|
||
/** | ||
* Return the width of the content in a video format. | ||
*/ | ||
default int getChannels(MediaFormat format) { | ||
return format.getInteger(MediaFormat.KEY_CHANNEL_COUNT); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
litr/src/main/java/com/linkedin/android/litr/resample/DownsampleAudioResampler.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,73 @@ | ||
/* | ||
* Copyright (C) 2021 natario1 Transcoder | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
// from: https://github.com/natario1/Transcoder/blob/main/lib/src/main/java/com/otaliastudios/transcoder/resample/DownsampleAudioResampler.java | ||
// modified: changed the signature of resample() method | ||
// modified: small modification for "outputBuffer.put(inputBuffer.get());" logic | ||
// modified: small modification for "inputBuffer.position(inputBuffer.position() + 1);" logic | ||
package com.linkedin.android.litr.resample; | ||
|
||
import android.media.MediaFormat; | ||
import androidx.annotation.NonNull; | ||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* An {@link AudioResampler} that downsamples from a higher sample rate to a lower sample rate. | ||
*/ | ||
public class DownsampleAudioResampler implements AudioResampler { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this adapted code from a different open source project? In that case we have to keep their copyright notice and add a note that we made modifications (feel free to add your name as author of modifications) |
||
|
||
private float ratio(int remaining, int all) { | ||
return (float) remaining / all; | ||
} | ||
|
||
@Override | ||
public void resample(@NonNull ByteBuffer inputBuffer, @NonNull ByteBuffer outputBuffer, | ||
@NonNull MediaFormat sourceMediaFormat, @NonNull MediaFormat targetMediaFormat) { | ||
int inputSampleRate = getSampleRate(sourceMediaFormat); | ||
int outputSampleRate = getSampleRate(targetMediaFormat); | ||
int channels = getChannels(targetMediaFormat); | ||
if (inputSampleRate < outputSampleRate) { | ||
throw new IllegalArgumentException("Illegal use of DownsampleAudioResampler"); | ||
} | ||
if (channels != 1 && channels != 2) { | ||
throw new IllegalArgumentException("Illegal use of DownsampleAudioResampler. Channels:" + channels); | ||
} | ||
final int inputSamples = inputBuffer.remaining() / channels; | ||
final int outputSamples = (int) Math.ceil(inputSamples * ((double) outputSampleRate / inputSampleRate)); | ||
final int dropSamples = inputSamples - outputSamples; | ||
int remainingOutputSamples = outputSamples; | ||
int remainingDropSamples = dropSamples; | ||
float remainingOutputSamplesRatio = ratio(remainingOutputSamples, outputSamples); | ||
float remainingDropSamplesRatio = ratio(remainingDropSamples, dropSamples); | ||
while (remainingOutputSamples > 0 || remainingDropSamples > 0) { | ||
// Will this be an input sample or a drop sample? | ||
// Choose the one with the bigger ratio. | ||
if (remainingOutputSamplesRatio >= remainingDropSamplesRatio) { | ||
for (int i = 0; i < channels; i++) { | ||
outputBuffer.put(inputBuffer.get()); | ||
} | ||
remainingOutputSamples--; | ||
remainingOutputSamplesRatio = ratio(remainingOutputSamples, outputSamples); | ||
} else { | ||
// Drop this - read from input without writing. | ||
for (int i = 0; i < channels; i++) { | ||
inputBuffer.position(inputBuffer.position() + 1); | ||
} | ||
remainingDropSamples--; | ||
remainingDropSamplesRatio = ratio(remainingDropSamples, dropSamples); | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
litr/src/main/java/com/linkedin/android/litr/resample/PassThroughAudioResampler.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,36 @@ | ||
/* | ||
* Copyright (C) 2021 natario1 Transcoder | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
// from: https://github.com/natario1/Transcoder/blob/main/lib/src/main/java/com/otaliastudios/transcoder/resample/PassThroughAudioResampler.java | ||
// modified: changed the signature of resample() method | ||
// modified: removed the first if-condition in resample() method | ||
package com.linkedin.android.litr.resample; | ||
|
||
import android.media.MediaFormat; | ||
import androidx.annotation.NonNull; | ||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* An {@link AudioResampler} that does nothing, meant to be used when sample | ||
* rates are identical. | ||
*/ | ||
public class PassThroughAudioResampler implements AudioResampler { | ||
|
||
@Override | ||
public void resample(@NonNull ByteBuffer inputBuffer, @NonNull ByteBuffer outputBuffer, | ||
@NonNull MediaFormat sourceMediaFormat, @NonNull MediaFormat targetMediaFormat) { | ||
outputBuffer.put(inputBuffer); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For all new classes that are not modified classes from other project please add LinkedIn copyright notice