-
Notifications
You must be signed in to change notification settings - Fork 109
/
SAPlayerFeatures.swift
167 lines (136 loc) · 7.61 KB
/
SAPlayerFeatures.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// SAPlayerFeature.swift
// SwiftAudioPlayer
//
// Created by Tanha Kabir on 3/10/21.
//
import Foundation
import AVFoundation
extension SAPlayer {
/**
Special features for audio manipulation. These are examples of manipulations you can do with the player outside of this library. This is just an aggregation of community contibuted ones.
- Note: These features assume default state of the player and `audioModifiers` meaning some expect the first audio modifier to be the default `AVAudioUnitTimePitch` that comes with the SAPlayer.
*/
public struct Features {
/**
Feature to skip silences in spoken word audio. The player will speed up the rate of audio playback when silence is detected.
- Important: The first audio modifier must be the default `AVAudioUnitTimePitch` that comes with the SAPlayer for this feature to work.
*/
public struct SkipSilences {
static var enabled: Bool = false
static var originalRate: Float = 1.0
/**
Enable feature to skip silences in spoken word audio. The player will speed up the rate of audio playback when silence is detected. This can be called at any point of audio playback.
- Precondition: The first audio modifier must be the default `AVAudioUnitTimePitch` that comes with the SAPlayer for this feature to work.
- Important: If you want to change the rate of the overall player while having skip silences on, please use `SAPlayer.Features.SkipSilences.setRateSafely()` to properly set the rate of the player. Any rate changes to the player will be ignored while using Skip Silences otherwise.
*/
public static func enable() -> Bool {
guard let engine = SAPlayer.shared.engine else { return false }
Log.info("enabling skip silences feature")
enabled = true
originalRate = SAPlayer.shared.rate ?? 1.0
let format = engine.mainMixerNode.outputFormat(forBus: 0)
// look at documentation here to get an understanding of what is happening here: https://www.raywenderlich.com/5154-avaudioengine-tutorial-for-ios-getting-started#toc-anchor-005
engine.mainMixerNode.installTap(onBus: 0, bufferSize: 1024, format: format) { buffer, when in
guard let channelData = buffer.floatChannelData else {
return
}
let channelDataValue = channelData.pointee
let channelDataValueArray = stride(from: 0,
to: Int(buffer.frameLength),
by: buffer.stride).map { channelDataValue[$0] }
let rms = sqrt(channelDataValueArray.map { $0 * $0 }.reduce(0, +) / Float(buffer.frameLength))
let avgPower = 20 * log10(rms)
let meterLevel = self.scaledPower(power: avgPower)
Log.debug("meterLevel: \(meterLevel)")
if meterLevel < 0.6 { // below 0.6 decibels is below audible audio
SAPlayer.shared.rate = originalRate + 0.5
Log.debug("speed up rate to \(String(describing: SAPlayer.shared.rate))")
} else {
SAPlayer.shared.rate = originalRate
Log.debug("slow down rate to \(String(describing: SAPlayer.shared.rate))")
}
}
return true
}
/**
Disable feature to skip silences in spoken word audio. The player will speed up the rate of audio playback when silence is detected. This can be called at any point of audio playback.
- Precondition: The first audio modifier must be the default `AVAudioUnitTimePitch` that comes with the SAPlayer for this feature to work.
*/
public static func disable() -> Bool {
guard let engine = SAPlayer.shared.engine else { return false }
Log.info("disabling skip silences feature")
engine.mainMixerNode.removeTap(onBus: 0)
SAPlayer.shared.rate = originalRate
enabled = false
return true
}
/**
Use this function to set the overall rate of the player for when skip silences is on. This ensures that the overall rate will be what is set through this function even as skip silences is on; if this function is not used then any changes asked of from the overall player while skip silences is on won't be recorded!
- Important: The first audio modifier must be the default `AVAudioUnitTimePitch` that comes with the SAPlayer for this feature to work.
*/
public static func setRateSafely(_ rate: Float) {
originalRate = rate
SAPlayer.shared.rate = rate
}
private static func scaledPower(power: Float) -> Float {
guard power.isFinite else { return 0.0 }
let minDb: Float = -80.0
if power < minDb {
return 0.0
} else if power >= 1.0 {
return 1.0
} else {
return (abs(minDb) - abs(power)) / abs(minDb)
}
}
}
/**
Feature to pause the player after a delay. This will happen regardless of if another audio clip has started.
*/
public struct SleepTimer {
static var timer: Timer?
/**
Enable feature to pause the player after a delay. This will happen regardless of if another audio clip has started.
- Parameter afterDelay: The number of seconds to wait before pausing the audio
*/
public static func enable(afterDelay delay: Double) {
timer = Timer.scheduledTimer(withTimeInterval: delay, repeats: false, block: { _ in
SAPlayer.shared.pause()
})
}
/**
Disable feature to pause the player after a delay.
*/
public static func disable() {
timer?.invalidate()
}
}
/**
Feature to play the current playing audio on repeat until feature is disabled.
*/
public struct Loop {
static var enabled: Bool = false
static var playingStatusId: UInt?
/**
Enable feature to play the current playing audio on loop. This will continue until the feature is disabled. And this feature works for both remote and saved audio.
*/
public static func enable() {
enabled = true
guard playingStatusId == nil else { return }
playingStatusId = SAPlayer.Updates.PlayingStatus.subscribe({ (status) in
if status == .ended && enabled {
SAPlayer.shared.seekTo(seconds: 0.0)
SAPlayer.shared.play()
}
})
}
/**
Disable feature playing audio on loop.
*/
public static func disable() {
enabled = false
}
}
}
}