Class: Hindbaer::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/hindbaer/session.rb

Constant Summary collapse

ATTRIBUTES =
%w{
version samplerate
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Session

Returns a new instance of Session.



39
40
41
42
43
44
45
46
47
# File 'lib/hindbaer/session.rb', line 39

def initialize(&block)
  self.info = {}
  self.audio_pool = {}
  self.tracks = []
  self.clipboard_groups = []
  self.markers = []
  
  block.arity > 0 ? block.call(self) : instance_eval(&block)
end

Instance Attribute Details

#audio_poolObject

Returns the value of attribute audio_pool.



9
10
11
# File 'lib/hindbaer/session.rb', line 9

def audio_pool
  @audio_pool
end

#clipboard_groupsObject

Returns the value of attribute clipboard_groups.



9
10
11
# File 'lib/hindbaer/session.rb', line 9

def clipboard_groups
  @clipboard_groups
end

#infoObject

Returns the value of attribute info.



9
10
11
# File 'lib/hindbaer/session.rb', line 9

def info
  @info
end

#markersObject

Returns the value of attribute markers.



9
10
11
# File 'lib/hindbaer/session.rb', line 9

def markers
  @markers
end

#tracksObject

Returns the value of attribute tracks.



9
10
11
# File 'lib/hindbaer/session.rb', line 9

def tracks
  @tracks
end

Class Method Details

.parse(xml) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hindbaer/session.rb', line 11

def self.parse(xml)
  doc = Nokogiri::XML(xml)
  
  new do
    ATTRIBUTES.each do |attribute|
      self.send("#{attribute.to_sym}=",
       doc.at_css('Session')[attribute.capitalize])
    end
    
    self.info = Hindbaer::Info.parse(doc.at_css('Info'))
    
    self.audio_pool = 
      Hindbaer::AudioPool.parse(doc.at_css('AudioPool'))
    
    self.tracks = doc.css('Tracks Track').map do |t|
      Hindbaer::Track.parse(t)
    end
    
    self.clipboard_groups = doc.css('Clipboard Group').map do |g|
      Hindbaer::Group.parse(g)
    end
    
    self.markers = doc.css('Markers Marker').map do |m|
      Hindbaer::Marker.parse(m)
    end
  end
end

Instance Method Details

#lengthObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/hindbaer/session.rb', line 49

def length
  regions = tracks.map do |t| 
    t.regions.last
  end.compact
  
  regions.map do |r| 
    Hindbaer.tc_to_secs(r.start) + 
    Hindbaer.tc_to_secs(r.length)
  end.max
end

#to_xmlObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hindbaer/session.rb', line 60

def to_xml
  Nokogiri::XML::Builder.new do |xml|
    xml.Session Version: version, Samplerate: samplerate do
      info.to_xml(xml)
      audio_pool.to_xml(xml)
      xml.Tracks do
        tracks.each do |track|
          track.to_xml(xml)
        end
      end
      xml.Clipboard do
        clipboard_groups.each do |group|
          group.to_xml(xml)
        end
      end
      xml.Markers do
        markers.each do |marker|
          marker.to_xml(xml)
        end
      end
    end
  end.to_xml
end