From 108ebe30d65c881a7887e050a7c062b31438e3b6 Mon Sep 17 00:00:00 2001 From: wl <356485255@qq.com> Date: Fri, 3 Sep 2021 10:08:36 +0800 Subject: [PATCH] 1. Update to swift 5 --- Beethoven.podspec | 4 +-- .../GuitarTuner.xcodeproj/project.pbxproj | 18 +++++++--- Example/GuitarTuner/Podfile | 14 -------- Example/GuitarTuner/Podfile.lock | 8 ++--- Source/Library/YINUtil.swift | 35 ++++++++++++++----- Source/PitchEngine.swift | 16 ++++++--- Source/SignalTracking/SignalTracker.swift | 6 ++-- .../Transform/Strategies/FFTTransformer.swift | 11 ++++-- 8 files changed, 67 insertions(+), 45 deletions(-) diff --git a/Beethoven.podspec b/Beethoven.podspec index 67be609..4bcd5ff 100644 --- a/Beethoven.podspec +++ b/Beethoven.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "Beethoven" s.summary = "A maestro of pitch detection" - s.version = "4.0.2" + s.version = "5.0.0" s.homepage = "https://github.com/vadymmarkov/Beethoven" s.license = 'MIT' s.author = { "Vadym Markov" => "markov.vadym@gmail.com" } @@ -19,5 +19,5 @@ Pod::Spec.new do |s| s.frameworks = 'Foundation', 'AVFoundation', 'Accelerate' s.dependency 'Pitchy', '~> 3.0' - s.pod_target_xcconfig = { 'SWIFT_VERSION' => '3.0' } + s.pod_target_xcconfig = { 'SWIFT_VERSION' => '5.0' } end diff --git a/Example/GuitarTuner/GuitarTuner.xcodeproj/project.pbxproj b/Example/GuitarTuner/GuitarTuner.xcodeproj/project.pbxproj index 5ef94e5..a82e378 100644 --- a/Example/GuitarTuner/GuitarTuner.xcodeproj/project.pbxproj +++ b/Example/GuitarTuner/GuitarTuner.xcodeproj/project.pbxproj @@ -125,8 +125,8 @@ D57D40F91BE95A350026B005 /* Sources */, D57D40FA1BE95A350026B005 /* Frameworks */, D57D40FB1BE95A350026B005 /* Resources */, - 9A1FE1805890F1ADECBB5C91 /* [CP] Embed Pods Frameworks */, BBF9D165E87DC40FC3115EAE /* 📦 Embed Pods Frameworks */, + A747E035BB13D8303B3FA356 /* [CP] Embed Pods Frameworks */, ); buildRules = ( ); @@ -143,7 +143,7 @@ D57D40F51BE95A350026B005 /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0900; + LastUpgradeCheck = 1250; ORGANIZATIONNAME = "Vadym Markov"; TargetAttributes = { D57D40FC1BE95A350026B005 = { @@ -218,7 +218,7 @@ shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n"; showEnvVarsInLog = 0; }; - 9A1FE1805890F1ADECBB5C91 /* [CP] Embed Pods Frameworks */ = { + A747E035BB13D8303B3FA356 /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( @@ -287,6 +287,7 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; @@ -295,14 +296,17 @@ CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -327,7 +331,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -340,6 +344,7 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES; CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; @@ -348,14 +353,17 @@ CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -374,7 +382,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 12.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; diff --git a/Example/GuitarTuner/Podfile b/Example/GuitarTuner/Podfile index d490dac..5b9dc36 100644 --- a/Example/GuitarTuner/Podfile +++ b/Example/GuitarTuner/Podfile @@ -8,17 +8,3 @@ target 'GuitarTuner' do pod 'Cartography', '~> 3.0' end -post_install do |installer| - installer.pods_project.targets.each do |target| - target.build_configurations.each do |config| - config.build_settings['SWIFT_VERSION'] = '5.0' - config.build_settings.delete('CODE_SIGNING_ALLOWED') - config.build_settings.delete('CODE_SIGNING_REQUIRED') - end - end - installer.pods_project.build_configurations.each do |config| - config.build_settings['SWIFT_VERSION'] = '5.0' - config.build_settings.delete('CODE_SIGNING_ALLOWED') - config.build_settings.delete('CODE_SIGNING_REQUIRED') - end -end diff --git a/Example/GuitarTuner/Podfile.lock b/Example/GuitarTuner/Podfile.lock index 6a8220f..cecba3d 100644 --- a/Example/GuitarTuner/Podfile.lock +++ b/Example/GuitarTuner/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - Beethoven (4.0.2): + - Beethoven (5.0.0): - Pitchy (~> 3.0) - Cartography (3.1.0) - Hue (5.0.0) @@ -21,11 +21,11 @@ EXTERNAL SOURCES: :path: "../../" SPEC CHECKSUMS: - Beethoven: 04f99571a7ad2a37184161762be757fe0b8ccc20 + Beethoven: e318691697c38933a69081d4bf4d2506213505fd Cartography: 1988b7578871a56c036e7af17195cb2190edf18c Hue: c129cb67be7d093a82bbbc30ce8a96757bf6f37a Pitchy: 15407e4425b6d88fa3e7eef8927ba659fab52d7a -PODFILE CHECKSUM: 36c8d115bba4c2b8ff7ae95956fd288002fa13e5 +PODFILE CHECKSUM: 09339adf6e9f2003f0c75ae60b9eca9f6719d5e8 -COCOAPODS: 1.8.4 +COCOAPODS: 1.10.1 diff --git a/Source/Library/YINUtil.swift b/Source/Library/YINUtil.swift index 2b970aa..de98f1c 100644 --- a/Source/Library/YINUtil.swift +++ b/Source/Library/YINUtil.swift @@ -38,7 +38,8 @@ final class YINUtil { var vSum: Float = 0.0 for tau in 0 ..< bufferHalfCount { - let bufferTau = UnsafePointer(buffer).advanced(by: tau) + + let bufferTau = buffer.withUnsafeBufferPointer({ $0 }).baseAddress!.advanced(by: tau) // do a diff of buffer with itself at tau offset vDSP_vsub(buffer, 1, bufferTau, 1, &tempBuffer, 1, len) // square each value of the diff vector @@ -90,9 +91,14 @@ final class YINUtil { let fftSetup = vDSP_create_fftsetup(log2n, Int32(kFFTRadix2)) var audioRealp = [Float](repeating: 0, count: inputCount) var audioImagp = [Float](repeating: 0, count: inputCount) - var audioTransformedComplex = DSPSplitComplex(realp: &audioRealp, imagp: &audioImagp) - - let temp = UnsafePointer(buffer) + var audioTransformedComplex:DSPSplitComplex! + audioRealp.withUnsafeMutableBufferPointer { realp in + audioImagp.withUnsafeMutableBufferPointer { imagp in + audioTransformedComplex = DSPSplitComplex(realp: realp.baseAddress!, imagp: imagp.baseAddress!) + } + } + + let temp = buffer.withUnsafeBufferPointer({ $0 }).baseAddress! temp.withMemoryRebound(to: DSPComplex.self, capacity: buffer.count) { (typeConvertedTransferBuffer) -> Void in vDSP_ctoz(typeConvertedTransferBuffer, 2, &audioTransformedComplex, 1, vDSP_Length(inputCount)) @@ -115,9 +121,14 @@ final class YINUtil { var kernelRealp = [Float](repeating: 0, count: frameSize) var kernelImagp = [Float](repeating: 0, count: frameSize) - var kernelTransformedComplex = DSPSplitComplex(realp: &kernelRealp, imagp: &kernelImagp) - - let ktemp = UnsafePointer(kernel) + var kernelTransformedComplex:DSPSplitComplex! + kernelRealp.withUnsafeMutableBufferPointer { realp in + kernelImagp.withUnsafeMutableBufferPointer { imagp in + kernelTransformedComplex = DSPSplitComplex(realp: realp.baseAddress!, imagp: imagp.baseAddress!) + } + } + + let ktemp = kernel.withUnsafeBufferPointer({ $0 }).baseAddress! ktemp.withMemoryRebound(to: DSPComplex.self, capacity: kernel.count) { (typeConvertedTransferBuffer) -> Void in vDSP_ctoz(typeConvertedTransferBuffer, 2, &kernelTransformedComplex, 1, vDSP_Length(inputCount)) @@ -127,7 +138,13 @@ final class YINUtil { var yinStyleACFRealp = [Float](repeating: 0, count: frameSize) var yinStyleACFImagp = [Float](repeating: 0, count: frameSize) - var yinStyleACFComplex = DSPSplitComplex(realp: &yinStyleACFRealp, imagp: &yinStyleACFImagp) + var yinStyleACFComplex:DSPSplitComplex! + yinStyleACFRealp.withUnsafeMutableBufferPointer { realp in + yinStyleACFImagp.withUnsafeMutableBufferPointer { imagp in + yinStyleACFComplex = DSPSplitComplex(realp: realp.baseAddress!, imagp: imagp.baseAddress!) + } + } + for j in 0 ..< inputCount { yinStyleACFRealp[j] = audioRealp[j] * kernelRealp[j] - audioImagp[j] * kernelImagp[j] @@ -211,7 +228,7 @@ final class YINUtil { betterTau = Float(tau) } - return fabs(betterTau) + return abs(betterTau) } class func sumSquare(yinBuffer: [Float], start: Int, end: Int) -> Float { diff --git a/Source/PitchEngine.swift b/Source/PitchEngine.swift index 696b3dd..1f0bfe7 100644 --- a/Source/PitchEngine.swift +++ b/Source/PitchEngine.swift @@ -2,7 +2,7 @@ import UIKit import AVFoundation import Pitchy -public protocol PitchEngineDelegate: class { +public protocol PitchEngineDelegate: AnyObject { func pitchEngine(_ pitchEngine: PitchEngine, didReceivePitch pitch: Pitch) func pitchEngine(_ pitchEngine: PitchEngine, didReceiveError error: Error) func pitchEngineWentBelowLevelThreshold(_ pitchEngine: PitchEngine) @@ -74,15 +74,19 @@ public final class PitchEngine { let audioSession = AVAudioSession.sharedInstance() switch audioSession.recordPermission { - case AVAudioSessionRecordPermission.granted: + case AVAudioSession.RecordPermission.granted: activate() - case AVAudioSessionRecordPermission.denied: + case AVAudioSession.RecordPermission.denied: DispatchQueue.main.async { if let settingsURL = URL(string: UIApplication.openSettingsURLString) { - UIApplication.shared.openURL(settingsURL) + if #available(iOS 10.0, *) { + UIApplication.shared.open(settingsURL, options: [:], completionHandler: nil) + } else { + UIApplication.shared.openURL(settingsURL) + } } } - case AVAudioSessionRecordPermission.undetermined: + case AVAudioSession.RecordPermission.undetermined: AVAudioSession.sharedInstance().requestRecordPermission { [weak self] granted in guard let weakSelf = self else { return } @@ -96,6 +100,8 @@ public final class PitchEngine { weakSelf.activate() } } + @unknown default: + break } } diff --git a/Source/SignalTracking/SignalTracker.swift b/Source/SignalTracking/SignalTracker.swift index 82a21d0..64fb1b7 100644 --- a/Source/SignalTracking/SignalTracker.swift +++ b/Source/SignalTracking/SignalTracker.swift @@ -1,6 +1,6 @@ import AVFoundation -public protocol SignalTrackerDelegate: class { +public protocol SignalTrackerDelegate: AnyObject { func signalTracker(_ signalTracker: SignalTracker, didReceiveBuffer buffer: AVAudioPCMBuffer, atTime time: AVAudioTime) @@ -11,12 +11,12 @@ public enum SignalTrackerMode { case record, playback } -public protocol SignalTracker: class { +public protocol SignalTracker: AnyObject { var mode: SignalTrackerMode { get } var levelThreshold: Float? { get set } var peakLevel: Float? { get } var averageLevel: Float? { get } - var delegate: SignalTrackerDelegate? { get set } + var delegate: SignalTrackerDelegate? { get set } func start() throws func stop() diff --git a/Source/Transform/Strategies/FFTTransformer.swift b/Source/Transform/Strategies/FFTTransformer.swift index 12cba48..00634b8 100644 --- a/Source/Transform/Strategies/FFTTransformer.swift +++ b/Source/Transform/Strategies/FFTTransformer.swift @@ -11,7 +11,13 @@ final class FFTTransformer: Transformer { var realp = [Float](repeating: 0, count: inputCount) var imagp = [Float](repeating: 0, count: inputCount) - var output = DSPSplitComplex(realp: &realp, imagp: &imagp) + + var output:DSPSplitComplex! + realp.withUnsafeMutableBufferPointer { realp in + imagp.withUnsafeMutableBufferPointer { imagp in + output = DSPSplitComplex(realp: realp.baseAddress!, imagp: imagp.baseAddress!) + } + } let windowSize = bufferSizePOT var transferBuffer = [Float](repeating: 0, count: windowSize) @@ -21,8 +27,7 @@ final class FFTTransformer: Transformer { vDSP_vmul((buffer.floatChannelData?.pointee)!, 1, window, 1, &transferBuffer, 1, vDSP_Length(windowSize)) - let temp = UnsafePointer(transferBuffer) - + let temp = transferBuffer.withUnsafeBufferPointer({ $0 }).baseAddress! temp.withMemoryRebound(to: DSPComplex.self, capacity: transferBuffer.count) { (typeConvertedTransferBuffer) -> Void in vDSP_ctoz(typeConvertedTransferBuffer, 2, &output, 1, vDSP_Length(inputCount)) }