Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bramtayl committed Nov 12, 2024
1 parent c5f52a9 commit b0778d5
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 282 deletions.
13 changes: 13 additions & 0 deletions src/other/other.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
#include "other/other.hpp"

#include <QWidget>
#include <cmath>

void prevent_compression(QWidget &widget) {
widget.setMinimumSize(widget.minimumSizeHint());
}

auto get_number_schema(const char *type, const char *description, int minimum,
int maximum) -> nlohmann::json {
return nlohmann::json({{"type", type},
{"description", description},
{"minimum", minimum},
{"maximum", maximum}});
}

auto to_int(double value) -> int {
return static_cast<int>(std::round(value));
}
7 changes: 7 additions & 0 deletions src/other/other.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QVariant>
#include <QtGlobal>
#include <nlohmann/json.hpp>

class QWidget;

Expand All @@ -24,4 +25,10 @@ auto variant_to(const QVariant &variant) -> SubType {
return variant.value<SubType>();
}

auto to_int(double value) -> int;

void prevent_compression(QWidget &widget);

[[nodiscard]] auto get_number_schema(const char *type, const char *description,
int minimum,
int maximum) -> nlohmann::json;
67 changes: 62 additions & 5 deletions src/row/Row.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#include "row/Row.hpp"

#include <algorithm>
#include <vector>

#include "abstract_rational/AbstractRational.hpp"
#include "abstract_rational/interval/Interval.hpp"
#include "named/percussion_instrument/PercussionInstrument.hpp"
#include "named/program/instrument/Instrument.hpp"
#include "named/program/percussion_set/PercussionSet.hpp"
#include "other/other.hpp"

static auto json_field_to_words(const nlohmann::json &json_row) -> QString {
if (json_row.contains("words")) {
Expand All @@ -9,6 +17,14 @@ static auto json_field_to_words(const nlohmann::json &json_row) -> QString {
return "";
}

auto get_object_schema(const char *description,
const nlohmann::json &properties_json)
-> nlohmann::json {
return nlohmann::json({{"type", "object"},
{"description", description},
{"properties", properties_json}});
}

Row::Row(const nlohmann::json &json_chord)
: beats(json_field_to_abstract_rational<Rational>(json_chord, "beats")),
velocity_ratio(json_field_to_abstract_rational<Rational>(
Expand All @@ -18,18 +34,59 @@ Row::Row(const nlohmann::json &json_chord)
[[nodiscard]] auto Row::to_json() const -> nlohmann::json {
auto json_row = nlohmann::json::object();
add_abstract_rational_to_json(json_row, beats, "beats");
add_abstract_rational_to_json(json_row, velocity_ratio,
"velocity_ratio");
add_abstract_rational_to_json(json_row, velocity_ratio, "velocity_ratio");
add_words_to_json(json_row, words);
return json_row;
}

auto Row::is_column_editable(int /*column_number*/) -> bool {
return true;
}
auto Row::is_column_editable(int /*column_number*/) -> bool { return true; }

void add_words_to_json(nlohmann::json &json_row, const QString &words) {
if (!words.isEmpty()) {
json_row["words"] = words.toStdString().c_str();
}
}

template <std::derived_from<Named> SubNamed>
static void add_named_schema(nlohmann::json &json_row) {
std::vector<std::string> names;
const auto &all_nameds = SubNamed::get_all_nameds();
std::transform(all_nameds.cbegin(), all_nameds.cend(),
std::back_inserter(names),
[](const SubNamed &item) { return item.name.toStdString(); });
json_row[SubNamed::get_field_name()] =
nlohmann::json({{"type", "string"},
{"description", SubNamed::get_type_name()},
{"enum", std::move(names)}});
};

auto get_rational_fields_schema() -> nlohmann::json {
return nlohmann::json(
{{"numerator",
get_number_schema("integer", "numerator", 1, MAX_RATIONAL_NUMERATOR)},
{"denominator", get_number_schema("integer", "denominator", 1,
MAX_RATIONAL_DENOMINATOR)}});
}

void add_pitched_fields_to_schema(nlohmann::json &schema) {
add_named_schema<Instrument>(schema);
auto interval_fields_schema = get_rational_fields_schema();
interval_fields_schema["octave"] =
get_number_schema("integer", "octave", -MAX_OCTAVE, MAX_OCTAVE);
schema["interval"] = get_object_schema("an interval", interval_fields_schema);
}

void add_unpitched_fields_to_schema(nlohmann::json &schema) {
add_named_schema<PercussionSet>(schema);
add_named_schema<PercussionInstrument>(schema);
}

auto Row::get_fields_schema() -> nlohmann::json {
return nlohmann::json(
{{"beats",
get_object_schema("the number of beats", get_rational_fields_schema())},
{"velocity_ratio",
get_object_schema("velocity ratio", get_rational_fields_schema())},
{"words",
nlohmann::json({{"type", "string"}, {"description", "the words"}})}});
}
19 changes: 19 additions & 0 deletions src/row/Row.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ struct Row {
columns_to_json(int left_column,
int right_column) const -> nlohmann::json = 0;
[[nodiscard]] virtual auto to_json() const -> nlohmann::json;
[[nodiscard]] static auto get_fields_schema() -> nlohmann::json;
};

[[nodiscard]] auto
get_object_schema(const char *description,
const nlohmann::json &properties_json) -> nlohmann::json;

template <std::derived_from<Row> SubRow>
void partial_json_to_rows(QList<SubRow> &new_rows,
const nlohmann::json &json_rows, int number_of_rows) {
Expand Down Expand Up @@ -88,3 +93,17 @@ template <std::derived_from<Named> SubNamed>
};
return nullptr;
}

auto get_rational_fields_schema() -> nlohmann::json;

void add_pitched_fields_to_schema(nlohmann::json &schema);
void add_unpitched_fields_to_schema(nlohmann::json &schema);

template <std::derived_from<Row> SubRow>
void add_row_array_schema(nlohmann::json &schema) {
schema[SubRow::get_plural_field_for()] =
nlohmann::json({{"type", "array"},
{"description", SubRow::get_plural_description()},
{"items", get_object_schema(SubRow::get_type_name(),
SubRow::get_fields_schema())}});
}
29 changes: 22 additions & 7 deletions src/row/chord/Chord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

template <std::derived_from<Row> SubRow>
static auto json_field_to_rows(nlohmann::json json_object,
const char *field) -> QList<SubRow> {
const char *field) -> QList<SubRow> {
if (json_object.contains(field)) {
QList<SubRow> rows;
const auto &json_rows = json_object[field];
Expand All @@ -30,13 +30,11 @@ static auto json_field_to_rows(nlohmann::json json_object,

Chord::Chord(const nlohmann::json &json_chord)
: Row(json_chord),
instrument_pointer(
json_field_to_named_pointer<Instrument>(json_chord)),
percussion_set_pointer(json_field_to_named_pointer<PercussionSet>(
json_chord)),
instrument_pointer(json_field_to_named_pointer<Instrument>(json_chord)),
percussion_set_pointer(
json_field_to_named_pointer<PercussionSet>(json_chord)),
percussion_instrument_pointer(
json_field_to_named_pointer<PercussionInstrument>(
json_chord)),
json_field_to_named_pointer<PercussionInstrument>(json_chord)),
interval(
json_field_to_abstract_rational<Interval>(json_chord, "interval")),
tempo_ratio(
Expand All @@ -48,6 +46,23 @@ Chord::Chord(const nlohmann::json &json_chord)

auto Chord::get_number_of_columns() -> int { return number_of_chord_columns; }

auto Chord::get_fields_schema() -> nlohmann::json {
auto schema = Row::get_fields_schema();
add_pitched_fields_to_schema(schema);
add_unpitched_fields_to_schema(schema);
schema["tempo_ratio"] =
get_object_schema("tempo ratio", get_rational_fields_schema());
add_row_array_schema<PitchedNote>(schema);
add_row_array_schema<UnpitchedNote>(schema);
return schema;
}

auto Chord::get_plural_field_for() -> const char * { return "chords"; }

auto Chord::get_type_name() -> const char * { return "chord"; }

auto Chord::get_plural_description() -> const char * { return "chords"; }

auto Chord::get_column_name(int column_number) -> const char * {
switch (column_number) {
case chord_instrument_column:
Expand Down
9 changes: 7 additions & 2 deletions src/row/chord/Chord.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "abstract_rational/interval/Interval.hpp"
#include "abstract_rational/rational/Rational.hpp"
#include "row/Row.hpp"
#include "row/note/pitched_note/PitchedNote.hpp" // IWYU pragma: keep
#include "row/note/pitched_note/PitchedNote.hpp" // IWYU pragma: keep
#include "row/note/unpitched_note/UnpitchedNote.hpp" // IWYU pragma: keep

struct Instrument;
Expand All @@ -26,7 +26,12 @@ struct Chord : public Row {
Chord() = default;
explicit Chord(const nlohmann::json &json_chord);

[[nodiscard]] static auto get_column_name(int column_number) -> const char*;
[[nodiscard]] static auto get_fields_schema() -> nlohmann::json;

[[nodiscard]] static auto get_plural_field_for() -> const char *;
[[nodiscard]] static auto get_type_name() -> const char *;
[[nodiscard]] static auto get_plural_description() -> const char *;
[[nodiscard]] static auto get_column_name(int column_number) -> const char *;
[[nodiscard]] static auto get_number_of_columns() -> int;
[[nodiscard]] static auto is_column_editable(int column_number) -> bool;

Expand Down
20 changes: 18 additions & 2 deletions src/row/note/pitched_note/PitchedNote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ auto PitchedNote::get_closest_midi(Player &player, int channel_number,

fluid_event_pitch_bend(
player.event_pointer, channel_number,
static_cast<int>(round((midi_float - closest_midi + ZERO_BEND_HALFSTEPS) *
BEND_PER_HALFSTEP)));
to_int((midi_float - closest_midi + ZERO_BEND_HALFSTEPS) *
BEND_PER_HALFSTEP));
send_event_at(player, player.current_time);
return closest_midi;
}
Expand All @@ -47,8 +47,24 @@ auto PitchedNote::get_program(const Player &player, int chord_number,
note_number, PitchedNote::get_note_type());
}

auto PitchedNote::get_fields_schema() -> nlohmann::json {
auto schema = Row::get_fields_schema();
add_pitched_fields_to_schema(schema);
return schema;
}

auto PitchedNote::get_note_type() -> const char * { return "pitched"; };

auto PitchedNote::get_plural_field_for() -> const char * {
return "pitched_notes";
}

auto PitchedNote::get_type_name() -> const char * { return "pitched note"; }

auto PitchedNote::get_plural_description() -> const char * {
return "pitched notes";
}

auto PitchedNote::get_column_name(int column_number) -> const char * {
switch (column_number) {
case pitched_note_instrument_column:
Expand Down
5 changes: 5 additions & 0 deletions src/row/note/pitched_note/PitchedNote.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ struct PitchedNote : Note {
get_program(const Player &player, int chord_number,
int note_number) const -> const Program & override;

[[nodiscard]] static auto get_fields_schema() -> nlohmann::json;

[[nodiscard]] static auto get_note_type() -> const char *;
[[nodiscard]] static auto get_plural_field_for() -> const char *;
[[nodiscard]] static auto get_type_name() -> const char *;
[[nodiscard]] static auto get_plural_description() -> const char *;

[[nodiscard]] static auto get_column_name(int column_number) -> const char *;
[[nodiscard]] static auto get_number_of_columns() -> int;
Expand Down
16 changes: 16 additions & 0 deletions src/row/note/unpitched_note/UnpitchedNote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,24 @@ auto UnpitchedNote::get_program(const Player &player, int chord_number,
UnpitchedNote::get_note_type());
}

auto UnpitchedNote::get_fields_schema() -> nlohmann::json {
auto schema = Row::get_fields_schema();
add_unpitched_fields_to_schema(schema);
return schema;
}

auto UnpitchedNote::get_note_type() -> const char * { return "unpitched"; };

auto UnpitchedNote::get_plural_field_for() -> const char * {
return "unpitched_notes";
}

auto UnpitchedNote::get_type_name() -> const char * { return "unpitched note"; }

auto UnpitchedNote::get_plural_description() -> const char * {
return "unpitched notes";
}

auto UnpitchedNote::get_number_of_columns() -> int {
return number_of_unpitched_note_columns;
}
Expand Down
5 changes: 5 additions & 0 deletions src/row/note/unpitched_note/UnpitchedNote.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ struct UnpitchedNote : Note {
get_program(const Player &player, int chord_number,
int note_number) const -> const Program & override;

[[nodiscard]] static auto get_fields_schema() -> nlohmann::json;

[[nodiscard]] static auto get_note_type() -> const char *;
[[nodiscard]] static auto get_plural_field_for() -> const char *;
[[nodiscard]] static auto get_type_name() -> const char *;
[[nodiscard]] static auto get_plural_description() -> const char *;

[[nodiscard]] static auto get_column_name(int column_number) -> const char *;
[[nodiscard]] static auto get_number_of_columns() -> int;
Expand Down
3 changes: 2 additions & 1 deletion src/song/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <concepts>
#include <fluidsynth.h>

#include "named/program/Program.hpp"
#include "row/note/Note.hpp"

struct Instrument;
Expand Down Expand Up @@ -110,7 +111,7 @@ void play_notes(Player &player, int chord_number,
}
const auto &sub_note = sub_notes.at(note_number);

const auto &program =
const Program &program =
sub_note.get_program(player, chord_number, note_number);

fluid_event_program_select(event_pointer, channel_number, soundfont_id,
Expand Down
6 changes: 3 additions & 3 deletions src/song/Song.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <QObject>
#include <QTextStream>
#include <QtGlobal>
#include <cmath>
#include <cstdlib>

#include "abstract_rational/interval/Interval.hpp"
#include "other/other.hpp"
#include "row/chord/Chord.hpp"
#include "song/Player.hpp"
#include "song/Song.hpp"
Expand Down Expand Up @@ -69,13 +69,13 @@ auto get_key_text(const Song &song, int chord_number, double ratio) -> QString {
}
key = key * ratio;
auto midi_float = get_midi(key);
auto closest_midi = static_cast<int>(round(midi_float));
auto closest_midi = to_int(midi_float);
auto difference_from_c = closest_midi - C_0_MIDI;
auto octave =
difference_from_c / HALFSTEPS_PER_OCTAVE; // floor integer division
auto degree = difference_from_c - octave * HALFSTEPS_PER_OCTAVE;
auto cents =
static_cast<int>(round((midi_float - closest_midi) * CENTS_PER_HALFSTEP));
to_int((midi_float - closest_midi) * CENTS_PER_HALFSTEP);

QString result;
QTextStream stream(&result);
Expand Down
Loading

0 comments on commit b0778d5

Please sign in to comment.