AVAudioPlayerにレベルメータを付けました。Swiftで書かれていますが、最適ではありません。
// AppDelegate.swift import Cocoa import AVFoundation var player : AVAudioPlayer! @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate, AVAudioPlayerDelegate { @IBOutlet weak var window: NSWindow! @IBOutlet weak var myCustomView: NSView! func applicationDidFinishLaunching(aNotification: NSNotification) { self.window.delegate = self NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "update", userInfo: nil, repeats: true) let audioPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("test", ofType: "wav")!) player = try! AVAudioPlayer(contentsOfURL: audioPath, fileTypeHint: nil) player.meteringEnabled = true player.prepareToPlay() player.play() player.delegate = self } func update() { myCustomView.display() } }
// MyView.swift import Cocoa class MyView: NSView { override func drawRect(dirtyRect: NSRect) { super.drawRect(dirtyRect) NSColor.grayColor().set() NSRectFill(self.bounds) let rect4 = NSMakeRect (20, 80, 600, 10) NSColor(red: 0.4, green: 0.4, blue: 0.8, alpha: 1.0).set(); NSRectFill(rect4) let dur = player?.duration let cur = player?.currentTime if cur != nil && dur != nil { let y3 = CGFloat( 600.0 * cur! / dur!) let rect3 = NSMakeRect (20 + y3, 80, 10, 10) NSColor(red: 1.0, green: 1.0, blue: 0.0, alpha: 1.0).set(); NSRectFill(rect3) for var ch: Int = 0; ch < 2; ch++ { switch ch { case 0: NSColor(red: 0.8, green: 1.0, blue: 0.4, alpha: 1.0).set() case 1: NSColor(red: 0.4, green: 0.8, blue: 1.0, alpha: 1.0).set() default: break } player?.updateMeters() let ave = player?.averagePowerForChannel(ch) if ave != nil { let nbar = CGFloat (600.0 * (ave! + 160.0) / 160.0) let y2 = CGFloat (10 + 35 * ch) let rect2 = NSMakeRect(20, y2, nbar, 30) NSRectFill(rect2) } } let font = NSFont.boldSystemFontOfSize(40) let textFontAttributes = [ NSFontAttributeName: font, NSForegroundColorAttributeName: NSColor.whiteColor() ] let dd : Int = Int(dur!) let cc : Int = Int(cur!) let s:String = " duration: \(dd)sec, current: \(cc)sec" s.drawInRect(self.bounds, withAttributes: textFontAttributes) } } }