Jon Aquino's Mental Garden

Engineering beautiful software jon aquino labs | personal blog

Sunday, October 03, 2004

Ruby script to read aloud from the Bible

This Ruby script uses RealPlayer to play an MP3 from BibleGateway's excellently read audio NIV bible. Every time it is run, it will read the next chapter from the Hebrew Bible, the Gospels, or the stuff after the gospels (it alternates between the three sources on each run). It also uses a free text-to-speech utility called DSAY.EXE to announce the name of the book.

I'm quite pleased with the results!


class Chapter
attr_reader :book, :url
attr_accessor :next
def initialize(book, url)
@book = book
@url = url
end
def passage
@url =~ /.*passage=(.*)$/
return $1
end
end

dummyChapter = Chapter.new("x", "x")
categoryToChaptersMap = {""=>[dummyChapter], "PreGospels"=>[], "Gospels"=>[], "PostGospels"=>[]}
category = ""
book = ""
chapter = dummyChapter
IO.foreach("bible.txt") { | line |
if line =~ /category=(.*)$/ then
chapter.next = categoryToChaptersMap[category][0]
chapter = dummyChapter
category = $1
elsif line =~ /book=(.*)$/ then
book = $1
elsif line =~ /chapter=(.*)$/ then
newChapter = Chapter.new(book, $1)
chapter.next = newChapter
chapter = newChapter
categoryToChaptersMap[category] << chapter
else
puts line
throw :shouldNeverReachHere
end
}
chapter.next = categoryToChaptersMap[category][0]

class State
attr_accessor :lastPreGospelPassage, :lastGospelPassage, :lastPostGospelPassage, :lastPassage
def initialize(categoryToChaptersMap)
@categoryToChaptersMap = categoryToChaptersMap
end
def inc
if @lastPassage == @lastPreGospelPassage
@lastGospelPassage = chapter(@lastGospelPassage).next.passage
@lastPassage = @lastGospelPassage
elsif @lastPassage == @lastGospelPassage
@lastPostGospelPassage = chapter(@lastPostGospelPassage).next.passage
@lastPassage = @lastPostGospelPassage
elsif @lastPassage == @lastPostGospelPassage
@lastPreGospelPassage = chapter(@lastPreGospelPassage).next.passage
@lastPassage = @lastPreGospelPassage
else
throw :shouldNeverReachHere
end
end
def lastChapter
chapter(@lastPassage)
end
def chapter(passage)
@categoryToChaptersMap.values.each { |chapters|
chapters.each { |chapter|
if chapter.passage == passage then
return chapter
end
}
}
puts passage
throw :shouldNeverReachHere
end
end

state = State.new(categoryToChaptersMap)

IO.foreach("bible-state.txt") { | line |
if line =~ /lastPreGospelPassage=(.*)$/ then
state.lastPreGospelPassage = $1
elsif line =~ /lastGospelPassage=(.*)$/ then
state.lastGospelPassage = $1
elsif line =~ /lastPostGospelPassage=(.*)$/ then
state.lastPostGospelPassage = $1
elsif line =~ /lastPassage=(.*)$/ then
state.lastPassage = $1
else
puts line
throw :shouldNeverReachHere
end
}

state.inc
`c:\\dsay\\dsay.exe #{state.lastChapter.book}`
`"C:\\Program Files\\Real\\RealPlayer\\realplay.exe" #{state.lastChapter.url}`

File.open("bible-state.txt", "w") do |file|
file << "lastPreGospelPassage="+state.lastPreGospelPassage+"\n"
file << "lastGospelPassage="+state.lastGospelPassage+"\n"
file << "lastPostGospelPassage="+state.lastPostGospelPassage+"\n"
file << "lastPassage="+state.lastPassage+"\n"
end

0 Comments:

Post a Comment

<< Home