Forráskód Böngészése

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 éve
szülő
commit
ad3bc71499
1 módosított fájl, 9 hozzáadás és 0 törlés
  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
 // WriteTo writes captcha audio in WAVE format into the given io.Writer, and
 // returns the number of bytes written and an error if any.
 // returns the number of bytes written and an error if any.
 func (a *Audio) WriteTo(w io.Writer) (n int64, err os.Error) {
 func (a *Audio) WriteTo(w io.Writer) (n int64, err os.Error) {
+	// Header.
 	nn, err := w.Write(waveHeader)
 	nn, err := w.Write(waveHeader)
 	n = int64(nn)
 	n = int64(nn)
 	if err != nil {
 	if err != nil {
 		return
 		return
 	}
 	}
+	// Chunk length.
 	err = binary.Write(w, binary.LittleEndian, uint32(a.body.Len()))
 	err = binary.Write(w, binary.LittleEndian, uint32(a.body.Len()))
 	if err != nil {
 	if err != nil {
 		return
 		return
 	}
 	}
 	nn += 4
 	nn += 4
+	// Chunk data.
 	n, err = a.body.WriteTo(w)
 	n, err = a.body.WriteTo(w)
 	n += int64(nn)
 	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
 	return
 }
 }