« 11 / 30 »

Separate Noisy and Pitched Sounds

2014-10-01 11:14 supercollider

clean-up: #38

Here's a little fun trick how to use the not so well known clarity mode of SuperCollider's built-in pitch tracker. Set the clar argument to 1 and then grab the second value returned. By default, this is a binary flag (hasFreq), but with clar=1 it becomes a continuous value 0.0 to 1.0.

The example below includes a one-second delay to be able to hear the sounds you make being panned left-right depending on how 'clear' they are. There's also a short lag and a curved mapping to make the example work better.

//example: noise in the left ear, clear tones in the right
//use headphones
{
  var src= DelayN.ar(SoundIn.ar, 1, 1);
  var clarity= Pitch.kr(src, clar:1)[1].lag(0.1);
  Pan2.ar(src, clarity.lincurve(0.1, 0.9, -1, 1, 4).poll);
}.play

As variations on the above one could easily mute noises...

//example: mute noise, only play pitched sounds
//use headphones
{
  var src= DelayN.ar(SoundIn.ar, 1, 1);
  var clarity= Pitch.kr(src, clar:1)[1].lag(0.1);
  Pan2.ar(src)*clarity.lincurve(0.1, 0.9, 0, 1, 4);
}.play

and of course by just flipping a value around we only play the noises...

//example: mute pitched sounds, only play noises
//use headphones
{
  var src= DelayN.ar(SoundIn.ar, 1, 1);
  var clarity= Pitch.kr(src, clar:1)[1].lag(0.1);
  Pan2.ar(src)*(1-clarity.lincurve(0.1, 0.9, 0, 1, 4));
}.play

last an example to demonstrate how to route signals to effects depending on clarity...

//example: pitched sounds with reverb
//use headphones
{
  var src= DelayN.ar(SoundIn.ar, 1, 1)*0.5;
  var clarity= Pitch.kr(src, clar:1)[1].lag(2);
  XFade2.ar(src, GVerb.ar(src, 50), clarity.lincurve(0.3, 0.9, -1, 1, -2).poll);
}.play

Quartz Composer in SC

2014-09-30 01:08 supercollider

clean-up: #37

Thanks to Scott Wilson it is possible to load and embed Quartz Composer sketches in SuperCollider.

Below is an example that demonstrates how to use it with a camera input. You'll need the attached .qtz file and edit the path in the SuperCollider code.

cam qtz

note1: this is macOS only.

note2: this works on SC 3.7 and newer (or SC 3.5 if you replace QuartzComposerView with SCQuartzComposerView).

(
var width= 1024, height= 576;
w= Window("qc", Rect(100, 100, width, height)).front;
m= QuartzComposerView(w, Rect(0, 0, width, height));
//m.path= "~/Desktop/cam.qtz".standardizePath;  //edit to match filepath
m.path= thisProcess.nowExecutingPath.dirname+/+"cam.qtz";
CmdPeriod.doOnce({w.close});
m.start;
m.inputKeys.postln;  //should return [X, Y]
s.waitForBoot{
  ~speedx= 0.1;
  ~speedy= 0.11;
  a= {|x= 0, y= 0| Pan2.ar(MoogFF.ar(Saw.ar(1-y.lag(0.1)*99+99), y.lag(0.1)*199+99, 3), x.lag(0.1)*2-1)}.play;
  r= Routine({
    inf.do{|i|
      var x= sin(i*~speedx)*0.5+0.5;
      var y= cos(i*~speedy)*0.5+0.5;
      m.setInputValue(\X, x);
      m.setInputValue(\Y, y);
      a.set(\x, x, \y, y);
      (1/60).wait;
    };
  }).play(AppClock);
};
)

~speedx= 0.02
~speedx= 0.4
~speedy= 0.05
~speedy= 0.5

r.stop;
a.free;
m.stop;
w.close;

In the above example frequency is mapped to video effect vertical movement, and panning to video effect horizontal movement.

update 200116: I found an older example that could play movie files and added it here as well. It will play back the smoothest if the movie file is compressed using a codec that compresses each frame individually. e.g. photoJPEG.

