Explorar el Código

Apply gofmt; fix comment in generate.go.

Dmitry Chestnykh hace 14 años
padre
commit
82004bd901
Se han modificado 2 ficheros con 13 adiciones y 9 borrados
  1. 12 8
      audio.go
  2. 1 1
      originals/generate.go

+ 12 - 8
audio.go

@@ -12,8 +12,11 @@ import (
 
 
 const sampleRate = 8000
 const sampleRate = 8000
 
 
-// Length of the longest number sound
-var longestNumSndLen int
+var (
+	// Length of the longest number sound
+	longestNumSndLen int
+	endingBeepSound  []byte
+)
 
 
 // mixSound mixes src into dst. Dst must have length equal to or greater than
 // mixSound mixes src into dst. Dst must have length equal to or greater than
 // src length.
 // src length.
@@ -22,7 +25,7 @@ func mixSound(dst, src []byte) {
 		av := int(v)
 		av := int(v)
 		bv := int(dst[i])
 		bv := int(dst[i])
 		if av < 128 && bv < 128 {
 		if av < 128 && bv < 128 {
-			dst[i] = byte(av*bv/128)
+			dst[i] = byte(av * bv / 128)
 		} else {
 		} else {
 			dst[i] = byte(2*(av+bv) - av*bv/128 - 256)
 			dst[i] = byte(2*(av+bv) - av*bv/128 - 256)
 		}
 		}
@@ -64,7 +67,7 @@ func changeSpeed(a []byte, pitch float64) []byte {
 
 
 // rndFloat64n returns a random float64 number in range [from, to].
 // rndFloat64n returns a random float64 number in range [from, to].
 func rndFloat64n(from, to float64) float64 {
 func rndFloat64n(from, to float64) float64 {
-	return (to-from)*rand.Float64()+from
+	return (to-from)*rand.Float64() + from
 }
 }
 
 
 func randomSpeed(a []byte) []byte {
 func randomSpeed(a []byte) []byte {
@@ -107,7 +110,7 @@ func makeBackgroundSound(length int) []byte {
 	for i := 0; i < length/(sampleRate/10); i++ {
 	for i := 0; i < length/(sampleRate/10); i++ {
 		snd := numberSounds[rand.Intn(10)]
 		snd := numberSounds[rand.Intn(10)]
 		snd = changeSpeed(reversedSound(snd), rndFloat64n(0.8, 1.4))
 		snd = changeSpeed(reversedSound(snd), rndFloat64n(0.8, 1.4))
-		place := rand.Intn(len(b)-len(snd))
+		place := rand.Intn(len(b) - len(snd))
 		setSoundLevel(snd, rndFloat64n(0.5, 1.2))
 		setSoundLevel(snd, rndFloat64n(0.5, 1.2))
 		mixSound(b[place:], snd)
 		mixSound(b[place:], snd)
 	}
 	}
@@ -127,6 +130,7 @@ func init() {
 			longestNumSndLen = len(v)
 			longestNumSndLen = len(v)
 		}
 		}
 	}
 	}
+	endingBeepSound = changeSpeed(beepSound, 1.4)
 }
 }
 
 
 type CaptchaAudio struct {
 type CaptchaAudio struct {
@@ -151,12 +155,12 @@ func NewAudio(numbers []byte) *CaptchaAudio {
 		intervals[i] = dur
 		intervals[i] = dur
 	}
 	}
 	// Background noise
 	// Background noise
-	bg := makeBackgroundSound(longestNumSndLen*len(numbers)+intdur)
+	bg := makeBackgroundSound(longestNumSndLen*len(numbers) + intdur)
 	// --
 	// --
 	a := new(CaptchaAudio)
 	a := new(CaptchaAudio)
 	a.body = bytes.NewBuffer(nil)
 	a.body = bytes.NewBuffer(nil)
 	// Prelude, three beeps
 	// Prelude, three beeps
-	sil := makeSilence(sampleRate/5)
+	sil := makeSilence(sampleRate / 5)
 	a.body.Write(beepSound)
 	a.body.Write(beepSound)
 	a.body.Write(sil)
 	a.body.Write(sil)
 	a.body.Write(beepSound)
 	a.body.Write(beepSound)
@@ -170,7 +174,7 @@ func NewAudio(numbers []byte) *CaptchaAudio {
 	}
 	}
 	a.body.Write(bg)
 	a.body.Write(bg)
 	// Ending
 	// Ending
-	a.body.Write(changeSpeed(beepSound, 1.4))
+	a.body.Write(endingBeepSound)
 	return a
 	return a
 }
 }
 
 

+ 1 - 1
originals/generate.go

@@ -1,4 +1,4 @@
-// Generates pcm.go from WAVE files [0-9].wav
+// Generates ../sounds.go from WAVE files
 package main
 package main
 
 
 import (
 import (