# Timestretching sound clips

**URL:** <https://discourse.processing.org/t/timestretching-sound-clips/44008>\
**Category:** Libraries\
**Created:** [February 29, 2024, 3:24pm UTC](https://discourse.processing.org/t/timestretching-sound-clips/44008 "2024-02-29T15:24:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![larsh](https://avatars.discourse-cdn.com/v4/letter/l/46a35a/32.png) [@larsh](https://discourse.processing.org/u/larsh)\
**Post date:** [February 29, 2024, 3:24pm UTC](https://discourse.processing.org/t/timestretching-sound-clips/44008/1 "2024-02-29T15:24:34Z")

</div>

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.

```auto
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

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [February 29, 2024, 4:30pm UTC](https://discourse.processing.org/t/timestretching-sound-clips/44008/2 "2024-02-29T16:30:29Z")

</div>

Hi @larsh,

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

[https://code.compartmental.net/minim/index.html](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

---

<div class="post-metadata">

**Author:** ![larsh](https://avatars.discourse-cdn.com/v4/letter/l/46a35a/32.png) [@larsh](https://discourse.processing.org/u/larsh)\
**Post date:** [February 29, 2024, 5:20pm UTC](https://discourse.processing.org/t/timestretching-sound-clips/44008/3 "2024-02-29T17:20:29Z")

</div>

> [@mnse](#):
>
> minim

Thanks, that is a useful tip 🙂 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.

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [March 1, 2024, 12:36pm UTC](https://discourse.processing.org/t/timestretching-sound-clips/44008/4 "2024-03-01T12:36:12Z")

</div>

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…

```auto
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
