Skip to content
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

Audio processing examples #22

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"repositoryURL": "https://github.com/google/promises.git",
"state": {
"branch": null,
"revision": "ec957ccddbcc710ccc64c9dcbd4c7006fcf8b73a",
"version": "2.2.0"
"revision": "e70e889c0196c76d22759eb50d6a0270ca9f1d9e",
"version": "2.3.1"
}
},
{
Expand All @@ -32,27 +32,27 @@
"package": "WebRTC",
"repositoryURL": "https://github.com/webrtc-sdk/Specs.git",
"state": {
"branch": null,
"revision": "2f6bab30c8df0fe59ab3e58bc99097f757f85f65",
"version": "104.5112.17"
"branch": "audio-processing",
"revision": "e92a92cfdaec60c1b89bb94969d8009507a107ae",
"version": null
}
},
{
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "32e8d724467f8fe623624570367e3d50c5638e46",
"version": "1.5.2"
"revision": "532d8b529501fb73a2455b179e0bbb6d49b652ed",
"version": "1.5.3"
}
},
{
"package": "SwiftProtobuf",
"repositoryURL": "https://github.com/apple/swift-protobuf.git",
"state": {
"branch": null,
"revision": "0af9125c4eae12a4973fb66574c53a54962a9e1e",
"version": "1.21.0"
"revision": "ce20dc083ee485524b802669890291c0d8090170",
"version": "1.22.1"
}
}
]
Expand Down
22 changes: 18 additions & 4 deletions LiveKitExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
68B3854C271E780700711D5F /* LiveKitExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68B3853C271E780600711D5F /* LiveKitExample.swift */; };
68B3854D271E780700711D5F /* LiveKitExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68B3853C271E780600711D5F /* LiveKitExample.swift */; };
68EA18E827F2E91000F9AE48 /* BroadcastExt.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 683F05F3273F96B20080C7AC /* BroadcastExt.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
68FC569C2A8AB369004B4989 /* SimpleReverbAudioCapturePostProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68FC569B2A8AB369004B4989 /* SimpleReverbAudioCapturePostProcessor.swift */; };
68FC569D2A8AB586004B4989 /* SimpleReverbAudioCapturePostProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68FC569B2A8AB369004B4989 /* SimpleReverbAudioCapturePostProcessor.swift */; };
D7AA477B285A0FFC00EB41AE /* SampleHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7AA477A285A0FFC00EB41AE /* SampleHandler.swift */; };
D7FECD462860B42700D04D1C /* LoggingOSLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7FECD452860B42700D04D1C /* LoggingOSLog.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -96,6 +98,7 @@
68B38549271E780700711D5F /* LiveKitExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LiveKitExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
68B3854B271E780700711D5F /* macOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = macOS.entitlements; sourceTree = "<group>"; };
68DEF27E2919EEFA00258494 /* ReplayKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ReplayKit.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.1.sdk/System/Library/Frameworks/ReplayKit.framework; sourceTree = DEVELOPER_DIR; };
68FC569B2A8AB369004B4989 /* SimpleReverbAudioCapturePostProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleReverbAudioCapturePostProcessor.swift; sourceTree = "<group>"; };
9E7835E62751A71500559DEC /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.0.sdk/System/Library/Frameworks/CoreGraphics.framework; sourceTree = DEVELOPER_DIR; };
D7AA477A285A0FFC00EB41AE /* SampleHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SampleHandler.swift; sourceTree = "<group>"; };
D7FECD452860B42700D04D1C /* LoggingOSLog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggingOSLog.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -207,6 +210,7 @@
68B3853B271E780600711D5F /* Shared */ = {
isa = PBXGroup;
children = (
68FC569A2A8AB35D004B4989 /* AudioProcessors */,
683720D427A0640D007DA986 /* Support */,
6884B77A2750505B00732D47 /* Views */,
685271EA27443907006B4D6A /* Controllers */,
Expand Down Expand Up @@ -240,6 +244,14 @@
path = macOS;
sourceTree = "<group>";
};
68FC569A2A8AB35D004B4989 /* AudioProcessors */ = {
isa = PBXGroup;
children = (
68FC569B2A8AB369004B4989 /* SimpleReverbAudioCapturePostProcessor.swift */,
);
path = AudioProcessors;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -402,6 +414,7 @@
683720D227A06404007DA986 /* ConnectionHistory.swift in Sources */,
681E3F43271FC7AD007BB547 /* RoomView.swift in Sources */,
6816B1B0272D9198005ADB85 /* ParticipantView.swift in Sources */,
68FC569C2A8AB369004B4989 /* SimpleReverbAudioCapturePostProcessor.swift in Sources */,
68816CC527B4DCD500E24622 /* SecureStore.swift in Sources */,
68B3854C271E780700711D5F /* LiveKitExample.swift in Sources */,
681E3F3F271FC795007BB547 /* Custom.swift in Sources */,
Expand All @@ -421,6 +434,7 @@
683720D327A06404007DA986 /* ConnectionHistory.swift in Sources */,
681E3F44271FC7AD007BB547 /* RoomView.swift in Sources */,
6816B1B1272D9198005ADB85 /* ParticipantView.swift in Sources */,
68FC569D2A8AB586004B4989 /* SimpleReverbAudioCapturePostProcessor.swift in Sources */,
68816CC627B4DCD500E24622 /* SecureStore.swift in Sources */,
68B3854D271E780700711D5F /* LiveKitExample.swift in Sources */,
681E3F40271FC795007BB547 /* Custom.swift in Sources */,
Expand Down Expand Up @@ -619,7 +633,7 @@
CODE_SIGN_ENTITLEMENTS = "$(SRCROOT)/iOS/iOS.entitlements";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 27;
CURRENT_PROJECT_VERSION = 28;
DEVELOPMENT_TEAM = 76TVFCUKK7;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = iOS/Info.plist;
Expand All @@ -634,7 +648,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.27;
MARKETING_VERSION = 1.0.28;
PRODUCT_BUNDLE_IDENTIFIER = io.livekit.example.SwiftSDK.1;
PRODUCT_NAME = LiveKitExample;
PROVISIONING_PROFILE_SPECIFIER = "";
Expand All @@ -653,7 +667,7 @@
CODE_SIGN_ENTITLEMENTS = "$(SRCROOT)/iOS/iOS.entitlements";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 27;
CURRENT_PROJECT_VERSION = 28;
DEVELOPMENT_TEAM = 76TVFCUKK7;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = iOS/Info.plist;
Expand All @@ -668,7 +682,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.27;
MARKETING_VERSION = 1.0.28;
PRODUCT_BUNDLE_IDENTIFIER = io.livekit.example.SwiftSDK.1;
PRODUCT_NAME = LiveKitExample;
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
47 changes: 47 additions & 0 deletions Shared/AudioProcessors/SimpleReverbAudioCapturePostProcessor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Foundation
import LiveKit
import WebRTC

class SimpleReverbAudioCapturePostProcessor: NSObject, RTCAudioCustomProcessingDelegate {

private var delayBuffer: [Float] = []
private let delayBufferSize: Int = 4410 // 0.1 second at 44.1kHz
private var delayBufferIndex: Int = 0
private let feedback: Float = 0.6

func audioProcessingInitialize(sampleRate sampleRateHz: Int, channels: Int) {
print("\(String(describing: self)) initialize(sampleRateHz: \(sampleRateHz), channels: \(channels))")
delayBuffer = Array(repeating: 0.0, count: delayBufferSize)
}

func audioProcessingProcess(audioBuffer: RTCAudioBuffer) {

// print("\(String(describing: self)) process(audioBuffer: \(audioBuffer))")

for channelIndex in 0..<audioBuffer.channels {
let channelBuffer = audioBuffer.rawBuffer(forChannel: channelIndex)

for frameIndex in 0..<audioBuffer.frames {
let inputSample = channelBuffer[frameIndex]
let delayedSample = delayBuffer[delayBufferIndex]

// Mix the current sample with the delayed sample
let outputSample = inputSample + delayedSample * feedback

// Store the output sample into the delay buffer
delayBuffer[delayBufferIndex] = outputSample

// Write the output sample back to the channel buffer
channelBuffer[frameIndex] = outputSample

// Increment and wrap the delay buffer index
delayBufferIndex = (delayBufferIndex + 1) % delayBufferSize
}
}
}

func audioProcessingRelease() {
print("\(String(describing: self)) destroy()")
delayBuffer.removeAll()
}
}
34 changes: 13 additions & 21 deletions Shared/Controllers/AppContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extension ObservableObject where Self.ObjectWillChangePublisher == ObservableObj
final class AppContext: ObservableObject {

private let store: ValueStore<Preferences>
private let simpleReverbAudioCapturePostProcessor = SimpleReverbAudioCapturePostProcessor()

@Published var videoViewVisible: Bool = true {
didSet { store.value.videoViewVisible = videoViewVisible }
Expand All @@ -38,30 +39,30 @@ final class AppContext: ObservableObject {
didSet { store.value.connectionHistory = connectionHistory }
}

@Published var outputDevice: RTCAudioDevice = RTCAudioDevice.defaultDevice(with: .output) {
@Published var outputDevice: RTCIODevice = RTCIODevice.defaultDevice(with: .output) {
didSet {
print("didSet outputDevice: \(String(describing: outputDevice))")

if !Room.audioDeviceModule.setOutputDevice(outputDevice) {
print("failed to set value")
}
Room.audioDeviceModule.outputDevice = outputDevice
}
}

@Published var inputDevice: RTCAudioDevice = RTCAudioDevice.defaultDevice(with: .input) {
@Published var inputDevice: RTCIODevice = RTCIODevice.defaultDevice(with: .input) {
didSet {
print("didSet inputDevice: \(String(describing: inputDevice))")

if !Room.audioDeviceModule.setInputDevice(inputDevice) {
print("failed to set value")
}
Room.audioDeviceModule.inputDevice = inputDevice
}
}

@Published var preferSpeakerOutput: Bool = true {
didSet { AudioManager.shared.preferSpeakerOutput = preferSpeakerOutput }
}

@Published var sampleCapturePostProcessingEnabled: Bool = false {
didSet {
Room.audioProcessingModule.capturePostProcessingDelegate = sampleCapturePostProcessingEnabled ? simpleReverbAudioCapturePostProcessor : nil
}
}

public init(store: ValueStore<Preferences>) {
self.store = store

Expand All @@ -76,17 +77,8 @@ final class AppContext: ObservableObject {
print("devices did update")
// force UI update for outputDevice / inputDevice
DispatchQueue.main.async {

// set to default device if selected device is removed
if !Room.audioDeviceModule.outputDevices.contains(where: { self.outputDevice == $0 }) {
self.outputDevice = RTCAudioDevice.defaultDevice(with: .output)
}

// set to default device if selected device is removed
if !Room.audioDeviceModule.inputDevices.contains(where: { self.inputDevice == $0 }) {
self.inputDevice = RTCAudioDevice.defaultDevice(with: .input)
}

self.outputDevice = Room.audioDeviceModule.outputDevice
self.inputDevice = Room.audioDeviceModule.inputDevice
self.objectWillChange.send()
}
}
Expand Down
2 changes: 2 additions & 0 deletions Shared/RoomView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ struct RoomView: View {
}

Toggle("Prefer speaker output", isOn: $appCtx.preferSpeakerOutput)

Toggle("(Demo) Audio Capture-post processing", isOn: $appCtx.sampleCapturePostProcessingEnabled)
}

} label: {
Expand Down