Class: Sndfile::File

Inherits:
Object
  • Object
show all
Includes:
SndfileApi
Defined in:
lib/sndfile/file.rb

Constant Summary

Constants included from Enums

Enums::Command, Enums::ENCODING_MASK, Enums::ENDIAN_MASK, Enums::Encoding, Enums::Endian, Enums::ErrorCode, Enums::FORMAT_MASK, Enums::FileMode, Enums::Format

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, opts = {}) ⇒ File

Create a File instance. Options are:

:mode => :READ (default), :WRITE, or :RDWR
:format => see list at File#format  (default is :WAV)
:encoding => see list at File#encoding (default is :PCM_16)
:endian => see list at File#endian (default is :FILE)
:samplerate => default is 44100
:channles => default is 2

When mode is :READ, then :format, :channels, and :samplerate are ignored – unless the format is :RAW

May raise Sndfile::Error on various error conditions



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sndfile/file.rb', line 48

def initialize(path,opts={})
  opts = opts.keyword_args(:mode => { :valid => Enums::FileMode.symbols, :default => :READ },
                           :format => { :valid => Enums::Format.symbols, :default => :WAV },
                           :encoding => { :valid => Enums::Encoding.symbols, :default => :PCM_16 },
                           :endian => { :valid => Enums::Endian.symbols, :default => :FILE },
                           :samplerate => 44100,
                           :channels => 2
                          )

  @path = path
  sfinfo = SfInfo.new
  if opts.mode == :WRITE or opts.format == :RAW
    sfinfo[:format] = Enums::Format[opts.format]|Enums::Encoding[opts.encoding]|Enums::Endian[opts.endian]
    sfinfo[:channels] = opts.channels
    sfinfo[:samplerate] = opts.samplerate
  end
  @sfpointer = sf_open(path.to_s, opts.mode, sfinfo)
  @info = Info.from_sfinfo(sfinfo)

  check_error
  sf_command @sfpointer, :SFC_SET_CLIPPING, nil, 1
  check_error
end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



6
7
8
# File 'lib/sndfile/file.rb', line 6

def info
  @info
end

Class Method Details

.info(path) ⇒ Object

Returns the ‘Info` for a file.

Equivalent to File.open(path) { |f| f.info }



29
30
31
# File 'lib/sndfile/file.rb', line 29

def self.info(path)
  open(path) { |f| f.info }
end

.open(path, opts = {}) ⇒ Object

Without a block, this is the same as File.new(path, opts). With a block, yields the File object to the block and ensures that File#close is then called .



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sndfile/file.rb', line 13

def self.open(path, opts={})
  file = new(path, opts)
  if block_given?
    begin
      yield file
    ensure
      file.close
    end
  else
    file
  end
end

Instance Method Details

#closeObject

Closes the File instance.

May raise Sndfile::Error for various error conditions



75
76
77
# File 'lib/sndfile/file.rb', line 75

def close
  check_error sf_close(@sfpointer)
end

#read(nframes) ⇒ Object

Reads frames from the file, returning the data in a GSLng::Matrix of dimensions frames x channels. (For convenience, the height and width methods of GSLng::Matrix are aliased as GSLng::Matrix#frames and GSLng::Matrix#channels)

Normally the requested number of frames is read, unless end of file is reached. If no frames are read (i.e. already at end of file), returns nil instead of a GSLng::Matrix.

May raise Sndfile::Error in case of error.



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sndfile/file.rb', line 97

def read(nframes)

  buf = GSLng::Matrix.new(nframes, info.channels)

  count = sf_readf_double @sfpointer, buf.data_ptr, nframes
  check_error
  case count
  when 0 then nil
  when nframes then buf
  else buf.view(0, 0, count, info.channels)
  end
end

#write(buf) ⇒ Object

Writes frames to the file. The data must be (quack like) a GSLng::Matrix with dimensions frames x channels. The matrix can contain any number of frames.

When writing to a file with integer encoding, values are clipped to [-1, 1].

May raise Sndfile::Error in case of error.



118
119
120
121
# File 'lib/sndfile/file.rb', line 118

def write(buf)
  sf_writef_double @sfpointer, buf.data_ptr, buf.frames
  check_error
end