You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
13 lines
394 B
13 lines
394 B
2 years ago
|
import Foundation
|
||
|
|
||
|
func decodeWaveFile(_ url: URL) throws -> [Float] {
|
||
|
let data = try Data(contentsOf: url)
|
||
|
let floats = stride(from: 44, to: data.count, by: 2).map {
|
||
|
return data[$0..<$0 + 2].withUnsafeBytes {
|
||
|
let short = Int16(littleEndian: $0.load(as: Int16.self))
|
||
|
return max(-1.0, min(Float(short) / 32767.0, 1.0))
|
||
|
}
|
||
|
}
|
||
|
return floats
|
||
|
}
|