Swift and Cocoa

clockBargraph2

Now trying to write everything in Swift.

//  MyView.swift

import Cocoa

class MyView: NSView {
    
    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
        
        NSColor.brownColor().set()
        NSRectFill(self.bounds)
        
        let font = NSFont.boldSystemFontOfSize(70)
        let textFontAttributes = [
            NSFontAttributeName: font,
            NSForegroundColorAttributeName: NSColor.whiteColor()
        ]
        
        let now = NSDate()
        let df = NSDateFormatter()
        df.dateFormat = "HH:mm:ss"
        var s = df.stringFromDate(now)
        s.drawInRect(self.bounds, withAttributes: textFontAttributes)
        
        let hhmmss: [String] = ["hh", "mm", "ss"]
        
        for (var j: Int = 0; j<3; j++) {
            
            df.dateFormat = hhmmss[j]
            s = df.stringFromDate(now)
            switch j {
            case 0: NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0).set()
            case 1: NSColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0).set()
            case 2: NSColor(red: 0.0, green: 0.0, blue: 1.0, alpha: 1.0).set()
            default: break
            }
            for (var i: Int = 0; i<Int(s); i++) {
                let x = CGFloat(  20 + 10*i )
                let y = CGFloat( 150 + i%10 - j*50)
                let rect = NSMakeRect(x, y, 7, 40)
                NSRectFill(rect)
            }
            
        }
    }
}
//  AppDelegate.swift

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var myCustomView: NSView!

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
        
        self.window.delegate = self
        NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "update", userInfo: nil, repeats: true)
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }

    func update() {
        myCustomView.display()
    }
}

AVAudioPlayer

//
//  AppDelegate.swift
//

import Cocoa
import AVFoundation

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!
    
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        let audioPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("test", ofType: "wav")!)
        let player : AVAudioPlayer
        player = try! AVAudioPlayer(contentsOfURL: audioPath, fileTypeHint: nil)
        player.prepareToPlay()
        player.play()
    }
}

Note that I am using Xcode 7.2.1. At line 16, if you do not use try, you will get the error message, Call can throw, but it is not marked with `try’ and the error is not handled.

try

I am using try! to disable error propagation.

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html

trybang

GLFW and Xcode

project102

Here is how you use GLFW with Xcode.

project102a

First, you add some frameworks.

project102b

Then, the search paths for header files and for libraries.

project102c

Finally, a linker flag.

LIGO Detected Gravitational Waves from Black Holes

LIGO1

No, this is not a waterfall of CW signals, but..

On September 14, 2015 at 09:50:45 UTC the two detectors of the Laser Interferometer Gravitational-Wave Observatory simultaneously observed a transient gravitational-wave signal. The signal sweeps upwards in frequency from 35 to 250 Hz with a peak gravitational-wave strain of 1.0 × 10−21.

The paper, Observation of Gravitational Waves from a Binary Black Hole Merger, is here: http://journals.aps.org/prl/pdf/10.1103/PhysRevLett.116.061102

LIGO2

Very nice and clean results just as is predicted by theories and simulations.

Go forward to 31m:11s.

Orpheus and Mac mini

orpheus

Using a Thunderbolt to FireWire adapter and a FireWire 800-400(9Pin-6Pin)cable, I connected my Orpheus to a Mac mini (El Capitan 10.11.3).

CarminaCeltica

I can play a 192kHz/24bit audio file all right, although I do not know what is happening internally.

orpheus2