-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: native audio processing apis and various useTrackVolume hooks
- Loading branch information
Showing
47 changed files
with
2,498 additions
and
125 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
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
6 changes: 6 additions & 0 deletions
6
android/src/main/java/com/livekit/reactnative/audio/events/Events.kt
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,6 @@ | ||
package com.livekit.reactnative.audio.events | ||
|
||
enum class Events { | ||
LK_VOLUME_PROCESSED, | ||
LK_MULTIBAND_PROCESSED, | ||
} |
2 changes: 2 additions & 0 deletions
2
android/src/main/java/com/livekit/reactnative/audio/processing/AudioFormat.kt
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,2 @@ | ||
package com.livekit.reactnative.audio.processing | ||
data class AudioFormat(val bitsPerSample: Int, val sampleRate: Int, val numberOfChannels: Int) |
27 changes: 27 additions & 0 deletions
27
android/src/main/java/com/livekit/reactnative/audio/processing/AudioProcessingController.kt
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,27 @@ | ||
package com.livekit.reactnative.audio.processing | ||
|
||
/** | ||
* Interface for controlling external audio processing. | ||
*/ | ||
interface AudioProcessingController { | ||
/** | ||
* the audio processor to be used for capture post processing. | ||
*/ | ||
var capturePostProcessor: AudioProcessorInterface? | ||
|
||
/** | ||
* the audio processor to be used for render pre processing. | ||
*/ | ||
var renderPreProcessor: AudioProcessorInterface? | ||
|
||
/** | ||
* whether to bypass mode the render pre processing. | ||
*/ | ||
var bypassRenderPreProcessing: Boolean | ||
|
||
/** | ||
* whether to bypass the capture post processing. | ||
*/ | ||
var bypassCapturePostProcessing: Boolean | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
android/src/main/java/com/livekit/reactnative/audio/processing/AudioProcessorInterface.kt
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,52 @@ | ||
package com.livekit.reactnative.audio.processing | ||
|
||
import java.nio.ByteBuffer | ||
|
||
/** | ||
* Interface for external audio processing. | ||
*/ | ||
interface AudioProcessorInterface { | ||
/** | ||
* Check if the audio processing is enabled. | ||
*/ | ||
fun isEnabled(): Boolean | ||
|
||
/** | ||
* Get the name of the audio processing. | ||
*/ | ||
fun getName(): String | ||
|
||
/** | ||
* Initialize the audio processing. | ||
* | ||
* Note: audio processing methods will be called regardless of whether | ||
* [isEnabled] returns true or not. | ||
*/ | ||
fun initializeAudioProcessing(sampleRateHz: Int, numChannels: Int) | ||
|
||
/** | ||
* Called when the sample rate has changed. | ||
* | ||
* Note: audio processing methods will be called regardless of whether | ||
* [isEnabled] returns true or not. | ||
*/ | ||
fun resetAudioProcessing(newRate: Int) | ||
|
||
/** | ||
* Process the audio frame (10ms). | ||
* | ||
* Note: audio processing methods will be called regardless of whether | ||
* [isEnabled] returns true or not. | ||
*/ | ||
fun processAudio(numBands: Int, numFrames: Int, buffer: ByteBuffer) | ||
} | ||
|
||
/** | ||
* @suppress | ||
*/ | ||
interface AuthedAudioProcessorInterface : AudioProcessorInterface { | ||
/** | ||
* @suppress | ||
*/ | ||
fun authenticate(url: String, token: String) | ||
} |
72 changes: 72 additions & 0 deletions
72
...id/src/main/java/com/livekit/reactnative/audio/processing/AudioRecordSamplesDispatcher.kt
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,72 @@ | ||
/* | ||
* Copyright 2024 LiveKit, Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.livekit.reactnative.audio.processing | ||
|
||
import android.media.AudioFormat | ||
import android.os.SystemClock | ||
import org.webrtc.AudioTrackSink | ||
import org.webrtc.audio.JavaAudioDeviceModule | ||
import java.nio.ByteBuffer | ||
|
||
/** | ||
* Dispatches recorded audio samples from the local microphone. | ||
*/ | ||
class AudioRecordSamplesDispatcher : JavaAudioDeviceModule.SamplesReadyCallback { | ||
|
||
private val sinks = mutableSetOf<AudioTrackSink>() | ||
|
||
@Synchronized | ||
fun registerSink(sink: AudioTrackSink) { | ||
sinks.add(sink) | ||
} | ||
|
||
@Synchronized | ||
fun unregisterSink(sink: AudioTrackSink) { | ||
sinks.remove(sink) | ||
} | ||
|
||
// Reference from Android code, AudioFormat.getBytesPerSample. BitPerSample / 8 | ||
// Default audio data format is PCM 16 bits per sample. | ||
// Guaranteed to be supported by all devices | ||
private fun getBytesPerSample(audioFormat: Int): Int { | ||
return when (audioFormat) { | ||
AudioFormat.ENCODING_PCM_8BIT -> 1 | ||
AudioFormat.ENCODING_PCM_16BIT, AudioFormat.ENCODING_IEC61937, AudioFormat.ENCODING_DEFAULT -> 2 | ||
AudioFormat.ENCODING_PCM_FLOAT -> 4 | ||
AudioFormat.ENCODING_INVALID -> throw IllegalArgumentException("Bad audio format $audioFormat") | ||
else -> throw IllegalArgumentException("Bad audio format $audioFormat") | ||
} | ||
} | ||
|
||
@Synchronized | ||
override fun onWebRtcAudioRecordSamplesReady(samples: JavaAudioDeviceModule.AudioSamples) { | ||
val bitsPerSample = getBytesPerSample(samples.audioFormat) * 8 | ||
val numFrames = samples.sampleRate / 100 // 10ms worth of samples. | ||
val timestamp = SystemClock.elapsedRealtime() | ||
for (sink in sinks) { | ||
val byteBuffer = ByteBuffer.wrap(samples.data) | ||
sink.onData( | ||
byteBuffer, | ||
bitsPerSample, | ||
samples.sampleRate, | ||
samples.channelCount, | ||
numFrames, | ||
timestamp, | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.