Granular time stretching in Max

Adaptation of Andy Farnell’s Pd granular timestretch patch

Adjustable chunk-size, pitch, and speed – as well as manual scrubbing.

The original Pd patch (timestretch.pd) is available here: http://aspress.co.uk/sd/index.php

download

https://github.com/tkzic/max-projects

folder: granular-timestretch

patches

  • timestretch5.maxpat (main patch)
  • tabread4~.maxpat (Pd abstraction) 
  • hip~.maxpat (Pd abstraction)

How it works

An index pointer (file phasor) scans from the beginning to the end of the file. For example, at 44.1KHz, a 1 second file of audio would have 44,100 samples. Each sample is 022676 ms. Another phasor (grain phasor) scans small chunks (grains) of audio. If audio is playing back at the normal rate and pitch, this grain phasor runs at zero. the file phasor just moves sequentially from one grain to the next.

To stretch the time, the file phasor is slowed down, but the grain phasor speeds up, scanning grains of audio, that start at the current file phasor index. In this way, in listening to the file from beginning to end, you are actually listening to a series of overlapping grains.

As the chunk (grain) size increases the grain phasor frequency decreases.

To raise the pitch, the grain phasor frequency would be increased. To lower the pitch, the phasor frequency goes negative and increases in a negative direction to reduce pitch further.

As you can see, while running this patch, the chunk size, pitch, and speed are all related by a single equation.

phasor frequency = ((pitch / 120)**2 – speed) / chunksize

  • where pitch is in cents (0 is normal)
  • speed is a scaling factor (1 is normal)
  • chunk (grain) size  is in ms.

So the input values interact with each other.

In addition, the grain player uses a technique called PSOLA. Pitch synchronous overlap and add. There are actually two grain players playing simultaneously 180 degrees out of phase. Each is windowed using a positive cosine function. This helps to reduce clicks by crossfading from one grain to the next.

There is some comb-filtering and ringing present on the audio. There are various techniques to reduce this, including:

  • trying different window functions
  • higher sample rates
  • changing the overlap
  • varying the grain size
Interesting artifacts:
  • reducing the grain size to several ms. causes oscillation similar to the Karplus-Strong effect
  • increasing the grain size to around 1000 ms. causes a feedback delay effect due to the out of phase grain players
  • setting the overlap to anything other than .5 causes random phase cancellation
Manual or automated scrubbing.

There is also an option to manually scrub the file. This way you can listen to the texture of grains of various sizes from various points in the file.

You can also set random automatic scrubbing to branch to various points in the file.

Web video streaming in Max

Stream realtime video from a web browser into Max or any program that uses Syphon.

CefWithSyphon (developed by Vibeke Bertelsen) launches a web browser and Syphon server. The Max patch operates a Syphon client that receives a video stream from the server and makes it available to Jitter objects.

 download

https://github.com/tkzic/max-projects

folder: web-video-streaming

patch: jit.gl.syphonclient.maxhelp

externals: 
  • jit.gl.syphonclient.mxo:

Note: externals are included with max-projects but can also be downloaded here: http://syphon.v002.info

other dependencies (app) 
  • CefWithSyphon.app

Download the CefWIthSyphon app from here:  https://github.com/vibber/CefWithSyphon – A Mac OS binary is available and has been tested with Mac OS 10.9.2

Instructions
  • Open the client example /jit.gl.syphonclient.maxhelp in  Max
  • Run the CefWithSyphon app. It will launch a browser
  • The browser content should now stream to the clientcontext window in Max 
  • If not streaming, click the “getavailableservers” message and select the correct server/client from menu objects outlined in red

 

max-projects index

A collection of experiments using Max

Each project is in a separate folder. Several projects require additional external objects or dependencies. Get instructions by clicking links next to each project names below.

download

max-projects on Github: https://github.com/tkzic/max-projects

Runs in Max 6.1.7 on Mac OS 10.9

index

3/27/2015 – Note: the index is not current with the contents at github. To find information about a patch, search for the patch name – or github folder name – at this site.

Leap Motion musical UI for closed eyes

How many musicians do you know that play with their eyes closed? Not many computer music apps allow this. Bloom is an exception.  http://www.generativemusic.com/bloom.html

