Skip to content

Commit c04471c

Browse files
committed
support for note octave, duration and accidental
1 parent c02f09e commit c04471c

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

src/main/scala/de/htwg/scalala/advancedDsl/Note.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package de.htwg.scalala.advancedDsl
22

33
import de.htwg.scalala.music.Key
44

5-
case class Note(name: String) extends NoteElement {
5+
case class Note(name: String, accidental: String = "", duration: Int = 0, octave: String = "") extends NoteElement {
66
val notes = Map[String, Key](
77
"c" -> Key(midiNumber = 60),
88
"d" -> Key(midiNumber = 62),
@@ -17,5 +17,19 @@ case class Note(name: String) extends NoteElement {
1717
"HiHatOpen" -> Key(midiNumber = 46)
1818
)
1919

20-
val note: Key = notes.get(name).orNull
20+
val note: Key = {
21+
println(accidental)
22+
var n = accidental match {
23+
case "sharp" => notes.get(name).get.sharp
24+
case "flat" => notes.get(name).get.flat
25+
case "dot" => notes.get(name).get.dot
26+
case _ => notes.get(name).orNull
27+
}
28+
n = octave match {
29+
case "+" => n +
30+
case "-" => n -
31+
case _ => n
32+
}
33+
n.ticks(if (duration > 0) 16/duration else 16)
34+
}
2135
}

src/main/scala/de/htwg/scalala/advancedDsl/Reader.scala

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ class Reader extends StandardTokenParsers {
1313
"play",
1414
"plays",
1515
"tempo",
16-
"at")
17-
lexical.delimiters += (",", "(", ")")
16+
"at",
17+
"c" , "d" , "e" , "f" , "g" , "a" , "h",
18+
"2", "4", "8", "16",
19+
"sharp", "flat", "dot"
20+
)
21+
lexical.delimiters += (",", "(", ")", "/", ".", "+", "-")
1822

1923

2024
def song: Parser[Song] = rep(musician | track) ^^ {
@@ -73,8 +77,8 @@ class Reader extends StandardTokenParsers {
7377

7478

7579

76-
def note: Parser[Note] = ident ^^ {
77-
case i => new Note(i)
80+
def note: Parser[Note] = opt(octave) ~ ("c" | "d" | "e" | "f" | "g" | "a" | "h") ~ opt("." ~> accidental) ~ opt("/" ~> duration) ^^ {
81+
case o ~ n ~ a ~ d => new Note(n, a.getOrElse(""), d.getOrElse(0), o.getOrElse(""))
7882
}
7983

8084
def chord: Parser[Chord] = ("chord" ~> "(") ~> repsep(note, ",") <~ ")" ^^ {
@@ -83,7 +87,21 @@ class Reader extends StandardTokenParsers {
8387

8488

8589

86-
90+
def octave: Parser[String] = ("+" | "-") ^^ {
91+
o => o
92+
}
93+
94+
def accidental: Parser[String] = ("sharp" | "flat" | "dot") ^^ {
95+
a => a
96+
}
97+
98+
def duration: Parser[Int] = numericLit ^^ {
99+
d => d.toInt
100+
}
101+
102+
103+
104+
87105
def parseAll[T](p: Parser[T], in: String): ParseResult[T] = {
88106
phrase(p)(new lexical.Scanner(in))
89107
}

0 commit comments

Comments
 (0)