Class: Wavify::Codecs::Wav
- Defined in:
- lib/wavify/codecs/wav.rb,
sig/codecs.rbs
Overview
WAV codec with PCM, float, and WAVE_FORMAT_EXTENSIBLE support.
Constant Summary collapse
- EXTENSIONS =
Recognized filename extensions.
%w[.wav .wave].freeze
- WAV_FORMAT_PCM =
:nodoc:
0x0001- WAV_FORMAT_FLOAT =
:nodoc:
0x0003- WAV_FORMAT_EXTENSIBLE =
:nodoc:
0xFFFE- RIFF_MAX_SIZE =
:nodoc:
0xFFFF_FFFF- GUID_TAIL =
:nodoc:
[0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71].pack("C*").freeze
- PCM_SUBFORMAT_GUID =
:nodoc:
([WAV_FORMAT_PCM, 0x0000, 0x0010].pack("V v v") + GUID_TAIL).freeze
- FLOAT_SUBFORMAT_GUID =
:nodoc:
([WAV_FORMAT_FLOAT, 0x0000, 0x0010].pack("V v v") + GUID_TAIL).freeze
- SPEAKER_BITS =
{ front_left: 0x0001, front_right: 0x0002, front_center: 0x0004, low_frequency: 0x0008, back_left: 0x0010, back_right: 0x0020, front_left_of_center: 0x0040, front_right_of_center: 0x0080, back_center: 0x0100, side_left: 0x0200, side_right: 0x0400, top_center: 0x0800, top_front_left: 0x1000, top_front_center: 0x2000, top_front_right: 0x4000, top_back_left: 0x8000, top_back_center: 0x1_0000, top_back_right: 0x2_0000 }.freeze
- INFO_TAGS =
:nodoc:
{ "INAM" => :title, "IART" => :artist, "ICMT" => :comment, "ICOP" => :copyright, "ICRD" => :date, "IGNR" => :genre, "ISFT" => :software, "ITCH" => :technician }.freeze
- INFO_TAG_CODES =
:nodoc:
INFO_TAGS.invert.freeze
- SMPL_LOOP_TYPES =
:nodoc:
{ 0 => :forward, 1 => :alternating, 2 => :backward }.freeze
Class Method Summary collapse
- .can_read?(io_or_path) ⇒ Boolean
-
.metadata(io_or_path) ⇒ Hash
Reads WAV metadata without fully decoding samples.
-
.read(io_or_path, format: nil, warning_io: nil) ⇒ Wavify::Core::SampleBuffer
Reads a WAV file/IO into a sample buffer.
-
.stream_read(io_or_path, chunk_size: 4096, warning_io: nil) {|arg0| ... } ⇒ Enumerator
Streams WAV data chunks as sample buffers.
-
.stream_write(io_or_path, format:, info: nil, **codec_options) {|arg0| ... } ⇒ Enumerator, ...
Streams WAV encoding and finalizes the RIFF header on completion.
-
.write(io_or_path, sample_buffer, format: nil, info: nil, **codec_options) ⇒ String, IO
Writes a sample buffer as WAV.
Methods inherited from Base
Class Method Details
.can_read?(io_or_path) ⇒ Boolean
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/wavify/codecs/wav.rb', line 57 def can_read?(io_or_path) if io_or_path.is_a?(String) return true if EXTENSIONS.include?(File.extname(io_or_path).downcase) return false unless File.file?(io_or_path) end io, close_io = open_input(io_or_path) return false unless io header = probe_bytes(io, 12) (header&.start_with?("RIFF") || header&.start_with?("RF64")) && header[8, 4] == "WAVE" rescue StreamError false ensure io.close if close_io && io end |
.metadata(io_or_path) ⇒ Hash
Reads WAV metadata without fully decoding samples.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/wavify/codecs/wav.rb', line 192 def (io_or_path) io, close_io = open_input(io_or_path) ensure_seekable!(io) info = parse_chunk_directory(io) format = info.fetch(:format) sample_frame_count = info.fetch(:sample_frame_count) { format: format, sample_frame_count: sample_frame_count, duration: Core::Duration.from_samples(sample_frame_count, format.sample_rate), fact_sample_length: info[:fact_sample_length], smpl: info[:smpl], loops: normalized_smpl_loops(info[:smpl]), cue: info[:cue], cue_points: info[:cue]&.fetch(:points) || [], info: info[:info], bext: info[:bext], broadcast_extension: info[:bext], rf64: info[:rf64], container_bit_depth: info[:container_bit_depth], valid_bits_per_sample: info[:valid_bits_per_sample], channel_mask: info[:channel_mask], channel_layout: format.channel_layout, warnings: info[:warnings].freeze } ensure io.close if close_io && io end |
.read(io_or_path, format: nil, warning_io: nil) ⇒ Wavify::Core::SampleBuffer
Reads a WAV file/IO into a sample buffer.
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/wavify/codecs/wav.rb', line 79 def read(io_or_path, format: nil, warning_io: nil) io, close_io = open_input(io_or_path) ensure_seekable!(io) info = parse_chunk_directory(io) emit_warnings(info.fetch(:warnings), warning_io) source_format = info.fetch(:format) samples = read_data_chunk(io, info, source_format) buffer = Core::SampleBuffer.new(samples, source_format) format ? buffer.convert(format) : buffer ensure io.close if close_io && io end |
.stream_read(io_or_path, chunk_size: 4096, warning_io: nil) {|arg0| ... } ⇒ Enumerator
Streams WAV data chunks as sample buffers.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/wavify/codecs/wav.rb', line 117 def stream_read(io_or_path, chunk_size: 4096, warning_io: nil) return enum_for(__method__, io_or_path, chunk_size: chunk_size, warning_io: warning_io) unless block_given? raise InvalidParameterError, "chunk_size must be a positive Integer" unless chunk_size.is_a?(Integer) && chunk_size.positive? io, close_io = open_input(io_or_path) ensure_seekable!(io) info = parse_chunk_directory(io) emit_warnings(info.fetch(:warnings), warning_io) format = info.fetch(:format) bytes_per_frame = format.block_align remaining = info.fetch(:data_size) io.seek(info.fetch(:data_offset), IO::SEEK_SET) while remaining.positive? to_read = [remaining, chunk_size * bytes_per_frame].min chunk_data = read_exact(io, to_read, "truncated data chunk") samples = decode_samples(chunk_data, format) yield Core::SampleBuffer.new(samples, format) remaining -= to_read end ensure io.close if close_io && io end |
.stream_write(io_or_path, format:, info: nil, **codec_options) {|arg0| ... } ⇒ Enumerator, ...
Streams WAV encoding and finalizes the RIFF header on completion.
147 148 149 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 |
# File 'lib/wavify/codecs/wav.rb', line 147 def stream_write(io_or_path, format:, info: nil, **) (, operation: "WAV stream_write") return enum_for(__method__, io_or_path, format: format, info: info, **) unless block_given? raise InvalidParameterError, "format must be Core::Format" unless format.is_a?(Core::Format) io, close_io = open_output(io_or_path) ensure_seekable!(io) prepare_output!(io, owned: close_io) header = write_stream_header(io, format, info: info) total_data_bytes = 0 total_sample_frames = 0 writer = lambda do |chunk| raise InvalidParameterError, "stream chunk must be Core::SampleBuffer" unless chunk.is_a?(Core::SampleBuffer) buffer = chunk.format == format ? chunk : chunk.convert(format) encoded_bytes = buffer.sample_frame_count * format.block_align next_data_bytes = total_data_bytes + encoded_bytes next_sample_frames = total_sample_frames + buffer.sample_frame_count projected_container_bytes = (io.pos - header.fetch(:container_start)) + encoded_bytes + (next_data_bytes.odd? ? 1 : 0) validate_riff_sizes!(next_data_bytes, next_sample_frames, projected_container_bytes) encoded = encode_samples(buffer.samples, format) unless encoded.bytesize == encoded_bytes raise StreamError, "WAV encoder produced an unexpected byte count" end write_all(io, encoded) total_data_bytes = next_data_bytes total_sample_frames = next_sample_frames end yield writer write_all(io, "\x00") if total_data_bytes.odd? finalize_stream_header(io, header, total_data_bytes, total_sample_frames) finalize_output!(io, owned: close_io) io_or_path ensure io.close if close_io && io end |
.write(io_or_path, sample_buffer, format: nil, info: nil, **codec_options) ⇒ String, IO
Writes a sample buffer as WAV.
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/wavify/codecs/wav.rb', line 99 def write(io_or_path, sample_buffer, format: nil, info: nil, **) (, operation: "WAV write") raise InvalidParameterError, "sample_buffer must be Core::SampleBuffer" unless sample_buffer.is_a?(Core::SampleBuffer) target_format = format || sample_buffer.format raise InvalidParameterError, "format must be Core::Format" unless target_format.is_a?(Core::Format) buffer = sample_buffer.format == target_format ? sample_buffer : sample_buffer.convert(target_format) stream_write(io_or_path, format: target_format, info: info) do |writer| writer.call(buffer) end end |