ruby-cafinfo

by Oleguer Huguet Ibars

DESCRIPTION:

A ruby library to retrieve information of CAF (Core Audio Format) files, heavily inspired by the excellent ruby-mp3info.

From the CAF format specification:

“Apple’s Core Audio Format (CAF) is a file format for storing and transporting digital audio data. It simplifies the management and manipulation of many types of audio data without the file-size limitations of other audio file formats.”

Specs for CAF format can be found here.

FEATURES:

  • written in pure ruby

  • read low-level informations like bitrate, length, samplerate, etc…

  • only some chunk types are recognized (Audio Description, Data, Free, Magick Cookie)

SYNOPSIS:

Using as a lib is easy:

require "rubygems"
require "cafinfo"
# read and display info
CafInfo.open("myfile.caf") do |cafinfo|
  puts cafinfo
end

# read and display some attributes
CafInfo.open("myfile.caf") do |caf|
  puts caf.length
  puts caf.samplerate
  puts caf.format
end

# extract payload audio data from a (small) CAF and write to another file
audio_pos, audio_length = CafInfo.open("myfile.caf") { |caf| caf.audio_content }
File.open("myfile.caf", "rb") do |caf_file|
  File.open("myaudio.data", "wb") do |f|
    caf_file.seek(audio_pos)
    data = caf_file.read(audio_length)
    f.write(data)
  end
end

# extract payload audio data from an arbitrarily large CAF and write
# to another file, considering that data chunk may not be at the end
# of caf file
audio_pos, audio_length = CafInfo.open("myfile.caf") { |caf| caf.audio_content }
File.open("myfile.caf", "rb") do |caf_file|
  File.open("myaudio-buffered.data", "wb") do |f|
    caf_file.seek(audio_pos)
    while buffer = caf_file.read([4096, audio_length].min) and buffer.size > 0
      f.write(buffer)
      audio_length -= buffer.size
    end
  end
end

INSTALL:

# gem install ruby-cafinfo

LICENSE:

MIT license

TODO:

  • support for other chunk types

  • tests