Skip to content

Commit

Permalink
Merge pull request iina#5070 from iina/eq
Browse files Browse the repository at this point in the history
Added EQ presets and EQ profile saving functionalities
  • Loading branch information
lhc70000 committed Jul 22, 2024
2 parents 06a3c7f + 9534f5d commit d34ba97
Show file tree
Hide file tree
Showing 11 changed files with 666 additions and 301 deletions.
4 changes: 4 additions & 0 deletions iina.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
1326717E20852D0D000FA7E2 /* SubChooseViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1326718020852D0D000FA7E2 /* SubChooseViewController.xib */; };
3412DB8E2C2FC64C00BBC142 /* Equalizations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3412DB8D2C2FC64800BBC142 /* Equalizations.swift */; };
34ACA04B29DBB0090030C09C /* LogWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 34ACA04D29DBB0090030C09C /* LogWindowController.xib */; };
34ACAB722C142A0500F871A0 /* libintl.8.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 34ACAB2D2C142A0300F871A0 /* libintl.8.dylib */; };
34ACAB732C142A0500F871A0 /* libgraphite2.3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 34ACAB2E2C142A0300F871A0 /* libgraphite2.3.dylib */; };
Expand Down Expand Up @@ -652,6 +653,7 @@
2FE1FCAE1F98D3DA004C4D6B /* sk */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sk; path = sk.lproj/Localizable.strings; sourceTree = "<group>"; };
2FE1FCB01F98D3DB004C4D6B /* sk */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sk; path = sk.lproj/FilterPresets.strings; sourceTree = "<group>"; };
2FE1FCB11F98D3DB004C4D6B /* sk */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sk; path = sk.lproj/KeyBinding.strings; sourceTree = "<group>"; };
3412DB8D2C2FC64800BBC142 /* Equalizations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Equalizations.swift; sourceTree = "<group>"; };
348B000028DC688D0045F683 /* sr-Latn */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "sr-Latn"; path = "sr-Latn.lproj/MiniPlayerWindowController.strings"; sourceTree = "<group>"; };
348B000128DC688D0045F683 /* sr-Latn */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "sr-Latn"; path = "sr-Latn.lproj/GuideWindowController.strings"; sourceTree = "<group>"; };
348B000228DC688E0045F683 /* sr-Latn */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "sr-Latn"; path = "sr-Latn.lproj/CropSettingsViewController.strings"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2441,6 +2443,7 @@
84AABE8A1DBF634600D138FD /* CharEncoding.swift */,
8450403F1E0ACADD0079C194 /* MPVNode.swift */,
842F55A41EA17E7E0081D475 /* IINACommand.swift */,
3412DB8D2C2FC64800BBC142 /* Equalizations.swift */,
);
name = Data;
sourceTree = "<group>";
Expand Down Expand Up @@ -3189,6 +3192,7 @@
E35306FC214813B7008FE492 /* JavascriptPluginInstance.swift in Sources */,
84A0BA971D2FA1CE00BC8DA1 /* PlayerCore.swift in Sources */,
84D377671D737F58007F7396 /* MPVChapter.swift in Sources */,
3412DB8E2C2FC64C00BBC142 /* Equalizations.swift in Sources */,
84A886E61E24F3BD008755BB /* OnlineSubtitle.swift in Sources */,
847C62CA1DC13CDA00E1EF16 /* PrefGeneralViewController.swift in Sources */,
E3530700214908DD008FE492 /* JavascriptAPIHttp.swift in Sources */,
Expand Down
32 changes: 32 additions & 0 deletions iina/Base.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"general.username" = "Username";
"general.password" = "Password";

"input.value_is_empty" = "Value cannot be empty.";
"input.already_exists" = "Value already exists.";

// Guide Window
"guide.highlights" = "Highlights";
"guide.basic_settings" = "Basic Settings";
Expand Down Expand Up @@ -48,6 +51,35 @@
"quicksetting.hdr" = "HDR";
"quicksetting.hide" = "Hide";

// EQ Presets
"eq.preset.flat" = "Flat";
"eq.preset.acoustic" = "Acoustic";
"eq.preset.classical" = "Classical";
"eq.preset.dance" = "Dance";
"eq.preset.deep" = "Deep";
"eq.preset.electronic" = "Electronic";
"eq.preset.hip_hop" = "Hip Hop";
"eq.preset.increase_bass" = "Increase Bass";
"eq.preset.increase_treble" = "Increase Treble";
"eq.preset.increase_vocal" = "Increase Vocal";
"eq.preset.jazz" = "Jazz";
"eq.preset.latin" = "Latin";
"eq.preset.loundness" = "Loundness";
"eq.preset.lounge" = "Lounge";
"eq.preset.piano" = "Piano";
"eq.preset.pop" = "Pop";
"eq.preset.rnb" = "R&B";
"eq.preset.reduce_bass" = "Reduce Bass";
"eq.preset.reduce_treble" = "Reduce Treble";
"eq.preset.rock" = "Rock";
"eq.preset.small_speaker" = "Small Speaker";
"eq.preset.spoken_word" = "Spoken Word";

