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

#initializeAudite

Returns a new instance of Audite.



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

def initialize
  @events = Events.new
  @stream = Portaudio.new
  @thread = start_thread
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



147
148
149
# File 'lib/audite.rb', line 147

def forward(seconds = 2)
  seek(position + seconds)
end

#lengthObject



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

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

#length_in_secondsObject



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.inject(0) {|s, i| s + i.abs } / slice.size
end

#load(file) ⇒ Object



79
80
81
82
# File 'lib/audite.rb', line 79

def load(file)
  @file = file
  @mp3 = Mpg123.new(file)
end

#positionObject



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



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_streamObject



57
58
59
60
61
62
# File 'lib/audite.rb', line 57

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

#start_threadObject



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_streamObject



64
65
66
67
68
69
# File 'lib/audite.rb', line 64

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

#tellObject



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

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

#time_per_frameObject



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

def time_per_frame
  @mp3.tpf
end

#toggleObject



71
72
73
74
75
76
77
# File 'lib/audite.rb', line 71

def toggle
  if @active
    stop_stream
  else
    start_stream
  end
end