Class: AudioWaveform::WaveformDataFile
- Inherits:
-
Object
- Object
- AudioWaveform::WaveformDataFile
- Defined in:
- lib/audio_waveform/waveform_data_file.rb
Overview
Provides access to audio waveform data, and serialization to and from binary and JSON data formats.
Instance Method Summary collapse
-
#append(min_sample, max_sample) ⇒ WaveformDataFile
Appends a waveform minimum/maximum pair.
-
#bits ⇒ Integer
Resolution of waveform data points, either 8 or 16 bits.
-
#bits=(bits) ⇒ Object
Sets the resolution of waveform data points.
-
#initialize(args) ⇒ WaveformDataFile
constructor
A new instance of WaveformDataFile.
-
#max_sample(index) ⇒ Integer
The maximum waveform data point at the specified index.
-
#min_sample(index) ⇒ Integer
The minimum waveform data point at the specified index.
-
#sample_rate ⇒ Integer
Audio sample rate, in Hz.
- #sample_rate=(sample_rate) ⇒ Object
-
#samples_per_pixel ⇒ Integer
Number of audio samples per waveform minimum/maximum pair.
-
#samples_per_pixel=(samples_per_pixel) ⇒ Object
Sets the waveform data scaling.
-
#save_as_binary(filename) ⇒ WaveformDataFile
Writes the waveform data to file in binary (.dat) format.
-
#save_as_json(filename) ⇒ WaveformDataFile
Writes the waveform data to file in JSON format.
-
#size ⇒ Integer
Length of waveform data (number of minimum/maximum value pairs).
-
#start_time ⇒ Numeric?
Start time of the waveform data, in seconds, or nil if not set.
-
#start_time=(start_time) ⇒ Object
Sets the start time of the waveform data.
-
#to_binary ⇒ String
A binary representation of the waveform data file.
-
#to_hash ⇒ Hash
A hash representation of the waveform data file.
-
#to_json ⇒ String
A JSON representation of the waveform data file.
Constructor Details
#initialize(args) ⇒ WaveformDataFile
Returns a new instance of WaveformDataFile.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 21 def initialize(args) if args[:filename] read(args[:filename]) else self.sample_rate = args[:sample_rate] self.samples_per_pixel = args[:samples_per_pixel] self.bits = args[:bits] self.start_time = args[:start_time] @data = [] end end |
Instance Method Details
#append(min_sample, max_sample) ⇒ WaveformDataFile
Appends a waveform minimum/maximum pair.
185 186 187 188 189 190 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 185 def append(min_sample, max_sample) @data << min_sample @data << max_sample self end |
#bits ⇒ Integer
Returns Resolution of waveform data points, either 8 or 16 bits.
73 74 75 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 73 def bits @bits end |
#bits=(bits) ⇒ Object
Sets the resolution of waveform data points.
81 82 83 84 85 86 87 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 81 def bits=(bits) if bits != 8 && bits != 16 raise Error, "Invalid bits: #{bits}" else @bits = bits end end |
#max_sample(index) ⇒ Integer
Returns The maximum waveform data point at the specified index.
200 201 202 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 200 def max_sample(index) @data[2 * index + 1] end |
#min_sample(index) ⇒ Integer
Returns The minimum waveform data point at the specified index.
194 195 196 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 194 def min_sample(index) @data[2 * index] end |
#sample_rate ⇒ Integer
Returns Audio sample rate, in Hz.
36 37 38 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 36 def sample_rate @sample_rate end |
#sample_rate=(sample_rate) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 43 def sample_rate=(sample_rate) if sample_rate <= 0 raise Error, "Invalid sample rate: #{sample_rate}" else @sample_rate = sample_rate end end |
#samples_per_pixel ⇒ Integer
Returns Number of audio samples per waveform minimum/maximum pair.
54 55 56 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 54 def samples_per_pixel @samples_per_pixel end |
#samples_per_pixel=(samples_per_pixel) ⇒ Object
Sets the waveform data scaling.
63 64 65 66 67 68 69 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 63 def samples_per_pixel=(samples_per_pixel) if samples_per_pixel <= 0 raise Error, "Invalid samples per pixel: #{samples_per_pixel}" else @samples_per_pixel = samples_per_pixel end end |
#save_as_binary(filename) ⇒ WaveformDataFile
Writes the waveform data to file in binary (.dat) format.
130 131 132 133 134 135 136 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 130 def save_as_binary(filename) File.open(filename, "wb") do |file| file.write(to_binary) end self end |
#save_as_json(filename) ⇒ WaveformDataFile
Writes the waveform data to file in JSON format.
117 118 119 120 121 122 123 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 117 def save_as_json(filename) File.open(filename, "w") do |file| file.write(to_json) end self end |
#size ⇒ Integer
Returns Length of waveform data (number of minimum/maximum value pairs).
207 208 209 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 207 def size @data.size / 2 end |
#start_time ⇒ Numeric?
Returns Start time of the waveform data, in seconds, or nil if not set.
92 93 94 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 92 def start_time @start_time end |
#start_time=(start_time) ⇒ Object
Sets the start time of the waveform data.
102 103 104 105 106 107 108 109 110 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 102 def start_time=(start_time) if start_time.nil? @start_time = nil elsif start_time < 0 raise Error, "Invalid start time: #{start_time}" else @start_time = start_time end end |
#to_binary ⇒ String
Returns A binary representation of the waveform data file.
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 164 def to_binary if @bits == 8 flags = FLAG_8_BIT format = "c*" else flags = 0 format = "s*" end output = encode_header(1, flags, @sample_rate, @samples_per_pixel, size) output += @data.pack(format) output end |
#to_hash ⇒ Hash
Returns A hash representation of the waveform data file.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 140 def to_hash obj = { sample_rate: @sample_rate, bits: @bits, samples_per_pixel: @samples_per_pixel, length: @data.size / 2, data: @data } if @start_time obj[:start_time] = @start_time end obj end |
#to_json ⇒ String
Returns A JSON representation of the waveform data file.
158 159 160 |
# File 'lib/audio_waveform/waveform_data_file.rb', line 158 def to_json JSON.generate(to_hash) end |