Class: Audite

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

Defined Under Namespace

Classes: Events

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer_size = 2**12) ⇒ Audite

Returns a new instance of Audite.



23
24
25
26
27
# File 'lib/audite.rb', line 23

def initialize(buffer_size = 2**12)
  @buffer_size = buffer_size
  @events = Events.new
  @stream = Portaudio.new(@buffer_size)
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



21
22
23
# File 'lib/audite.rb', line 21

def active
  @active
end

#eventsObject (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

#lengthObject



95
96
97
# File 'lib/audite.rb', line 95

def length
  @mp3 ? @mp3.length : 0
end

#length_in_secondsObject



130
131
132
# File 'lib/audite.rb', line 130

def length_in_seconds
  samples_to_seconds(length)
end

#levelObject



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

#positionObject



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.message
  $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_frameObject



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_streamObject



55
56
57
58
59
60
# File 'lib/audite.rb', line 55

def start_stream
  unless @active
    @active = true
    @stream.start
  end
end

#start_threadObject



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_streamObject



62
63
64
65
66
67
# File 'lib/audite.rb', line 62

def stop_stream
  if @active
    @active = false
    @stream.stop
  end
end

#tellObject



91
92
93
# File 'lib/audite.rb', line 91

def tell
  @mp3 ? @mp3.tell : 0
end

#time_per_frameObject



83
84
85
# File 'lib/audite.rb', line 83

def time_per_frame
  @mp3.tpf
end

#toggleObject



69
70
71
72
73
74
75
# File 'lib/audite.rb', line 69

def toggle
  if @active
    stop_stream
  else
    start_stream
  end
end