Jump to content

Need Probability & Transpose Script help


Recommended Posts

Hi there, I was turned on to the Scripter plugin today and had my mind expanded in ways I truly needed. The unfortunate part is, I'm not a coder. Thus, I'm hoping someone here can help me with a goal. I'd like to have a script that allows me to take a recorded midi region and allow me to set two things: 1. The probability a note will transpose. 2. The amount in semitones a note will transpose.

 

Does anyone have an example, or the code to make this happen? Wiling to post here so I could make use of it? Or are there some really good tutorials on the subject?

 

Thank you for your time.

Link to comment
Share on other sites

  • 1 month later...

Hi guys,

 

I'm interested in exactly this same thing, and registered on this forum (which looks rather helpful anyway) to respond to this conversation.

 

The script provided doesn't seem to work for me and even though I tried adjusting some of the parameters (I have no knowledge of Javascript yet) I can't get it to work. Could someone post a version of this script that works in Logic Pro X 10.3.1 ? Also if you have some good pointers for learning Javascript it would be greatly appreciated!

 

Cheers

Link to comment
Share on other sites

Hey Schoorstijn, good idea signing up. I'm pretty new here myself, but I've found a lot of good info here. IMO Unheardofski is essentially gifted when it comes to Scripter, so I was surprised to hear it didn't work out for you. I just tried it on Logic 10.3.1 and it works fine both by keyboard input and by playback of midi region... perhaps its a setup issue on your side? Could you describe it? Back to basics, you need a midi track with an instrument as well as the scripter plugin added to it at the very minimum.

 

In relation to getting started with JavaScript, I'm shooting in the dark here, but http://exploringjs.com is great for specifics, and something like Sitepoint for a general heads up if you're just starting out. And in relation to getting started with Scripter, I found this post useful as it gives a good overview of the API... its going to be hard finding a JavaScript resource that doesn't presuppose use of the browser....

Link to comment
Share on other sites

  • 1 month later...

Hi Nseruame!

 

Thanks for the respone and all the useful info! Yeah I'm not sure what I'm doing wrong, I have a project with a MIDI track, a midi region (just for the sake of testing) with some notes, the scripter in the midi effects slot. I copy the entire script I download which looks as attached.

 

I've added a screenshot of what happens when I press 'run script'. As said I've tried to edit some of the parameters, but to me it looks like it doesn't use the right code language necessary to define all the assets. I might be wrong of course.. Any help you could point me to as to what I'm doing wrong? I will definitely dive into those Javascript resources, but it will probably take some before I can diagnose the errors correctly myself.. ;-)

 

Transpose probability script run screenshot

 

//

//  Probably Note

//  Created by Attila Enhardt on 2017-04-14

//     Copyright codecave STHLM / LogicScripts 2017.

//

 

PluginParameters = [];

let scriptName = new Parameter(null, "Probably Note by codecave STHLM")

let prob = new Parameter(null, "Transpose probability", "lin", "prop", null, 0, 100, 0, 100, " %");

let semiTones = new Parameter(null, "Transpose semitones", "lin", null, null, -12, 12, 0, 24, " st.");

let noteLookUp = [];

for (let i = 0; i < 128; i++) {

    noteLookUp.push(i);

}

 

function HandleMIDI(e) {

    if (e instanceof NoteOn) {

        treatNoteOn(e);

    }

    if (e instanceof NoteOff) {

        treatNoteOff(e);

    }

    e.send();

}

 

function treatNoteOn(e) {

    let p = Math.floor(Math.random() * 101);

    if (p <= GetParameter("Transpose probability")) {

        let originalPitch = e.pitch;

        e.pitch += GetParameter("Transpose semitones");

        noteLookUp[originalPitch] = e.pitch;

    }

}

 

function treatNoteOff(e) {

    let originalPitch = e.pitch;

    e.pitch = noteLookUp[e.pitch];

    noteLookUp[originalPitch] = originalPitch;

}

 

 

 

