Class: Voice_Chapters

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text: nil, marker: nil, file_name: file_name) ⇒ Voice_Chapters

Returns a new instance of Voice_Chapters.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/voice_chapters.rb', line 9

def initialize(text:nil, marker:nil, file_name:file_name)
  @text = text
  @file_name = file_name
  chapter_marker(marker)
  create_uuid
  create_audio_folder
  create_audio_for_chapters
  create_audio_for_text
  set_chapter_marker
  clean_chapter_file
end

Instance Attribute Details

#chaptersObject (readonly)

Returns the value of attribute chapters.



8
9
10
# File 'lib/voice_chapters.rb', line 8

def chapters
  @chapters
end

Instance Method Details

#chapter_marker(expression) ⇒ Object



25
26
27
# File 'lib/voice_chapters.rb', line 25

def chapter_marker(expression)
  @chapters = @text.scan expression
end

#chapters_mark_hashObject



46
47
48
49
50
# File 'lib/voice_chapters.rb', line 46

def chapters_mark_hash
  @chapter_files.each_with_index.map do |file, index|
    {'title' => @chapters[index].first[0..18] + '...', 'duration' => get_file_duration(file)}
  end
end

#clean_chapter_fileObject



73
74
75
76
77
# File 'lib/voice_chapters.rb', line 73

def clean_chapter_file
  @chapter_files.each do |file|
    File.delete "#{save_dir}/#{file}"
  end
end

#create_audio_folderObject



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

def create_audio_folder
  Dir.mkdir(Dir.pwd) unless File.directory? @feed_path
end

#create_audio_for_chaptersObject



37
38
39
40
41
42
43
44
# File 'lib/voice_chapters.rb', line 37

def create_audio_for_chapters
  @chapter_files = []
  @chapters.each_with_index do |chapter, index|
    @chapter_files << "#{@uuid}-#{index}.aiff"
    Voice.new({ string: chapter,
                "output-file" => "#{save_dir}/#{@chapter_files.last}" })
  end
end

#create_audio_for_textObject



56
57
58
59
60
# File 'lib/voice_chapters.rb', line 56

def create_audio_for_text
  Voice.new({ string: @text,
              "output-file" => "'#{save_dir}/#{@file_name}.m4a'",
              "file-format" => "m4af" })
end

#create_uuidObject



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

def create_uuid
  @uuid = (Digest::SHA1.hexdigest @text)[0...8]
end

#file_nameObject



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

def file_name
  @file_names[@uuid] = @chapters
end

#get_file_duration(file) ⇒ Object



52
53
54
# File 'lib/voice_chapters.rb', line 52

def get_file_duration(file)
  (Afinfo.duration("#{save_dir}/#{file}").to_f * 1000).ceil
end

#save_dirObject



69
70
71
# File 'lib/voice_chapters.rb', line 69

def save_dir
  File.expand_path('audio',Dir.pwd)
end

#set_chapter_markerObject



62
63
64
65
66
67
# File 'lib/voice_chapters.rb', line 62

def set_chapter_marker
  file = "#{save_dir}/#{@file_name}.m4a"
  chapters = chapters_mark_hash
  puts "Chapter.set_chapters('#{file}', #{chapters})"
  Chapter.set_chapters(file, chapters)
end