Advertisement
Guest User

Untitled

a guest
Oct 29th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1.  
  2. import Cocoa
  3. import AVFoundation
  4.  
  5. class AppDelegate: NSObject, NSApplicationDelegate{
  6.  
  7. @IBOutlet weak var window: NSWindow!
  8. @IBOutlet weak var levelIndicator: NSLevelIndicator!
  9. @IBOutlet weak var tempoLabel: NSTextField!
  10.  
  11. var timer : NSTimer
  12. var tempo : Int = 60
  13. var signature = Signature(upper: 4, lower: 4);
  14. let signature4_4 = Signature(upper: 4, lower: 4), signature3_4 = Signature(upper:3, lower:4);
  15. let tick = NSSound(named: "tick.wav")
  16. let tock = NSSound(named: "tock.mp3")
  17.  
  18.  
  19.  
  20. var recorder : AVAudioRecorder?
  21. var tuneTimer : NSTimer?
  22.  
  23.  
  24.  
  25. struct Signature {
  26. var upper : Int
  27. var lower : Int;
  28. };
  29.  
  30. override init() {
  31. timer = NSTimer()
  32. tuneTimer = nil
  33. recorder = nil
  34. super.init();
  35. }
  36.  
  37. func applicationDidFinishLaunching(aNotification: NSNotification?) {
  38. levelIndicator.maxValue = 4;
  39. levelIndicator.integerValue = 0;
  40. tempoLabel.integerValue = 60;
  41. }
  42.  
  43. func applicationWillTerminate(aNotification: NSNotification?) {
  44. // Insert code here to tear down your application
  45. }
  46.  
  47. @IBAction func playStop(sender: AnyObject) {
  48. if !timer.valid{
  49. println(60/tempo)
  50. timer = NSTimer(timeInterval: NSTimeInterval(60/Double(tempo)), target: self, selector: "update", userInfo: nil, repeats: true)
  51. NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
  52. }
  53. else{
  54. timer.invalidate()
  55. }
  56. }
  57.  
  58. func update()
  59. {
  60. tock?.stop()
  61. tick?.stop()
  62. levelIndicator.integerValue++
  63. if ((Double)(levelIndicator.integerValue) == levelIndicator.maxValue + 1)
  64. {
  65. levelIndicator.integerValue = 1;
  66. tick?.play()
  67. }else{
  68. tock?.play()
  69. }
  70. }
  71.  
  72. @IBAction func changeSignature(sender: AnyObject) {
  73.  
  74. switch(sender.indexOfSelectedItem())
  75. {
  76. case 0:
  77. playStop(self)
  78. signature = signature4_4;
  79. break;
  80. case 1:
  81. playStop(self)
  82. signature = signature3_4;
  83. break;
  84. default:
  85. break
  86.  
  87. }
  88. levelIndicator.integerValue = 1
  89. levelIndicator.maxValue = Double(signature.upper)
  90. playStop(self)
  91.  
  92. }
  93. func control(_control: NSControl,
  94. textShouldEndEditing fieldEditor: NSText) -> Bool
  95. {
  96. if (tempoLabel.integerValue <= 0)
  97. {
  98. return false
  99. }
  100. tempo = tempoLabel.integerValue
  101. timer.invalidate()
  102. println(tempo)
  103. playStop(self)
  104. return true
  105.  
  106. }
  107. @IBAction func tune(sender: AnyObject) {
  108. let url = NSURL(fileURLWithPath: "/dev/null")
  109. var settings = NSDictionary()
  110. settings.setValue(NSNumber(double: 44100.0), forKey: AVSampleRateKey)
  111. //settings.setValue(NSNumber(int: kAudioFormatAppleLossless), forKey: AVFormatIDKey)
  112. settings.setValue(NSNumber(int: 1), forKey: AVNumberOfChannelsKey)
  113. settings.setValue(NSNumber(int: 0x7F), forKey: AVEncoderAudioQualityKey)
  114. var error : NSError?
  115. recorder = AVAudioRecorder(URL: url, settings: settings, error: &error)
  116.  
  117. if (recorder != nil)
  118. {
  119. recorder?.prepareToRecord()
  120. recorder?.meteringEnabled = true
  121. recorder?.record()
  122. tuneTimer = NSTimer(timeInterval: 0.03, target: self, selector: "tuneUpdate", userInfo: nil, repeats: true)
  123. }
  124. else
  125. {
  126. println(error?.description)
  127. }
  128.  
  129. }
  130. func tuneUpdate()
  131. {
  132. recorder?.updateMeters()
  133. println(recorder?.averagePowerForChannel(0))
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement