-
Notifications
You must be signed in to change notification settings - Fork 22
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
dashodanger
committed
Jun 25, 2024
1 parent
7f30b49
commit 4f4f7ea
Showing
45 changed files
with
6,176 additions
and
5 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,5 @@ | ||
project(steve) | ||
|
||
file(GLOB_RECURSE SOURCES src/*.cpp src/*.h) | ||
|
||
add_library(steve ${SOURCES}) |
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,24 @@ | ||
This is free and unencumbered software released into the public domain. | ||
|
||
Anyone is free to copy, modify, publish, use, compile, sell, or | ||
distribute this software, either in source code form or as a compiled | ||
binary, for any purpose, commercial or non-commercial, and by any | ||
means. | ||
|
||
In jurisdictions that recognize copyright laws, the author or authors | ||
of this software dedicate any and all copyright interest in the | ||
software to the public domain. We make this dedication for the benefit | ||
of the public at large and to the detriment of our heirs and | ||
successors. We intend this dedication to be an overt act of | ||
relinquishment in perpetuity of all present and future rights to this | ||
software under copyright law. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
For more information, please refer to <http://unlicense.org> |
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,8 @@ | ||
# Steve | ||
|
||
Steve is a C++ program that composes music and generates MIDI files from scratch. | ||
|
||
## Assumptions | ||
- Harmony is defined such as, at any given time (preferably at a measure granularity), the set of all tones played fits inside a chord | ||
- Melody is defined by a scale (kept throughout an entire music and common to all instruments), all tones must be fit in the scale | ||
- Rhythm is defined by the fact that notes can only have a power of two duration, and a note can only be placed on a position dividable by its duration |
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,18 @@ | ||
#include "Chord.h" | ||
|
||
using namespace steve; | ||
|
||
uint8_t ChordDescription::get_tone(uint8_t index) const { | ||
uint8_t tone = 0; | ||
while(index > 0) { | ||
tone += 1; | ||
if(((1 << tone) & tones) != 0) { | ||
index -= 1; | ||
} | ||
} | ||
return tone; | ||
} | ||
|
||
std::string Chord::to_short_string() const { | ||
return key_name(key) + desc->suffix; | ||
} |
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,33 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "ItemDescription.h" | ||
#include "Steve.h" | ||
|
||
namespace steve { | ||
struct ChordDescription : public ItemDescription { | ||
std::string suffix; | ||
ToneSet tones = 1; | ||
bool uppercase = false; | ||
|
||
uint8_t get_tone(uint8_t index) const; | ||
|
||
inline uint8_t get_tone_count() const { return tone_set_count(tones); } | ||
}; | ||
|
||
struct Chord { | ||
public: | ||
std::shared_ptr<const ChordDescription> desc; | ||
uint8_t key; | ||
ToneSet tones; | ||
|
||
inline Chord(std::shared_ptr<const ChordDescription> desc, uint8_t key) | ||
: desc(desc), key(key), tones(tone_set_shift(desc->tones, key)) { | ||
} | ||
|
||
std::string to_short_string() const; | ||
inline Chord shifted(uint8_t semitones) const { return Chord(desc, (key + semitones) % 12); } | ||
}; | ||
} |
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,13 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include "Chord.h" | ||
#include "ItemDescription.h" | ||
|
||
namespace steve { | ||
struct ChordChange : ItemDescription { | ||
std::shared_ptr<ChordDescription> source_chord, target_chord; | ||
uint8_t tone_shift = 0; | ||
}; | ||
} |
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,122 @@ | ||
#include "Config.h" | ||
|
||
#include <iostream> | ||
|
||
#include "Music.h" | ||
#include "Rand.h" | ||
#include "creator/Arpeggio.h" | ||
#include "creator/Bass.h" | ||
#include "creator/Chords.h" | ||
#include "creator/Drums.h" | ||
#include "creator/Melody.h" | ||
|
||
using namespace steve; | ||
|
||
std::shared_ptr<ChordChange> Config::get_chord_change(const std::shared_ptr<ChordDescription>& source, const std::shared_ptr<ChordDescription>& target, uint8_t tone_shift) { | ||
std::shared_ptr<ChordChange> chord_change = _chord_changes.get_item(source->name + "->" + target->name + "+" + std::to_string(tone_shift)); | ||
chord_change->source_chord = source; | ||
chord_change->target_chord = target; | ||
chord_change->tone_shift = tone_shift; | ||
return chord_change; | ||
} | ||
|
||
Config::Config() { | ||
_creators.get_item("Arpeggio")->func = [](Music* music) { | ||
return new Arpeggio(music); | ||
}; | ||
_creators.get_item("Bass")->func = [](Music* music) { | ||
return new Bass(music); | ||
}; | ||
_creators.get_item("Chords")->func = [](Music* music) { | ||
return new Chords(music); | ||
}; | ||
_creators.get_item("Drums")->func = [](Music* music) { | ||
return new Drums(music); | ||
}; | ||
_creators.get_item("Melody")->func = [](Music* music) { | ||
return new Melody(music); | ||
}; | ||
} | ||
|
||
void Config::compute_cache() { | ||
// This needs to happen before computing scale chords | ||
_chords.compute_cache(); | ||
for(auto& scale : _scales.get_all()) { | ||
scale->compute_chords(*this); | ||
} | ||
|
||
_scales.compute_cache(); | ||
_instruments.compute_cache(); | ||
_creators.compute_cache(); | ||
_signatures.compute_cache(); | ||
_chord_changes.compute_cache(); | ||
} | ||
|
||
void Config::list_scales(std::ostream& out) const { | ||
for(const auto& scale_desc : _scales.get_allowed()) { | ||
Scale scale(scale_desc, 0); | ||
out << scale.desc->name << ":" << std::endl; | ||
for(const auto& chord : scale.desc->chords) { | ||
out << '\t' << scale.get_degree_string_for_chord(chord) << "\n"; | ||
} | ||
} | ||
} | ||
|
||
uint32_t Config::get_random_tempo() const { | ||
const float tempo_range = max_tempo - min_tempo; | ||
const uint32_t full_precision_tempo = uint32_t(tempo_range * Rand::next_normal()) + min_tempo; | ||
return (full_precision_tempo / 5) * 5; | ||
} | ||
|
||
std::vector<Chord> Config::get_chords_inside(ToneSet tones) const { | ||
std::vector<Chord> chords; | ||
for(const auto& desc : _chords.get_allowed()) { | ||
for(int key(0); key < 12; key++) { | ||
const Chord shifted_chord(desc, key); | ||
if((tones | shifted_chord.tones) == tones) { // All chord tones are in the toneset | ||
chords.push_back(shifted_chord); | ||
} | ||
} | ||
} | ||
return chords; | ||
} | ||
std::vector<Chord> Config::get_chord_progression(const Scale& scale) const { | ||
std::vector<Chord> chords; | ||
|
||
// Start with first degree chord | ||
chords.push_back(Chord(_chords.get_random_item([scale](std::shared_ptr<ChordDescription> chord) { | ||
return std::find_if(scale.desc->chords.begin(), scale.desc->chords.end(), [chord](const Chord& scale_chord) { | ||
return scale_chord.desc == chord && scale_chord.key == 0; | ||
}) != scale.desc->chords.end(); | ||
}), | ||
scale.key)); | ||
|
||
// Progress backwards | ||
while(chords.size() < 4) { | ||
const Chord dest_chord = chords.back(); | ||
const std::shared_ptr<ChordChange> chord_change = _chord_changes.get_random_item([&](std::shared_ptr<ChordChange> chord_change) { | ||
return dest_chord.desc == chord_change->target_chord && tone_set_within(scale.tones, tone_set_shift(chord_change->source_chord->tones, (dest_chord.key - chord_change->tone_shift + 12) % 12)); | ||
}); | ||
chords.push_back(Chord(chord_change->source_chord, (dest_chord.key - chord_change->tone_shift + 12) % 12)); | ||
} | ||
|
||
// Reverse but keep first as start | ||
std::reverse(chords.begin() + 1, chords.end()); | ||
|
||
return chords; | ||
} | ||
|
||
std::vector<std::shared_ptr<const CreatorDescription>> Config::get_creators() const { | ||
std::vector<std::shared_ptr<const CreatorDescription>> creators; | ||
|
||
while(creators.empty()) { | ||
for(const auto creator : _creators.get_allowed()) { | ||
const uint32_t count = Rand::next(creator->min_count, creator->max_count); | ||
for(uint32_t i = 0; i < count; i++) { | ||
creators.push_back(creator); | ||
} | ||
} | ||
} | ||
|
||
return creators; | ||
} |
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,41 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
#include "Chord.h" | ||
#include "ChordChange.h" | ||
#include "ConfigItemList.h" | ||
#include "Instrument.h" | ||
#include "Scale.h" | ||
#include "TimeSignature.h" | ||
#include "creator/Creator.h" | ||
|
||
namespace steve { | ||
class Config { | ||
protected: | ||
uint32_t min_tempo = 0, max_tempo = 360; | ||
ConfigItemList<TimeSignature> _signatures; | ||
ConfigItemList<CreatorDescription> _creators; | ||
ConfigItemList<ChordDescription> _chords; | ||
ConfigItemList<ScaleDescription> _scales; | ||
ConfigItemList<ChordChange> _chord_changes; | ||
ConfigItemList<Instrument> _instruments; | ||
|
||
std::shared_ptr<ChordChange> get_chord_change(const std::shared_ptr<ChordDescription>& source, const std::shared_ptr<ChordDescription>& target, uint8_t tone_shift); | ||
|
||
public: | ||
Config(); | ||
void compute_cache(); | ||
void list_scales(std::ostream&) const; | ||
|
||
uint32_t get_random_tempo() const; | ||
std::vector<Chord> get_chords_inside(ToneSet tones) const; | ||
std::vector<Chord> get_chord_progression(const Scale&) const; | ||
std::vector<std::shared_ptr<const CreatorDescription>> get_creators() const; | ||
|
||
inline const auto& get_signatures() const { return _signatures; } | ||
inline const auto& get_scales() const { return _scales; } | ||
inline const auto& get_instruments() const { return _instruments; } | ||
}; | ||
} |
Oops, something went wrong.