Method: WaveFile::Writer#write
- Defined in:
- lib/wavefile/writer.rb
#write(buffer) ⇒ Object
Public: Appends the sample data in the given Buffer to the end of the wave file.
buffer - A Buffer instance containing the sample data to be written to the file. The format of the Buffer doesn't have to match the format of the file being written to by the Writer - if it doesn't match, it will automatically be converted to the correct format.
Examples
square_wave_samples = ([0.5] * 100) + ([-0.5] * 100)
buffer = Buffer.new(square_wave_samples, Format.new(1, :float, 44100))
Writer.new("my_file.wav", Format.new(:stereo, :pcm_16, 44100)) do |writer|
writer.write(buffer)
end
writer = Writer.new("my_file.wav", Format.new(:stereo, :pcm_16, 44100))
writer.write(buffer)
writer.close
# This will raise WriterClosedError because the Writer has already been closed.
writer.write(buffer)
Returns the number of sample frames that have been written to the file so far.
Raises WriterClosedError if the Writer has been closed.
Raises BufferConversionError if the Buffer can't be converted to the Writer's format.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/wavefile/writer.rb', line 100 def write(buffer) if @closed raise WriterClosedError end samples = buffer.convert(@format).samples if @format.bits_per_sample == 24 && @format.sample_format == :pcm samples.flatten.each do |sample| @io.write([sample].pack("l<X")) end else @io.write(samples.flatten.pack(@pack_code)) end @total_sample_frames += samples.length end |