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

257 lines
8.7 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 uint8_t num_drums;
extern drum_config_t drum_config[DRUM_MAX_INSTRUMENTS];
void read_drum_config(void)
{
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();
num_drums = drums_json.size();
#ifdef DEBUG
Serial.print(F("Drum Objects: "));
Serial.println(num_drums);
if (num_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
num_drums = constrain(num_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 'A':
if (midi_note[1] == '#')
{
note_number = 1;
oct = midi_note[2] - 48;
}
else
{
note_number = 0;
oct = midi_note[1] - 48;
}
break;
case 'B':
note_number = 2;
oct = midi_note[1] - 48;
break;
case 'C':
if (midi_note[1] == '#')
{
note_number = 4;
oct = midi_note[2] - 48;
}
else
{
note_number = 3;
oct = midi_note[1] - 48;
}
break;
case 'D':
if (midi_note[1] == '#')
{
note_number = 6;
oct = midi_note[2] - 48;
}
else
{
note_number = 5;
oct = midi_note[1] - 48;
}
break;
case 'E':
note_number = 7;
oct = midi_note[1] - 48;
break;
case 'F':
if (midi_note[1] == '#')
{
note_number = 9;
oct = midi_note[2] - 48;
}
else
{
note_number = 8;
oct = midi_note[1] - 48;
}
break;
case 'G':
if (midi_note[1] == '#')
{
note_number = 11;
oct = midi_note[2] - 48;
}
else
{
note_number = 10;
oct = midi_note[1] - 48;
}
break;
}
drum_config[drum].midinote = note_number + 9 + (oct * 12);
}
}
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 = drum_kv.value().as<float>();
if (drum_kv.value().is<int>())
drum_config[drum].pan = float(drum_kv.value().as<int>());
}
else if (strcmp(drum_kv.key().c_str(), "vol_max") == 0)
{
if (drum_kv.value().is<float>())
drum_config[drum].vol_max = drum_kv.value().as<float>();
if (drum_kv.value().is<int>())
drum_config[drum].vol_max = float(drum_kv.value().as<int>());
}
else if (strcmp(drum_kv.key().c_str(), "vol_min") == 0)
{
if (drum_kv.value().is<float>())
drum_config[drum].vol_min = drum_kv.value().as<float>();
if (drum_kv.value().is<int>())
drum_config[drum].vol_min = float(drum_kv.value().as<int>());
}
else if (strcmp(drum_kv.key().c_str(), "reverb_send") == 0)
{
if (drum_kv.value().is<float>())
drum_config[drum].reverb_send = drum_kv.value().as<float>();
if (drum_kv.value().is<int>())
drum_config[drum].reverb_send = float(drum_kv.value().as<int>());
}
}
#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++;
}
}