//macOS only
(
var x= 10, y= 10;
var width= 1280, height= 720;
var path= "somefolder/somefilm.mov";

var qtz= thisProcess.nowExecutingPath.dirname+/+"filmplayer2.qtz";
var win= Window("filmplayer", Rect(x, y, width, height), false, false);
var view= QuartzComposerView(win, Rect(0, 0, width, height));
view.path= qtz;
view.setInputValue('Movie_Location', path);
view.start;
view.front;
win.front;
CmdPeriod.doOnce({view.close});
)
//macOS only
(
var qtz= thisProcess.nowExecutingPath.dirname+/+"filmplayer2.qtz";
var win= Window("filmplayer2", Rect(100, 100, 640, 260));
var readButton= Button(win, Rect(10, 10, 100, 30)).states_([["read"]]);
var pathText= StaticText(win, Rect(10, 50, 300, 30));
var playButton= Button(win, Rect(10, 100, 100, 30)).states_([["enable"], ["disable"]]);
var durText= StaticText(win, Rect(10, 130, 300, 30)).string_("Dur:");
var posText= StaticText(win, Rect(10, 150, 300, 30)).string_("Pos:");
var qc= QuartzComposerView(win, Rect(310, 10, 320, 240));
qc.path= qtz;
readButton.action= {
  playButton.valueAction= 0;
  Dialog.openPanel({|path|
    pathText.string= path;
    qc.setInputValue('Movie_Location', path);
    {
      playButton.valueAction= 1;
      durText.string= "Dur:"+qc.getOutputValue('Movie_Duration');
    }.defer(0.5);
  });
};
playButton.action= {|view|
  qc.setInputValue('Enable', view.value);
};
Routine({
  var lastPos;
  inf.do{
    var pos= qc.getOutputValue('Movie_Position');
    if(pos!=lastPos, {
      posText.string= "Pos:"+pos;
    });
    (1/25).wait;
  };
}).play(AppClock);
qc.front;
win.front;
CmdPeriod.doOnce({win.close});
)

extMultiSliderView

2014-09-29 00:34 supercollider

clean-up: #36

In November 2011 there was some discussion on the sc-users mailinglist about a multislider point scroll similar to the one in MaxMSPJitter. There were many nice suggestions in the thread, but as so often nothing got decided/implemented. Below is my suggestion - a simple FIFO.

extMultiSliderView

Here is the class extension...

//for SC 3.7 or newer
//copy and save as extMultiSliderView.sc in extensions folder
+MultiSliderView {
  add {|val|
    this.value= this.value.copyRange(1, this.value.size-1)++val;
  }
  addFirst {|val|
    this.value= val.asArray++this.value.copyRange(0, this.value.size-2);
  }
}

Note that if you're using an old version of SuperCollider (<= 3.6) the class extension(s) need to look like this...

//for SC 3.6 or earlier
//copy and save as extSCMultiSliderView.sc in extensions folder
+SCMultiSliderView {
  add {|val|
    this.value= this.value.copyRange(1, this.value.size-1)++val;
  }
  addFirst {|val|
    this.value= val.asArray++this.value.copyRange(0, this.value.size-2);
  }
}
+QMultiSliderView {
  add {|val|
    this.value= this.value.copyRange(1, this.value.size-1)++val;
  }
  addFirst {|val|
    this.value= val.asArray++this.value.copyRange(0, this.value.size-2);
  }
}

And here are the examples from the video...

//--basic example
(
var n= 100;  //number of sliders
var w= Window("scroll test").front;
var a= MultiSliderView(w, Rect(10, 10, w.bounds.width-20, w.bounds.height-20));
a.elasticMode= 1;
a.value= Array.fill(n, {0});
Routine({
  inf.do{|i|
    0.05.wait;  //scroll speed
    a.addFirst(sin(i/10)*0.5+0.5);  //try .add instead of .addFirst
  };
}).play(AppClock);
CmdPeriod.doOnce({w.close});
)

//--example with sound input
(
s.waitForBoot{
  var n= 100;  //number of sliders
  var w= Window("scroll test audio").front;
  var a= MultiSliderView(w, Rect(0, 0, w.bounds.width, w.bounds.height));
  var b= {
    var src= SoundIn.ar;  //mic input
    SendReply.kr(Impulse.kr(60), '/trk', Amplitude.kr(src, 0.1, 0.1));
    DC.ar(0);  //silence
  }.play;
  a.elasticMode= 1;
  a.valueThumbSize= 8;
  a.indexThumbSize= 1;
  a.drawLines= true;
  a.value= Array.fill(n, {0});
  OSCFunc({|msg| {a.addFirst(msg[3])}.defer}, '/trk');
  CmdPeriod.doOnce({w.close});
};
)

