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**14) ⇒ Audite

Returns a new instance of Audite.



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

def initialize(buffer_size = 2**14)
  @buffer_size = buffer_size
  @events = Events.new
  @stream = Portaudio.new(@buffer_size)
  @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



151
152
153
# File 'lib/audite.rb', line 151

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

#lengthObject



100
101
102
# File 'lib/audite.rb', line 100

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

#length_in_secondsObject



139
140
141
# File 'lib/audite.rb', line 139

def length_in_seconds
  samples_to_seconds(length)
end

#level(slice) ⇒ Object



143
144
145
# File 'lib/audite.rb', line 143

def level(slice)
  slice.map {|i| i.abs }.max
end

#load(file) ⇒ Object



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

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

#positionObject



135
136
137
# File 'lib/audite.rb', line 135

def position
  samples_to_seconds(tell)
end

#process(samples) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/audite.rb', line 42

def process(samples)
  if tell >= length
    stop_stream
    events.trigger(:complete)
    silence(samples)

  elsif slice = read(samples)
    events.trigger(:level, level(slice))
    events.trigger(:position_change, position)
    slice
  else
    silence(samples)
  end

rescue => e
  $stderr.puts e.message
  $stderr.puts e.backtrace
end

#read(samples) ⇒ Object



104
105
106
# File 'lib/audite.rb', line 104

def read(samples)
  @mp3.read(samples)
end

#rewind(seconds = 2) ⇒ Object



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

def rewind(seconds = 2)
  seek(position - seconds)
end

#samples_per_frameObject



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

def samples_per_frame
  @mp3.spf
end

#samples_to_frames(samples) ⇒ Object



120
121
122
# File 'lib/audite.rb', line 120

def samples_to_frames(samples)
  samples / samples_per_frame
end

#samples_to_seconds(samples) ⇒ Object



116
117
118
# File 'lib/audite.rb', line 116

def samples_to_seconds(samples)
  samples_to_frames(samples) * time_per_frame
end

#seconds_to_frames(seconds) ⇒ Object



108
109
110
# File 'lib/audite.rb', line 108

def seconds_to_frames(seconds)
  seconds / time_per_frame
end

#seconds_to_samples(seconds) ⇒ Object



112
113
114
# File 'lib/audite.rb', line 112

def seconds_to_samples(seconds)
  seconds_to_frames(seconds) * samples_per_frame
end

#seek(seconds) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/audite.rb', line 124

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

#silence(samples) ⇒ Object



38
39
40
# File 'lib/audite.rb', line 38

def silence(samples)
  Array.new(samples, 0)
end

#start_streamObject



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

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

#start_threadObject



30
31
32
33
34
35
36
# File 'lib/audite.rb', line 30

def start_thread
  Thread.start do
    loop do
      @stream.write(process(@buffer_size * 2))
    end
  end
end

#stop_streamObject



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

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

#tellObject



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

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

#time_per_frameObject



88
89
90
# File 'lib/audite.rb', line 88

def time_per_frame
  @mp3.tpf
end

#toggleObject



75
76
77
78
79
80
81
# File 'lib/audite.rb', line 75

def toggle
  if @active
    stop_stream
  else
    start_stream
  end
end