Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Cocoa
- import AVFoundation
- class AppDelegate: NSObject, NSApplicationDelegate{
- @IBOutlet weak var window: NSWindow!
- @IBOutlet weak var levelIndicator: NSLevelIndicator!
- @IBOutlet weak var tempoLabel: NSTextField!
- var timer : NSTimer
- var tempo : Int = 60
- var signature = Signature(upper: 4, lower: 4);
- let signature4_4 = Signature(upper: 4, lower: 4), signature3_4 = Signature(upper:3, lower:4);
- let tick = NSSound(named: "tick.wav")
- let tock = NSSound(named: "tock.mp3")
- var recorder : AVAudioRecorder?
- var tuneTimer : NSTimer?
- struct Signature {
- var upper : Int
- var lower : Int;
- };
- override init() {
- timer = NSTimer()
- tuneTimer = nil
- recorder = nil
- super.init();
- }
- func applicationDidFinishLaunching(aNotification: NSNotification?) {
- levelIndicator.maxValue = 4;
- levelIndicator.integerValue = 0;
- tempoLabel.integerValue = 60;
- }
- func applicationWillTerminate(aNotification: NSNotification?) {
- // Insert code here to tear down your application
- }
- @IBAction func playStop(sender: AnyObject) {
- if !timer.valid{
- println(60/tempo)
- timer = NSTimer(timeInterval: NSTimeInterval(60/Double(tempo)), target: self, selector: "update", userInfo: nil, repeats: true)
- NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
- }
- else{
- timer.invalidate()
- }
- }
- func update()
- {
- tock?.stop()
- tick?.stop()
- levelIndicator.integerValue++
- if ((Double)(levelIndicator.integerValue) == levelIndicator.maxValue + 1)
- {
- levelIndicator.integerValue = 1;
- tick?.play()
- }else{
- tock?.play()
- }
- }
- @IBAction func changeSignature(sender: AnyObject) {
- switch(sender.indexOfSelectedItem())
- {
- case 0:
- playStop(self)
- signature = signature4_4;
- break;
- case 1:
- playStop(self)
- signature = signature3_4;
- break;
- default:
- break
- }
- levelIndicator.integerValue = 1
- levelIndicator.maxValue = Double(signature.upper)
- playStop(self)
- }
- func control(_control: NSControl,
- textShouldEndEditing fieldEditor: NSText) -> Bool
- {
- if (tempoLabel.integerValue <= 0)
- {
- return false
- }
- tempo = tempoLabel.integerValue
- timer.invalidate()
- println(tempo)
- playStop(self)
- return true
- }
- @IBAction func tune(sender: AnyObject) {
- let url = NSURL(fileURLWithPath: "/dev/null")
- var settings = NSDictionary()
- settings.setValue(NSNumber(double: 44100.0), forKey: AVSampleRateKey)
- //settings.setValue(NSNumber(int: kAudioFormatAppleLossless), forKey: AVFormatIDKey)
- settings.setValue(NSNumber(int: 1), forKey: AVNumberOfChannelsKey)
- settings.setValue(NSNumber(int: 0x7F), forKey: AVEncoderAudioQualityKey)
- var error : NSError?
- recorder = AVAudioRecorder(URL: url, settings: settings, error: &error)
- if (recorder != nil)
- {
- recorder?.prepareToRecord()
- recorder?.meteringEnabled = true
- recorder?.record()
- tuneTimer = NSTimer(timeInterval: 0.03, target: self, selector: "tuneUpdate", userInfo: nil, repeats: true)
- }
- else
- {
- println(error?.description)
- }
- }
- func tuneUpdate()
- {
- recorder?.updateMeters()
- println(recorder?.averagePowerForChannel(0))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement