audio: Fix examples' performance issue

This commit is contained in:
Hajime Hoshi 2015-06-14 03:27:02 +09:00
parent 00f582d435
commit 564248641b
2 changed files with 14 additions and 16 deletions

View File

@ -15,8 +15,6 @@
package main package main
import ( import (
"bytes"
"encoding/binary"
"fmt" "fmt"
"log" "log"
"math" "math"
@ -76,13 +74,14 @@ func toBytes(l, r []int16) []byte {
if len(l) != len(r) { if len(l) != len(r) {
panic("len(l) must equal to len(r)") panic("len(l) must equal to len(r)")
} }
b := &bytes.Buffer{} b := make([]byte, len(l)*4)
for i := 0; i < len(l); i++ { for i, _ := range l {
if err := binary.Write(b, binary.LittleEndian, []int16{l[i], r[i]}); err != nil { b[4*i] = byte(l[i])
panic(err) b[4*i+1] = byte(l[i] >> 8)
b[4*i+2] = byte(r[i])
b[4*i+3] = byte(r[i] >> 8)
} }
} return b
return b.Bytes()
} }
func addNote() { func addNote() {

View File

@ -15,8 +15,6 @@
package main package main
import ( import (
"bytes"
"encoding/binary"
"fmt" "fmt"
"image/color" "image/color"
"log" "log"
@ -60,13 +58,14 @@ func toBytes(l, r []int16) []byte {
if len(l) != len(r) { if len(l) != len(r) {
panic("len(l) must equal to len(r)") panic("len(l) must equal to len(r)")
} }
b := &bytes.Buffer{} b := make([]byte, len(l)*4)
for i := 0; i < len(l); i++ { for i, _ := range l {
if err := binary.Write(b, binary.LittleEndian, []int16{l[i], r[i]}); err != nil { b[4*i] = byte(l[i])
panic(err) b[4*i+1] = byte(l[i] >> 8)
b[4*i+2] = byte(r[i])
b[4*i+3] = byte(r[i] >> 8)
} }
} return b
return b.Bytes()
} }
func addNote(freq float64, vol float64) { func addNote(freq float64, vol float64) {