Class: Audite
- Inherits:
-
Object
- Object
- Audite
- Defined in:
- lib/audite.rb
Defined Under Namespace
Classes: Events
Instance Attribute Summary collapse
-
#active ⇒ Object
readonly
Returns the value of attribute active.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
Instance Method Summary collapse
- #forward(seconds = 2) ⇒ Object
-
#initialize(buffer_size = 2**12) ⇒ Audite
constructor
A new instance of Audite.
- #length ⇒ Object
- #length_in_seconds ⇒ Object
- #level ⇒ Object
- #load(file) ⇒ Object
- #position ⇒ Object
- #process(status) ⇒ Object
- #rewind(seconds = 2) ⇒ Object
- #samples_per_frame ⇒ Object
- #samples_to_frames(samples) ⇒ Object
- #samples_to_seconds(samples) ⇒ Object
- #seconds_to_frames(seconds) ⇒ Object
- #seconds_to_samples(seconds) ⇒ Object
- #seek(seconds) ⇒ Object
- #start_stream ⇒ Object
- #start_thread ⇒ Object
- #stop_stream ⇒ Object
- #tell ⇒ Object
- #time_per_frame ⇒ Object
- #toggle ⇒ Object
Constructor Details
Instance Attribute Details
#active ⇒ Object (readonly)
Returns the value of attribute active.
21 22 23 |
# File 'lib/audite.rb', line 21 def active @active end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
21 22 23 |
# File 'lib/audite.rb', line 21 def events @events end |
Instance Method Details
#forward(seconds = 2) ⇒ Object
138 139 140 |
# File 'lib/audite.rb', line 138 def forward(seconds = 2) seek(position + seconds) end |
#length ⇒ Object
95 96 97 |
# File 'lib/audite.rb', line 95 def length @mp3 ? @mp3.length : 0 end |
#length_in_seconds ⇒ Object
130 131 132 |
# File 'lib/audite.rb', line 130 def length_in_seconds samples_to_seconds(length) end |
#level ⇒ Object
38 39 40 |
# File 'lib/audite.rb', line 38 def level @stream.rms end |
#load(file) ⇒ Object
77 78 79 80 81 |
# File 'lib/audite.rb', line 77 def load(file) @file = file @mp3 = Mpg123.new(file) @thread ||= start_thread end |
#position ⇒ Object
126 127 128 |
# File 'lib/audite.rb', line 126 def position samples_to_seconds(tell) end |
#process(status) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/audite.rb', line 42 def process(status) if status == :done stop_stream events.trigger(:complete) else events.trigger(:position_change, position) end rescue => e $stderr.puts e. $stderr.puts e.backtrace end |
#rewind(seconds = 2) ⇒ Object
134 135 136 |
# File 'lib/audite.rb', line 134 def rewind(seconds = 2) seek(position - seconds) end |
#samples_per_frame ⇒ Object
87 88 89 |
# File 'lib/audite.rb', line 87 def samples_per_frame @mp3.spf end |
#samples_to_frames(samples) ⇒ Object
111 112 113 |
# File 'lib/audite.rb', line 111 def samples_to_frames(samples) samples / samples_per_frame end |
#samples_to_seconds(samples) ⇒ Object
107 108 109 |
# File 'lib/audite.rb', line 107 def samples_to_seconds(samples) samples_to_frames(samples) * time_per_frame end |
#seconds_to_frames(seconds) ⇒ Object
99 100 101 |
# File 'lib/audite.rb', line 99 def seconds_to_frames(seconds) seconds / time_per_frame end |
#seconds_to_samples(seconds) ⇒ Object
103 104 105 |
# File 'lib/audite.rb', line 103 def seconds_to_samples(seconds) seconds_to_frames(seconds) * samples_per_frame end |
#seek(seconds) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/audite.rb', line 115 def seek(seconds) if @mp3 samples = seconds_to_samples(seconds) if (0..length).include?(samples) @mp3.seek(samples) events.trigger(:position_change, position) end end end |
#start_stream ⇒ Object
55 56 57 58 59 60 |
# File 'lib/audite.rb', line 55 def start_stream unless @active @active = true @stream.start end end |
#start_thread ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/audite.rb', line 29 def start_thread Thread.start do loop do process @stream.write_from_mpg(@mp3) @stream.wait end end end |
#stop_stream ⇒ Object
62 63 64 65 66 67 |
# File 'lib/audite.rb', line 62 def stop_stream if @active @active = false @stream.stop end end |
#tell ⇒ Object
91 92 93 |
# File 'lib/audite.rb', line 91 def tell @mp3 ? @mp3.tell : 0 end |
#time_per_frame ⇒ Object
83 84 85 |
# File 'lib/audite.rb', line 83 def time_per_frame @mp3.tpf end |
#toggle ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/audite.rb', line 69 def toggle if @active stop_stream else start_stream end end |