Timestretching sound clips

Hi all, I’m quite new to Processing and coding in general. I am developing a sketch where I want the user to be able to play the same sound clip at various speeds without altering the pitch - I believe this is called “timestretching”. I struggle finding a way to do this - ChatGPT pointed me towards the Minim and Beads libraries, but when I try to run the example code the active function seem to be non-existent (perhaps a result of generative AI “hallucination”?) I haven’t been able to find a good answer elsewhere either.

Here is the GPT example just in case Im missing some fundamentals, Processing says that the “setTempo” function does not exist.

import ddf.minim.*;
import ddf.minim.analysis.*;

Minim minim;
AudioPlayer player;
AudioOutput out;

float tempoFactor = 1.5; // Adjust the tempo factor as needed

void setup() {
  size(100, 100); // Set your desired window size
  minim = new Minim(this);
  player = minim.loadFile("your_sound_clip.mp3");
  player.loop();
  out = minim.getLineOut();
}

void draw() {
  // Adjust the tempo while maintaining the pitch
  player.setTempo(tempoFactor);
  
  // You can add more processing here if needed
  
  // Draw your visual interface or anything else
}

void stop() {
  player.close();
  minim.stop();
  super.stop();
}

Does anyone have any tips for how to solve my issue? - Lars

Hi @larsh,

Do not trust completely on chatgpt and study always the reference page of the libraries you use, minim in that case. :wink:

https://code.compartmental.net/minim/index.html

Then you’ll find out, that the setTempo not belonging to the AudioPlayer, but rather the AudioOutput…

Cheers
— mnse

1 Like

Thanks, that is a useful tip :slight_smile: Studying the reference more closely I still struggle imagining how this could be used to timestretch an existing audio clip. I would appreciate it If you let me know if you see something I don’t, or if you have other tips towards the function I’m after.

Hi @larsh ,
Sorry, for the late answer. I’m quite busy currently …

I would recommend to use the Beads library (download via Contribution Manager).

See simple example here…

import beads.*;

AudioContext ac;
SamplePlayer player;

void setup() {
  size(600,300);
  ac = AudioContext.getDefaultContext();
  player = new SamplePlayer(SampleManager.sample(dataPath("groove.mp3"))); // set your file here
  // if you want to loop, btw. LOOP_ALTERNATING funny :)
  player.setLoopType(SamplePlayer.LoopType.LOOP_FORWARDS);
  Gain g = new Gain(1, 1);  
  g.addInput(player);
  ac.out.addInput(g);
  ac.start();
}

void mousePressed() {
  // depending on mouseX position it sets the speed between half and double speed ...
  player.setRate(new Glide(ac, map(mouseX,0,width,0.5,2)));
}

void draw() {
}

Hope that helps!

Cheers
— mnse