Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drum Randomizer #122

Merged
merged 2 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/deluge/gui/views/instrument_clip_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
#include "gui/ui_timer_manager.h"
#include "modulation/patch/patch_cable_set.h"
#include "model/drum/midi_drum.h"
#include "storage/multi_range/multi_range.h"
#include "storage/audio/audio_file_holder.h"
#include "model/settings/runtime_feature_settings.h"

#if HAVE_OLED
#include "hid/display/oled.h"
Expand Down Expand Up @@ -1117,6 +1120,77 @@ const uint32_t auditionPadActionUIModes[] = {UI_MODE_AUDITIONING,

int InstrumentClipView::padAction(int x, int y, int velocity) {

if (x == 15 && y == 2 && velocity > 0
&& runtimeFeatureSettings.get(RuntimeFeatureSettingType::DrumRandomizer) == RuntimeFeatureStateToggle::On) {
int numRandomized = 0;
for (int i = 0; i < 8; i++) {
if (getCurrentUI() == this && this->auditionPadIsPressed[i]) {
if (currentSong->currentClip->output->type != INSTRUMENT_TYPE_KIT) continue;
AudioEngine::stopAnyPreviewing();
Drum* drum = getCurrentClip()->getNoteRowOnScreen(i, currentSong)->drum;
if (drum->type != DRUM_TYPE_SOUND) continue;
SoundDrum* soundDrum = (SoundDrum*)drum;
MultiRange* r = soundDrum->sources[0].getRange(0);
AudioFileHolder* afh = r->getAudioFileHolder();

static int MaxFiles = 25;
String fnArray[MaxFiles];
char const* currentPathChars = afh->filePath.get();
char const* slashAddress = strrchr(currentPathChars, '/');
if (slashAddress) {
int slashPos = (uint32_t)slashAddress - (uint32_t)currentPathChars;
String dir;
dir.set(&afh->filePath);
dir.shorten(slashPos);
FRESULT result = f_opendir(&staticDIR, dir.get());
FilePointer thisFilePointer;
int numSamples = 0;

if (result != FR_OK) {
numericDriver.displayError(ERROR_SD_CARD);
return false;
}
while (true) {
result = f_readdir_get_filepointer(&staticDIR, &staticFNO,
&thisFilePointer); /* Read a directory item */
if (result != FR_OK || staticFNO.fname[0] == 0) break; // Break on error or end of dir
if (staticFNO.fname[0] == '.' || staticFNO.fattrib & AM_DIR
|| !isAudioFilename(staticFNO.fname))
continue; // Ignore dot entry
audioFileManager.loadAnyEnqueuedClusters();
fnArray[numSamples].set(staticFNO.fname);
numSamples++;
if (numSamples >= MaxFiles) break;
}

if (numSamples >= 2) {
soundDrum->unassignAllVoices();
afh->setAudioFile(NULL);
String filePath; //add slash
filePath.set(&dir);
int dirWithSlashLength = filePath.getLength();
if (dirWithSlashLength) {
filePath.concatenateAtPos("/", dirWithSlashLength);
dirWithSlashLength++;
}
char const* fn = fnArray[random(numSamples - 1)].get();
filePath.concatenateAtPos(fn, dirWithSlashLength);
AudioEngine::stopAnyPreviewing();
afh->filePath.set(&filePath);
afh->loadFile(false, true, true, 1, 0, false);
soundDrum->name.set(fn);
numRandomized++;
((Instrument*)currentSong->currentClip->output)->beenEdited();
}
}
}
}
if (numRandomized > 0) {
numericDriver.displayPopup(HAVE_OLED ? "Randomized" : "RND");
return ACTION_RESULT_DEALT_WITH;
}
}

// Edit pad action...
if (x < displayWidth) {
if (sdRoutineLock) return ACTION_RESULT_REMIND_ME_OUTSIDE_CARD_ROUTINE;
Expand Down
9 changes: 9 additions & 0 deletions src/deluge/model/settings/runtime_feature_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum RuntimeFeatureStateToggle : uint32_t { Off = 0, On = 1 };
/// Every setting needs to be delcared in here
enum RuntimeFeatureSettingType : uint32_t {
// FileFolderSorting // @TODO: Replace with actual identifier on first use
DrumRandomizer,
MaxElement // Keep as boundary
};

Expand Down Expand Up @@ -79,6 +80,14 @@ class RuntimeFeatureSettings {

// Please extend RuntimeFeatureSettingType and here for additional settings
// Usage example -> (runtimeFeatureSettings.get(RuntimeFeatureSettingType::FileFolderSorting) == RuntimeFeatureStateToggle::On)

[RuntimeFeatureSettingType::DrumRandomizer] =
{.displayName = "Drum Randomizer",
.xmlName = "drumRandomizer",
.value = RuntimeFeatureStateToggle::On, // Default value
.options = {{.displayName = "Off", .value = RuntimeFeatureStateToggle::Off},
{.displayName = "On", .value = RuntimeFeatureStateToggle::On},
{.displayName = NULL, .value = 0}}},
};

private:
Expand Down