示例#1
0
func TestDecode(t *testing.T) {
	for i, test := range decodeTests {
		t.Logf("\ntest %d: %s", i, test.name)
		data := undump(test.data)
		p, err := drum.Decode(bytes.NewReader(data))
		if test.expectError != "" {
			if err == nil {
				t.Fatalf("got no error; expected error matching %q", test.expectError)
			}
			ok, err1 := regexp.MatchString("^("+test.expectError+")$", err.Error())
			if err1 != nil {
				t.Fatalf("bad error pattern in test: %v", err1)
			}
			if !ok {
				t.Fatalf("got unexpected error %q; want %q", err.Error(), test.expectError)
			}
			if p != nil {
				t.Fatalf("non-nil return from error-returning Decode")
			}
			continue
		}
		if err != nil {
			t.Fatalf("unexpected error when decoding: %v", err)
		}
		output := p.String()
		if output != test.expectOutput {
			t.Fatalf("unexpected output\nGot\n%s\nWant\n%s\n", output, test.expectOutput)
		}
		// Test that it round-trips OK through MarshalBinary
		data, err = p.MarshalBinary()
		if err != nil {
			t.Fatalf("cannot marshal binary: %v", err)
		}
		p1, err := drum.Decode(bytes.NewReader(data))
		if err != nil {
			t.Fatalf("cannot decode round-tripped binary: %v", err)
		}
		output = p1.String()
		if output != test.expectOutput {
			t.Fatalf("round trip gave unexpected output\nGot\n%s\nWant\n%s\n", output, test.expectOutput)
		}
	}
}
示例#2
0
func Example_cowbell() {
	// This example demonstrates how to add more cowbell to
	// a drum machine pattern.

	p, err := drum.Decode(bytes.NewReader(cowbellExampleData))
	if err != nil {
		panic(err)
	}
	var cowbellTrack *drum.Track
	maxChan := 0
	for i := range p.Tracks {
		tr := &p.Tracks[i]
		if tr.Name == "cowbell" {
			cowbellTrack = tr
			break
		}
		if tr.Channel > maxChan {
			maxChan = tr.Channel
		}
	}
	if cowbellTrack == nil {
		p.Tracks = append(p.Tracks, drum.Track{
			Channel: maxChan + 1,
			Name:    "cowbell",
		})
		cowbellTrack = &p.Tracks[len(p.Tracks)-1]
	}
	for i := range cowbellTrack.Beats {
		if i%2 == 0 {
			cowbellTrack.Beats[i] = true
		}
	}
	fmt.Print(p)

	data, err := p.MarshalBinary()
	if err != nil {
		panic(err)
	}

	// To save the file, write data to a new file.
	_ = data
	// Output: Saved with HW Version: 0.808-alpha
	//Tempo: 120
	//(0) kick	|x---|x---|x---|x---|
	//(1) snare	|----|x---|----|x---|
	//(2) clap	|----|x-x-|----|----|
	//(3) hh-open	|--x-|--x-|x-x-|--x-|
	//(4) hh-close	|x---|x---|----|x--x|
	//(5) cowbell	|x-x-|x-x-|x-x-|x-x-|
}
示例#3
0
func TestMarshalBinaryWithNoVersion(t *testing.T) {
	p := &drum.Pattern{
		Tempo: 120,
	}
	data, err := p.MarshalBinary()
	if err != nil {
		t.Fatalf("cannot marshal: %v", err)
	}
	p1, err := drum.Decode(bytes.NewReader(data))
	if err != nil {
		t.Fatalf("error decoding: %v", err)
	}
	want := "1.0"
	if p1.Version != want {
		t.Fatalf("bad version, got %q; want %q", p1.Version, want)
	}
}