Skip to content

Instantly share code, notes, and snippets.

@sletz
Created February 5, 2025 19:38
Show Gist options
  • Select an option

  • Save sletz/b7a285fc925bb07bf8d09596312be64d to your computer and use it in GitHub Desktop.

Select an option

Save sletz/b7a285fc925bb07bf8d09596312be64d to your computer and use it in GitHub Desktop.
Faust looper
import("stdfaust.lib");
//sound = soundfile("sound[url:{'levot.wav'}]",2);
//sound = soundfile("sound[url:{'violon.wav'}]",2);
sound = soundfile("sound[url:{'tango.wav'}]",2);
looper(sf) = (0, loop_index) : sf : !, !, attach(_,pos), si.bus(outputs(sf)-3)
with {
// User controls
play = checkbox("h:Controls/[3]Play/Stop"); // Checkbox to start/stop playback
reverse = checkbox("h:Controls/[4]Reverse"); // Checkbox to play in reverse
reset = button("h:Controls/[5]Reset"); // Button to reset playback position
// Loop start and end positions (scaled to the sound file length)
loop_start = length * hslider("[1]Loop_start", 0, 0, 1, 0.01);
loop_end = length * hslider("[2]Loop_end", 1, 0, 1, 0.01);
// Sound file length
length = (0, 0) : sf : (_, si.block(outputs(sf) - 1));
// Current playback position within the loop
loop_index = min_loop_pos + (index % loop_length)
with {
// Minimum loop position
min_loop_pos = min(loop_start, loop_end);
// Loop duration
loop_length = abs(loop_end - loop_start);
// Index counter that tracks playback position
index = windex
letrec {
'windex = ba.if(windex < 0, windex + length, // Wrap around if index goes negative
ba.if(windex > length, 0, // Wrap around if index exceeds length
windex + play * ba.if(reverse, -1, 1))); // Update index based on play state and direction
};
};
// Display playback position as a horizontal bar graph
pos = (loop_index / length) : hbargraph("[5]position", 0, 1);
};
process = looper(sound);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment