From 48dc0eeb47c32d5b29463b77df27b93a70ebfb0b Mon Sep 17 00:00:00 2001 From: John Sardo Date: Sun, 28 Feb 2016 18:07:15 -0500 Subject: [PATCH] basic twelve tone composition --- src/twelvetone.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++ src/twelvetone.h | 3 +++ 2 files changed, 50 insertions(+) create mode 100644 src/twelvetone.cpp create mode 100644 src/twelvetone.h diff --git a/src/twelvetone.cpp b/src/twelvetone.cpp new file mode 100644 index 0000000..5e96ce1 --- /dev/null +++ b/src/twelvetone.cpp @@ -0,0 +1,47 @@ +#include "twelvetone.h" +#include +#include +#include +#include + +int nrand(int n) +{ + return rand() % n; +} + +// write a 12 tone "row" to the track tr. +void make_row(Track& tr) +{ + srand(time(NULL)); + int tones[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + int tone_len = 12; + int current_tone; + int index; + int k = 1; + + std::vector row; + + for (int i = 0; i < tone_len; ++i) { + current_tone = -1; + k = 1; + while (current_tone == -1) { + index = nrand(tone_len); + if (tones[index] != -1) { + current_tone = tones[index]; + tones[index] = -1; + } else if (index + k < tone_len && tones[index + k] != -1) { + current_tone = tones[index + k]; + tones[index + k] = -1; + } else if (index - k >= 0 && tones[index - k] != -1) { + current_tone = tones[index - k]; + tones[index - k] = -1; + } + k++; + } + row.push_back(current_tone); + } + for (auto t : row) { + tr.push_back(Note(t, nrand(3)+3, nrand(8)+1)); + } +} + diff --git a/src/twelvetone.h b/src/twelvetone.h new file mode 100644 index 0000000..3d73ecf --- /dev/null +++ b/src/twelvetone.h @@ -0,0 +1,3 @@ +#include "note.h" + +void make_row(Track& tr);