Voice message waveform from audio files (#1919)

* Add DSWaveformImage

* Add estimated waveform

* Add progress mask modifier

* Add real waveform

* Fix cursor

* Fix scale factor

* Refine ProgressMaskModifier

* Refine waveform logic

* Add docs

* Add waveform placeholder
This commit is contained in:
Alfonso Grillo
2023-10-19 11:01:45 +02:00
committed by GitHub
parent 75da7ba3ec
commit fbcf037240
16 changed files with 175 additions and 61 deletions
@@ -30,7 +30,7 @@ enum AudioPlayerPlaybackState {
class AudioPlayerState: ObservableObject, Identifiable {
let id = UUID()
let duration: Double
let waveform: Waveform
let waveform: EstimatedWaveform
@Published private(set) var playbackState: AudioPlayerPlaybackState
@Published private(set) var progress: Double
@@ -38,6 +38,10 @@ class AudioPlayerState: ObservableObject, Identifiable {
private var cancellables: Set<AnyCancellable> = []
private var displayLink: CADisplayLink?
/// The file url that the last player attached to this object has loaded.
/// The file url persists even if the AudioPlayer will be detached later.
private(set) var fileURL: URL?
var isAttached: Bool {
audioPlayer != nil
}
@@ -46,9 +50,9 @@ class AudioPlayerState: ObservableObject, Identifiable {
displayLink != nil
}
init(duration: Double, waveform: Waveform? = nil, progress: Double = 0.0) {
init(duration: Double, waveform: EstimatedWaveform? = nil, progress: Double = 0.0) {
self.duration = duration
self.waveform = waveform ?? Waveform(data: [])
self.waveform = waveform ?? EstimatedWaveform(data: [])
self.progress = progress
playbackState = .stopped
}
@@ -110,6 +114,7 @@ class AudioPlayerState: ObservableObject, Identifiable {
playbackState = .loading
case .didFinishLoading:
playbackState = .readyToPlay
fileURL = audioPlayer?.url
case .didStartPlaying:
if let audioPlayer {
await restoreAudioPlayerState(audioPlayer: audioPlayer)