Class: Voice_Chapters
- Inherits:
-
Object
- Object
- Voice_Chapters
- Defined in:
- lib/voice_chapters.rb
Instance Attribute Summary collapse
-
#chapters ⇒ Object
readonly
Returns the value of attribute chapters.
Instance Method Summary collapse
- #chapter_marker(expression) ⇒ Object
- #chapters_mark_hash ⇒ Object
- #clean_chapter_file ⇒ Object
- #create_audio_folder ⇒ Object
- #create_audio_for_chapters ⇒ Object
- #create_audio_for_text ⇒ Object
- #create_uuid ⇒ Object
- #file_name ⇒ Object
- #get_file_duration(file) ⇒ Object
-
#initialize(text: nil, marker: nil, file_name: file_name) ⇒ Voice_Chapters
constructor
A new instance of Voice_Chapters.
- #save_dir ⇒ Object
- #set_chapter_marker ⇒ Object
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
#chapters ⇒ Object (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_hash ⇒ Object
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_file ⇒ Object
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_folder ⇒ Object
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_chapters ⇒ Object
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_text ⇒ Object
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_uuid ⇒ Object
29 30 31 |
# File 'lib/voice_chapters.rb', line 29 def create_uuid @uuid = (Digest::SHA1.hexdigest @text)[0...8] end |
#file_name ⇒ Object
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_dir ⇒ Object
69 70 71 |
# File 'lib/voice_chapters.rb', line 69 def save_dir File.('audio',Dir.pwd) end |
#set_chapter_marker ⇒ Object
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 |