Looking for help with a script

I’m trying to make a script that should
1.) make a random selection that is 5% of the active wave’s total length
2.) cut it out and paste it back in a random location
3.) repeat this 50 times

I’m stuck. My script does nothing past line 54… Happy for any pointers in the right direction (I’m not an experienced coder).

Here’s a link to the script and the pasted code:

/* ----------------------------------------------------------------------------
Scramble2000.js – Extract the essence of your sound by shredding it into pieces


DESCRIPTION
-----------
This script will displace random parts of the current file, making the sound
shattered and unrecognizable. The idea is to extract the actual 'essence' of 
the sound. It might be a bit similar to how granulizers work, but I wanted to 
have this functionality as a destructive function in my audio editor.

AUTHOR
------
Henrik Cederblad | https://github.com/hced

COMPATIBILITY
-------------
Developed for WaveLab Pro 10+. May or may not work in lower WaveLab versions.

VERSION HISTORY
---------------
0.1a - Initial version


PSEUDO CODE (there were slight difference in the actual code)
-----------
- Define FILELENGTH as active wave's total length in samples.
- Define SNAPSHOTLENGTH as a 5% percentage of FILELENGTH in samples.
- Define REPS as the number of times the scramble should be repeated. (Note: in
  the future, I wish to make this an exponential formula (more repetitions 
  the longer the FILELENGTH is).

- Scramble() function:
	- Pick a random location for a 5% snapshot (range) of the current file.
	- Cut the snapshot.
	- Paste the snapshot in a random location.

- Repeat Scramble() 50 times. (Note: Might want this value to be increasingly 
  higher, the longer the audio file is, later on.)
-----------------------------------------------------------------------------*/

// Set constants
FILELENGTH 		= activeWave.size(); // Total samples
SNAPSHOTLENGTH 	= (FILELENGTH/100)*5; // 5% of the total length
REPS 			=  50; 	// Todo: Make exponential formula, something like:
						// Math.pow( (FILELENGTH/100), 3 ) I don't know

// Log: Clear log
logWindow.clear();
// Log: Output total length in samples of the active file
logWindow.printInfo("This file has a total of " + FILELENGTH + " samples");

function randomEndLoc(min, max) {
	min = Math.ceil(min);
	max = Math.floor(max);
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

function Scramble() {

	// Set thisEnd to a random location between 0 and FILELENGTH
	thisEnd = randomEndLoc(0, FILELENGTH);
	logWindow.printInfo(thisEnd); // Debug

	// ...but not lower than SNAPSHOTLENGTH.
	if (thisEnd < SNAPSHOTLENGTH) {
		thisEnd = SNAPSHOTLENGTH;
	}

	// Make range selection
	select( (thisEnd - SNAPSHOTLENGTH), SNAPSHOTLENGTH );

	// Cut the selected range
	cut();

	// Move cursor
	setCursorPosition(thisEnd - SNAPSHOTLENGTH); //Todo: round up to prevent 0?

	// Paste the cut-out selection
	paste();
	
}

// Repeat Scramble() for REPS amount of times
for (i in REPS) {
	logWindow.printInfo(i); // Debug
	Scramble();
}

You should not call
select(…);
but
activeWave.select(…);

etc…

Also, your construct
for (i in REPS) {
Scramble();
}

does not seem valid. eg. this works:

Scramble();
Scramble();
Scramble();

Do I need to provide arguments to things like activeWave.cut() or activeWave.paste() functions? I’m currently under the assumption that they operate on whatever is being selected and copied.

I managed to make it work as I intended! This was fun. I’ve published the script on my GitHub: GitHub - hced/WaveLab-Scripts: Scripts for various WaveLab operations, mostly intended for sound design.

For reference, here’s the current code. There are a few kinks that I might consider fixing at a later point, but it does what I want (see code for description).

/* ----------------------------------------------------------------------------
Scramble2000.js – Extract the essence of your sound by shredding it into pieces
-------------------------------------------------------------------------------

DESCRIPTION
-----------
This script will displace random parts of the current file, making the sound
shattered and unrecognizable. The idea of this script is to extract the actual
'essence' of the sound. It's a bit related to how granulizers work, but I 
wanted to have this functionality as a destructive function in my audio editor.

Note: You may want to perform Correct Errors on the resulting file to soften
up hard cuts a bit. I haven't found a good way to strip silence, otherwise I 
would have recommended doing that as well because for some reason, there's 
short, occasional micro bits of silence in the resulting audio.


AUTHOR
------
Henrik Cederblad | https://github.com/hced

COMPATIBILITY
-------------
Works in WaveLab Pro 10+. May or may not work in lower WaveLab versions.

VERSION HISTORY
---------------
0.1 - First functional version. Removed commented pseudo-code. There's some
corrections to be made, probably in the selection procedure, because somehow
the audiofile gets slightly longer every time you run the script against it.
0.1a - Initial version

-----------------------------------------------------------------------------*/

const FILELENGTH = activeWave.size(); // Total samples
const SNAPSHOTLENGTH = (FILELENGTH/100)*5; // 5% of total samples
const REPS = 200;

// Debug
logWindow.clear();
var TS = new Date();
function timeStamp() { return TS.toISOString(); }
logWindow.printInfo(timeStamp()); // Log timestamp
logWindow.printInfo("This file has a total of " + FILELENGTH + " samples");

function randomPos(min, max) {
	min = Math.ceil(min);
	max = Math.floor(max);
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

function Scramble() {
	// Set thisEnd to a random location between 0 and FILELENGTH
	thisEnd = randomPos(0, FILELENGTH);
	logWindow.printInfo(thisEnd); // Debug
	// ...but not lower than SNAPSHOTLENGTH.
	if (thisEnd < SNAPSHOTLENGTH) {
		thisEnd = SNAPSHOTLENGTH;
	}

	// Make range selection
	activeWave.select( (thisEnd - SNAPSHOTLENGTH), SNAPSHOTLENGTH );

	// Cut the selected range
	activeWave.cut();

	// Move cursor to a random location
	cursorPos = randomPos(0, FILELENGTH);
	activeWave.setCursorPosition(cursorPos); //Todo: round up to prevent 0?
	logWindow.printInfo("Moved cursor to position " + cursorPos); // Debug

	// Paste the cut-out selection
	activeWave.paste();
	
}

// Repeat the process according to number of times defined in REPS
for (i = 0; i < REPS; i++) {
	Scramble();
}

Here’s an example where I used the script to make a train cabin sound.

Original sound.
Same sound but processed by the script.
Example final sound, a train cabin sfx (a few hand tweaks, EQ and delay). (To hear the sounds, one might need to download the files from GitHub since they don’t appear to stream sounds from their website for apparent reasons.)