As an exercise, I tried to make something like Bloom, using Leap Motion. With your eyes closed you can accurately position your hand at the level of eyes, shoulders, hips, etc., And you can quickly move to a point directly outward from your nose, or shoulders. This is the basis of sobriety tests.

The interface, works with a hand motion like sprinkling seeds. Every time you open your hand, it triggers a note based on the height of your hand. It also triggers one of the “Bloom”  circles at the XY position.

The prototype was done in Max/MSP Jitter. It was derived from a “bloom clone” project by John Mayrose at: http://www.johnmayrose.com/maxmsp.html

Here’s an example:

download

https://github.com/tkzic/max-projects

folder: bloom

patches

  • (main patch) circlething.maxpst
  • (poly~ sub-patch) FMPoly2~.maxpat
  • (Leap Motion main-patch) leap-finger-switch.maxpat
  • (Leap Motion sub-patch) leap-switch-test2.maxpat
externals and dependencies

Note: If you don’t have a Leap Motion sensor, you can use a mouse.

If you are using Leap Motion, download the aka.leapmotion external – and add it to your Max file path in options | file preferences: http://akamatsu.org/aka/max/objects/

instructions

(if not using Leap Motion sensor, skip to step 4)

  1. Plug in the Leap Motion sensor.
  2. Open leap-finger-switch.maxpat and click the “start” toggle. 
  3. Wave your hand around – it should be detected and displayed
  4. Open circlething.maxpat
  5. If using mouse, just click in the black “circlething” window to play.
  6. If using Leap Motion, click the message box to activate Leap Motion
  7. Then open and close your hand, over the sensor to play
  8. High notes are higher in the window.

near-ultrasound data transfer in Max using FSK (rtty)

An example of sending data through the air, from one computer to another, using sound. The carrier frequency – 18Khz – just below ultrasound, is inaudible to most humans. The data protocol is Audio Frequency Shift Keying (AFSK) at 45 bits/second. It uses two tones – to encode 1’s and 0’s. This protocol was developed for radio teletype (rtty).

One computer sends the word “hello” to the other computer every 10 seconds.

The idea came from a paper by Michael Hanspach and Michael Goetz – “On Covert Acoustical Mesh Networks in Air” – http://www.jocm.us/uploadfile/2013/1125/20131125103803901.pdf. In the paper they explain that the concept was borrowed from current technology for underwater data networks. They warn of the vulnerability of “air-gapping” as a method of computer security.

Actually, after watching the video I realized its difficult to make exciting videos that feature sounds you can’t hear. Especially when you are whispering. Well, the pro camera crew should be knocking on the door at any moment.

download

https://github.com/tkzic/max-projects

folder: frequency-shift-keying

patches:

  • rtty-recv12.maxpat
  • rtty-send12.maxpat
javascript files:
  • rtty-encode10.js
  • rtty-recv9.js

Notes:

To run patches:

  • in rtty-send12 set carrier frequency to something like 200
  • then toggle start/stop
  • You may want to increase the speed to 32
  • On the receive side, clear the text box
  • Make sure to set audio as described below

In audio settings try

  • io vector = 512,
  •  signal vector = 32,
  •  overdrive and audio-interrupt should be enabled,
  •  SR=96 Khz.

Interesting discovery – or maybe a coincidence. Bit rates which are powers of 2 seem to work way better than arbitrary speeds.  I wonder if its because the signal vector size is also a power of 2?

Going from a direct audio connection to an through-the-air connection led to a number of issues with filtering and and levels.  The latest version of the Max patches have been organized into modules, like: encoder, transmitter, decoder, phasor-clock, etc., But they would benefit from some encapsulation.

It was interesting that throughput was better above 14Khz. Possibly due to less interference from environmental sounds – and less critical filtering. During the video I was able to talk (whisper), without interfering  with data transfer. But if I squeaked a chair or tapped the desk, it would screw-up. Also, the builtin mic/speaker on MacBooks have response curves that are all over the place.

The next version will have sharper filters and an automatic level control (compressor).  There’s  difficult interaction in the detection process between filtering and timing. Up to this point I’ve been reluctant to use frequency domain filtering due to loss of timing resolution. Latency is ok though. But the other thing is that we don’t want filters which soften or distort the shapes of the pulses.

So one question is how high can you go – with the built-in mic and speaker. They are not rated above 20 Khz. but you never know?