/* MicroDexed MicroDexed is a port of the Dexed sound engine Dexed ist heavily based on https://github.com/google/music-synthesizer-for-android (c)2018-2021 H. Wirtz Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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 OR COPYRIGHT HOLDERS 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. */ #include #include #include #include "config.h" #include "drums.h" extern drum_config_t drum_config[DRUM_MAX_INSTRUMENTS]; uint8_t read_drum_config(const char* drumset) { uint8_t config_drums = 0; char ds[26]; File json; DynamicJsonDocument drums_json(4096); sprintf(ds, "/DRM/%s.json", drumset); #ifdef DEBUG Serial.print(F("Searching for drum configuration file [")); Serial.print(ds); Serial.println(F("]")); #endif AudioNoInterrupts(); if (SD.exists(ds)) { #ifdef DEBUG Serial.print(F("Found drum configuration file [")); Serial.print(ds); Serial.println(F("]")); #endif json = SD.open(ds); if (json) { deserializeJson(drums_json, json); json.close(); config_drums = drums_json["drums"].size(); #ifdef DEBUG Serial.print(F("Drum Objects: ")); Serial.println(config_drums); if (config_drums > DRUM_MAX_INSTRUMENTS) Serial.println(F("*** Maximum number of drum samples exceeded! ***")); //Serial.println(F("Drum Configuration: ")); //serializeJsonPretty(drums_json, Serial); //Serial.println(); #endif config_drums = constrain(config_drums, 0, DRUM_MAX_INSTRUMENTS); } } #ifdef DEBUG else { Serial.print(F("Drum configuration file [")); Serial.print(ds); Serial.println(F("] not found!")); } #endif AudioInterrupts(); uint8_t drum = 0; JsonObject root = drums_json["drums"].as(); sprintf(ds, "/DRM/%s", drumset); for (JsonPair kv : root) { sprintf(drum_config[drum].filename, "%s/%s", ds, kv.key().c_str()); char f[DRUM_CFG_FILENAME_LEN]; strcpy(f, drum_config[drum].filename); strcat(f, ".wav"); if (!SD.exists(f)) { #ifdef DEBUG Serial.print(F("Cannot find WAV file [")); Serial.print(f); Serial.println(F("]")); #endif continue; } JsonObject drum_root = drums_json["drums"][kv.key().c_str()].as(); for (JsonPair drum_kv : drum_root) { if (strcmp(drum_kv.key().c_str(), "drum_class") == 0) { if (drum_kv.value().is()) { if (strcmp(drum_kv.value().as(), "DRUM_NONE") == 0) drum_config[drum].drum_class = DRUM_NONE; else if (strcmp(drum_kv.value().as(), "DRUM_BASS") == 0) drum_config[drum].drum_class = DRUM_BASS; else if (strcmp(drum_kv.value().as(), "DRUM_SNARE") == 0) drum_config[drum].drum_class = DRUM_SNARE; else if (strcmp(drum_kv.value().as(), "DRUM_HIHAT") == 0) drum_config[drum].drum_class = DRUM_HIHAT; else if (strcmp(drum_kv.value().as(), "DRUM_HANDCLAP") == 0) drum_config[drum].drum_class = DRUM_HANDCLAP; else if (strcmp(drum_kv.value().as(), "DRUM_RIDE") == 0) drum_config[drum].drum_class = DRUM_RIDE; else if (strcmp(drum_kv.value().as(), "DRUM_CHRASH") == 0) drum_config[drum].drum_class = DRUM_CHRASH; else if (strcmp(drum_kv.value().as(), "DRUM_LOWTOM") == 0) drum_config[drum].drum_class = DRUM_LOWTOM; else if (strcmp(drum_kv.value().as(), "DRUM_MIDTOM") == 0) drum_config[drum].drum_class = DRUM_MIDTOM; else if (strcmp(drum_kv.value().as(), "DRUM_HIGHTOM") == 0) drum_config[drum].drum_class = DRUM_HIGHTOM; else if (strcmp(drum_kv.value().as(), "DRUM_PERCUSSION") == 0) drum_config[drum].drum_class = DRUM_PERCUSSION; } } else if (strcmp(drum_kv.key().c_str(), "midinote") == 0) { if (drum_kv.value().is()) { uint8_t note_number = 0; uint8_t oct = 0; char midi_note[4]; strcpy(midi_note, drum_kv.value().as()); switch (midi_note[0]) { case 'C': if (midi_note[1] == '#') { note_number = 1; oct = midi_note[2] - 48; } else { note_number = 0; oct = midi_note[1] - 48; } break; case 'D': if (midi_note[1] == '#') { note_number = 3; oct = midi_note[2] - 48; } else { note_number = 2; oct = midi_note[1] - 48; } break; case 'E': note_number = 4; oct = midi_note[1] - 48; break; case 'F': if (midi_note[1] == '#') { note_number = 6; oct = midi_note[2] - 48; } else { note_number = 5; oct = midi_note[1] - 48; } break; case 'G': if (midi_note[1] == '#') { note_number = 8; oct = midi_note[2] - 48; } else { note_number = 7; oct = midi_note[1] - 48; } break; case 'A': if (midi_note[1] == '#') { note_number = 10; oct = midi_note[2] - 48; } else { note_number = 9; oct = midi_note[1] - 48; } break; case 'B': note_number = 11; oct = midi_note[1] - 48; break; } drum_config[drum].midinote = constrain(note_number + 12 + (oct * 12), 21, 127); } } else if (strcmp(drum_kv.key().c_str(), "shortname") == 0) { memset(drum_config[drum].shortname, 0, 2); if (drum_kv.value().is()) drum_config[drum].shortname[0] = drum_kv.value().as()[0]; } else if (strcmp(drum_kv.key().c_str(), "pan") == 0) { if (drum_kv.value().is()) drum_config[drum].pan = constrain(drum_kv.value().as(), 0.0, 1.0); if (drum_kv.value().is()) drum_config[drum].pan = constrain(float(drum_kv.value().as()), 0.0, 1.0); } else if (strcmp(drum_kv.key().c_str(), "vol_max") == 0) { if (drum_kv.value().is()) drum_config[drum].vol_max = constrain(drum_kv.value().as(), 0.0, 1.0); if (drum_kv.value().is()) drum_config[drum].vol_max = constrain(float(drum_kv.value().as()), 0.0, 1.0); } else if (strcmp(drum_kv.key().c_str(), "vol_min") == 0) { if (drum_kv.value().is()) drum_config[drum].vol_min = constrain(drum_kv.value().as(), 0.0, 1.0); if (drum_kv.value().is()) drum_config[drum].vol_min = constrain(float(drum_kv.value().as()), 0.0, 1.0); } else if (strcmp(drum_kv.key().c_str(), "reverb_send") == 0) { if (drum_kv.value().is()) drum_config[drum].reverb_send = constrain(drum_kv.value().as(), 0.0, 1.0); if (drum_kv.value().is()) drum_config[drum].reverb_send = constrain(float(drum_kv.value().as()), 0.0, 1.0); } } #ifdef DEBUG Serial.print(F("[Drum ")); Serial.print(drum, DEC); Serial.println(F("]")); Serial.print(F("drum_class = ")); Serial.println(drum_config[drum].drum_class, DEC); Serial.print(F("midinote = ")); Serial.println(drum_config[drum].midinote, DEC); Serial.print(F("filename = ")); Serial.println(f); Serial.print(F("shortname = ")); Serial.println(drum_config[drum].shortname); Serial.print(F("pan = ")); Serial.println(drum_config[drum].pan); Serial.print(F("vol_max = ")); Serial.println(drum_config[drum].vol_max); Serial.print(F("vol_min = ")); Serial.println(drum_config[drum].vol_min); Serial.print(F("reverb_send = ")); Serial.println(drum_config[drum].reverb_send); #endif drum++; } drum++; drum_config[drum].drum_class = DRUM_NONE; drum_config[drum].midinote = 0; strcpy(drum_config[drum].filename, "EMPTY"); strcpy(drum_config[drum].shortname, " - "); drum_config[drum].pan = 0.0; drum_config[drum].vol_max = 0.0; drum_config[drum].vol_min = 0.0; drum_config[drum].reverb_send = 0.0; return (drum); }