"alert.eq.new_profile.title" = "New EQ Profile";
"alert.eq.new_profile.message" = "Please enter a profile name.";
"alert.eq.rename.title" = "Rename Currnet Profile";
"alert.eq.rename.message" = "Please enter a new profile name.";

// OSDMessage.swift
"osd.pause" = "Pause";
"osd.resume" = "Resume";
Expand Down
555 changes: 306 additions & 249 deletions iina/Base.lproj/QuickSettingViewController.xib

Large diffs are not rendered by default.

68 changes: 68 additions & 0 deletions iina/Equalizations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Untitled.swift
// iina
//
// Created by Yuze Jiang on 2024/06/29.
// Copyright © 2024 lhc. All rights reserved.
//

class EQProfile: Codable {
var gains = [Double](repeatElement(0.0, count: 10))

init(_ values: [Double]) {
gains = values
}

init(fromCurrentSliders sliders: [NSSlider]) {
gains = sliders.map { $0.doubleValue }
}
}

class PresetEQProfile: EQProfile {
let localizationKey: String
let name: String

init(_ name: String, _ values: [Double]) {
self.localizationKey = "eq.preset." + name
self.name = NSLocalizedString(localizationKey, comment: localizationKey)
super.init(values)
}

required init(from decoder: any Decoder) throws {
fatalError("init(from:) has not been implemented")
}
}

let presetEQs: [PresetEQProfile] = [
.init("flat", [0,0,0,0,0,0,0,0,0,0]),
.init("acoustic", [5.0, 4.9, 3.95,1.05,2.15,1.75,3.5,4.1,3.55,2.15]),
.init("classical", [4.75,3.75,3,2.5,-1.5,-1.5,0,2.25,3.25,3.75]),
.init("dance", [3.57,6.55,4.99,0,1.92,3.65,5.15,4.54,3.59,0]),
.init("deep",[4.95,3.55,1.75,1.00,2.85,2.50,1.45,-2.15,-3.55,-4.60]),
.init("electronic", [4.25,3.8,1.2,0,-2.15,2.25,0.85,1.25,3.95,4.8]),
.init("hip_hop", [5,4.25,1.5,3,-1,-1,1.5,-0.5,2,3]),
.init("increase_bass", [5.5,4.25,3.5,2.5,1.25,0,0,0,0,0]),
.init("increase_treble", [0,0,0,0,0,1.25,2.5,3.5,4.25,5.5]),
.init("increase_vocal",[-1.50,-3.00,-3.00,1.50,3.75,3.75,3.00,1.50,0,-1.50]),
.init("jazz", [4,3,1.5,2.25,-1.5,-1.5,0,1.5,3,3.75]),
.init("latin",[4.5,3,0,0,-1.5,-1.5,-1.5,0,3,4.5]),
.init("loundness",[6,4,0,0,-2,0,-1,-5,5,1]),
.init("lounge",[-3.00,-1.50,-0.50,1.50,4.00,2.50,0,-1.50,2.00,1.00]),
.init("piano", [3,2,0,2.5,3,1.5,3.5,4.5,3,3.5]),
.init("pop", [-1.5,-1,0,2,4,4,2,0,-1,-1.5]),
.init("rnb", [2.62,6.92,5.65,1.33,-2.19,-1.50,2.32,2.65,3.0,3.75]),
.init("reduce_bass", [-5.5,-4.25,-3.5,-2.5,-1.25,0,0,0,0,0]),
.init("reduce_treble", [0,0,0,0,0,-1.25,-2.5,-3.5,-4.25,-5.5]),
.init("rock",[5,4,3,1.5,-0.5,-1,0.5,2.5,3.5,4.5]),
.init("small_speaker",[5.5,4.25,3.5,2.5,1.25,0,-1.25,-2.5,-3.5,-4.25]),
.init("spoken_word", [-3.46,-0.47,0,0.69,3.46,4.61,4.84,4.28,2.54,0]),
]

