-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from nathanntg/meters
Input and output level meters
- Loading branch information
Showing
3 changed files
with
156 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// SummaryStat.swift | ||
// SyllableDetector | ||
// | ||
// Created by Nathan Perkins on 11/11/15. | ||
// Copyright © 2015 Gardner Lab. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol Stat | ||
{ | ||
mutating func appendValue(value: Double) | ||
func readStat() -> Double? | ||
mutating func resetStat() | ||
} | ||
|
||
struct StatMean: Stat | ||
{ | ||
var sum: Double = 0.0 | ||
var count: Int = 0 | ||
|
||
mutating func appendValue(value: Double) { | ||
sum += value | ||
++count | ||
} | ||
|
||
func readStat() -> Double? { | ||
guard 0 < count else { return nil } | ||
return sum / Double(count) | ||
} | ||
|
||
mutating func resetStat() { | ||
sum = 0.0 | ||
count = 0 | ||
} | ||
} | ||
|
||
struct StatMax: Stat | ||
{ | ||
var largest: Double? | ||
|
||
mutating func appendValue(value: Double) { | ||
if let cur = largest { | ||
if value > cur { | ||
largest = value | ||
} | ||
} | ||
else { | ||
largest = value | ||
} | ||
} | ||
|
||
func readStat() -> Double? { | ||
return largest | ||
} | ||
|
||
mutating func resetStat() { | ||
largest = nil | ||
} | ||
} | ||
|
||
class SummaryStat | ||
{ | ||
private var stat: Stat | ||
private var queue: dispatch_queue_t | ||
|
||
init(withStat stat: Stat) { | ||
self.stat = stat | ||
self.queue = dispatch_queue_create("SummaryStat\(stat)", DISPATCH_QUEUE_SERIAL) | ||
} | ||
|
||
func writeValue(value: Double) { | ||
dispatch_async(queue) { | ||
self.stat.appendValue(value) | ||
} | ||
} | ||
|
||
func readStatAndReset() -> Double? { | ||
var ret: Double? | ||
dispatch_sync(queue) { | ||
ret = self.stat.readStat() | ||
self.stat.resetStat() | ||
} | ||
return ret | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters