-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
6 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef NOTE_H | ||
#define NOTE_H | ||
|
||
#define OCTAVE 12 | ||
|
||
enum ScaleIndex { | ||
MAJOR_SCALE = 0, | ||
MINOR_SCALE, | ||
}; | ||
|
||
static const short scales[][12] = {{1, -1, 0, -1, 0, 0, -1, 0, -1, 0, -1, 0}, | ||
{1, -1, 0, 0, -1, 0, -1, 0, 0, -1, 0, -1}}; | ||
|
||
/** | ||
* @brief Checks if a note is in a scale. | ||
* @param note The note to check. | ||
* @param scale The root note of the scale. | ||
* @param scale_index The index of the scale table. | ||
* @return -1 if the note is not in the scale, 0 if it is, 1 if it is and it's | ||
* the root note. | ||
*/ | ||
short is_note_in_scale(unsigned char note, unsigned char scale, | ||
int scale_index); | ||
|
||
#endif // NOTE_H |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "note.h" | ||
|
||
short is_note_in_scale(const unsigned char note, const unsigned char scale, | ||
const int scale_index) { | ||
int idx = (note - scale) % OCTAVE; | ||
if (idx < 0) { | ||
idx += OCTAVE; | ||
} | ||
|
||
return scales[scale_index][idx]; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <stdarg.h> | ||
#include <stddef.h> | ||
#include <setjmp.h> | ||
#include <stdint.h> | ||
#include <cmocka.h> | ||
#include "note.h" | ||
|
||
static void test_is_note_in_scale(void **state) { | ||
assert_int_equal(is_note_in_scale(0, 0, MAJOR_SCALE), 1); | ||
assert_int_equal(is_note_in_scale(92, 0, MINOR_SCALE), 0); | ||
assert_int_equal(is_note_in_scale(0, 3, MAJOR_SCALE), 0); | ||
assert_int_equal(is_note_in_scale(95, 0, MINOR_SCALE), -1); | ||
} | ||
|
||
int main(void) { | ||
const struct CMUnitTest tests[] = { | ||
cmocka_unit_test(test_is_note_in_scale), | ||
}; | ||
|
||
return cmocka_run_group_tests(tests, NULL, NULL); | ||
} |