var userEQs: Dictionary<String, EQProfile> = [:] {
didSet {
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(userEQs) {
UserDefaults.standard.set(encoded, forKey: Preference.Key.userEQPresets.rawValue)
}
}
}
2 changes: 1 addition & 1 deletion iina/PlaybackInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class PlaybackInfo {
var cropFilter: MPVFilter?
var flipFilter: MPVFilter?
var mirrorFilter: MPVFilter?
var audioEqFilters: [MPVFilter?]?
var audioEqFilter: MPVFilter?
var delogoFilter: MPVFilter?

var deinterlace: Bool = false
Expand Down
31 changes: 12 additions & 19 deletions iina/PlayerCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1347,19 +1347,13 @@ class PlayerCore: NSObject {
}

func setAudioEq(fromGains gains: [Double]) {
let channelCount = mpv.getInt(MPVProperty.audioParamsChannelCount)
let freqList = [31.25, 62.5, 125, 250, 500, 1000, 2000, 4000, 8000, 16000]
let filters = freqList.enumerated().map { (index, freq) -> MPVFilter in
let string = [Int](0..<channelCount).map { "c\($0) f=\(freq) w=\(freq / 1.224744871) g=\(gains[index])" }.joined(separator: "|")
return MPVFilter(name: "lavfi", label: "\(Constants.FilterName.audioEq)\(index)", paramString: "[anequalizer=\(string)]")
}
filters.forEach { _ = addAudioFilter($0) }
info.audioEqFilters = filters
}

func removeAudioEqFilter() {
info.audioEqFilters?.compactMap { $0 }.forEach { _ = removeAudioFilter($0) }
info.audioEqFilters = nil
let freqList = [32, 64, 125, 250, 500, 1000, 2000, 4000, 8000, 16000]
let paramString = freqList.enumerated().map { (index, freq) in
"equalizer=f=\(freq):t=h:width=\(Double(freq) / 1.224744871):g=\(gains[index])"
}.joined(separator: ",")
let filter = MPVFilter(name: "lavfi", label: Constants.FilterName.audioEq, paramString: "[\(paramString)]")
addAudioFilter(filter)
info.audioEqFilter = filter
}

/// Add a video filter given as a `MPVFilter` object.
Expand Down Expand Up @@ -1489,11 +1483,13 @@ class PlayerCore: NSObject {
/// Add an audio filter given as a `MPVFilter` object.
/// - Parameter filter: The filter to add.
/// - Returns: `true` if the filter was successfully added, `false` otherwise.
@discardableResult
func addAudioFilter(_ filter: MPVFilter) -> Bool { addAudioFilter(filter.stringFormat) }

/// Add an audio filter given as a string.
/// - Parameter filter: The filter to add.
/// - Returns: `true` if the filter was successfully added, `false` otherwise.
@discardableResult
func addAudioFilter(_ filter: String) -> Bool {
log("Adding audio filter \(filter)...")
var result = true
Expand Down Expand Up @@ -1539,6 +1535,7 @@ class PlayerCore: NSObject {
/// remove a filter.
/// - Parameter filter: The filter to remove.
/// - Returns: `true` if the filter was successfully removed, `false` otherwise.
@discardableResult
func removeAudioFilter(_ filter: MPVFilter) -> Bool { removeAudioFilter(filter.stringFormat) }

/// Remove an audio filter given as a string.
Expand All @@ -1550,6 +1547,7 @@ class PlayerCore: NSObject {
/// methods that identify the filter to be removed based on its position in the filter list are the preferred way to remove a filter.
/// - Parameter filter: The filter to remove.
/// - Returns: `true` if the filter was successfully removed, `false` otherwise.
@discardableResult
func removeAudioFilter(_ filter: String) -> Bool {
log("Removing audio filter \(filter)...")
var result = true
Expand Down Expand Up @@ -2509,12 +2507,7 @@ class PlayerCore: NSObject {
for filter in audioFilters {
guard let label = filter.label else { continue }
if label.hasPrefix(Constants.FilterName.audioEq) {
if info.audioEqFilters == nil {
info.audioEqFilters = Array(repeating: nil, count: 10)
}
if let index = Int(String(label.last!)) {
info.audioEqFilters![index] = filter
}
info.audioEqFilter = filter
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions iina/Preference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ struct Preference {
static let replayGainClip = Key("replayGainClip")
static let replayGainFallback = Key("replayGainFallback")

static let userEQPresets = Key("userEQPresets")

// Subtitle

static let subAutoLoadIINA = Key("subAutoLoadIINA")
Expand Down
Loading

0 comments on commit d34ba97

Please sign in to comment.