Skip to content

Commit

Permalink
Restore AudioEffect feature
Browse files Browse the repository at this point in the history
  • Loading branch information
levs42 committed Sep 28, 2023
1 parent 7c94fbb commit caa921d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions HaishinKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
035AFA042263868E009DD0BB /* RTMPStreamTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 035AFA032263868E009DD0BB /* RTMPStreamTests.swift */; };
1A216BCE25DBFE269F68C42F /* AudioEffect.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A21649B4F13B31B3CE59AE8 /* AudioEffect.swift */; };
1A216F07B0BD8E05C8ECC8F1 /* AVAudioFormat+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A2166D3A449D813866FE9D9 /* AVAudioFormat+Extension.swift */; };
2901A4EE1D437170002BBD23 /* MediaLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2901A4ED1D437170002BBD23 /* MediaLink.swift */; };
290686031DFDB7A7008EB7ED /* RTMPConnectionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 290686021DFDB7A6008EB7ED /* RTMPConnectionTests.swift */; };
Expand Down Expand Up @@ -410,6 +411,7 @@

/* Begin PBXFileReference section */
035AFA032263868E009DD0BB /* RTMPStreamTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RTMPStreamTests.swift; sourceTree = "<group>"; };
1A21649B4F13B31B3CE59AE8 /* AudioEffect.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AudioEffect.swift; sourceTree = "<group>"; };
1A2166D3A449D813866FE9D9 /* AVAudioFormat+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AVAudioFormat+Extension.swift"; sourceTree = "<group>"; };
2901A4ED1D437170002BBD23 /* MediaLink.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MediaLink.swift; sourceTree = "<group>"; };
290686021DFDB7A6008EB7ED /* RTMPConnectionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RTMPConnectionTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1065,6 +1067,7 @@
BC6FC91D29609A6800A746EE /* ShapeFactory.swift */,
29B8768D1CD70AFE00FC07DA /* SoundTransform.swift */,
29B8768F1CD70AFE00FC07DA /* VideoEffect.swift */,
1A21649B4F13B31B3CE59AE8 /* AudioEffect.swift */,
);
path = Media;
sourceTree = "<group>";
Expand Down Expand Up @@ -1802,6 +1805,7 @@
29B876901CD70AFE00FC07DA /* IOAudioUnit.swift in Sources */,
29B876771CD70ACE00FC07DA /* HTTPResponse.swift in Sources */,
1A216F07B0BD8E05C8ECC8F1 /* AVAudioFormat+Extension.swift in Sources */,
1A216BCE25DBFE269F68C42F /* AudioEffect.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
9 changes: 9 additions & 0 deletions Sources/Media/AudioEffect.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import AVFoundation
import Foundation

/// An object that apply an audio effect.
open class AudioEffect: NSObject {
/// Executes to apply an audio effect.
open func execute(_ buffer: AVAudioBuffer, presentationTimeStamp: CMTime) {
}
}
12 changes: 12 additions & 0 deletions Sources/Media/IOAudioUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ final class IOAudioUnit: NSObject, IOUnit {
return codec.outputFormat?.formatDescription
}
private(set) var presentationTimeStamp: CMTime = .invalid
private var effects: Set<AudioEffect> = []
private lazy var resampler: IOAudioResampler<IOAudioUnit> = {
var resampler = IOAudioResampler<IOAudioUnit>()
resampler.delegate = self
Expand Down Expand Up @@ -84,6 +85,14 @@ final class IOAudioUnit: NSObject, IOUnit {
presentationTimeStamp = sampleBuffer.presentationTimeStamp
resampler.appendSampleBuffer(sampleBuffer.muted(muted))
}

func registerEffect(_ effect: AudioEffect) -> Bool {
effects.insert(effect).inserted
}

func unregisterEffect(_ effect: AudioEffect) -> Bool {
effects.remove(effect) != nil
}
}

extension IOAudioUnit: IOUnitEncoding {
Expand Down Expand Up @@ -165,6 +174,9 @@ extension IOAudioUnit: IOAudioResamplerDelegate {
}

func resampler(_ resampler: IOAudioResampler<IOAudioUnit>, didOutput audioBuffer: AVAudioPCMBuffer, presentationTimeStamp: CMTime) {
for effect in effects {
effect.execute(audioBuffer, presentationTimeStamp: presentationTimeStamp)
}
if let mixer {
mixer.delegate?.mixer(mixer, didOutput: audioBuffer, presentationTimeStamp: presentationTimeStamp)
if mixer.recorder.isRunning.value, let sampleBuffer = audioBuffer.makeSampleBuffer(presentationTimeStamp) {
Expand Down
14 changes: 14 additions & 0 deletions Sources/Net/NetStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ open class NetStream: NSObject {
}
}

/// Register a audio effect.
public func registerAudioEffect(_ effect: AudioEffect) -> Bool {
mixer.audioIO.lockQueue.sync {
self.mixer.audioIO.registerEffect(effect)
}
}

/// Unregister a audio effect.
public func unregisterAudioEffect(_ effect: AudioEffect) -> Bool {
mixer.audioIO.lockQueue.sync {
self.mixer.audioIO.unregisterEffect(effect)
}
}

/// Register a video effect.
public func registerVideoEffect(_ effect: VideoEffect) -> Bool {
mixer.videoIO.lockQueue.sync {
Expand Down

0 comments on commit caa921d

Please sign in to comment.