You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MicroDexed/drums.cpp

270 lines
9.3 KiB

/*
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 <wirtz@parasitstudio.de>
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 <Arduino.h>
#include <ArduinoJson.h>
#include <Audio.h>
#include "config.h"
#include "drums.h"
extern drum_config_t drum_config[DRUM_MAX_INSTRUMENTS];
uint8_t read_drum_config(void)
{
uint8_t config_drums = 0;
File json;
DynamicJsonDocument drums_json(4096);
AudioNoInterrupts();
if (SD.exists("/DRM/CFGDrums.json"))
{
#ifdef DEBUG
Serial.println(F("Found drum configuration file."));
#endif
json = SD.open("/DRM/CFGDrums.json");
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);
}
}
AudioInterrupts();
uint8_t drum = 0;
JsonObject root = drums_json["drums"].as<JsonObject>();
for (JsonPair kv : root)
{
sprintf(drum_config[drum].filename, "/drm/%s.wav", kv.key().c_str());
if (!SD.exists(drum_config[drum].filename))
{
#ifdef DEBUG
Serial.print(F("Cannot find WAV file ["));
Serial.print(drum_config[drum].filename);
Serial.println(F("]"));
#endif
continue;
}
JsonObject drum_root = drums_json["drums"][kv.key().c_str()].as<JsonObject>();
for (JsonPair drum_kv : drum_root)
{
if (strcmp(drum_kv.key().c_str(), "drum_class") == 0)
{
if (drum_kv.value().is<const char*>())
{
if (strcmp(drum_kv.value().as<const char*>(), "DRUM_NONE") == 0)
drum_config[drum].drum_class = DRUM_NONE;
else if (strcmp(drum_kv.value().as<const char*>(), "DRUM_BASS") == 0)
drum_config[drum].drum_class = DRUM_BASS;
else if (strcmp(drum_kv.value().as<const char*>(), "DRUM_SNARE") == 0)
drum_config[drum].drum_class = DRUM_SNARE;
else if (strcmp(drum_kv.value().as<const char*>(), "DRUM_HIHAT") == 0)
drum_config[drum].drum_class = DRUM_HIHAT;
else if (strcmp(drum_kv.value().as<const char*>(), "DRUM_HANDCLAP") == 0)
drum_config[drum].drum_class = DRUM_HANDCLAP;
else if (strcmp(drum_kv.value().as<const char*>(), "DRUM_RIDE") == 0)
drum_config[drum].drum_class = DRUM_RIDE;
else if (strcmp(drum_kv.value().as<const char*>(), "DRUM_CHRASH") == 0)
drum_config[drum].drum_class = DRUM_CHRASH;
else if (strcmp(drum_kv.value().as<const char*>(), "DRUM_LOWTOM") == 0)
drum_config[drum].drum_class = DRUM_LOWTOM;
else if (strcmp(drum_kv.value().as<const char*>(), "DRUM_MIDTOM") == 0)
drum_config[drum].drum_class = DRUM_MIDTOM;
else if (strcmp(drum_kv.value().as<const char*>(), "DRUM_HIGHTOM") == 0)
drum_config[drum].drum_class = DRUM_HIGHTOM;
else if (strcmp(drum_kv.value().as<const char*>(), "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<const char*>())
{
uint8_t note_number = 0;
uint8_t oct = 0;
char midi_note[4];
strcpy(midi_note, drum_kv.value().as<const char*>());
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<const char*>())
drum_config[drum].shortname[0] = drum_kv.value().as<const char*>()[0];
}
else if (strcmp(drum_kv.key().c_str(), "pan") == 0)
{
if (drum_kv.value().is<float>())
drum_config[drum].pan = constrain(drum_kv.value().as<float>(), 0.0, 1.0);
if (drum_kv.value().is<int>())
drum_config[drum].pan = constrain(float(drum_kv.value().as<int>()), 0.0, 1.0);
}
else if (strcmp(drum_kv.key().c_str(), "vol_max") == 0)
{
if (drum_kv.value().is<float>())
drum_config[drum].vol_max = constrain(drum_kv.value().as<float>(), 0.0, 1.0);
if (drum_kv.value().is<int>())
drum_config[drum].vol_max = constrain(float(drum_kv.value().as<int>()), 0.0, 1.0);
}
else if (strcmp(drum_kv.key().c_str(), "vol_min") == 0)
{
if (drum_kv.value().is<float>())
drum_config[drum].vol_min = constrain(drum_kv.value().as<float>(), 0.0, 1.0);
if (drum_kv.value().is<int>())
drum_config[drum].vol_min = constrain(float(drum_kv.value().as<int>()), 0.0, 1.0);
}
else if (strcmp(drum_kv.key().c_str(), "reverb_send") == 0)
{
if (drum_kv.value().is<float>())
drum_config[drum].reverb_send = constrain(drum_kv.value().as<float>(), 0.0, 1.0);
if (drum_kv.value().is<int>())
drum_config[drum].reverb_send = constrain(float(drum_kv.value().as<int>()), 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(drum_config[drum].filename);
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);
}