Also, see Marije's SWPloterMonitor from her same day clean-up.


Linear Feedback Shift Register

2014-09-27 20:16 supercollider

clean-up: #35

Over a year ago I was playing with LFSR to generate pseudo-random noise. Below I cleaned up and published the results.

LFSR

With inspiration from Sebastian Tomczak's lfsr-in-maxmsp and this video.

//4-bit Fibonacci LFSR
//https://en.wikipedia.org/wiki/Linear_feedback_shift_register
(
b= 2r1000;  //seed
a= [];
15.do{
  a= a++b.asBinaryDigits(4);
  b= (b>>1)|((b&1).bitXor(b&2>>1)<<3);
  b.postln;
};
"";
)

s.boot
c= Buffer.loadCollection(s, a)
c.plot
{PlayBuf.ar(1, c, MouseX.kr(0, 1), loop:1)!2}.play




//4-bit Fibonacci LFSR with local in/out
(
a= {|rate= 4, iseed= 2r1000|
  var b, trig;
  var in= LocalIn.kr(1, iseed);
  trig= Impulse.kr(rate);
  b= Latch.kr(in, trig);  //read
  b= (b>>1)|((b&1).bitXor(b&2>>1)<<3);  //modify
  LocalOut.kr(b);
  b.poll(trig);
  DC.ar(0);
}.play;
)
a.set(\rate, 30)
a.free




//4-bit Fibonacci LFSR as single sample feedback with Dbufrd/Dbufwr
(
a= {|rate= 4, iseed= 2r1000|
  var b, trig;
  var buf= LocalBuf(1);
  buf.set(iseed);
  trig= Impulse.ar(rate);
  b= Demand.ar(trig, 0, Dbufrd(buf));  //read
  b= (b>>1)|((b&1).bitXor(b&2>>1)<<3);  //modify
  Demand.ar(trig, 0, Dbufwr(b, buf));  //write
  b.poll(trig);
  DC.ar(0);
}.play;
)
a.set(\rate, 30)
a.free




//LFSR sound example (not sure this is correct but sounds ok)
(
a= {|rate= 400, iseed= 2r1000, tap1= 1, tap2= 3, tap3= 5, length= 16|
  var l, b, trig, o;
  var buf= LocalBuf(1);
  buf.set(iseed);
  trig= Impulse.ar(rate);
  l= Demand.ar(trig, 0, Dbufrd(buf));  //read
  b= l.bitXor(l>>tap1).bitXor(l>>tap2).bitXor(l>>tap3)&1;  //modify
  l= (l>>1)|(b<<15);  //lfsr
  Demand.ar(trig, 0, Dbufwr(l, buf));  //write
  o= PulseCount.ar(Impulse.ar(rate*length), trig);  //bits
  l>>o&1!2;  //output
}.play;
)

a.set(\rate, 300)
a.set(\rate, 100)
a.set(\length, 3)
a.set(\length, 14)
a.set(\tap1, 14.rand)
a.set(\tap2, 14.rand)
a.set(\tap3, 14.rand)
a.set(\length, 32)
a.set(\rate, 1000)
a.free




//LFSR sound with GUI (not sure this is correct but sounds ok)
(
var w, a;
w= Window("lfsr", Rect(100, 100, 520, 200)).front;
Slider(w, Rect(10, 10, 500, 25)).action_({|view| a.set(\rate, view.value*2000)}).value= 400/2000;
Slider(w, Rect(10, 40, 500, 25)).action_({|view| a.set(\length, view.value*32)}).value= 16/32;
a= {|rate= 400, iseed= 2r1000, tap1= 1, tap2= 3, tap3= 5, length= 16|
  var l, b, trig, o;
  var buf= LocalBuf(1);
  buf.set(iseed);
  trig= Impulse.ar(rate);
  l= Demand.ar(trig, 0, Dbufrd(buf));  //read
  b= l.bitXor(l>>tap1).bitXor(l>>tap2).bitXor(l>>tap3)&1;  //modify
  l= (l>>1)|(b<<15);  //lfsr
  Demand.ar(trig, 0, Dbufwr(l, buf));  //write
  o= PulseCount.ar(Impulse.ar(rate*length), trig);  //bits
  l>>o&1!2;  //output
}.play;
CmdPeriod.doOnce({w.close});
)

redAscii

2014-09-27 01:23 supercollider

clean-up: #34

