Class: AudioFingerprint::WaveFile
- Inherits:
-
Object
- Object
- AudioFingerprint::WaveFile
- Defined in:
- lib/audio_fingerprint/wave_file.rb
Constant Summary collapse
- CHUNK_ID =
"RIFF"
- FORMAT =
"WAVE"
- FORMAT_CHUNK_ID =
"fmt "
- SUB_CHUNK1_SIZE =
16
- PCM =
1
- DATA_CHUNK_ID =
"data"
- HEADER_SIZE =
36
Instance Attribute Summary collapse
-
#bits_per_sample ⇒ Object
Returns the value of attribute bits_per_sample.
-
#block_align ⇒ Object
readonly
Returns the value of attribute block_align.
-
#byte_rate ⇒ Object
readonly
Returns the value of attribute byte_rate.
-
#num_channels ⇒ Object
Returns the value of attribute num_channels.
-
#sample_rate ⇒ Object
Returns the value of attribute sample_rate.
Class Method Summary collapse
Instance Method Summary collapse
- #duration ⇒ Object
-
#initialize(num_channels, sample_rate, bits_per_sample, sample_data = []) ⇒ WaveFile
constructor
A new instance of WaveFile.
- #inspect ⇒ Object
- #mono? ⇒ Boolean
- #normalized_sample_data ⇒ Object
- #reverse ⇒ Object
- #sample_data ⇒ Object
- #sample_data=(sample_data) ⇒ Object
- #save(path) ⇒ Object
- #stereo? ⇒ Boolean
Constructor Details
#initialize(num_channels, sample_rate, bits_per_sample, sample_data = []) ⇒ WaveFile
Returns a new instance of WaveFile.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/audio_fingerprint/wave_file.rb', line 16 def initialize(num_channels, sample_rate, bits_per_sample, sample_data = []) if num_channels == :mono @num_channels = 1 elsif num_channels == :stereo @num_channels = 2 else @num_channels = num_channels end @sample_rate = sample_rate @bits_per_sample = bits_per_sample @sample_data = sample_data @byte_rate = sample_rate * @num_channels * (bits_per_sample / 8) @block_align = @num_channels * (bits_per_sample / 8) end |
Instance Attribute Details
#bits_per_sample ⇒ Object
Returns the value of attribute bits_per_sample.
311 312 313 |
# File 'lib/audio_fingerprint/wave_file.rb', line 311 def bits_per_sample @bits_per_sample end |
#block_align ⇒ Object (readonly)
Returns the value of attribute block_align.
311 312 313 |
# File 'lib/audio_fingerprint/wave_file.rb', line 311 def block_align @block_align end |
#byte_rate ⇒ Object (readonly)
Returns the value of attribute byte_rate.
311 312 313 |
# File 'lib/audio_fingerprint/wave_file.rb', line 311 def byte_rate @byte_rate end |
#num_channels ⇒ Object
Returns the value of attribute num_channels.
311 312 313 |
# File 'lib/audio_fingerprint/wave_file.rb', line 311 def num_channels @num_channels end |
#sample_rate ⇒ Object
Returns the value of attribute sample_rate.
312 313 314 |
# File 'lib/audio_fingerprint/wave_file.rb', line 312 def sample_rate @sample_rate end |
Class Method Details
.open(path) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/audio_fingerprint/wave_file.rb', line 32 def self.open(path) file = File.open(path, "rb") begin header = read_header(file) errors = validate_header(header) if errors == [] sample_data = read_sample_data(file, header[:num_channels], header[:bits_per_sample], header[:sub_chunk2_size]) wave_file = self.new(header[:num_channels], header[:sample_rate], header[:bits_per_sample], sample_data) else error_msg = "#{path} can't be opened, due to the following errors:\n" errors.each {|error| error_msg += " * #{error}\n" } raise StandardError, error_msg end rescue EOFError raise StandardError, "An error occured while reading #{path}." ensure file.close() end return wave_file end |
Instance Method Details
#duration ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/audio_fingerprint/wave_file.rb', line 205 def duration() total_samples = sample_data.length samples_per_millisecond = @sample_rate / 1000.0 samples_per_second = @sample_rate samples_per_minute = samples_per_second * 60 samples_per_hour = samples_per_minute * 60 hours, minutes, seconds, milliseconds = 0, 0, 0, 0 if(total_samples >= samples_per_hour) hours = total_samples / samples_per_hour total_samples -= samples_per_hour * hours end if(total_samples >= samples_per_minute) minutes = total_samples / samples_per_minute total_samples -= samples_per_minute * minutes end if(total_samples >= samples_per_second) seconds = total_samples / samples_per_second total_samples -= samples_per_second * seconds end milliseconds = (total_samples / samples_per_millisecond).floor return { :hours => hours, :minutes => minutes, :seconds => seconds, :milliseconds => milliseconds } end |
#inspect ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/audio_fingerprint/wave_file.rb', line 299 def inspect() duration = self.duration() result = "Channels: #{@num_channels}\n" + "Sample rate: #{@sample_rate}\n" + "Bits per sample: #{@bits_per_sample}\n" + "Block align: #{@block_align}\n" + "Byte rate: #{@byte_rate}\n" + "Sample count: #{@sample_data.length}\n" + "Duration: #{duration[:hours]}h:#{duration[:minutes]}m:#{duration[:seconds]}s:#{duration[:milliseconds]}ms\n" end |
#mono? ⇒ Boolean
193 194 195 |
# File 'lib/audio_fingerprint/wave_file.rb', line 193 def mono?() return num_channels == 1 end |
#normalized_sample_data ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/audio_fingerprint/wave_file.rb', line 112 def normalized_sample_data() if @bits_per_sample == 8 min_value = 128.0 max_value = 127.0 midpoint = 128 elsif @bits_per_sample == 16 min_value = 32768.0 max_value = 32767.0 midpoint = 0 else raise StandardError, "Bits per sample is #{@bits_per_samples}, only 8 or 16 are supported" end if mono? normalized_sample_data = @sample_data.map {|sample| sample -= midpoint if sample < 0 sample.to_f / min_value else sample.to_f / max_value end } else normalized_sample_data = @sample_data.map {|sample| sample.map {|sub_sample| sub_sample -= midpoint if sub_sample < 0 sub_sample.to_f / min_value else sub_sample.to_f / max_value end } } end return normalized_sample_data end |
#reverse ⇒ Object
201 202 203 |
# File 'lib/audio_fingerprint/wave_file.rb', line 201 def reverse() sample_data.reverse!() end |
#sample_data ⇒ Object
108 109 110 |
# File 'lib/audio_fingerprint/wave_file.rb', line 108 def sample_data() return @sample_data end |
#sample_data=(sample_data) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/audio_fingerprint/wave_file.rb', line 150 def sample_data=(sample_data) if sample_data.length > 0 && ((mono? && sample_data[0].class == Float) || (!mono? && sample_data[0][0].class == Float)) if @bits_per_sample == 8 # Samples in 8-bit wave files are stored as a unsigned byte # Effective values are 0 to 255, midpoint at 128 min_value = 128.0 max_value = 127.0 midpoint = 128 elsif @bits_per_sample == 16 # Samples in 16-bit wave files are stored as a signed little-endian short # Effective values are -32768 to 32767, midpoint at 0 min_value = 32768.0 max_value = 32767.0 midpoint = 0 else raise StandardError, "Bits per sample is #{@bits_per_samples}, only 8 or 16 are supported" end if mono? @sample_data = sample_data.map {|sample| if(sample < 0.0) (sample * min_value).round + midpoint else (sample * max_value).round + midpoint end } else @sample_data = sample_data.map {|sample| sample.map {|sub_sample| if(sub_sample < 0.0) (sub_sample * min_value).round + midpoint else (sub_sample * max_value).round + midpoint end } } end else @sample_data = sample_data end end |
#save(path) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/audio_fingerprint/wave_file.rb', line 63 def save(path) # All numeric values should be saved in little-endian format sample_data_size = @sample_data.length * @num_channels * (@bits_per_sample / 8) # Write the header file_contents = CHUNK_ID file_contents += [HEADER_SIZE + sample_data_size].pack("V") file_contents += FORMAT file_contents += FORMAT_CHUNK_ID file_contents += [SUB_CHUNK1_SIZE].pack("V") file_contents += [PCM].pack("v") file_contents += [@num_channels].pack("v") file_contents += [@sample_rate].pack("V") file_contents += [@byte_rate].pack("V") file_contents += [@block_align].pack("v") file_contents += [@bits_per_sample].pack("v") file_contents += DATA_CHUNK_ID file_contents += [sample_data_size].pack("V") # Write the sample data if !mono? output_sample_data = [] @sample_data.each{|sample| sample.each{|sub_sample| output_sample_data << sub_sample } } else output_sample_data = @sample_data end if @bits_per_sample == 8 file_contents += output_sample_data.pack("C*") elsif @bits_per_sample == 16 file_contents += output_sample_data.pack("s*") else raise StandardError, "Bits per sample is #{@bits_per_samples}, only 8 or 16 are supported" end file = File.open(path, "w") file.syswrite(file_contents) file.close end |
#stereo? ⇒ Boolean
197 198 199 |
# File 'lib/audio_fingerprint/wave_file.rb', line 197 def stereo?() return num_channels == 2 end |