forked from bitgapp/eqMac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolumeConverter.swift
More file actions
43 lines (36 loc) · 1.03 KB
/
Copy pathVolumeConverter.swift
File metadata and controls
43 lines (36 loc) · 1.03 KB
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
//
// VolumeConverter.swift
// eqMac
//
// Created by Nodeful on 10/09/2021.
// Copyright © 2021 Bitgapp. All rights reserved.
//
import Foundation
public class VolumeConverter {
public static func toDecibel (_ volume: Float32) -> Float32 {
if (volume <= powf(10.0, kMinVolumeDB / 20.0)) {
return kMinVolumeDB
} else {
return 20.0 * log10f(volume)
}
}
public static func fromDecibel (_ decibel: Float32) -> Float32 {
if (decibel <= kMinVolumeDB) {
return 0.0
} else {
return powf(10.0, decibel / 20.0)
}
}
public static func toScalar (_ volume: Float32) -> Float32 {
let decibel = toDecibel(volume);
return (decibel - kMinVolumeDB) / (kMaxVolumeDB - kMinVolumeDB);
}
public static func fromScalar (_ scalar: Float32) -> Float32 {
let decibel = scalar * (kMaxVolumeDB - kMinVolumeDB) + kMinVolumeDB
return fromDecibel(decibel)
}
public static func toRelative (_ volume: Float32) -> Float32 {
if volume == 0 { return 0 }
return exp(4 * volume - 4)
}
}