Created
May 29, 2022 17:02
-
-
Save bartosz-witkowski/a8654d55c4355ee6fd563c768407197f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
import jm.music.data.Note | |
import jm.constants.Durations | |
import scala.util.Random | |
def digitToNote(d: Digit): Note = { | |
/* | |
* C is 1, D is 2, E is 3 etc | |
*/ | |
val (duration, delta) = d match { | |
case 0 => /* B MINOR_SECOND */ Durations.SIXTEENTH_NOTE -> -1 | |
case 1 => /* C UNISON */ Durations.QUARTER_NOTE -> 0 | |
case 2 => /* D MAJOR_SECOND */ Durations.EIGHTH_NOTE -> 2 | |
case 3 => /* E MAJOR_THIRD */ Durations.QUARTER_NOTE -> 4 | |
case 4 => /* F PERFECT_FOURTH */ Durations.QUARTER_NOTE -> 5 | |
case 5 => /* G PERFECT_FIFTH */ Durations.QUARTER_NOTE -> 7 | |
case 6 => /* A MAJOR_SIXTH */ Durations.EIGHTH_NOTE -> 9 | |
case 7 => /* B MAJOR_SIXTH */ Durations.SIXTEENTH_NOTE -> 11 | |
case 8 => /* C OCTAVE */ Durations.QUARTER_NOTE -> 12 | |
case 9 => /* D OCTAVE+M2 */ Durations.EIGHTH_NOTE -> 14 | |
} | |
val n: Int = delta + (60 + 12) | |
new Note(n, duration) | |
} | |
def digitToNoteMajor(d: Digit): Note = { | |
val delta = d match { | |
case 0 => /* B */ -1 | |
case 1 => /* C */ 0 | |
case 2 => /* D */ 2 | |
case 3 => /* E */ 4 | |
case 4 => /* F */ 5 | |
case 5 => /* G */ 7 | |
case 6 => /* A */ 9 | |
case 7 => /* B */ 11 | |
case 8 => /* C */ 12 | |
case 9 => /* D */ 14 | |
} | |
val n: Int = delta + (60 + 12) | |
new Note(n, Durations.QUARTER_NOTE) | |
} | |
def digitToNotePentatonic(d: Digit): Note = { | |
/* | |
* C D E G A | |
*/ | |
val delta = d match { | |
case 0 => /* A */ -3 | |
case 1 => /* C */ 0 | |
case 2 => /* D */ 2 | |
case 3 => /* E */ 4 | |
case 4 => /* G */ 7 | |
case 5 => /* A */ 9 | |
case 6 => /* C */ 12 | |
case 7 => /* D */ 12 + 2 | |
case 8 => /* E */ 12 + 4 | |
case 9 => /* G */ 12 + 7 | |
} | |
// middle C | |
val n: Int = delta + (60 + 12) | |
new Note(n, Durations.QUARTER_NOTE) | |
} | |
val PiDigits: List[Digit] = List( | |
3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4, | |
3, 3, 8, 3, 2, 7, 9, 5, 0, 2, 8, 8, 4, 1) | |
val EDigits: List[Digit] = List( | |
2, 7, 1, 8, 2, 8, 1, 8, 2, 8, 4, 5, 9, 0, 4, 5, 2, 3, 5, 3, 6, 0, 2, 8, | |
7, 4, 7, 1) | |
import jm.util.Write; | |
import jm.music.data.Part | |
import jm.music.data.Score | |
def renderDigits( | |
fileName: String, | |
digits: List[Digit], | |
d2n: Digit => Note): Unit = { | |
val notes = digits.map(d2n) | |
val part = new Part("part", 0, Part.DEFAULT_CHANNEL) | |
val phrase = new jm.music.data.Phrase(notes.toArray) | |
part.addPhrase(phrase) | |
val score = new Score | |
score.setTempo(120) | |
score.addPart(part) | |
Write.midi(score, fileName) | |
} | |
import jm.music.data.Phrase | |
def accompany( | |
duration: Double): Phrase = { | |
import jm.constants.Pitches._ | |
val Cmaj7 = Array(c3, e3, g3, b3) | |
val phrase = new Phrase(4) | |
val rest = new jm.music.data.Rest(Durations.QUARTER_NOTE) | |
var cur = 4.0 | |
while (cur <= (duration - 2 * Durations.WHOLE_NOTE)) { | |
phrase.addChord(Cmaj7, Durations.WHOLE_NOTE) | |
phrase.addRest(rest) | |
phrase.addChord(Cmaj7, Durations.HALF_NOTE + Durations.QUARTER_NOTE) | |
cur += 2 * Durations.WHOLE_NOTE | |
} | |
phrase.setDynamic(jm.constants.Volumes.MP) | |
phrase | |
} | |
def renderDigitsWithAccompaniament( | |
fileName: String, | |
digits: List[Digit], | |
d2n: Digit => Note): Unit = { | |
val notes = digits.map(d2n) | |
val part = new Part("part", 0, Part.DEFAULT_CHANNEL) | |
val phrase = new jm.music.data.Phrase(notes.toArray) | |
part.addPhrase(phrase) | |
val leftHand = accompany(phrase.getEndTime) | |
part.addPhrase(leftHand) | |
val score = new Score | |
score.setTempo(120) | |
score.addPart(part) | |
Write.midi(score, fileName) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment