Skip to content

Commit

Permalink
added tests for issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptix committed Apr 25, 2015
1 parent c731116 commit 61bc089
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions rw_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package wav

import (
"io/ioutil"
"math"
"os"
"testing"
)

func TestWriteRead_Int32(t *testing.T) {
const (
bits = 32
rate = 44100
)

f, err := ioutil.TempFile("", "wavPkgtest")
if err != nil {
t.Fatal(err)
}

testFname := f.Name()

meta := File{
Channels: 1,
SampleRate: rate,
SignificantBits: bits,
}

writer, err := meta.NewWriter(f)
if err != nil {
t.Fatal(err)
}

var freq float64
freq = 0.0001

// one second
for n := 0; n < rate; n += 1 {
y := int32(0.8 * math.Pow(2, bits-1) * math.Sin(freq*float64(n)))
freq += 0.000002

err = writer.WriteInt32(y)
if err != nil {
t.Fatal(err)
}
}
err = writer.Close()
if err != nil {
t.Fatal(err)
}

f, err = os.Open(testFname)
if err != nil {
t.Fatal(err)
}

stat, err := f.Stat()
if err != nil {
t.Fatal(err)
}

_, err = NewReader(f, stat.Size())
if err != nil {
t.Fatal(err)
}

os.Remove(testFname)
}

func TestWriteRead_Sample(t *testing.T) {
const (
bits = 16
rate = 44100
)

f, err := ioutil.TempFile("", "wavPkgtest")
if err != nil {
t.Fatal(err)
}

testFname := f.Name()

meta := File{
Channels: 1,
SampleRate: rate,
SignificantBits: bits,
}

writer, err := meta.NewWriter(f)
if err != nil {
t.Fatal(err)
}

var freq float64
freq = 0.0001

var s = make([]byte, 2)
// one second
for n := 0; n < rate; n += 1 {
y := int32(0.8 * math.Pow(2, bits-1) * math.Sin(freq*float64(n)))
freq += 0.000002

// s[3] = byte((y >> 24) & 0xFF)
// s[2] = byte((y >> 16) & 0xFF)
s[1] = byte((y >> 8) & 0xFF)
s[0] = byte(y & 0xFF)

err = writer.WriteSample(s)
if err != nil {
t.Fatal(err)
}
}
err = writer.Close()
if err != nil {
t.Fatal(err)
}

f, err = os.Open(testFname)
if err != nil {
t.Fatal(err)
}

stat, err := f.Stat()
if err != nil {
t.Fatal(err)
}

_, err = NewReader(f, stat.Size())
if err != nil {
t.Fatal(err)
}

os.Remove(testFname)
}

0 comments on commit 61bc089

Please sign in to comment.