-
Notifications
You must be signed in to change notification settings - Fork 133
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
Sleep Timer Improvements: fade out 5s before pausing playback #1629
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
38e4d8f
Add a new method to the playback protocol
leandroalonso 102b34d
Implement set volume on DefaultPlayer
leandroalonso d75cee1
Add audio mixer node to Effects Player to support changing volume
leandroalonso f6f4af7
Connect the audio mixer node
leandroalonso b974c96
Ensure volume is 1 when playing
leandroalonso 202624f
Add a manager to perform fade out on a given PlaybackProtocol
leandroalonso 9744092
Add a method to perform a fade out
leandroalonso be00a5f
Fade out when sleep timer has 5s remaining
leandroalonso 01a52ce
Merge branch 'sleep-timer/shake-device-restart-timer' into sleep-time…
leandroalonso 355aad1
Update CHANGELOG.md
leandroalonso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Foundation | ||
|
||
class FadeOutManager { | ||
weak var player: PlaybackProtocol? | ||
|
||
private var timer: Timer? | ||
|
||
private let volumeChangesPerSecond = 30.0 | ||
private var currentChange = 0.0 | ||
private var fadeDuration = 5.0 | ||
private let fadeVelocity = 2.0 | ||
private let fromVolume = 1.0 | ||
private let toVolume = 0.0 | ||
private var totalNumberOfVolumeChanges = 0.0 | ||
private lazy var timerDelay = 1.0 / volumeChangesPerSecond | ||
|
||
|
||
/// Performs a fade out on the given `PlaybackProtocol` by using a logarithmic algorithm | ||
/// This way the final results is smooth 🧈 to the human's hearing 👂 | ||
/// - Parameter duration: the duration of the fade out effect | ||
func fadeOut(duration: TimeInterval) { | ||
fadeDuration = duration | ||
currentChange = 0 | ||
totalNumberOfVolumeChanges = fadeDuration * volumeChangesPerSecond | ||
timer = Timer.scheduledTimer(withTimeInterval: TimeInterval(timerDelay), repeats: true) { [weak self] _ in | ||
guard let self, | ||
currentChange < totalNumberOfVolumeChanges else { | ||
self?.timer?.invalidate() | ||
return | ||
} | ||
|
||
let normalizedTime = (currentChange / totalNumberOfVolumeChanges).betweenZeroAndOne | ||
let volumeMultiplier = pow(M_E, -fadeVelocity * normalizedTime) * (1 - normalizedTime) | ||
let newVolume = toVolume - (toVolume - fromVolume) * volumeMultiplier | ||
|
||
player?.playing() == true ? player?.setVolume(Float(newVolume)) : timer?.invalidate() | ||
|
||
currentChange += 1 | ||
} | ||
} | ||
} | ||
|
||
private extension Double { | ||
var betweenZeroAndOne: Double { | ||
max(0, min(1, self)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this name at all, but couldn't think of something better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tricky one maybe:
VolumeControl
orFaderMixer