Skip to content

Commit 9ca584e

Browse files
committed
feat(AudioVisualizationView): addition function buffering audio file
1 parent 702d50a commit 9ca584e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

SoundWave/Classes/AudioVisualizationView.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Bastien Falcou on 12/6/16.
66
//
77

8+
import AVFoundation
89
import UIKit
910

1011
public class AudioVisualizationView: BaseNibView {
@@ -149,6 +150,22 @@ public class AudioVisualizationView: BaseNibView {
149150
return self.meteringLevelsClusteredArray
150151
}
151152

153+
// PRAGMA: - Analyze Sound File
154+
155+
private func buffer(url: URL) {
156+
do {
157+
let track = try AVAudioFile(forReading: url)
158+
let format = AVAudioFormat(commonFormat:.pcmFormatFloat32,
159+
sampleRate:track.fileFormat.sampleRate,
160+
channels: track.fileFormat.channelCount,
161+
interleaved: false)
162+
let buffer = AVAudioPCMBuffer(pcmFormat: format!, frameCapacity: UInt32(track.length))!
163+
try track.read(into : buffer, frameCount:UInt32(track.length))
164+
} catch {
165+
print(error)
166+
}
167+
}
168+
152169
// PRAGMA: - Play Mode Handling
153170

154171
public func play(for duration: TimeInterval) {

0 commit comments

Comments
 (0)