Skip to content

Commit

Permalink
Migrate the source code to Swift 4
Browse files Browse the repository at this point in the history
  • Loading branch information
vadymmarkov committed Oct 9, 2017
1 parent d62df86 commit 865f42f
Show file tree
Hide file tree
Showing 28 changed files with 72 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0
4.0
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "Quick/Nimble" "v7.0.2"
github "Quick/Quick" "v1.2.0"
github "vadymmarkov/Pitchy" "2.0.1"
github "vadymmarkov/Pitchy" "3.0.0"
5 changes: 2 additions & 3 deletions Source/Estimation/EstimationFactory.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
struct EstimationFactory {

static func create(_ strategy: EstimationStrategy) -> Estimator {
final class EstimationFactory {
func create(_ strategy: EstimationStrategy) -> Estimator {
let estimator: Estimator

switch strategy {
Expand Down
8 changes: 2 additions & 6 deletions Source/Estimation/Estimator.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
protocol Estimator {
var transformer: Transformer { get }

func estimateFrequency(sampleRate: Float, buffer: Buffer) throws -> Float
func estimateFrequency(sampleRate: Float, location: Int, bufferCount: Int) -> Float
}

extension Estimator {

// MARK: - Default implementation
// MARK: - Default implementations

extension Estimator {
func estimateFrequency(sampleRate: Float, location: Int, bufferCount: Int) -> Float {
return Float(location) * sampleRate / (Float(bufferCount) * 2)
}

// MARK: - Helpers

func maxBufferIndex(from buffer: [Float]) throws -> Int {
guard buffer.count > 0 else {
throw EstimationError.emptyBuffer
Expand Down
5 changes: 2 additions & 3 deletions Source/Estimation/LocationEstimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ protocol LocationEstimator: Estimator {
func estimateLocation(buffer: Buffer) throws -> Int
}

extension LocationEstimator {

// MARK: - Default implementation
// MARK: - Default implementation

extension LocationEstimator {
var transformer: Transformer {
return FFTTransformer()
}
Expand Down
3 changes: 1 addition & 2 deletions Source/Estimation/Strategies/BarycentricEstimator.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation

struct BarycentricEstimator: LocationEstimator {

final class BarycentricEstimator: LocationEstimator {
func estimateLocation(buffer: Buffer) throws -> Int {
let elements = buffer.elements
let maxIndex = try maxBufferIndex(from: elements)
Expand Down
7 changes: 3 additions & 4 deletions Source/Estimation/Strategies/HPSEstimator.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
struct HPSEstimator: LocationEstimator {

let harmonics = 5
let minIndex = 20
final class HPSEstimator: LocationEstimator {
private let harmonics = 5
private let minIndex = 20

func estimateLocation(buffer: Buffer) throws -> Int {
var spectrum = buffer.elements
Expand Down
3 changes: 1 addition & 2 deletions Source/Estimation/Strategies/JainsEstimator.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation

struct JainsEstimator: LocationEstimator {

final class JainsEstimator: LocationEstimator {
func estimateLocation(buffer: Buffer) throws -> Int {
let elements = buffer.elements
let maxIndex = try maxBufferIndex(from: elements)
Expand Down
3 changes: 1 addition & 2 deletions Source/Estimation/Strategies/MaxValueEstimator.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
struct MaxValueEstimator: LocationEstimator {

final class MaxValueEstimator: LocationEstimator {
func estimateLocation(buffer: Buffer) throws -> Int {
return try maxBufferIndex(from: buffer.elements)
}
Expand Down
3 changes: 1 addition & 2 deletions Source/Estimation/Strategies/QuadradicEstimator.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation

struct QuadradicEstimator: LocationEstimator {

final class QuadradicEstimator: LocationEstimator {
func estimateLocation(buffer: Buffer) throws -> Int {
let elements = buffer.elements
let maxIndex = try maxBufferIndex(from: elements)
Expand Down
3 changes: 1 addition & 2 deletions Source/Estimation/Strategies/QuinnsFirstEstimator.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation

struct QuinnsFirstEstimator: LocationEstimator {

final class QuinnsFirstEstimator: LocationEstimator {
func estimateLocation(buffer: Buffer) throws -> Int {
let elements = buffer.elements
let maxIndex = try maxBufferIndex(from: elements)
Expand Down
5 changes: 2 additions & 3 deletions Source/Estimation/Strategies/QuinnsSecondEstimator.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation

struct QuinnsSecondEstimator: LocationEstimator {

final class QuinnsSecondEstimator: LocationEstimator {
func estimateLocation(buffer: Buffer) throws -> Int {
let elements = buffer.elements
let maxIndex = try maxBufferIndex(from: elements)
Expand All @@ -27,7 +26,7 @@ struct QuinnsSecondEstimator: LocationEstimator {
return sanitize(location: location, reserveLocation: maxIndex, elements: elements)
}

func tau(_ x: Float) -> Float {
private func tau(_ x: Float) -> Float {
let p1 = log(3 * pow(x, 2.0) + 6 * x + 1)
let part1 = x + 1 - sqrt(2/3)
let part2 = x + 1 + sqrt(2/3)
Expand Down
3 changes: 1 addition & 2 deletions Source/Estimation/Strategies/YINEstimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

import UIKit

struct YINEstimator: Estimator {

final class YINEstimator: Estimator {
let transformer: Transformer = YINTransformer()
let threshold: Float = 0.05

Expand Down
1 change: 0 additions & 1 deletion Source/Library/Buffer.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
struct Buffer {

var elements: [Float]
var realElements: [Float]?
var imagElements: [Float]?
Expand Down
1 change: 0 additions & 1 deletion Source/Library/Extensions/Array+Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
extension Array where Element:Comparable {

static func fromUnsafePointer(_ data: UnsafePointer<Element>, count: Int) -> [Element] {
let buffer = UnsafeBufferPointer(start: data, count: count)
return Array(buffer)
Expand Down
1 change: 1 addition & 0 deletions Source/Library/YINUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ final class YINUtil {
// https://code.soundsoftware.ac.uk/projects/pyin/repository but I don't know what
//
// Kept for reference only.
// swiftlint:disable function_body_length
class func difference_broken_do_not_use(buffer: [Float]) -> [Float] {
let frameSize = buffer.count
let yinBufferSize = frameSize / 2
Expand Down
29 changes: 16 additions & 13 deletions Source/PitchEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ public protocol PitchEngineDelegate: class {
func pitchEngineWentBelowLevelThreshold(_ pitchEngine: PitchEngine)
}

public enum PitchEngineError: Error {
case recordPermissionDenied
}

public final class PitchEngine {
public enum Error: Swift.Error {
case recordPermissionDenied
}

public let bufferSize: AVAudioFrameCount
public var active = false
public private(set) var active = false
public weak var delegate: PitchEngineDelegate?

private var estimator: Estimator
private var signalTracker: SignalTracker
private var queue: DispatchQueue
private let estimator: Estimator
private let signalTracker: SignalTracker
private let queue: DispatchQueue

public var mode: SignalTrackerMode {
return signalTracker.mode
Expand All @@ -44,7 +44,9 @@ public final class PitchEngine {
signalTracker: SignalTracker? = nil,
delegate: PitchEngineDelegate? = nil) {
bufferSize = config.bufferSize
estimator = EstimationFactory.create(config.estimationStrategy)

let factory = EstimationFactory()
estimator = factory.create(config.estimationStrategy)

if let signalTracker = signalTracker {
self.signalTracker = signalTracker
Expand All @@ -56,7 +58,7 @@ public final class PitchEngine {
}
}

queue = DispatchQueue(label: "BeethovenQueue", attributes: [])
self.queue = DispatchQueue(label: "BeethovenQueue", attributes: [])
self.signalTracker.delegate = self
self.delegate = delegate
}
Expand Down Expand Up @@ -85,8 +87,10 @@ public final class PitchEngine {
guard let weakSelf = self else { return }

guard granted else {
weakSelf.delegate?.pitchEngineDidReceiveError(weakSelf,
error: PitchEngineError.recordPermissionDenied as Error)
weakSelf.delegate?.pitchEngineDidReceiveError(
weakSelf,
error: Error.recordPermissionDenied
)
return
}

Expand Down Expand Up @@ -115,7 +119,6 @@ public final class PitchEngine {
// MARK: - SignalTrackingDelegate

extension PitchEngine: SignalTrackerDelegate {

public func signalTracker(_ signalTracker: SignalTracker,
didReceiveBuffer buffer: AVAudioPCMBuffer,
atTime time: AVAudioTime) {
Expand Down
18 changes: 8 additions & 10 deletions Source/SignalTracking/Units/InputSignalTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ public enum InputSignalTrackerError: Error {
case inputNodeMissing
}

class InputSignalTracker: SignalTracker {

final class InputSignalTracker: SignalTracker {
weak var delegate: SignalTrackerDelegate?
var levelThreshold: Float?

fileprivate let bufferSize: AVAudioFrameCount
fileprivate var audioChannel: AVCaptureAudioChannel?
fileprivate let captureSession = AVCaptureSession()
fileprivate var audioEngine: AVAudioEngine?
fileprivate let session = AVAudioSession.sharedInstance()
fileprivate let bus = 0
private let bufferSize: AVAudioFrameCount
private var audioChannel: AVCaptureAudioChannel?
private let captureSession = AVCaptureSession()
private var audioEngine: AVAudioEngine?
private let session = AVAudioSession.sharedInstance()
private let bus = 0

var peakLevel: Float? {
return audioChannel?.peakHoldLevel
Expand All @@ -34,7 +33,6 @@ class InputSignalTracker: SignalTracker {
delegate: SignalTrackerDelegate? = nil) {
self.bufferSize = bufferSize
self.delegate = delegate

setupAudio()
}

Expand Down Expand Up @@ -84,7 +82,7 @@ class InputSignalTracker: SignalTracker {
captureSession.stopRunning()
}

fileprivate func setupAudio() {
private func setupAudio() {
do {
let audioDevice = AVCaptureDevice.default(for: AVMediaType.audio)
let audioCaptureInput = try AVCaptureDeviceInput(device: audioDevice!)
Expand Down
14 changes: 6 additions & 8 deletions Source/SignalTracking/Units/OutputSignalTracker.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import AVFoundation

class OutputSignalTracker: SignalTracker {

final class OutputSignalTracker: SignalTracker {
weak var delegate: SignalTrackerDelegate?
var levelThreshold: Float?

fileprivate let bufferSize: AVAudioFrameCount
fileprivate let audioUrl: URL

fileprivate var audioEngine: AVAudioEngine!
fileprivate var audioPlayer: AVAudioPlayerNode!
fileprivate let bus = 0
private let bufferSize: AVAudioFrameCount
private let audioUrl: URL
private var audioEngine: AVAudioEngine!
private var audioPlayer: AVAudioPlayerNode!
private let bus = 0

var peakLevel: Float? {
return 0.0
Expand Down
28 changes: 13 additions & 15 deletions Source/SignalTracking/Units/SimulatorSignalTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,18 @@ import AVFoundation
* #endif
*
*/
public class SimulatorSignalTracker: SignalTracker {
public final class SimulatorSignalTracker: SignalTracker {
private static let sampleRate = 8000.0
private static let sampleCount = 1024

public var mode: SignalTrackerMode = .record

public var levelThreshold: Float?

public var peakLevel: Float?

public var averageLevel: Float?

public weak var delegate: SignalTrackerDelegate?

private var frequencies: [Double]?
private var delay: Int

private static let sampleRate = 8000.0
private static let sampleCount = 1024
private let frequencies: [Double]?
private let delay: Int

public init(delegate: SignalTrackerDelegate? = nil, frequencies: [Double]? = nil, delayMs: Int = 0) {
self.delegate = delegate
Expand All @@ -57,34 +52,37 @@ public class SimulatorSignalTracker: SignalTracker {
guard let frequencies = self.frequencies else { return }

let time = AVAudioTime(sampleTime: 0, atRate: SimulatorSignalTracker.sampleRate)

var i = 0

for frequency in frequencies {
let buffer = createPCMBuffer(frequency)

if i == 0 {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(50), execute: {
self.delegate?.signalTracker(self, didReceiveBuffer: buffer, atTime: time)
})

} else {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(delay * i), execute: {
self.delegate?.signalTracker(self, didReceiveBuffer: buffer, atTime: time)
})
}

i += 1
}

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(delay * i), execute: {
self.delegate?.signalTrackerWentBelowLevelThreshold(self)
})
}

public func stop() {
}
public func stop() {}

private func createPCMBuffer(_ frequency: Double) -> AVAudioPCMBuffer {
let format = AVAudioFormat(standardFormatWithSampleRate: SimulatorSignalTracker.sampleRate, channels: 1)
let buffer = AVAudioPCMBuffer(pcmFormat: format!, frameCapacity: AVAudioFrameCount(SimulatorSignalTracker.sampleCount))
let buffer = AVAudioPCMBuffer(
pcmFormat: format!,
frameCapacity: AVAudioFrameCount(SimulatorSignalTracker.sampleCount)
)

if let channelData = buffer?.floatChannelData {
let velocity = Float32(2.0 * .pi * frequency / SimulatorSignalTracker.sampleRate)
Expand Down
5 changes: 2 additions & 3 deletions Source/Transform/Strategies/FFTTransformer.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import AVFoundation
import Accelerate

struct FFTTransformer: Transformer {

final class FFTTransformer: Transformer {
func transform(buffer: AVAudioPCMBuffer) throws -> Buffer {
let frameCount = buffer.frameLength
let log2n = UInt(round(log2(Double(frameCount))))
Expand Down Expand Up @@ -46,7 +45,7 @@ struct FFTTransformer: Transformer {

// MARK: - Helpers

func sqrtq(_ x: [Float]) -> [Float] {
private func sqrtq(_ x: [Float]) -> [Float] {
var results = [Float](repeating: 0.0, count: x.count)
vvsqrtf(&results, x, [Int32(x.count)])

Expand Down
2 changes: 1 addition & 1 deletion Source/Transform/Strategies/SimpleTransformer.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AVFoundation

struct SimpleTransformer: Transformer {
final class SimpleTransformer: Transformer {
enum SimpleTransformerError: Error {
case floatChannelDataIsNil
}
Expand Down
Loading

0 comments on commit 865f42f

Please sign in to comment.