Scripting: Creating a Parameter event with an instrument per slot

Hi, I’d like to speed up event creation with a script that will make a parameter sheet event, and load samples into single instruments inside each slot of a single parameter, so it will look something like this (I created this one manually in Studio):

I have 49 note samples from C4 to C8 that I want to go into each parameter value 0-49. The idea is C4 will be parameter value 0, C#4 will be value 1 etc. (The actual files are named Keys - Soft n sweet_C4_78.wav etc)

My attempt at the set up:

// Define the base path for audio files
var basePath = "C:\\em-sounds\\";

// Define the event name and parameter name
var eventName = "SoftKey"; 
var parameterName = "Note"; 

// Create a new event
var newEvent = studio.project.create("Event");
newEvent.name = eventName;

// Create a new parameter
var newParameter = newEvent.addGameParameter({
    name: parameterName,
    type: studio.project.parameterType.User,
    min: 0,
    max: 49
});

// List of musical notes from C4 to C8
var notes = [
    "C4", "C#4", "D4", "D#4", "E4", "F4", "F#4", "G4", "G#4", "A4", "A#4", "B4",
    "C5", "C#5", "D5", "D#5", "E5", "F5", "F#5", "G5", "G#5", "A5", "A#5", "B5",
    "C6", "C#6", "D6", "D#6", "E6", "F6", "F#6", "G6", "G#6", "A6", "A#6", "B6",
    "C7", "C#7", "D7", "D#7", "E7", "F7", "F#7", "G7", "G#7", "A7", "A#7", "B7",
    "C8"
];

// Create instruments for each note
for (var i = 0; i < notes.length; i++) {
    var note = notes[i];
    
    // Construct the sample filename
    var sampleFilename = "Keys - Soft n sweet_" + note + "_78.wav";


	///No idea how to actually create the instrument in the parameter slot :)
    
}

// Save the project
studio.project.save();

Hopefully I have the framework more or less right, but as you can see I’m missing the most important part - the actual instrument creation / adding to each parameter slot. Any help would be massively appreciated, thanks.

Hi,

Given an event event, to create an instrument on a parameter sheet for a range of parameter values, you’ll need a reference to the audio track in the event that you want the instrument to be on from event.groupTracks[] or event.masterTrack, and the parameter from event.parameters[].

Assuming you’re using a non-master audio track, and that the event has only one audio track and parameter, the code for creating the single instrument is as follows:

// value where instrument is placed on timeline/parameter sheet
var startValue = 45;
// length of instrument
var length = 4;
// create single instrument on first parameter sheet, on first audio track, at value 45, spanning length 4 (i.e. 45 to 49)
var instrument = event.groupTracks[0].addSound(event.parameters[0], 'SingleSound', startValue, length);

To get the asset using string sampleFilename from your imported assets, you can do the following:

var sample = studio.project.workspace.masterAssetFolder.getAsset(sampleFilename);

And assign it with:

instrument.audioFile = sample;

So, in your for loop, the code might look like this:

for (var i = 0; i < notes.length; i++) {
    var note = notes[i];
    
    // Construct the sample filename
    var sampleFilename = "Keys - Soft n sweet_" + note + "_78.wav";
    // get sample
    var sample = studio.project.workspace.masterAssetFolder.getAsset(sampleFilename);
    // add instrument to new parameter sheet
    var instrument = event.groupTracks[0].addSound(newParameter, 'SingleSound', i, 1);
    // assign sample to instrument
    instrument.audioFile = sample;
}
1 Like