function Parameter(cb, n, t, pc, vs, miv, mav, def, nos, unit) {

    let vsl = vs ? vs.length : null;

    this.callBack = cb || function(v) {

        this.currentValue = v;

    };

    this.name = n;

    this.type = t || "text";

    this.pc = pc || null;

    this.valueStrings = vs || null;

    if (t != "checkbox") {

        this.minValue = miv || 0;

        this.maxValue = mav || 0;

    }

    this.defaultValue = def || Math.floor(vsl / 2) || 0;

    this.numberOfSteps = nos || vsl || null;

    this.unit = unit || null;

    this.pc = pc || null;

    this.currentValue = 0;

    this._index = PluginParameters.length;

    this.remove = function() {

        PluginParameters.splice(this._index, 1);

    };

    PluginParameters.push(this);

Link to comment
Share on other sites

Hey Schoorstijn! Good to know you have the track setup correctly, but thats odd... I took a look at the screenshot first, and then I copied and pasted the script above into Scripter... and Atillas code worked as expected with no error messages! 

 

Can you confirm that there are no additional lines of code other than the ones you posted above? The error reported is a general javascript error... it should only pop-up if you happen to have the same variable declared elsewhere in your code. Is there a second declaration of the "scriptName" variable anywhere?

 

Hmmmm. 

 

Scripter uses whatever version of JavaScriptCore (Apples javascript interpreter) thats available on your system, and that depends on which version of macOS you have installed. Its unlikely to be the cause of the issue, but out of curiosity, what version of macOS are you using? And which version of Logic are you using?

 

And, by the wildest of chances, have you modified the original script that Scripter loads in the background from the its installation files? I remember doing something odd like that myself when starting out. If you don't know what ``EventTypes.js`` is, then discount the last question as its unlikely to be the cause.

 

Let me know and hopefully we can figure it out :)

Link to comment
Share on other sites

Hi Nseruame,

 

Thanks for the quick response again buddy! Hmmm yeah I thought it must be the case, did seem like it should just work.. There are definitely no other lines of code, I made sure to clean the whole area and just paste the code above.

 

I'm running El Capitan (10.11.2) and Logic Pro X 10.3.1, not sure if that would cause issues but if you think I need to upgrade to Sierra or 10.3.2 then I might try ;)

 

Hmm nope haven't modified any of the original scripts as far back as I can remember. Other preset scripts that are included with Logic are running fine as is..

 

Hope someone has an idea! Might have to do a fresh install of Logic, hopefully not the OS but might try that further down the line. Thanks!

Link to comment
Share on other sites

OK then - so:

- Atillas script doesn't have any bugs

- there are no typos in the script contents you provided (was just double checking!)

- it seems me  that you have a valid combination of OS and Logic so it _should_ be working

- you haven't modified EventTypes.js

 

From afar, it seems to me that its an issue with JavaScriptCore.  To double check if that is the cause of the issue, as Safari and Logic use the same system JavaScriptCore (or so experimentation has led me to believe), how about one final test. Copy the text below and save it as "script.html".

<!DOCTYPE html>
<head>
    <title> Script test </title>
</head>
<body>
    <script>
        /* Scripter globals */
        GetParameter = new Function;

        PluginParameters = [];
        let scriptName = new Parameter(null, "Probably Note by codecave STHLM")
        let prob = new Parameter(null, "Transpose probability", "lin", "prop", null, 0, 100, 0, 100, " %");
        let semiTones = new Parameter(null, "Transpose semitones", "lin", null, null, -12, 12, 0, 24, " st.");
        let noteLookUp = [];
        for (let i = 0; i < 128; i++) {
            noteLookUp.push(i);
        }

        function HandleMIDI(e) {
            if (e instanceof NoteOn) {
                treatNoteOn(e);
            }
            if (e instanceof NoteOff) {
                treatNoteOff(e);
            }
            e.send();
        }

        function treatNoteOn(e) {
            let p = Math.floor(Math.random() * 101);
            if (p <= GetParameter("Transpose probability")) {
                let originalPitch = e.pitch;
                e.pitch += GetParameter("Transpose semitones");
                noteLookUp[originalPitch] = e.pitch;
            }
        }

        function treatNoteOff(e) {
            let originalPitch = e.pitch;
            e.pitch = noteLookUp[e.pitch];
            noteLookUp[originalPitch] = originalPitch;
        }



        function Parameter(cb, n, t, pc, vs, miv, mav, def, nos, unit) {
            let vsl = vs ? vs.length : null;
            this.callBack = cb || function(v) {
                this.currentValue = v;
            };
            this.name = n;
            this.type = t || "text";
            this.pc = pc || null;
            this.valueStrings = vs || null;
            if (t != "checkbox") {
                this.minValue = miv || 0;
                this.maxValue = mav || 0;
            }
            this.defaultValue = def || Math.floor(vsl / 2) || 0;
            this.numberOfSteps = nos || vsl || null;
            this.unit = unit || null;
            this.pc = pc || null;
            this.currentValue = 0;
            this._index = PluginParameters.length;
            this.remove = function() {
                PluginParameters.splice(this._index, 1);
            };
            PluginParameters.push(this);
        }
    </script>
