Skip to content

Commit

Permalink
AudioUnitPlayer add mute
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Nov 5, 2023
1 parent 24f065d commit 5890f64
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 214 deletions.
47 changes: 26 additions & 21 deletions Sources/KSPlayer/MEPlayer/AudioEnginePlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,71 +12,76 @@ public protocol AudioOutput: FrameOutput {
var playbackRate: Float { get set }
var volume: Float { get set }
var isMuted: Bool { get set }
var attackTime: Float { get set }
var releaseTime: Float { get set }
var threshold: Float { get set }
var expansionRatio: Float { get set }
var overallGain: Float { get set }
init()
func prepare(audioFormat: AVAudioFormat)
func play(time: TimeInterval)
}

public final class AudioEnginePlayer: AudioOutput {
public var attackTime: Float {
public protocol AudioDynamicsProcessor {
var audioUnitForDynamicsProcessor: AudioUnit { get }
}

public extension AudioDynamicsProcessor {
var attackTime: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(dynamicsProcessor.audioUnit, kDynamicsProcessorParam_AttackTime, kAudioUnitScope_Global, 0, &value)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_AttackTime, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(dynamicsProcessor.audioUnit, kDynamicsProcessorParam_AttackTime, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_AttackTime, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public var releaseTime: Float {
var releaseTime: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(dynamicsProcessor.audioUnit, kDynamicsProcessorParam_ReleaseTime, kAudioUnitScope_Global, 0, &value)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_ReleaseTime, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(dynamicsProcessor.audioUnit, kDynamicsProcessorParam_ReleaseTime, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_ReleaseTime, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public var threshold: Float {
var threshold: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(dynamicsProcessor.audioUnit, kDynamicsProcessorParam_Threshold, kAudioUnitScope_Global, 0, &value)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_Threshold, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(dynamicsProcessor.audioUnit, kDynamicsProcessorParam_Threshold, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_Threshold, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public var expansionRatio: Float {
var expansionRatio: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(dynamicsProcessor.audioUnit, kDynamicsProcessorParam_ExpansionRatio, kAudioUnitScope_Global, 0, &value)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_ExpansionRatio, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(dynamicsProcessor.audioUnit, kDynamicsProcessorParam_ExpansionRatio, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_ExpansionRatio, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public var overallGain: Float {
var overallGain: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(dynamicsProcessor.audioUnit, kDynamicsProcessorParam_OverallGain, kAudioUnitScope_Global, 0, &value)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_OverallGain, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(dynamicsProcessor.audioUnit, kDynamicsProcessorParam_OverallGain, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_OverallGain, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}
}

public final class AudioEnginePlayer: AudioOutput, AudioDynamicsProcessor {
public var audioUnitForDynamicsProcessor: AudioUnit {
dynamicsProcessor.audioUnit
}

private let engine = AVAudioEngine()
private var sourceNode: AVAudioSourceNode?
Expand Down
61 changes: 4 additions & 57 deletions Sources/KSPlayer/MEPlayer/AudioGraphPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import AudioToolbox
import AVFAudio
import CoreAudio

public final class AudioGraphPlayer: AudioOutput {
public final class AudioGraphPlayer: AudioOutput, AudioDynamicsProcessor {
public private(set) var audioUnitForDynamicsProcessor: AudioUnit
private let graph: AUGraph
private var audioUnitForMixer: AudioUnit!
private var audioUnitForTimePitch: AudioUnit!
private var audioUnitForDynamicsProcessor: AudioUnit!
private var audioUnitForOutput: AudioUnit!
private var currentRenderReadOffset = UInt32(0)
private var sourceNodeAudioFormat: AVAudioFormat?
Expand Down Expand Up @@ -93,61 +93,6 @@ public final class AudioGraphPlayer: AudioOutput {
}
}

public var attackTime: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_AttackTime, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_AttackTime, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public var releaseTime: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_ReleaseTime, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_ReleaseTime, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public var threshold: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_Threshold, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_Threshold, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public var expansionRatio: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_ExpansionRatio, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_ExpansionRatio, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public var overallGain: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_OverallGain, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_OverallGain, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public init() {
var newGraph: AUGraph!
NewAUGraph(&newGraph)
Expand Down Expand Up @@ -189,7 +134,9 @@ public final class AudioGraphPlayer: AudioOutput {
AUGraphConnectNodeInput(graph, nodeForDynamicsProcessor, 0, nodeForMixer, 0)
AUGraphConnectNodeInput(graph, nodeForMixer, 0, nodeForOutput, 0)
AUGraphNodeInfo(graph, nodeForTimePitch, &descriptionForTimePitch, &audioUnitForTimePitch)
var audioUnitForDynamicsProcessor: AudioUnit?
AUGraphNodeInfo(graph, nodeForDynamicsProcessor, &descriptionForDynamicsProcessor, &audioUnitForDynamicsProcessor)
self.audioUnitForDynamicsProcessor = audioUnitForDynamicsProcessor!
AUGraphNodeInfo(graph, nodeForMixer, &descriptionForMixer, &audioUnitForMixer)
AUGraphNodeInfo(graph, nodeForOutput, &descriptionForOutput, &audioUnitForOutput)
addRenderNotify(audioUnit: audioUnitForOutput)
Expand Down
10 changes: 0 additions & 10 deletions Sources/KSPlayer/MEPlayer/AudioRendererPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ public class AudioRendererPlayer: AudioOutput {
}
}

public var attackTime: Float = 0

public var releaseTime: Float = 0

public var threshold: Float = 0

public var expansionRatio: Float = 0

public var overallGain: Float = 0

public weak var renderSource: OutputRenderSourceDelegate?
private var periodicTimeObserver: Any?
private let renderer = AVSampleBufferAudioRenderer()
Expand Down
94 changes: 14 additions & 80 deletions Sources/KSPlayer/MEPlayer/AudioUnitPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import AVFAudio
import CoreAudio

public final class AudioUnitPlayer: AudioOutput {
private var audioUnitForDynamicsProcessor: AudioUnit!
private var audioUnitForOutput: AudioUnit!
private var currentRenderReadOffset = UInt32(0)
private var sourceNodeAudioFormat: AVAudioFormat?
Expand Down Expand Up @@ -67,84 +66,8 @@ public final class AudioUnitPlayer: AudioOutput {
}
}

public var isMuted: Bool {
get {
var value = AudioUnitParameterValue(1.0)
#if os(macOS)
AudioUnitGetParameter(audioUnitForOutput, kStereoMixerParam_Volume, kAudioUnitScope_Input, 0, &value)
#else
AudioUnitGetParameter(audioUnitForOutput, kMultiChannelMixerParam_Enable, kAudioUnitScope_Input, 0, &value)
#endif
return value == 0
}
set {
let value = newValue ? 0 : 1
#if os(macOS)
if value == 0 {
volumeBeforeMute = volume
}
AudioUnitSetParameter(audioUnitForOutput, kStereoMixerParam_Volume, kAudioUnitScope_Input, 0, min(Float(value), volumeBeforeMute), 0)
#else
AudioUnitSetParameter(audioUnitForOutput, kMultiChannelMixerParam_Enable, kAudioUnitScope_Input, 0, AudioUnitParameterValue(value), 0)
#endif
}
}

public var attackTime: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_AttackTime, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_AttackTime, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public var releaseTime: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_ReleaseTime, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_ReleaseTime, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public var threshold: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_Threshold, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_Threshold, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public var expansionRatio: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_ExpansionRatio, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_ExpansionRatio, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public var overallGain: Float {
get {
var value = AudioUnitParameterValue(1.0)
AudioUnitGetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_OverallGain, kAudioUnitScope_Global, 0, &value)
return value
}
set {
AudioUnitSetParameter(audioUnitForDynamicsProcessor, kDynamicsProcessorParam_OverallGain, kAudioUnitScope_Global, 0, AudioUnitParameterValue(newValue), 0)
}
}

public var isMuted: Bool = false
public var latency = Float(0)
public init() {
var descriptionForOutput = AudioComponentDescription()
descriptionForOutput.componentType = kAudioUnitType_Output
Expand Down Expand Up @@ -192,6 +115,12 @@ public final class AudioUnitPlayer: AudioOutput {
UInt32(MemoryLayout<AudioChannelLayout>.size))
AudioUnitInitialize(audioUnitForOutput)
AudioOutputUnitStart(audioUnitForOutput)
var size = UInt32(MemoryLayout<Float64>.size)
AudioUnitGetProperty(audioUnitForOutput,
kAudioUnitProperty_Latency,
kAudioUnitScope_Global, 0,
&latency,
&size)
}

public func flush() {
Expand All @@ -200,6 +129,7 @@ public final class AudioUnitPlayer: AudioOutput {

deinit {
AudioOutputUnitStop(audioUnitForOutput)
AudioUnitUninitialize(audioUnitForOutput)
}
}

Expand Down Expand Up @@ -258,7 +188,11 @@ extension AudioUnitPlayer {
let bytesToCopy = Int(framesToCopy * sampleSize)
let offset = Int(currentRenderReadOffset * sampleSize)
for i in 0 ..< min(ioData.count, currentRender.data.count) {
(ioData[i].mData! + ioDataWriteOffset).copyMemory(from: currentRender.data[i]! + offset, byteCount: bytesToCopy)
if isMuted {
memset(ioData[i].mData! + ioDataWriteOffset, 0, bytesToCopy)
} else {
(ioData[i].mData! + ioDataWriteOffset).copyMemory(from: currentRender.data[i]! + offset, byteCount: bytesToCopy)
}
}
numberOfSamples -= framesToCopy
ioDataWriteOffset += bytesToCopy
Expand Down
47 changes: 1 addition & 46 deletions Sources/KSPlayer/MEPlayer/KSMEPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import AppKit
public class KSMEPlayer: NSObject {
private var loopCount = 1
private var playerItem: MEPlayerItem
private let audioOutput: AudioOutput = KSOptions.audioPlayerType.init()
public let audioOutput: AudioOutput = KSOptions.audioPlayerType.init()
private var options: KSOptions
private var bufferingCountDownTimer: Timer?
public private(set) var videoOutput: (VideoOutput & UIView)? {
Expand Down Expand Up @@ -549,49 +549,4 @@ public extension KSMEPlayer {
var bytesRead: Int64 {
playerItem.bytesRead
}

var attackTime: Float {
get {
audioOutput.attackTime
}
set {
audioOutput.attackTime = newValue
}
}

var releaseTime: Float {
get {
audioOutput.releaseTime
}
set {
audioOutput.releaseTime = newValue
}
}

var threshold: Float {
get {
audioOutput.threshold
}
set {
audioOutput.threshold = newValue
}
}

var expansionRatio: Float {
get {
audioOutput.expansionRatio
}
set {
audioOutput.expansionRatio = newValue
}
}

var overallGain: Float {
get {
audioOutput.overallGain
}
set {
audioOutput.overallGain = newValue
}
}
}

0 comments on commit 5890f64

Please sign in to comment.