About half a year ago I wrote a few silly SuperCollider classes that show sound as ASCII graphics. There are two types of level meters, a spectrogram and a stethoscope.

Only tested under SC-IDE (SC 3.6 or later).

redAscii

Attachments:


supercolliderArduino_sendAndReceive

2014-09-26 01:34 supercollider

clean-up: #33

Below is some SuperCollider code for sending and receiving data from an Arduino. It is a good example I think and worth sharing. It can be used as a template for when you want to read and write at the same time.

As the example is written SuperCollider writes data to six PWM channels (0-255) and reads data from six analogue inputs (also 0-255). The sliders are the PWM output controllers, and the sensor input data is just posted as an array.

//supercolliderArduino_sendAndReceive /f0
//  controlling 6 PWM channels and reading 6 analogue inputs at the same time
//  use with SC code supercolliderArduino_sendAndReceive.scd

//--serial protocol
// PWM SC->Arduino: 10 nn nn nn nn nn nn 11 (nn bytes 1-6= PWM channels 0-5)
// ADC Arduino->SC: 20 nn nn nn nn nn nn 21 (nn bytes 1-6= analogue channels 0-5)

//--settings
byte pwmPins[]= {5, 3, 9, 6, 11, 10};
byte adcPins[]= {0, 1, 2, 3, 4, 5};  //A0-A5

//--variables
boolean changed;
byte val, cnt= 0;
byte pwmData[]= {0, 0, 0, 0, 0, 0, 0, 0};
byte adcData[]= {0, 0, 0, 0, 0, 0, 0, 0};  //for detecting change and filter out repetitions

void setup() {
  Serial.begin(38400);  //baud rate must match in SC
  for(byte i= 0; i<6; i++) {
    pinMode(pwmPins[i], OUTPUT);
    pinMode(adcPins[i], INPUT);
  }
}
void loop() {

  //--from SC
  while(Serial.available()) {
    val= Serial.read();
    if(cnt==0) {
      if(val==10) {  //beginning of message found
        cnt= 1;  //start counter
      }
    } else if(cnt<7) {  //between 1 and 6 means message started and that bytes should be saved
      pwmData[cnt-1]= val;  //saving incoming bytes in temporary data array
      cnt++;
    } else {
      if(val==11) {
        for(byte i= 0; i<6; i++) {
          analogWrite(pwmPins[i], pwmData[i]);  //output read message to PWM pins
        }
      } else {
        //serial read error
      }
      cnt= 0;  //reset byte counter
    }
  }

  //--to SC
  changed= false;
  for(byte i= 0; i<6; i++) {
    val= analogRead(adcPins[i])>>4;  //scale from 10 to 8 bits
    if(val!=adcData[i]) {
      adcData[i]= val;
      changed= true;
    }
  }
  if(changed) {  //check if any sensor changed
    Serial.write(20);
    Serial.write(adcData[0]);
    Serial.write(adcData[1]);
    Serial.write(adcData[2]);
    Serial.write(adcData[3]);
    Serial.write(adcData[4]);
    Serial.write(adcData[5]);
    Serial.write(21);
  }
  delay(10);  //wait 10 milliseconds
}
//--use with supercolliderArduino_sendAndReceive.ino
//SuperCollider code for reading 6 analogue sensors and sending out to 6 PWM channels

SerialPort.listDevices;  //run this and see post window for the name of serial device

