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
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
Public: Constructs a Writer object which is available for writing sample data to the specified file
 (via the write method). When all sample data has been written, the Writer should be closed. Note
 that the wave file being written to will NOT be valid (and playable in other programs) until the
 Writer has been closed.

 If a block is given to this method, sample data can be written inside the given block. When the
 block terminates, the Writer will be automatically closed (and no more sample data can be written).

 If no block is given, then sample data can be written until the close method is called.

 io_or_file_name - The name of the wave file to read from, or an open IO object to read from.
                   Only implementations of IO that support seeking are supported, because
                   closing the Writer requires seeking back to the beginning of the file to
                   update information in the file's header.
 format - The sample data format that the file should contain

 Returns a Writer object that is ready to start writing the specified file's sample data.
 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.