Swift でwavファイルのパラメーターを取得

by oganosin. 0 Comments

おそらくこれでとれそう、サンプリングレートだけわからん・・・。


import AudioToolbox

var fileID:AudioFileID = nil
var err:OSStatus = Int32(noErr)
let cfMusicUrl:CFURLRef = NSURL(fileURLWithPath: input)!
err = AudioFileOpenURL(cfMusicUrl, Int8(kAudioFileReadPermission), 0, &fileID)
if err != Int32(noErr) {
// error pattern
}

var piDict:NSDictionary = NSDictionary()
var piDataSize : UInt32 = UInt32(sizeof(NSDictionary))
err = AudioFileGetProperty(fileID, UInt32(kAudioFilePropertyInfoDictionary), &piDataSize, &piDict)
if err != Int32(noErr) {
// error pattern
}
println(piDict)

// get bit depth
var sizze:UInt32 = 0
var val:UInt32 = 0
err = AudioFileGetPropertyInfo(fileID, UInt32(kAudioFilePropertySourceBitDepth), &sizze, nil)
err = AudioFileGetProperty(fileID, UInt32(kAudioFilePropertySourceBitDepth), &sizze, &val)
if err != Int32(noErr) {
// error pattern
}
println(val)

// get bit rate
err = AudioFileGetPropertyInfo(fileID, UInt32(kAudioFilePropertyBitRate), &sizze, nil)
err = AudioFileGetProperty(fileID, UInt32(kAudioFilePropertyBitRate), &sizze, &val)
if err == Int32(noErr) {
// error pattern
}
println(val)

// get sampling rate
err = AudioFileGetPropertyInfo(fileID, UInt32(kAudioFilePropertyFrameToPacket), &sizze, nil)
err = AudioFileGetProperty(fileID, UInt32(kAudioFilePropertyFrameToPacket), &sizze, &val)
if err != Int32(noErr) {
// error pattern
}
println(val)