From 564248641b5a4b693d4ca28be5c20070bf3ab047 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 14 Jun 2015 03:27:02 +0900 Subject: [PATCH] audio: Fix examples' performance issue --- example/audio/main.go | 15 +++++++-------- example/piano/main.go | 15 +++++++-------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/example/audio/main.go b/example/audio/main.go index 523b361fc..971d03b14 100644 --- a/example/audio/main.go +++ b/example/audio/main.go @@ -15,8 +15,6 @@ package main import ( - "bytes" - "encoding/binary" "fmt" "log" "math" @@ -76,13 +74,14 @@ func toBytes(l, r []int16) []byte { if len(l) != len(r) { panic("len(l) must equal to len(r)") } - b := &bytes.Buffer{} - for i := 0; i < len(l); i++ { - if err := binary.Write(b, binary.LittleEndian, []int16{l[i], r[i]}); err != nil { - panic(err) - } + b := make([]byte, len(l)*4) + for i, _ := range l { + b[4*i] = byte(l[i]) + 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.Bytes() + return b } func addNote() { diff --git a/example/piano/main.go b/example/piano/main.go index 5c3a8a8dc..1fce4dcf2 100644 --- a/example/piano/main.go +++ b/example/piano/main.go @@ -15,8 +15,6 @@ package main import ( - "bytes" - "encoding/binary" "fmt" "image/color" "log" @@ -60,13 +58,14 @@ func toBytes(l, r []int16) []byte { if len(l) != len(r) { panic("len(l) must equal to len(r)") } - b := &bytes.Buffer{} - for i := 0; i < len(l); i++ { - if err := binary.Write(b, binary.LittleEndian, []int16{l[i], r[i]}); err != nil { - panic(err) - } + b := make([]byte, len(l)*4) + for i, _ := range l { + b[4*i] = byte(l[i]) + 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.Bytes() + return b } func addNote(freq float64, vol float64) {