Browse Source

audio: write pad byte when data length is odd.

This is specified in RIFF format, but I forgot to implement.

     ckData  Binary data of fixed or variable size. The start of
	     ckData is word-aligned with respect to the start of the
	     RIFF file.  If the chunk size is an odd number of bytes, a
	     pad byte with value zero is written after ckData.  Word
	     aligning improves access speed (for chunks resident in
	     memory) and maintains compatibility with EA IFF.  The
	     ckSize value does not include the pad byte.

(From http://www.kk.iij4u.or.jp/~kondo/wave/mpidata.txt)
Dmitry Chestnykh 14 years ago
parent
commit
ad3bc71499
1 changed files with 9 additions and 0 deletions
  1. 9 0
      audio.go

+ 9 - 0
audio.go

@@ -86,18 +86,27 @@ func NewAudio(digits []byte) *Audio {
 // WriteTo writes captcha audio in WAVE format into the given io.Writer, and
 // returns the number of bytes written and an error if any.
 func (a *Audio) WriteTo(w io.Writer) (n int64, err os.Error) {
+	// Header.
 	nn, err := w.Write(waveHeader)
 	n = int64(nn)
 	if err != nil {
 		return
 	}
+	// Chunk length.
 	err = binary.Write(w, binary.LittleEndian, uint32(a.body.Len()))
 	if err != nil {
 		return
 	}
 	nn += 4
+	// Chunk data.
 	n, err = a.body.WriteTo(w)
 	n += int64(nn)
+	// Pad byte if chunk length is odd.
+	// (As header has even length, we can check if n is odd, not chunk).
+	if n % 2 != 0 {
+		w.Write([]byte{128})
+		n++
+	}
 	return
 }