Skip to content

Instantly share code, notes, and snippets.

@maxxfrazer
Last active December 6, 2021 10:44
Show Gist options
  • Save maxxfrazer/549427c6e95921b6114f84a072269adb to your computer and use it in GitHub Desktop.
Save maxxfrazer/549427c6e95921b6114f84a072269adb to your computer and use it in GitHub Desktop.
struct ContentView: View {
@State var joinedChannel: Bool = false
static var agview: AgoraViewer = {
AgoraViewer(
connectionData: AgoraConnectionData(
appId: AppKeys.agoraAppId
),
style: .floating
)
}()
var body: some View {
ZStack {
VideoCallView(joinedChannel: $joinedChannel)
if !joinedChannel {
Button("Join Channel") {
self.joinChannel()
}
}
}
}
func joinChannel() {
self.joinedChannel = true
ContentView.agview.join(
channel: "test", with: AppKeys.agoraToken,
as: .broadcaster
)
}
}
enum VoiceEffect {
case echo(EchoVoiceEffectConfiguration)
case reverb(ReverbVoiceEffectConfiguration)
case flanger(FlangerVoiceEffectConfiguration)
case pitch(PitchShiftVoiceEffectConfiguration)
}
func getVoiceFilter(from name: String) -> VoiceEffect? {
if name == "echo" {
return .echo(EchoVoiceEffectConfiguration(mix: 1.0))
} else if name == "reverb" {
return .reverb(ReverbVoiceEffectConfiguration(
dry: 0.9, wet: 0.3, mix: 0.6, width: 1.0,
damp: 1.0, roomSize: 1.0, preDelay: 300.0, lowCut: 5.0
))
} else if name == "flanger" {
return .flanger(FlangerVoiceEffectConfiguration(
wet: 1.0, depth: 1.0, lfoBeats: 30.0, bpm: 100.0, stereo: true
))
} else if name == "pitch shift" {
return .pitch(PitchShiftVoiceEffectConfiguration(shift: 500))
}
return nil
}
VStack {
HStack {
Spacer()
Menu {
ForEach(values: names) { name in
Button {
selection = name
} label: {
Text(name)
}
}
if selection != nil {
Button {
selection = nil
} label: {
Text("Clear")
Spacer()
Image(systemName: "clear")
}
}
} label: {
Image(systemName: "speaker.wave.3")
Text(selection ?? "Select a Voice Effect")
}.padding(3).background(Color.black)
.cornerRadius(3.0)
.onChange(of: selection, perform: { value in
// update value for voice FX
self.setVoiceFxParam(to: selection ?? "null")
})
Spacer()
if selection != nil {
Button {
selection = nil
} label: {
Image(systemName: "xmark.circle")
}.padding(.trailing, 10)
}
}
Spacer()
}
struct VideoCallView: View {
// other content...
@State var voiceFxRegistered = false
func setVoiceFxParam(to voiceName: String) {
if !self.voiceFxRegistered {
self.registerVoiceFX()
}
// Apply Voice Effects...
}
func registerVoiceFX() {
// Set API Credentials
ContentView.agview.viewer.setExtensionProperty(
VoiceFiltersAgoraExtensionVendor, extension: VoiceFiltersAgoraExtensionName,
key: VoiceFiltersAgoraExtensionApiKey, value: AppKeys.voiceFxApiKey
)
ContentView.agview.viewer.setExtensionProperty(
VoiceFiltersAgoraExtensionVendor, extension: VoiceFiltersAgoraExtensionName,
key: VoiceFiltersAgoraExtensionApiSecret, value: AppKeys.voiceFxApiSecret
)
ContentView.agview.viewer.enableExtension(
withVendor: VoiceFiltersAgoraExtensionVendor,
extension: VoiceFiltersAgoraExtensionName, enabled: true
)
self.voiceFxRegistered = true
}
}
struct VideoCallView: View {
// other content...
static let voiceFilter: SynervozVoiceFilter = SynervozVoiceFilter()
func setVoiceFxParam(to voiceName: String) {
if !self.voiceFxRegistered {
self.registerVoiceFX()
}
let newVoice = self.getVoiceFilter(from: voiceName)
let agoraEngine = ContentView.agview.viewer.agkit
switch newVoice {
case .echo(let echoConfig):
VideoCallView.voiceFilter.setEcho(echoConfig, inEngine: agoraEngine)
case .reverb(let reverbConfig):
VideoCallView.voiceFilter.setReverbConfiguration(reverbConfig, inEngine: agoraEngine)
case .flanger(let flangerConfig):
VideoCallView.voiceFilter.setFlangerConfiguration(flangerConfig, inEngine: agoraEngine)
case .pitch(let pitchShiftConfig):
VideoCallView.voiceFilter.setPitchShiftConfiguration(pitchShiftConfig, inEngine: agoraEngine)
case .none: break
}
VideoCallView.voiceFilter.enableEcho("echo" == voiceName, inEngine: agoraEngine)
VideoCallView.voiceFilter.enableReverb("reverb" == voiceName, inEngine: agoraEngine)
VideoCallView.voiceFilter.enableFlanger("flanger" == voiceName, inEngine: agoraEngine)
VideoCallView.voiceFilter.enablePitchShift("pitch shift" == voiceName, inEngine: agoraEngine)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment