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.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#mp3 ⇒ Object
readonly
Returns the value of attribute mp3.
-
#song_list ⇒ Object
readonly
Returns the value of attribute song_list.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Instance Method Summary collapse
- #close ⇒ Object
- #current_song_name ⇒ Object
- #forward(seconds = 2) ⇒ Object
-
#initialize(buffer_size = 2**12, device_name = '') ⇒ Audite
constructor
A new instance of Audite.
- #length ⇒ Object
- #length_in_seconds ⇒ Object
- #level ⇒ Object
- #load(files) ⇒ Object
- #position ⇒ Object
- #process(status) ⇒ Object
- #queue(song) ⇒ Object
- #queued_songs ⇒ Object
- #request_next_song ⇒ 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
- #set_current_song ⇒ Object
- #song_loaded? ⇒ Boolean
- #songs_in_queue? ⇒ Boolean
- #start_stream ⇒ Object
- #start_thread ⇒ Object
- #stop_stream ⇒ Object
- #tell ⇒ Object
- #time_per_frame ⇒ Object
- #toggle ⇒ Object
Constructor Details
#initialize(buffer_size = 2**12, device_name = '') ⇒ Audite
Returns a new instance of Audite.
23 24 25 26 27 28 29 |
# File 'lib/audite.rb', line 23 def initialize(buffer_size = 2**12, device_name = '') @buffer_size = buffer_size @device_name = device_name @events = Events.new @stream = Portaudio.new(@buffer_size, @device_name) @song_list = [] end |
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 |
#file ⇒ Object (readonly)
Returns the value of attribute file.
21 22 23 |
# File 'lib/audite.rb', line 21 def file @file end |
#mp3 ⇒ Object (readonly)
Returns the value of attribute mp3.
21 22 23 |
# File 'lib/audite.rb', line 21 def mp3 @mp3 end |
#song_list ⇒ Object (readonly)
Returns the value of attribute song_list.
21 22 23 |
# File 'lib/audite.rb', line 21 def song_list @song_list end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
21 22 23 |
# File 'lib/audite.rb', line 21 def stream @stream end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
21 22 23 |
# File 'lib/audite.rb', line 21 def thread @thread end |
Instance Method Details
#close ⇒ Object
70 71 72 73 |
# File 'lib/audite.rb', line 70 def close stream.close exit end |
#current_song_name ⇒ Object
57 58 59 |
# File 'lib/audite.rb', line 57 def current_song_name File.basename mp3.file end |
#forward(seconds = 2) ⇒ Object
185 186 187 |
# File 'lib/audite.rb', line 185 def forward(seconds = 2) seek(position + seconds) end |
#length ⇒ Object
142 143 144 |
# File 'lib/audite.rb', line 142 def length @mp3 ? @mp3.length : 0 end |
#length_in_seconds ⇒ Object
177 178 179 |
# File 'lib/audite.rb', line 177 def length_in_seconds samples_to_seconds(length) end |
#level ⇒ Object
40 41 42 |
# File 'lib/audite.rb', line 40 def level @stream.rms end |
#load(files) ⇒ Object
103 104 105 106 107 |
# File 'lib/audite.rb', line 103 def load(files) files = [] << files unless Array === files files.each {|file| queue file } set_current_song end |
#position ⇒ Object
173 174 175 |
# File 'lib/audite.rb', line 173 def position samples_to_seconds(tell) end |
#process(status) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/audite.rb', line 44 def process(status) if [:done, :need_more].include? status request_next_song events.trigger(:complete) else events.trigger(:position_change, position) end rescue => e $stderr.puts e. $stderr.puts e.backtrace end |
#queue(song) ⇒ Object
118 119 120 |
# File 'lib/audite.rb', line 118 def queue song @song_list << Mpg123.new(song) end |
#queued_songs ⇒ Object
122 123 124 |
# File 'lib/audite.rb', line 122 def queued_songs @song_list.map {|s| File.basename s.file } end |
#request_next_song ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/audite.rb', line 61 def request_next_song if songs_in_queue? set_current_song start_stream else stop_stream end end |
#rewind(seconds = 2) ⇒ Object
181 182 183 |
# File 'lib/audite.rb', line 181 def rewind(seconds = 2) seek(position - seconds) end |
#samples_per_frame ⇒ Object
134 135 136 |
# File 'lib/audite.rb', line 134 def samples_per_frame @mp3.spf end |
#samples_to_frames(samples) ⇒ Object
158 159 160 |
# File 'lib/audite.rb', line 158 def samples_to_frames(samples) samples / samples_per_frame end |
#samples_to_seconds(samples) ⇒ Object
154 155 156 |
# File 'lib/audite.rb', line 154 def samples_to_seconds(samples) samples_to_frames(samples) * time_per_frame end |
#seconds_to_frames(seconds) ⇒ Object
146 147 148 |
# File 'lib/audite.rb', line 146 def seconds_to_frames(seconds) seconds / time_per_frame end |
#seconds_to_samples(seconds) ⇒ Object
150 151 152 |
# File 'lib/audite.rb', line 150 def seconds_to_samples(seconds) seconds_to_frames(seconds) * samples_per_frame end |
#seek(seconds) ⇒ Object
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/audite.rb', line 162 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 |
#set_current_song ⇒ Object
113 114 115 116 |
# File 'lib/audite.rb', line 113 def set_current_song @mp3 = song_list.shift start_thread end |
#song_loaded? ⇒ Boolean
109 110 111 |
# File 'lib/audite.rb', line 109 def song_loaded? !@mp3.nil? end |
#songs_in_queue? ⇒ Boolean
126 127 128 |
# File 'lib/audite.rb', line 126 def songs_in_queue? !@song_list.empty? end |
#start_stream ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/audite.rb', line 75 def start_stream unless @active || !song_loaded? @active = true @stream.start start_thread events.trigger(:toggle, @active) end end |
#start_thread ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/audite.rb', line 31 def start_thread @thread ||= Thread.start do loop do process @stream.write_from_mpg(@mp3) @stream.wait end end end |
#stop_stream ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/audite.rb', line 84 def stop_stream if @active @active = false @thread = nil unless @thread.alive? unless @stream.stopped? @stream.abort events.trigger(:toggle, @active) end end end |
#tell ⇒ Object
138 139 140 |
# File 'lib/audite.rb', line 138 def tell @mp3 ? @mp3.tell : 0 end |
#time_per_frame ⇒ Object
130 131 132 |
# File 'lib/audite.rb', line 130 def time_per_frame @mp3.tpf end |
#toggle ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/audite.rb', line 95 def toggle if @active stop_stream else start_stream end end |