//--GUI code for sending 6 PWM
(
var name= "/dev/tty.usbserial-A101NB76";  //edit to match your serial device
var port= SerialPort(name, 38400, crtscts: true);
var pwm= [10, 0, 0, 0, 0, 0, 0, 11];
var win= Window("pwm", Rect(99, 99, 260, 200), false);
Slider(win, Rect(10, 10, 30, 170)).action_{|view| pwm.put(1, (view.value*255).asInteger); port.putAll(pwm.postln)};
Slider(win, Rect(50, 10, 30, 170)).action_{|view| pwm.put(2, (view.value*255).asInteger); port.putAll(pwm.postln)};
Slider(win, Rect(90, 10, 30, 170)).action_{|view| pwm.put(3, (view.value*255).asInteger); port.putAll(pwm.postln)};
Slider(win, Rect(130, 10, 30, 170)).action_{|view| pwm.put(4, (view.value*255).asInteger); port.putAll(pwm.postln)};
Slider(win, Rect(170, 10, 30, 170)).action_{|view| pwm.put(5, (view.value*255).asInteger); port.putAll(pwm.postln)};
Slider(win, Rect(210, 10, 30, 170)).action_{|view| pwm.put(6, (view.value*255).asInteger); port.putAll(pwm.postln)};
win.front;
CmdPeriod.doOnce({port.putAll([10, 0, 0, 0, 0, 0, 0, 11]); port.close; win.close});
//press cmd+. to stop and close window and serial port

s.waitForBoot{
  var byte, num= 6, index= 0, data= Array.newClear(num);
  Ndef(\arduino, {|data= #[0, 0, 0, 0, 0, 0]| Splay.ar(SinOsc.ar(data.lag(0.02)+500, 0, 0.4))}).play;  //temp sound
  Routine.run({
    inf.do{
      while({byte= port.read; byte.notNil}, {
        //byte.postln;  //debug
        if(index==0 and:{byte==20}, {  //check if first byte is 20
          index= 1;
        }, {
          if(index>=1 and:{index<(num+1)}, {  //ok, now start collecting bytes
            data[index-1]= byte;
            index= index+1;
          }, {
            if(index==(num+1) and:{byte==21}, {  //until last data byte
              data.postln;  //debug
              Ndef(\arduino).setn(\data, data);
              index= 0;  //done. reset index to prepare for new message
            }, {
              //something broke or beginning - restart
              "restart".postln;  //debug
              index= 0;
            });
          });
        });
      });
    };
  });
};
)

I also attached the complete example as a zip file.


SC2 Nostalgia

2014-09-24 23:58 supercollider

clean-up #32:

SuperCollider 2 (SC2) was such a great piece of software. Thank you, James McCartney. I plan to go back one day and I already have two old mac ti-books that still boot OS9 natively. I also have a lot of patches for max/nato that I still think produces great graphics/video under OS9 - so that's another reason for 'downgrading'.

Below is one of my first posts to sc-users. I gathered some courage and sent this mail in August 2001.

(Here is the full thread.) If you play the below sound in the newer SuperCollider versions it doesn't sound nearly as good (in my ears). To get a few steps closer to that SC2 sound, try replacing the RHPF with GlitchRHPF from SC3-plugins. Still, it isn't the same. Listen to the attached MP3 recorded with SC2 to hear these five lines of code go wild in SC2.

------------------------------

Date: Thu, 16 Aug 2001 21:12:27 +0200
From: Fredrik Olofsson <---@---.--->
Subject: noisy instr.

hello

this works... (sc 2.2.11)

({
	RHPF.ar(
		BrownNoise.ar([1,1]),
		LFSaw.ar(1,0.99,2),
		0.24).clip2(0.5)
}.scope
)


this doesn't...

({
	RHPF.ar(
		BrownNoise.ar([1,1]),
		LFSaw.ar(1,0.99,1),		//note add difference
		0.24).clip2(0.5)
}.scope
)

how come the latter example runs fine for about 40 seconds and then 
both channels 'get stuck' at +1, one after the other?  i've had the 
former playing for at least 9 min.

confused/fredrik

Attachments:


Nato to Jitter Bridge Object - For Peace and Understanding

2014-09-24 08:00 other

clean-up #31:

31jan 2003 I released two MaxMSP objects that could convert video from Jitter to nato and back. Jitter is the official library for doing video and graphics in MaxMSP. But before that was nato, or more precisely Nato.0+55+3d. Nato was a totally amazing piece of software and with it you could do similar things as with Jitter. It was mac OS9 only but super poetic and unique in all kinds of ways. I used it a lot for my live video works (klipp av tours, VJ-ing etc) and played my last gig using it as late as 2007 (at club Maria in Berlin). I still miss playing with it.

The binary OS9 objects themselves have been available for download since 2003, but I never released the C source code.

As I wasn't so good with C back then, it took weeks for me to reverse engineer the nato video format. Lots of trial-and-error using Metrowerk's CodeWarrior.

Releasing the objects to the public was scary. I risked losing my software license. n.n., the author of nato, was notorious for humiliating and threatening nato users online. And even terminating licenses if one said the wrong thing in public. One of the replies I got after the release was "!m go!ng 2 eat u". See plot.bek.no/pipermail/55/2003-March.txt. And another one was plot.bek.no/pipermail/55/2003-June/005486.html. Luckily my license didn't stop working.

Attachments:


« 11 / 30 »