Class: Kja

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

Instance Method Summary collapse

Constructor Details

#initialize(audio_player: '/usr/bin/ogg123 -q', ogg_baseurl: 'http://a0.jamesrobertson.me.uk/' \ + 'rorb/r/ruby/kja/ogg', debug: false) ⇒ Kja

Returns a new instance of Kja.



31
32
33
34
35
36
37
38
# File 'lib/kja.rb', line 31

def initialize(audio_player: '/usr/bin/ogg123 -q',
               ogg_baseurl: 'http://a0.jamesrobertson.me.uk/'  \
              + 'rorb/r/ruby/kja/ogg', debug: false)

  @ogg_baseurl, @audio_player = ogg_baseurl, audio_player
  @debug = debug

end

Instance Method Details

#download(s, outfile = nil) ⇒ Object

e.g. download ‘Eze 18, 30..32’ downloads the audio as an ogg file



72
73
74
75
76
77
78
79
80
81
82
83
84
85
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/kja.rb', line 72

def download(s, outfile=nil)

  puts 'downloading ...' if @debug

  a = KjReading.verses s

  downloads = a.map do |x|
    download_ogg(x.title)
  end

  puts 'converting to wav: ' + downloads.inspect if @debug

  dir = File.dirname(Tempfile.new.path)

  wavfiles = downloads.map do |oggfile|

    wavfile = oggfile.sub(/\.ogg$/,'.wav')
    EasyAudioUtils.new(oggfile, wavfile, working_dir: dir).convert
    sleep 1.4
    File.basename(wavfile)

  end

  puts 'concatenating wav files ...' if @debug

  # concat wav files
  #
  fullwavfile = Tempfile.new('kva').path + '.wav'
  puts 'creating fullwavfile:' + fullwavfile.inspect if @debug
  EasyAudioUtils.new(out: fullwavfile, working_dir: dir).concat(wavfiles,
                                                                sample_rate: 22050)

  # convert wav to ogg
  #
  fulloggfile = outfile || fullwavfile.sub(/\.wav$/, '.ogg')
  puts 'creating fulloggfile: ' + fulloggfile.inspect if @debug
  EasyAudioUtils.new(fullwavfile, fulloggfile).convert

  'downloaded ' + fulloggfile

end

#duration(s) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kja.rb', line 40

def duration(s)

  book_title, chapter_title, verse_no = fracture(s)

  url = @ogg_baseurl + '/' + File.join(book_title, chapter_title, 'dir.xml')

  dx = Dynarex.new url
  ogg = dx.find_by_name chapter_title + '_' + verse_no + '.ogg'
  ogg.description[/\d+\.\d+$/].to_f

end

#speak(title) ⇒ Object

e.g. speak ‘Eze 18, 30..32’ plays the audio of the title requested



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/kja.rb', line 55

def speak(title)

  a = KjReading.verses title

  a.each do |x|

    filename = download_ogg(x.title)
    command = @audio_player + ' ' + filename
    system command

  end

end