Class: STFTSpectrogram::AudioFile

Inherits:
Object
  • Object
show all
Includes:
WaveFile
Defined in:
lib/audio/audio_file.rb

Overview

Represents wave file

Constant Summary collapse

SAMPLE_RATE =
441_00

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, wsize = 1, woverlap = 0) ⇒ AudioFile

Returns a new instance of AudioFile.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/audio/audio_file.rb', line 13

def initialize(path, wsize = 1, woverlap = 0)
  unless File.file?(path)
    raise IOError, "File '" + path + "' does not exist!"
  end
  @samples = []
  self.window_size = wsize
  self.window_overlap = woverlap
  @window_overlap = @window_size / 2 if @window_size <= @window_overlap
  @window_pos = 0
  read_data(path)
end

Instance Attribute Details

#window_overlapObject

Returns the value of attribute window_overlap.



8
9
10
# File 'lib/audio/audio_file.rb', line 8

def window_overlap
  @window_overlap
end

#window_sizeObject

Returns the value of attribute window_size.



9
10
11
# File 'lib/audio/audio_file.rb', line 9

def window_size
  @window_size
end

Class Method Details

.sample_rateObject



58
59
60
# File 'lib/audio/audio_file.rb', line 58

def self.sample_rate
  SAMPLE_RATE
end

Instance Method Details

#current_timeObject

Gets the time corresponding to the window pointers location



68
69
70
# File 'lib/audio/audio_file.rb', line 68

def current_time
  (@window_pos + @window_size / 2) / (SAMPLE_RATE / 1000)
end

#end?Boolean

Checks if the window pointer is at the end of the file

Returns:

  • (Boolean)


73
74
75
# File 'lib/audio/audio_file.rb', line 73

def end?
  @window_pos + @window_size >= @samples.length
end

#next_windowObject

Gets window_size of data from audio samples and moves the window pointer



48
49
50
51
52
53
54
55
56
# File 'lib/audio/audio_file.rb', line 48

def next_window
  return [] if end?

  slice = @samples[@window_pos, @window_size]
  slice.fill(0, slice.length..@window_size - 1)
  @window_pos += @window_size - @window_overlap

  slice
end

#resetObject

Resets the window pointer



63
64
65
# File 'lib/audio/audio_file.rb', line 63

def reset
  @window_pos = 0
end