frame: Speed optimization.

This commit is contained in:
mewmew 2014-08-03 01:16:49 +02:00
parent caaecdbd81
commit f911f2e145
2 changed files with 18 additions and 1 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"bufio"
"flag"
"log"
"os"
@ -31,7 +32,14 @@ func main() {
}
func flacFrame(filePath string) (err error) {
s, err := flac.Open(filePath)
f, err := os.Open(filePath)
if err != nil {
log.Println(err)
}
defer f.Close()
br := bufio.NewReader(f)
s, err := flac.NewStream(br)
if err != nil {
return err
}

View file

@ -25,6 +25,8 @@ type SubFrame struct {
Samples []Sample
}
// TODO(u): Remove Sample type.
// A Sample is an audio sample. The size of each sample is between 4 and 32
// bits.
type Sample int32
@ -407,6 +409,9 @@ func (h *Header) DecodeRice(br *bit.Reader, predOrder int) (residuals []int32, e
// Rice partitions.
partCount := int(math.Pow(2, float64(partOrder)))
residuals = make([]int32, 0, h.SampleCount)
guess := int(h.SampleCount)
var total int
for partNum := 0; partNum < partCount; partNum++ {
partSampleCount := int(h.SampleCount) / partCount
@ -445,6 +450,7 @@ func (h *Header) DecodeRice(br *bit.Reader, predOrder int) (residuals []int32, e
} else {
partSampleCount = int(h.SampleCount)/int(math.Pow(2, float64(partOrder))) - predOrder
}
total += partSampleCount
dbg.Println("partSampleCount:", partSampleCount)
// Decode rice partition residuals.
@ -454,6 +460,9 @@ func (h *Header) DecodeRice(br *bit.Reader, predOrder int) (residuals []int32, e
}
residuals = append(residuals, partResiduals...)
}
if guess < total {
panic(fmt.Sprintf("guess (%d) < total (%d)", guess, total))
}
return residuals, nil
}