Load random drumPatches script error Help :)

JBDS183 views2 posts
  • JBDS1

    i have this Javascript

    var preset = getElement('preset');

    function PatchPicker() {
    this.drumDir = DIRS.DRUM_PATCHES + 'G01/'; // Define drumDir as a property
    var pickNewPatches = {
    drums: [],
    drumsByCategory: {
    BD: [],
    BS: [],
    CH: [],
    CL: [],
    CY: [],
    FX: [],
    OH: [],
    PR: [],
    RM: [],
    SD: [],
    SY: [],
    TM: [],
    ZP: [],
    unknown: []
    },
    inited: false
    };

    function catmatch(pattern, string) {
    var regex = new RegExp('^' + pattern.replace(/*/g, '.*') + '$');
    return regex.test(string);
    }

    if (!pickNewPatches.inited) {
    display('Building pickNewPatches');

    dir(this.drumDir, function(file) {
    if (catmatch('*.mtdrum', file)) {
    pickNewPatches.drums.push(file);
    }
    if (catmatch('..~~ {..~~~~~~} ..*.mtdrum', file)) {
    var category = '';
    for (var i = 0; i < file.length; i++) {
    var char = file.charAt(i);
    if (char === '.') {
    break;
    } else if (char === '_' || char === ' ') {
    continue;
    } else if (char >= 'A' && char <= 'Z') {
    category += char;
    }
    }
    category = category.toUpperCase();
    if (!pickNewPatches.drumsByCategory[category]) {
    pickNewPatches.drumsByCategory[category] = [];
    }
    pickNewPatches.drumsByCategory[category].push(file);
    }
    });

    // Log the list of collected drum patches
    for (var category in pickNewPatches.drumsByCategory) {
    var categoryDrums = pickNewPatches.drumsByCategory[category];
    if (categoryDrums.length > 0) {
    display('Category: ' + category);
    display('Drum Patches: ' + categoryDrums.join(', '));
    }
    }

    pickNewPatches.inited = true;
    }

    display("It works1");

    function pick(pattern, channel, categories) {
    display("It works2");

    var dp = preset.drumPatches[channel - 1]; // Access drum patch using array indexing
    var picked = false;
    var matchingDrums = [];

    outerLoop: // Label for the outer loop
    for (var i = 0; i < categories.length; i++) {
    var category = categories[i];
    var categoryDrums = pickNewPatches.drumsByCategory[category];
    if (categoryDrums && categoryDrums.length > 0) {
    for (var j = 0; j < categoryDrums.length; j++) {
    var file = categoryDrums[j];
    if (catmatch(pattern, file)) {
    matchingDrums.push(file);
    break outerLoop; // Break out of the outer loop
    }
    }
    }
    }

    if (matchingDrums.length > 0) {
    var randomIndex = Math.floor(Math.random() * matchingDrums.length);
    var file = matchingDrums[randomIndex];
    var path = this.drumDir + file;
    var s = load(path);
    if (isMarshaledFormat('drumPatch', s)) {
    // Store original output and choke settings
    var origOutput = dp.Output;
    var origChoke = dp.Choke;

    // Load the drum patch
    dp.Patch = s.drumPatch;

    // Restore original output and choke settings
    dp.Output = origOutput;
    dp.Choke = origChoke;

    picked = true;
    } else {
    display("Invalid drum patch format: " + file);
    }
    }

    if (!picked) {
    display("No matching drum patch found for pattern: " + pattern);
    }
    }

    // Expose the pick function publicly
    this.pick = pick;
    }

    var picker = new PatchPicker();
    picker.pick('*.mtdrum', 1, ['BD', 'BS', 'CH', 'CY', 'FX', 'OH', 'PR', 'RM', 'SD', 'SY', 'TM', 'ZP', 'unknown']);

    but it not working:

    1.Building pickNewPatches
    2.It works1
    3.It works2
    4.No matching drum patch found for pattern: *.mtdrum

    i have files with those categories... can anyone help? Thanks

  • JBDS1

    this it another version:

    var preset = getElement('preset');

    function PatchPicker() {
    this.drumDir = DIRS.DRUM_PATCHES + 'G01/'; // Define drumDir as a property
    var pickNewPatches = {
    drums: [],
    drumsByCategory: {
    BD: [],
    BS: [],
    CH: [],
    CL: [],
    CY: [],
    FX: [],
    OH: [],
    PR: [],
    RM: [],
    SD: [],
    SY: [],
    TM: [],
    ZP: []

    },
    inited: false
    };

    function catmatch(pattern, string) {
    var patternIndex = 0;
    var stringIndex = 0;

    while (patternIndex < pattern.length && stringIndex < string.length) {
    if (pattern.charAt(patternIndex) === '*') {
    patternIndex++;
    // Skip characters in the string until the next character in the pattern
    while (stringIndex < string.length && pattern.charAt(patternIndex) !== string.charAt(stringIndex)) {
    stringIndex++;
    }
    } else if (pattern.charAt(patternIndex) === '?' || pattern.charAt(patternIndex) === string.charAt(stringIndex)) {
    patternIndex++;
    stringIndex++;
    } else {
    return false; // Mismatched characters, return false
    }
    }

    return patternIndex === pattern.length && stringIndex === string.length;
    }

    if (!pickNewPatches.inited) {
    display('Building pickNewPatches');

    dir(this.drumDir, function(file) {
    if (catmatch('*.mtdrum', file)) {
    pickNewPatches.drums.push(file);
    }
    if (catmatch('..~~ {..~~~~~~} ..*.mtdrum', file)) {
    var category = '';
    for (var i = 0; i < file.length; i++) {
    var char = file.charAt(i);
    if (char === '.') {
    break;
    } else if (char === '_' || char === ' ') {
    continue;
    } else if (char >= 'A' && char <= 'Z') {
    category += char;
    }
    }
    category = category.toUpperCase();
    if (!pickNewPatches.drumsByCategory[category]) {
    pickNewPatches.drumsByCategory[category] = [];
    }
    pickNewPatches.drumsByCategory[category].push(file);
    }
    });

    // Log the list of collected drum patches
    for (var category in pickNewPatches.drumsByCategory) {
    var categoryDrums = pickNewPatches.drumsByCategory[category];
    if (categoryDrums.length > 0) {
    display('Category: ' + category);
    display('Drum Patches: ' + categoryDrums.join(', '));
    }
    }

    pickNewPatches.inited = true;
    }

    display("It works1");

    function pick(pattern, channel, categories) {
    display("It works2");

    var dp = preset.drumPatches[channel - 1]; // Access drum patch using array indexing
    var picked = false;
    var matchingDrums = [];

    outerLoop: // Label for the outer loop
    for (var i = 0; i < categories.length; i++) {
    var category = categories[i];
    var categoryDrums = pickNewPatches.drumsByCategory[category];
    if (categoryDrums && categoryDrums.length > 0) {
    for (var j = 0; j < categoryDrums.length; j++) {
    var file = categoryDrums[j];
    if (catmatch(file, pattern)) {
    matchingDrums.push(file);
    break outerLoop; // Break out of the outer loop
    }
    }
    }
    }

    if (matchingDrums.length > 0) {
    var randomIndex = Math.floor(Math.random() * matchingDrums.length);
    var file = matchingDrums[randomIndex];
    var path = this.drumDir + file;
    var s = load(path);
    if (isMarshaledFormat('drumPatch', s)) {
    // Store original output and choke settings
    var origOutput = dp.Output;
    var origChoke = dp.Choke;

    // Load the drum patch
    dp.Patch = s.drumPatch;

    // Restore original output and choke settings
    dp.Output = origOutput;
    dp.Choke = origChoke;

    picked = true;
    } else {
    display("Invalid drum patch format: " + file);
    }
    }

    if (!picked) {
    display("No matching drum patch found for pattern: " + pattern);
    }
    }

    // Expose the pick function publicly
    this.pick = pick;
    }

    var picker = new PatchPicker();
    picker.pick('*.mtdrum', 1, ['BD', 'BS', 'CH', 'CY', 'FX', 'OH', 'PR', 'RM', 'SD', 'SY', 'TM', 'ZP']);

You need to be to post a reply

Sign In / Sign Up


First time here? Just enter your current email and sign up.
×
Facebook sign in no longer available. Use the same email to set password and access your account. If you need help, contact us.