Browse Source

Add NewRandomAudio function.

Audio test cmd now writes to stdout.
Dmitry Chestnykh 14 years ago
parent
commit
e40250a124
2 changed files with 13 additions and 14 deletions
  1. 11 1
      audio.go
  2. 2 13
      cmd/audio/main.go

+ 11 - 1
audio.go

@@ -178,7 +178,17 @@ func NewAudio(numbers []byte) *Audio {
 	return a
 }
 
-// WriteTo writes captcha audio in WAVE format.
+// NewRandomAudio generates a sequence of random numbers with the given length,
+// and returns a new audio captcha with this numbers, and the sequence of
+// numbers itself.
+func NewRandomAudio(length int) (a *Audio, numbers []byte) {
+	numbers = randomNumbers(length)
+	a = NewAudio(numbers)
+	return
+}
+
+// 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) {
 	nn, err := w.Write(waveHeader)
 	n = int64(nn)

+ 2 - 13
cmd/audio/main.go

@@ -2,21 +2,10 @@ package main
 
 import (
 	"github.com/dchest/captcha"
-	"log"
 	"os"
 )
 
 func main() {
-	f, err := os.Create("mixed.wav")
-	if err != nil {
-		log.Fatalf("%s", err)
-	}
-	defer f.Close()
-
-	c := captcha.NewAudio([]byte{1, 2, 3, 4, 5, 6})
-	n, err := c.WriteTo(f)
-	if err != nil {
-		log.Fatalf("%s", err)
-	}
-	println("written", n)
+	c, _ := captcha.NewRandomAudio(captcha.StdLength)
+	c.WriteTo(os.Stdout)
 }