1 /**
2 * Copyright © Underground Rekordz 2019
3 * License: MIT (https://github.com/UndergroundRekordz/Musicpulator/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module musicpulator.direction;
7 
8 import musicpulator.notes;
9 
10 /// Enumeration of note directions.
11 enum NoteDirection
12 {
13   /// An upwards note direction. Ex. C5 -> E5
14   upwards,
15   /// A downwards note direction. Ex. E5 -> D#5
16   downwards,
17   /// A forward note direction. Ex. G#5 -> G#5
18   forward
19 }
20 
21 /**
22 * Gets the direction of a note relative to another note.
23 * Params:
24 *   noteX = The first note.
25 *   noteY = The secondary note.
26 * Returns:
27 *   The direction in which noteX progresses to noteY.
28 */
29 NoteDirection getNoteDirection(NoteId noteX, NoteId noteY)
30 {
31   if (noteX.id == noteY.id)
32   {
33     return NoteDirection.forward;
34   }
35   else if (noteX.id > noteY.id)
36   {
37     return NoteDirection.downwards;
38   }
39   else
40   {
41     return NoteDirection.upwards;
42   }
43 }
44 
45 /**
46 * Gets all directions based on a set of note ids.
47 * Params:
48 *   noteIds = The ids of the notes to get the directions for.
49 * Returns:
50 *   An array of directions that the notes progress in.
51 */
52 NoteDirection[] getNoteDirections(NoteId[] noteIds)
53 {
54   NoteDirection[] directions;
55 
56   if (noteIds && noteIds.length)
57   {
58     foreach (i; 0 .. (noteIds.length - 1))
59     {
60       auto noteX = noteIds[i];
61       auto noteY = noteIds[i + 1];
62 
63       directions ~= getNoteDirection(noteX, noteY);
64     }
65   }
66 
67   return directions;
68 }