</body>

 

Then run it in Safari. If you have not checked the console before, then setup Safari like so:

 

- Open Safari and choose Safari > Preferences > Advanced. Then select the checkbox 'Show Develop menu' in menu bar.

Choose Develop > Show Error Console.

 

Open the html page you created and let me know if you see any errors in console. As this can get tricky, its no problem to PM me if you like.

 

I understand if this approach is too cumbersome for you - it can be a pain debugging javascript. But, it will give me more information. 

 

An OS/ Logic update brings its own pain and I wouldn't like to advise an upgrade just yet. Ideally the script issue could be solved without upgrading. The last step after this would be to create a new Logic project from scratch and set it up as before.

 

Let me know how you get on!

Link to comment
Share on other sites

Thanks again for the great help! I think you're right about there being some kind of issue with JavaScriptCore..

I copied the text and created a 'script.html' file which I then opened in Safari with the developer Error console enable.

 

I open the page in Safari, but I just see the same text that I copied and pasted and saved (as a proper html file, no text file extension or something). Attached you can see how it looks! No error, just the script text on a webpage..

 

It's no problem for me to begin a debugging process of Javascript, but I don't think this will tell you anything right? About the last step, any new project I create with a midi channel + scripter + probability script will generate this error for me...

 

Cheers,

Stijn

 

Script.html in safari

Link to comment
Share on other sites

Ok then. To recap

 

- Atillas script doesn't have any bugs

- there are no typos in the script contents you provided

- you may have a valid combination of OS and Logic so it _should_ be working

- you haven't modified EventTypes.js

- You've also recreated new projects and encountered the same error. 

- The error is not appearing in the Safari runtime. 

 

Ok then, before reading the steps below, I'd like you to try one more thing. This will confirm the JavaScriptCore issue. I believe the issue is that the JavaScriptCore on your system is an ECMAScript 5 interpreter. The script uses the 'let' keyword, which is an ECMAScript 6 keyword. The script below is the same as the original, the only difference is that I have replaced the ES6 keyword 'let' for the ES5 keyword 'var' :

 

//
//  Probably Note
//  Created by Attila Enhardt on 2017-04-14
//     Copyright codecave STHLM / LogicScripts 2017.
//

PluginParameters = [];
var scriptName = new Parameter(null, "Probably Note by codecave STHLM")
var prob = new Parameter(null, "Transpose probability", "lin", "prop", null, 0, 100, 0, 100, " %");
var semiTones = new Parameter(null, "Transpose semitones", "lin", null, null, -12, 12, 0, 24, " st.");
var noteLookUp = [];
for (var i = 0; i < 128; i++) {
    noteLookUp.push(i);
}

function HandleMIDI(e) {
    if (e instanceof NoteOn) {
        treatNoteOn(e);
    }
    if (e instanceof NoteOff) {
        treatNoteOff(e);
    }
    e.send();
}

function treatNoteOn(e) {
    var p = Math.floor(Math.random() * 101);
    if (p <= GetParameter("Transpose probability")) {
        var originalPitch = e.pitch;
        e.pitch += GetParameter("Transpose semitones");
        noteLookUp[originalPitch] = e.pitch;
    }
}

function treatNoteOff(e) {
    var originalPitch = e.pitch;
    e.pitch = noteLookUp[e.pitch];
    noteLookUp[originalPitch] = originalPitch;
}



