Class: Daisy::Chapter

Inherits:
Object
  • Object
show all
Defined in:
lib/daisy/chapter.rb

Overview

A chapter of a book.

Constant Summary collapse

TIDY_MP3 =

lame command line

"lame -a -t --strictly-enforce-ISO -b56 --resample 44.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book, path) ⇒ Chapter

Creates a new Chapter for Book book, attached to the mp3 file path, retrieving its mp3tag information.



37
38
39
40
41
# File 'lib/daisy/chapter.rb', line 37

def initialize(book, path)
  @book = book
  @file = File.basename(path)
  read_mp3_tags
end

Instance Attribute Details

#albumObject (readonly)

mp3 tag “album”



18
19
20
# File 'lib/daisy/chapter.rb', line 18

def album
  @album
end

#authorObject (readonly)

mp3 tag “artist”



24
25
26
# File 'lib/daisy/chapter.rb', line 24

def author
  @author
end

#bookObject (readonly)

parent Book instance



12
13
14
# File 'lib/daisy/chapter.rb', line 12

def book
  @book
end

#durationObject (readonly)

mp3 duration (Float, seconds)



30
31
32
# File 'lib/daisy/chapter.rb', line 30

def duration
  @duration
end

#fileObject (readonly)

base mp3 file name



15
16
17
# File 'lib/daisy/chapter.rb', line 15

def file
  @file
end

#positionObject

assigned by the book



33
34
35
# File 'lib/daisy/chapter.rb', line 33

def position
  @position
end

#titleObject (readonly)

mp3 tag “title”



21
22
23
# File 'lib/daisy/chapter.rb', line 21

def title
  @title
end

#trackObject (readonly)

mp3 tag “tracknum”



27
28
29
# File 'lib/daisy/chapter.rb', line 27

def track
  @track
end

Instance Method Details

#chapter_titleObject

Returns the chapter title:

  • #title if set

  • otherwise, if this is the only chapter, the book title

  • otherwise, “Chapter X” where X is the #position.



59
60
61
# File 'lib/daisy/chapter.rb', line 59

def chapter_title
  title || (book.chapters.size > 1 ? "Chapitre #{position}" : book.title)
end

#copy_mp3(re_encode: false) ⇒ Object

Copies the mp3 source file to the book target directory, re-encoding it with lame if re_encode is true.



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/daisy/chapter.rb', line 151

def copy_mp3(re_encode: false)
  source = "#{book.source_dir}/#{file}"
  target = "#{book.target_dir}/#{file}"
  File.delete target if File.exist?(target)
  if re_encode
    puts "re-encoding #{file}"
    system "#{TIDY_MP3} #{source.inspect} #{target.inspect}"
  else
    FileUtils.cp source, target
  end
end

#ncc_refObject

Entry in the ncc.html file for the chapter.



69
70
71
72
73
74
75
76
77
# File 'lib/daisy/chapter.rb', line 69

def ncc_ref
  html = <<~HTML
    <h1 id="chapter_#{position}" class="section">
      <a href="#{smil_file}#read_#{position}">#{chapter_title.text_escape}</a>
    </h1>
  HTML

  html.lines.map { |line| "  #{line}" }.join
end

#smil_fileObject

Name of SMIL file for this chapter.



64
65
66
# File 'lib/daisy/chapter.rb', line 64

def smil_file
  file.sub('.mp3', '.smil')
end

#smil_refObject

Entry in the master.smil file for the chapter.



80
81
82
# File 'lib/daisy/chapter.rb', line 80

def smil_ref
  %(<ref title="#{chapter_title.attr_escape}" src="#{smil_file.attr_escape}" id="Master_#{position}"/>)
end

#write_smil(elapsed_time) ⇒ Object

Write the chapter smil_file file, with elapsed_time seconds representing the elapsed time for all preceding chapters.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/daisy/chapter.rb', line 86

def write_smil(elapsed_time)
  File.write "#{book.target_dir}/#{smil_file}", <<~XML, mode: 'wb:windows-1252'
    <?xml version="1.0" encoding="windows-1252"?>
    <!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 1.0//EN" "http://www.w3.org/TR/REC-SMIL/SMIL10.dtd">
    <smil>
    <head>
      <meta name="dc:format" content="Daisy 2.02" />
      <meta name="dc:title" content="#{book.title.attr_escape}" />
      <meta name="dc:identifier" content="#{book.identifier}" />
      <meta name="title" content="#{chapter_title.attr_escape}" />
      <meta name="ncc:totalElapsedTime" content="#{elapsed_time.to_hh}" />
      <meta name="ncc:timeInThisSmil" content="#{duration.to_hh}" />
      <layout>
        <region id="txtView" />
      </layout>
    </head>
    <body>
      <seq dur="#{'%5.3f' % duration}s">
        <par endsync="last" id="read_#{position}">
          <text src="ncc.html#chapter_#{position}" id="text_#{position}" />
          <audio src="#{file}" id="audio_#{position}" clip-begin="npt=0.000s" clip-end="npt=#{'%5.3f' % duration}s" />
        </par>
      </seq>
    </body>
    </smil>
  XML
end