-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9ccb4c
commit 39974ba
Showing
7 changed files
with
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package dev.kord.voice | ||
|
||
import dev.kord.common.annotation.KordVoice | ||
import dev.kord.voice.gateway.SendSpeaking | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onEach | ||
|
||
@KordVoice | ||
/** | ||
* Data that is used to configure for the lifetime of a [DefaultFrameInterceptor]. | ||
* | ||
* @param speakingState the [SpeakingFlags] to be sent when there audio being sent. By default, it is [microphone-only][SpeakingFlag.Microphone]. | ||
*/ | ||
public data class DefaultFrameInterceptorData( | ||
val speakingState: SpeakingFlags = SpeakingFlags { +SpeakingFlag.Microphone } | ||
) | ||
|
||
private const val FRAMES_OF_SILENCE_TO_PLAY = 5 | ||
|
||
@KordVoice | ||
/** | ||
* The default implementation for [FrameInterceptor]. | ||
* Any custom implementation should extend this and call the super [intercept] method, or else | ||
* the speaking flags will not be sent! | ||
* | ||
* @param data the data to configure this instance with. | ||
*/ | ||
public class DefaultFrameInterceptor(private val data: DefaultFrameInterceptorData = DefaultFrameInterceptorData()) : | ||
FrameInterceptor { | ||
override fun Flow<AudioFrame?>.intercept(configuration: FrameInterceptorConfiguration): Flow<AudioFrame?> { | ||
var framesOfSilence = 5 | ||
var isSpeaking = false | ||
|
||
suspend fun startSpeaking() { | ||
isSpeaking = true | ||
configuration.voiceGateway.send(SendSpeaking(data.speakingState, 0, configuration.ssrc)) | ||
} | ||
|
||
suspend fun stopSpeaking() { | ||
isSpeaking = false | ||
configuration.voiceGateway.send(SendSpeaking(SpeakingFlags(0), 0, configuration.ssrc)) | ||
} | ||
|
||
return map { frame -> | ||
when (framesOfSilence) { | ||
0 -> frame | ||
else -> frame ?: AudioFrame.SILENCE | ||
} | ||
}.onEach { frame -> | ||
if (frame != null && !isSpeaking) { | ||
startSpeaking() | ||
} else if (frame == null) { | ||
if (--framesOfSilence == 0) | ||
stopSpeaking() | ||
} else { | ||
framesOfSilence = FRAMES_OF_SILENCE_TO_PLAY | ||
} | ||
} | ||
} | ||
} |
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
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