function Parameter(cb, n, t, pc, vs, miv, mav, def, nos, unit) {
    var vsl = vs ? vs.length : null;
    this.callBack = cb || function(v) {
        this.currentValue = v;
    };
    this.name = n;
    this.type = t || "text";
    this.pc = pc || null;
    this.valueStrings = vs || null;
    if (t != "checkbox") {
        this.minValue = miv || 0;
        this.maxValue = mav || 0;
    }
    this.defaultValue = def || Math.floor(vsl / 2) || 0;
    this.numberOfSteps = nos || vsl || null;
    this.unit = unit || null;
    this.pc = pc || null;
    this.currentValue = 0;
    this._index = PluginParameters.length;
    this.remove = function() {
        PluginParameters.splice(this._index, 1);
    };
    PluginParameters.push(this);
} 

 

If that works, then your system version of JavaScriptCore cannot process ECMAScript 6 - it can only process ECMAScript 5. You never know, that may be ok for you - all the presets are in ES5, and if you're just starting out, ES5 is not too bad. If you are going to commit to Scripter though, then ES6 would probably be the best way to start working with the language again.

 

As JavaScriptCore is a provision of the OS, the only way out is to update to Sierra. I've had very mixed experiences updating over the years. I went from Mavericks to Sierra about 6 months ago skipping all the other editions in between (they screwed up my work mac goddamit!). I'm happy to say it worked out ok.

 

How do you feel about those options? I don't know myself... Its a drastic step, but you could consider a clean install of Sierra as long as you get other benefits from the update - I wouldn't update for Scripter only.

 

Anyhow, park all that until you've run the script I pasted above and get back to me, ok?

 

Thanks :)

Link to comment
Share on other sites

@Dewdman42 - did you try Atilla original script with the 'let' statement (ES6) or the modified one I posted above that uses 'var' instead (ES5)? Very curious myself. In my experience, the only way to update the system JavaScriptCore interpreter is indeed to update the OS. Its mad.
Link to comment
Share on other sites

the let version doesn't work for me I get the same error as the other guy (I'm on El capitan).  I changed the let's myself to var's.  it works, but runs a while and crashes logic.  Well now I've been able to run it a while with different midi data, no crash.  Dunno.  Strange.  I have no idea how it could crash logic...how ANY script could do so is not great...
Link to comment
Share on other sites

@Dewdman42 - thanks for the info! I think this will probably stand true for Schoorstijn too. So its an outdated version of JavaScriptCore insomuch as it is an ECMAScript 5 interpreter is on your OS, and therefore incompatible with code thats friendly to the more recent ECMAScript 6 compatible edition of it. Like Schoorstijn, you are on El Capitan. It is ridiculous that JavaScriptCore is so tightly coupled to the OS, but thats the way it is. Its also mad that you can crash Logic with a script, but I would put that down to the version on your system. I wouldn't recommend trying to update JSC manually. Its a system framework tool. The furthest termFu I'm willing to go with it is to give it an alias in bash_profile and thats it! This is what is used by Logic Pro X. I ran into this when on Mavericks. It was one of the reasons I upgraded to Sierra, and in order to do that, I went for a clean install similar to the details provided in the link I posted above. Its your machine and your work. Its your decision. The best advice I can offer is also the worst: update to Sierra to get ES6 JavaScriptCore into Logic Pro X. Most definitely do not upgrade casually! I can't really offer any more advice than that I'm afraid. I wish there was an easier way. Grrrrrapple. Edited by Nseruame
Link to comment
Share on other sites

  • 2 weeks later...
Hmmm alright, seems like upgrading is the way to go to update Java en then see if works like that. I think upgrading to Sierra is pretty safe at the moment anyways, so I might try it later this week :) Will get back to you guys to let you know how it went! Thanks for all the help guys and Unheardofski haha I can imagine!https://www.logicprohelp.com/memberlist.php?mode=viewprofile&u=67516
Link to comment
Share on other sites

I've made the plunge and upgraded to Sierra. Guess what...

It wall works without the blink of an error! Didn't expect that it would solve the problem immediately but it did. I would say El Capitan with the latest version of Logic pro x should still do the trick but I guess not.. Happy scripting, thanks a lot guys!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...