Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Note expression as sharp/flat is switched based on input #15

Merged
merged 1 commit into from
Mar 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions chord/chord.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (

// Chord in a particular key
type Chord struct {
Root note.Class
Tones map[Interval]note.Class
Root note.Class
AdjSymbol note.AdjSymbol
Tones map[Interval]note.Class
}

// Of a particular key, e.g. Of("C minor 7")
Expand All @@ -33,6 +34,9 @@ func (this *Chord) Notes() (notes []*note.Note) {
func (this *Chord) parse(name string) {
this.Tones = make(map[Interval]note.Class)

// determine whether the name is "sharps" or "flats"
this.AdjSymbol = note.AdjSymbolOf(name)

// parse the root, and keep the remaining string
this.Root, name = note.RootAndRemaining(name)

Expand Down
12 changes: 6 additions & 6 deletions chord/chord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
package chord

import (
"fmt"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"

"github.com/go-music-theory/music-theory/key"
"github.com/go-music-theory/music-theory/note"
"io/ioutil"
"gopkg.in/yaml.v2"
"fmt"
)

func TestChordExpectations(t *testing.T) {
Expand All @@ -24,12 +24,12 @@ func TestChordExpectations(t *testing.T) {
assert.True(t, len(testExpectations.Chords) > 0)
for name, expect := range testExpectations.Chords {
actual := Of(name)
assert.Equal(t, note.ClassNamed(expect.Root), actual.Root, fmt.Sprintf("name:%v expect.Root:%v actual.Root:%v", name, note.ClassNamed(expect.Root), actual.Root))
assert.Equal(t, expect.Root, actual.Root.String(actual.AdjSymbol), fmt.Sprintf("name:%v expect.Root:%v actual.Root:%v", name, expect.Root, actual.Root.String(actual.AdjSymbol)))
for i, c := range expect.Tones {
assert.Equal(t, note.ClassNamed(c), actual.Tones[i], fmt.Sprintf("name:%v expect.Tones[%v]:%v actual.Tones[%v]:%v", name, i, c, i, actual.Tones[i]))
assert.Equal(t, c, actual.Tones[i].String(actual.AdjSymbol), fmt.Sprintf("name:%v expect.Tones[%v]:%v actual.Tones[%v]:%v", name, i, c, i, actual.Tones[i].String(actual.AdjSymbol)))
}
for i, c := range actual.Tones {
assert.Equal(t, note.ClassNamed(expect.Tones[i]), c, fmt.Sprintf("name:%v actual.Tones[%v]:%v expect.Tones[%v]:%v", name, i, c, i, expect.Tones[i]))
assert.Equal(t, expect.Tones[i], c.String(actual.AdjSymbol), fmt.Sprintf("name:%v actual.Tones[%v]:%v expect.Tones[%v]:%v", name, i, c.String(actual.AdjSymbol), i, expect.Tones[i]))
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions chord/spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Chords are expressed in readable strings, e.g. CMb5b7 or Cm679-5
package chord

import (
"gopkg.in/yaml.v2"
)

func (c Chord) ToYAML() string {
spec := specFrom(c)
out, _ := yaml.Marshal(spec)
return string(out[:])
}

/*
*
private */

func specFrom(c Chord) specChord {
s := specChord{}
s.Root = c.Root.String(c.AdjSymbol)
s.Tones = make(map[int]string)
for i, t := range c.Tones {
s.Tones[int(i)] = t.String(c.AdjSymbol)
}
return s
}

type specChord struct {
Root string
Tones map[int]string
}

14 changes: 14 additions & 0 deletions chord/spec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Chords are expressed in readable strings, e.g. CMb5b7 or Cm679-5
package chord

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestToYAML(t *testing.T) {
c := Of("Cm769-5")
out := c.ToYAML()
assert.Equal(t, "root: C\ntones:\n 1: C\n 3: D#\n 6: A\n 7: A#\n 9: D\n", out)
}
85 changes: 47 additions & 38 deletions chord/testdata/expectations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ chords:
tones:
1: C
3: E
5: Ab
5: G#

C dim:
root: C
tones:
1: C
3: Eb
5: Gb
3: D#
5: F#

C sus:
root: C
Expand Down Expand Up @@ -111,15 +111,15 @@ chords:
1: C
3: E
5: G
7: Bb
7: A#

C dominant 7 flat 5:
root: C
tones:
1: C
3: E
5: Gb
7: Bb
5: F#
7: A#

Cdom7♭5:
root: C
Expand All @@ -133,48 +133,48 @@ chords:
root: C
tones:
1: C
3: Eb
5: Gb
3: D#
5: F#
7: A

Cdim7:
root: C
tones:
1: C
3: Eb
5: Gb
3: D#
5: F#
7: A

C half diminished 7:
root: C
tones:
1: C
3: Eb
5: Gb
7: Bb
3: D#
5: F#
7: A#

C diminished M7:
root: C
tones:
1: C
3: Eb
5: Gb
3: D#
5: F#
7: B

Cdim maj7:
root: C
tones:
1: C
3: Eb
5: Gb
3: D#
5: F#
7: B

CdimM7:
root: C
tones:
1: C
3: Eb
5: Gb
3: D#
5: F#
7: B

Cm nondominant -5 679:
Expand All @@ -189,15 +189,15 @@ chords:
root: C
tones:
1: C
3: Eb
3: D#
5: G
7: B

Cm(maj7): # minor 3rd + major 7th
root: C
tones:
1: C
3: Eb
3: D#
5: G
7: B

Expand All @@ -215,22 +215,22 @@ chords:
1: C
3: E
5: G#
7: Bb
7: A#

C nondominant 7:
root: C
tones:
3: E
5: G
7: Bb
7: A#

C 9: # dominant 9th
root: C
tones:
1: C
3: E
5: G
7: Bb
7: A#
9: D

C7#9: # dominant 7, sharp 9
Expand All @@ -239,7 +239,7 @@ chords:
1: C
3: E
5: G
7: Bb
7: A#
9: D#

CM9: # major 9th
Expand Down Expand Up @@ -274,28 +274,29 @@ chords:
tones:
1: C
5: G
7: Bb
7: A#
9: D
11: F

Cm11: # minor 11
root: C
tones:
1: C
3: E♭
3: D#
5: G
7: B♭
7: A#
9: D
11: F

C11: # dominant 11
root: C
B♭m11: # minor 11
root: Bb
tones:
1: C
5: G
7: B♭
9: D
11: F
1: Bb
3: Db
5: F
7: Ab
9: C
11: Eb

CM13: # major 13
root: C
Expand Down Expand Up @@ -339,9 +340,9 @@ chords:
root: C
tones:
1: C
3: Eb
3: D#
5: G
7: Bb
7: A#
9: D
11: F
13: A
Expand All @@ -351,7 +352,15 @@ chords:
tones:
1: C
5: G
7: Bb
7: A#
9: D
11: F
13: A

CMb5b7:
root: C
tones:
1: C
3: E
5: Gb
7: Bb
4 changes: 1 addition & 3 deletions music-theory.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ func main() {
Action: func(c *cli.Context) {
name := c.Args().First()
if len(name) > 0 {

out, _ := yaml.Marshal(chord.Of(name))
fmt.Printf("%s", string(out[:]))
fmt.Printf("%s", chord.Of(name).ToYAML())
} else {
// no arguments
cli.ShowCommandHelp(c, "chord")
Expand Down
14 changes: 12 additions & 2 deletions music-theory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ import (
"testing"
)

func TestSpec(t *testing.T) {
func TestMusicTheory(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

os.Args = []string{"cmd",
"-k", "G minor 7",
"chord", "G minor 7",
}
main()
}

func TestHelpCmd(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

os.Args = []string{"cmd",
"-h",
}
main()
}
33 changes: 33 additions & 0 deletions note/adjsymbol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Note has an adjustment symbol (Sharp or Flat) to render the "accidental notes for a given name (e.g. of a chord, scale or key)
package note

import "regexp"

// AdjSymbolOf the adjustment symbol (Sharp or Flat) for a given name (e.g. of a chord, scale or key)
func AdjSymbolOf(name string) AdjSymbol {
numSharps := len(rgxSharps.FindAllString(name, -1))
numFlats := len(rgxFlats.FindAllString(name, -1))
if numSharps >= numFlats {
return Sharp
} else {
return Flat
}
}

// Expression of the "accidental notes" as either Sharps or Flats
type AdjSymbol int

const (
Sharp AdjSymbol = iota
Flat
)

/*
*
private */

var (
rgxSharps, _ = regexp.Compile("[♯#]")
rgxFlats, _ = regexp.Compile("[♭b]")
)

Loading