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 ⇒ Audite
constructor
A new instance of Audite.
- #length ⇒ Object
- #length_in_seconds ⇒ Object
- #level(slice) ⇒ Object
- #load(file) ⇒ Object
- #position ⇒ Object
- #process(samples) ⇒ Object
- #read(samples) ⇒ 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
147 148 149 |
# File 'lib/audite.rb', line 147 def forward(seconds = 2) seek(position + seconds) end |
#length ⇒ Object
96 97 98 |
# File 'lib/audite.rb', line 96 def length @mp3 ? @mp3.length : 0 end |
#length_in_seconds ⇒ Object
135 136 137 |
# File 'lib/audite.rb', line 135 def length_in_seconds samples_to_seconds(length) end |
#level(slice) ⇒ Object
139 140 141 |
# File 'lib/audite.rb', line 139 def level(slice) slice.map {|i| i.abs }.max end |
#load(file) ⇒ Object
79 80 81 82 |
# File 'lib/audite.rb', line 79 def load(file) @file = file @mp3 = Mpg123.new(file) end |
#position ⇒ Object
131 132 133 |
# File 'lib/audite.rb', line 131 def position samples_to_seconds(tell) end |
#process(samples) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/audite.rb', line 37 def process(samples) if tell >= length stop_stream events.trigger(:complete) (0..samples).map { 0 } elsif slice = read(samples) events.trigger(:level, level(slice)) events.trigger(:position_change, position) slice else (0..samples).map { 0 } end rescue => e $stderr.puts e. $stderr.puts e.backtrace end |
#read(samples) ⇒ Object
100 101 102 |
# File 'lib/audite.rb', line 100 def read(samples) @mp3.read(samples) end |
#rewind(seconds = 2) ⇒ Object
143 144 145 |
# File 'lib/audite.rb', line 143 def rewind(seconds = 2) seek(position - seconds) end |
#samples_per_frame ⇒ Object
88 89 90 |
# File 'lib/audite.rb', line 88 def samples_per_frame @mp3.spf end |
#samples_to_frames(samples) ⇒ Object
116 117 118 |
# File 'lib/audite.rb', line 116 def samples_to_frames(samples) samples / samples_per_frame end |
#samples_to_seconds(samples) ⇒ Object
112 113 114 |
# File 'lib/audite.rb', line 112 def samples_to_seconds(samples) samples_to_frames(samples) * time_per_frame end |
#seconds_to_frames(seconds) ⇒ Object
104 105 106 |
# File 'lib/audite.rb', line 104 def seconds_to_frames(seconds) seconds / time_per_frame end |
#seconds_to_samples(seconds) ⇒ Object
108 109 110 |
# File 'lib/audite.rb', line 108 def seconds_to_samples(seconds) seconds_to_frames(seconds) * samples_per_frame end |
#seek(seconds) ⇒ Object
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/audite.rb', line 120 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
57 58 59 60 61 62 |
# File 'lib/audite.rb', line 57 def start_stream unless @active @active = true @stream.start end end |
#start_thread ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/audite.rb', line 29 def start_thread Thread.start do loop do @stream.write(process(4096 * 2)) end end end |
#stop_stream ⇒ Object
64 65 66 67 68 69 |
# File 'lib/audite.rb', line 64 def stop_stream if @active @active = false @stream.stop end end |
#tell ⇒ Object
92 93 94 |
# File 'lib/audite.rb', line 92 def tell @mp3 ? @mp3.tell : 0 end |
#time_per_frame ⇒ Object
84 85 86 |
# File 'lib/audite.rb', line 84 def time_per_frame @mp3.tpf end |
#toggle ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/audite.rb', line 71 def toggle if @active stop_stream else start_stream end end |