Mic-in real-time granular synthesis in Pure Data

As with the offline granulator that we’ve discussed in the previous post, finding resources for real-time granular synthesis on PD was for me even harder. As far as I could figure out, the simple approach that is presented here has not been previously discussed, but I think that this approach is the most efficient, lightweight and intuitive. So please have a look at this post and the example patch and I’ll be very happy to receive any comments, suggestions, questions etc…

Goal: we want to have a look at the intuition behind making a patch that allows basic granular synthesis in real-time on the mic-in signal, using some tools that Pure Data offers. Basic granular synthesis in the context of this post includes the following parameters:

  1. Grain starting time: how far back in the mic input will the grains be collected.

  2. Grain length: how long the grains will be.

  3. Playback speed: how quickly will the grain be played, affecting the pitch of the grain as well.

The example patch given in this tutorial is a bit more “dirty” than the patch parts that are shown in the text illustrations because there are some complicated relations that need to be taken care of; those relations are beyond the intuitive purposes of this tutorial, so the interested reader is referred to the example patch, which automatically adjusts the aforementioned plus some additional parameters, e.g. grain panning, volume, option for octave-locking and grain firing rate. By abstracting this patch, bigger patches can be made that allow more impressive results on real-time granular synthesis.

Real-time? Circular buffer! 

A circular buffer or a ring buffer, can be imagined as a round tape that has a recording head, say on top, and while it is turning, new audio is recorded over old audio. How far back will audio start being recorded over is a matter of the tape length. The circular buffer that comes built in with PD is the [delwrite~ ] object. Figure 1 shows a graphical example of a circular buffer and the PD implementation, considering that the tape has a length of 5 seconds.


Figure 1: The circular buffer (ideal for real-time mic-in processing) and it’s implementation in PD.

Play something on this circular buffer.

To make some sound out of this circular buffer, we need to place a play head, implemented with the [vd~ ] object in PD, at some position of this buffer. The delay time (given as a signal in the [vd~ ] object) defines the how far back we need to take sound from – later on this will be the grain starting time. Figure 2 shows the illustration and the PD implementation of a 300ms delay time.


Figure 2: Playing some audio from the circular buffer/tape.

Isolate a (short) part.

Until now we are able to go back in time in the mic input and start playing continuously what we has been played a while ago (300ms in our example). Figure 3 shows how we can actually trigger an amplitude curve that allows only a short part of what was going on 300ms ago to be heard. The volume envelope comprises a cosine function driven by a [line~ ] object and the time of the line object defines how long the audible segment will be – in this example the time segment is 100ms. Shorter segments are probably more usual in granular synthesis, but let’s consider 100ms for this tutorial.


Figure 3: Isolated short part from the tape.

Pitch up (faster) / pitch down (slower).

If we start moving the playhead on the tape, then the pitch of the recorded material will be affected.

  • If we move it closer to the recording head, then the playhead plays the tape’s content faster and therefore we have pitch-up.

  • If we move it away from the recording head, then the playhead plays the tape’s content slower and therefore we have pitch-down.

The illustration on the right in Figure 4 shows both directions, but the PD patch part on the left shows only the slower case. For playing the tape’s content slower we need to gradually move the playhead away from the recording head (increase the delay time in the [vd~ ] object). This is done by the [line~ ] object that substituted the [sig~ ] object on top of the [vd~ ] object. In this example, during the 100ms that the audible segment “survives”, the playhead moves from 300ms (direct initialisation of the [line~ ] object) to 400ms.


Figure 4: Pitch up/down (only pitch down is shown in the PD patch on the left)

That’s a grain!

As Figure 5 indicates, this patch part is what we need to control the three aforementioned parameters (grain starting time, length and playback speed); thus we have a basic implementation of a grain.

  1. Grain starting time is controlled by the parameter value in the green box.

  2. Grain length is controlled by the parameter value in the red boxes.

  3. Grain pitch is controlled by the parameter value in the bleu box (greater value than the one in green box causes pitch down and the vice versa).


Figure 5: That’s a grain!

A simple patch built by a online single grain from mic-in and randomness

The example patch given allows monophonic granular synthesis on mic input with setting some random parameters. This patch implements the ideas of granular synthesis discussed until Figure 5, along with some additional parameters, includes controls for setting the minimum and the range of random variation for the following parameters:

  • Firing rate (how often new grains are fired).

  • Grain starting time.

  • Grain length.

  • Grain speed-pitch.

  • Grain starting volume.

  • Grain panning.

Initially, maximum grain length and maximum delay time are set and almost all horizontal sliders are giving relative percentages based on those maximum values. There are many interrelations between the different parameters in order to avoid glitches and other things that can go wrong. E.g. it is not possible to have the play head 50ms away from the rec head and request (for pitch-up purposes) to move the playhead 100ms towards the rec head since this way the playhead will have to go 50ms in the… future!

I’m also preparing a bigger patch that includes combined abstractions of single grains based on this tutorial patch. I’ll make a new post about bigger patches for both file-based and mic-in-based granulators. Stay tuned!



  1. sinewave · June 3

    Hi…I know this was a couple of years ago now, but I enjoyed reading this. I’ve been looking at various ways to build a delay line granulator from scratch with certain specific features and this patch seems like a good starting point. Did you ever abstract this out to allow for several simultaneous grains? I’m having a go at it now… Cheers, Alan

    Liked by 1 person

    • maximoskalpap · June 3

      Hi Alan, thanks for your message!

      I have made an attempt to make abstractions out of the example patch and use it in the “grainman” patch:

      The abstraction for a grain is very simple, I just added a bunch of inlets and a couple of outlets – no dollar-sign variables etc – but it should be a good place to begin.

      In the video there is a link to the code:


      however, I’ll possibly upload it soon on GitHub (I don’t remember having made any necessary improvements to the code yet, so the video version is the last version I’ve made).

      Thanks again for your comment and please let me know if there’s something else you want to discuss!



  2. sinewave · June 3

    Hi Max, Thanks very much for getting back to me. I can see you’re coming from the same angle as me when it comes to guitar + granular fx. In fact (and maybe I’ve just missed a few tricks here) this is probably the only other example of the kind of stuff I do with granular fx that I’ve seen yet. I discovered Audiomulch a long, long time ago and have always used the DLGranulator as a kind of favourite go-to effect with guitar… not long ago I set out to re-create this effect in PD, which led me here. And true enough your updated version of the effect sounds much closer to the Audiomulch effect and is fairly similar to what I’ve been building today (but better!). However I also added a wet/dry section and more importantly a feedback control – which is probably the most distinctive part of the DLGranulator.

    In the autumn I will be building a few (iOS and android) apps based on my recent pd experiments. First up will probably be a granular effect, so I will be doing a few experiments in the coming weeks. If you find it interesting I would welcome some guidance and/or assistance in building the underlying patch for that app. Cheers, Alan.


  3. sinewave · June 14

    Hi again. I’ve noticed that when increasing speed to pitch upward, there is a lot of the input signal being output at the original speed, instead of just the pitch-transposed signal. I have removed the dry signal that you have in the patch, but I haven’t yet found the cause of the non-pitched output. The speed range is also at minimum. Any ideas?


    • maximoskalpap · June 14

      Hi Alan! If I understood correctly, pitch shifting (playback shift) doesn’t work, right? Is “octave lock” on? If so, it might be the case that you have to increase speed at least X2 (given that speed range is turned to minimum) to get the pitch shifting effect (“locked to one octave high”). Have you tried with “octave lock” off? Of course, and most possibly, this might be yet another bug in this implementation!

      During the forthcoming weekend I have planned to take a look at this patch again. Actually, your messages have come quite at a perfect timing! We’ve been discussing with Avi Bortnick to publish the Grainman for iOS (using a similar interface as in our Echo Pitch app). Would it make sense to join forces towards this effort – expanding to Android as well? We can discuss through email if you’re interested (maximoskalpap at gmail do com).

      I’ll check the patch out during the weekend and I’ll post any updates.

      Thanks again – cheers,


  4. sinewave · June 14

    Hmm. I just tested at work, with a sine wave, and managed to get a purely pitched signal. I need to test again at home this evening… Mailing you about the other thing!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s