«first  …3 4 5 6 7 8 9 last»

MP3 Speakers

2016-08-29 13:33 electronics

For an upcoming installation, we need a few speakers hanging on the walls playing back sound files in a loop. I found the DFPlayer Mini to work well. It's a small and cheap board that can play sound files from an SD card and it has a built-in 3W amplifier. With a decent 5V power bank (here 4400mAh) it can run for ~4 days continuously.

mp3speakers


Example Class

2016-05-04 23:30 supercollider

Here is a very basic SuperCollider class I wrote as an example of why and how to write classes.

//save this as MySequencerTrack.sc in your extensions folder and recompile
MySequencerTrack {
    var <steps;
    var <>array;
    *new {|steps= 16|
        ^super.newCopyArgs(steps).init;
    }
    init {
        array= Array.fill(4, {Array.fill(steps, 0)});  //4 here because of the four params: amp, freq, mod, pan
    }

    //array get/set
    amps {^array[0]}
    amps_ {|arr| array[0]= arr}
    freqs {^array[1]}
    freqs_ {|arr| array[1]= arr}
    mods {^array[2]}
    mods_ {|arr| array[2]= arr}
    pans {^array[3]}
    pans_ {|arr| array[3]= arr}

    //single value get/set
    amp {|index| ^array[0][index]}
    amp_ {|index, val| array[0].put(index, val)}
    freq {|index| ^array[1][index]}
    freq_ {|index, val| array[1].put(index, val)}
    mod {|index| ^array[2][index]}
    mod_ {|index, val| array[2].put(index, val)}
    pan {|index| ^array[3][index]}
    pan_ {|index, val| array[3].put(index, val)}
}

And here is some test code for it...

a= MySequencerTrack.new;
a.freqs
a.steps
a.amps= {1.0.rand}!a.steps
a.amps
a.amp(10)  //first one
//and then the same for a.freqs etc

(
s.waitForBoot{
    a.amps= {[0.5, 0.25, 0, 0, 0].choose}!a.steps;
    a.freqs= {[60, 66, 70].choose.midicps}!a.steps;
    a.mods= {1.0.linrand}!a.steps;
    a.pans= {1.0.rand2}!a.steps;
    b= {|freq= 400, amp= 0, mod= 0, pan= 0| Pan2.ar(SinOsc.ar(freq, SinOsc.ar*mod, amp), pan)}.play;
    s.sync;
    r= Routine.run({
        inf.do{
            a.steps.do{|i|
                b.set(\freq, a.freq(i), \amp, a.amp(i), \mod, a.mod(i), \pan, a.pan(i));
                0.125.wait;
            };
        };
    });
};
)

//and while it is running...  replace freqs
a.freqs= {[52, 66, 70, 80].choose.midicps}!a.steps;
a.amp_(0, 1)  //set first amp to 1.0
a.amps
a.amps= a.amps.rotate(-1)  //rotate amps left
a.freqs= a.freqs.rotate(2) //rotate freqs right
a.freqs= a.freqs+10  //transpose up
a.freqs= a.freqs.scramble //reorder

r.stop
b.free

//now the important thing and why classes are good...
//here we make 10 tracks all 32 values in length...
~mysequencer= {MySequencerTrack(32)}!10;
~mysequencer[0].amps  //amplitudes for first track
~mysequencer[0].amps= {1.0.rand}!a.steps
~mysequencer[0].amps
~mysequencer[0].amp(0)  //first one

(
s.waitForBoot{
    10.do{|i|
        var steps= ~mysequencer[i].steps;
        ~mysequencer[i].amps= {[0.5, 0.25, 0, 0, 0, 0, 0, 0].choose}!steps;
        ~mysequencer[i].freqs= {[60, 66, 70, 90].choose.midicps}!steps;
        ~mysequencer[i].mods= {1.0.linrand}!steps;
        ~mysequencer[i].pans= {1.0.rand2}!steps;
    };
    ~synths= {
        {|freq= 400, amp= 0, mod= 0, pan= 0| Pan2.ar(SinOsc.ar(freq, SinOsc.ar*mod, amp/2), pan)}.play;
    }!10;
    s.sync;
    r= ~mysequencer.collect{|trk, i|
        var syn= ~synths[i];
        Routine.run({
            inf.do{|j|
                var x= j%trk.steps;
                syn.set(\freq, trk.freq(x), \amp, trk.amp(x), \mod, trk.mod(x), \pan, trk.pan(x));
                0.125.wait;
            };
        });
    };
};
)

//while the above is running
~mysequencer[0..7].do{|trk| trk.amps= 0!32}  //mute all tracks except 8&9
~mysequencer[8..9].do{|trk| trk.mods= {4.0.linrand}!32}  //more fmod on tracks 8&9
~mysequencer[0..1].do{|trk, i| trk.freqs= {i+1*150}!32; trk.mods= {0}!32; trk.amps= {|j| [0, 1].wrapAt(i+j)}!32} //renew and add tracks 0&1
~mysequencer[8..9].do{|trk| trk.amps= {0.75.linrand*[1, 0].choose}!32}  //new amps for track 8&9
~mysequencer[8..9].do{|trk| trk.freqs= {8.linrand+1*150}!32}  //new freqs for track 8&9
~mysequencer[6].amps= {0.4}!9++({0}!23); ~mysequencer[6].freqs= {|i| 2**i*50+50}!32; //add arpeggio on track 6

~mysequencer.do{|x| x.freqs= x.freqs*1.1} //transpose all frequencies
~mysequencer.do{|x| x.amps= x.amps.rotate(-3)} //rotate all amps

r.do{|x| x.stop}
~synths.do{|x| x.free}

ESP8266 OpenSound Control Teensy

2016-04-30 17:29 electronics

Today I ported my Open Sound Control ESP8266 example for Arduino to run on a Teensy 3.

The version below is a bit simpler but still works the same. It is just to show how to send and receive OSC messages directly in SuperCollider or MaxMSPJitter.

Teensy code:

//f0 150705 - modified for Teensy3 160430
//sending and receiving UDP OSC with an ESP8266
//for Teensy + ESP8266 with firmware 0.9.5.2
#define WLAN_SSID  "ssid"
#define WLAN_PASS  "pass"
#define WLAN_ADDR  "192.168.1.3" //laptop running SC EDIT
#define ADDR "/tap" //incoming OSC addy
#define PORT  1112  //incoming OSC port
uint8_t buf[16];
char indata[12];
char inbuffer[256];
char OKrn[] = "OK\r\n";
byte wait_for_esp_response(int timeout, char* term = OKrn) {
  unsigned long t = millis();
  bool found = false;
  int i = 0;
  int len = strlen(term);
  while (millis() < (t + timeout)) {
    if (Serial1.available()) {
      inbuffer[i++] = Serial1.read();
      if (i >= len) {
        if (strncmp(inbuffer + i - len, term, len) == 0) {
          found = true;
          break;
        }
      }
    }
  }
  inbuffer[i] = 0;
  return found;
}
void setup() {
  //--OSC message
  buf[0] = 47;   // /
  buf[1] = 115;  // s
  buf[2] = 116;  // t
  buf[3] = 105;  // i
  buf[4] = 0;
  buf[5] = 0;
  buf[6] = 0;
  buf[7] = 0;
  buf[8] = 44;   // ,
  buf[9] = 105;  // i
  buf[10] = 0;
  buf[11] = 0;
  buf[12] = 4;   // a
  buf[13] = 3;   // b
  buf[14] = 2;   // c
  buf[15] = 0;   // d
  pinMode(23, OUTPUT);
  Serial.begin(115200);   //USB serial for feedback
  delay(400);
  Serial1.begin(115200);  //Teensy hardware pins 0 and 1
  Serial.println("starting");
  Serial.print("hard reset...");
  pinMode(4, OUTPUT);
  delay(10);
  pinMode(4, INPUT);
  Serial.print("ready...");
  boolean resp = wait_for_esp_response(1000, "ready\r\n");
  Serial.println(resp);
  Serial.print("mode1...");
  Serial1.println("AT+CWMODE=1");
  resp = wait_for_esp_response(1000);
  Serial.println(resp);
  Serial.print("connecting...");
  do {
    Serial1.print("AT+CWJAP=\"");
    Serial1.print(WLAN_SSID);
    Serial1.print("\",\"");
    Serial1.print(WLAN_PASS);
    Serial1.println("\"");
    resp = wait_for_esp_response(3000);
    Serial.print(resp);
  } while (!resp);
  Serial.print("\nmux1...");
  Serial1.println("AT+CIPMUX=1");
  resp = wait_for_esp_response(1000);
  Serial.println(resp);
  Serial.print("udp...");
  Serial1.print("AT+CIPSTART=4,\"UDP\",\"");
  Serial1.print(WLAN_ADDR);
  Serial1.print("\",57120,");
  Serial1.print(PORT);
  Serial1.println(",0");
  resp = wait_for_esp_response(1000);
  Serial.println(resp);
  Serial.println("setup done");
}
void loop() {
  if (wait_for_esp_response(1000, "\r\n+IPD,4,16:")) {
    if (wait_for_esp_response(1000, ADDR)) {
      Serial1.readBytes(indata, 12);
      buf[12] = indata[8] + 1; //add one to incomming values
      buf[13] = indata[9] + 1;
      buf[14] = indata[10] + 1;
      buf[15] = indata[11] + 1;
      Serial.println(int(indata[8]));
      Serial.println(int(indata[9]));
      Serial.println(int(indata[10]));
      Serial.println(int(indata[11]));
      Serial1.println("AT+CIPSEND=4,16");
      if (wait_for_esp_response(1000, "> ")) {
        Serial1.write(buf, sizeof(buf));
        if (wait_for_esp_response(1000)) {
          Serial.println("reply sent!");
        }
      }
    }
  }
}

SuperCollider code:

(
//--call&response
var send= 0, last= Main.elapsedTime;
OSCdef(\sti, {|msg, time, addr|
    //should receive the values you sent +1
    ([msg[1]>>24, (msg[1]>>16)&255, (msg[1]>>8)&255, msg[1]&255]).post;
    (" % sec since last: %, % sec since sent").format(addr, time-last, time-send).postln;
    last= time;
}, \sti);
n= NetAddr("192.168.1.4", 1112); //esp8266 ip address EDIT
f= {|id, on, hi, lo| (id&255<<24)|(on&255<<16)|(hi&255<<8)|(lo&255)};
r= Routine.run({
    inf.do{|i|
        n.sendMsg(\tap, f.value(4, 3, i.asInteger%256, 1));
        send= Main.elapsedTime;
        0.5.wait;
    };
});
)

Note: my new and better way to do this is described in this post: /f0blog/f0led/

Updates:

  • 180212: removed an unneeded wait for response. Thanks Niklas!
Attachments:
teensy_esp8266_osc.maxpat.zip

GreenPeace

2016-03-29 15:35 supercollider

Here's a handy class for SuperCollider. It's an audio clipping detector loosely based on Batuhan Bozkurt's StageLimiter.

To install it download and extract the zip file into your SuperCollider extensions folder. Then recompile and type...

GreenPeace.activate

Now as soon as you play a sound that's clipping (i.e. exceeds -1.0 or 1.0), the class will warn you.

To turn it off type...

GreenPeace.deactivate
Attachments:
GreenPeace.zip

Joule Thieves

2016-03-25 18:26 electronics

To make something useful out of 'dead' batteries I've been building Joule thief circuits. These circuits are very easy, cheap and fun to build plus it gives me a little bit less bad conscious when going to the recycling bin with the batteries. Watch this BigClive video to learn more.

Below are pictures of one variant. It has a small 3mm green led (salvaged from a broken printer), a hand-wound coil, a resistor and a transistor.

joulethief photo 0

joulethief photo 1

The batteries here came with my first ever (analogue) multimeter. They are 32 years old!. See the date code: 84-04. They still can drive the little led. Amazing. I think running this little green led is a good way to use the last energy stored in these beautiful and truly long-life batteries.

joulethief photo 2


Spiral

2015-12-29 00:30 supercollider, visuals

Just some hypnotic graphics...

The JavaScript code above is this...

<div style="background-color:black;">
<canvas id="canvas" width="800" height="600"></canvas>
<script>
(function() {
  let frameCount= 0;
  let can= document.getElementById('canvas');  //EDIT name of canvas to draw to
  function draw() {
    let bottom= can.getBoundingClientRect().bottom;
    if(bottom>=0 && bottom<=(innerHeight+can.height)) {  //only draw when visible in browser
      let ctx= can.getContext('2d');
      ctx.fillStyle= '#000';
      ctx.fillRect(0, 0, can.width, can.height);
      ctx.save();
      ctx.translate(can.width*0.5, can.height*0.5);
      ctx.fillStyle= '#FFF';
      ctx.beginPath();
      let theta= Math.sin(frameCount*0.001)*Math.PI*2*4;
      for(let y= 0; y<can.height; y++) {
        for(let i= 0; i<10; i++) {
          ctx.rotate(theta*0.001);
          ctx.fillRect((Math.sin(y*0.1+theta+(i*2))*100), y, 2, 2);
        }
      }
      ctx.restore();
      frameCount= frameCount+1;
    }
    window.requestAnimationFrame(draw);
  }
  window.requestAnimationFrame(draw);
})();
</script>
</div>

Originally this was a quick sketch made in Processing...

//spiral.pde - processing
void setup() {
  size(800, 600);
  noStroke();
}
void draw() {
  background(0);
  translate(width*0.5, height*0.5);
  float theta= sin(frameCount*0.001)*TWO_PI*4;
  for(int y= 0; y<height; y++) {
    for(int i= 0; i<10; i++) {
      rotate(theta*0.001);
      rect((sin(y*0.1+theta+(i*2))*100), y, 2, 2);
    }
  }
}

And then ported to SuperCollider...

//spiral.scd - supercollider
(
var width= 800, height= 600;
var win= Window("spiral", Rect(100, 100, width, height), false);
var usr= UserView(win, Rect(0, 0, width, height));
usr.background= Color.black;
usr.animate= true;
usr.drawFunc= {
    var theta= sin(usr.frame*0.001)*2pi*4;
    Pen.fillColor= Color.white;
    Pen.translate(width*0.5, height*0.5);
    height.do{|y|
        10.do{|i|
            Pen.rotate(theta*0.001);
            Pen.fillRect(Rect(sin(y*0.1+theta+(i*2))*100, y, 2, 2));
        };
    };
};
CmdPeriod.doOnce({win.close});
win.front;
)

Updates:


ESP8266 OpenSound Control

2015-07-05 22:29 electronics, supercollider

Here some example Arduino code for sending and receiving OSC via the cheap ESP8266 serial WiFi module.

Note that the Open Sound Control messages here are very basic - only 4 bytes packed into a single 32bit integer.

  • upload the code below to an Arduino.
  • connect ESP8266 TX pin to Arduino pin0.
  • connect ESP8299 RX to Arduino pin1. It is safest to use a 3V3 lever converter for this line (or at least a voltage divider).
  • power the ESP8266 (VCC and GND) from an external 3V source. Do not use the Arduino 3V3 pin as it cannot provide the required current. I used an LF33CV voltage regulator to get 3.3V from the 5V supply that also powers the Arduino.
  • connect ESP8288 RESET pin to Arduino pin4.
  • and last, connect ESP8266 CH_PD to 3V3

Optional: connect a separate USB-Serial (FTDI) chip to Arduino pins 2 and 3 to use software serial debugging. Start debugging in a terminal with something like screen /dev/tty.usbserial-A4015TKA 115200

The Arduino code sits and waits for an incoming OSC message (/tap). It then replies by sending out a counter-message (/sti).

//f0 150705
//sending and receiving UDP OSC with an ESP8266
//for an Arduino + ESP8266 with firmware 0.9.5.2

#include <SoftwareSerial.h>

#define WLAN_SSID  "SSID"
#define WLAN_PASS  "PASS"
#define WLAN_ADDR  "192.168.1.51" //laptop running sc
#define PORT  1112 //incoming OSC port
String tag = "/tap"; //incoming OSC addy

SoftwareSerial mySerial(2, 3);

uint8_t buf[16];
byte cnt;
byte id, on, hi, lo;
boolean resp;

void setup() {

  //--OSC message
  buf[0] = 47;   // /
  buf[1] = 115;  // s
  buf[2] = 116;  // t
  buf[3] = 105;  // i
  buf[4] = 0;
  buf[5] = 0;
  buf[6] = 0;
  buf[7] = 0;
  buf[8] = 44;   // ,
  buf[9] = 105;  // i
  buf[10] = 0;
  buf[11] = 0;
  buf[12] = 4;   // a high   (id)
  buf[13] = 3;   // a low    (on)
  buf[14] = 2;   // b high   (hi)
  buf[15] = 0;   // b low    (lo)

  mySerial.begin(115200);
  Serial.begin(115200);
  Serial.setTimeout(10000);
  mySerial.println("");
  mySerial.println("starting");

  mySerial.print("hard reset...");
  digitalWrite(4, 0);
  pinMode(4, OUTPUT);
  delay(10);
  pinMode(4, INPUT);
  resp = Serial.find("ready\r\n");
  mySerial.println(resp);

  mySerial.print("mode1...");
  Serial.println("AT+CWMODE=1");
  resp = Serial.find("OK\r\n");
  mySerial.println(resp);

  mySerial.print("connecting...");
  do {
    Serial.print("AT+CWJAP=\"");
    Serial.print(WLAN_SSID);
    Serial.print("\",\"");
    Serial.print(WLAN_PASS);
    Serial.println("\"");
    resp = Serial.find("OK\r\n");
    mySerial.println(resp);
  } while (!resp);

  mySerial.print("mux1...");
  Serial.println("AT+CIPMUX=1");
  resp = Serial.find("OK\r\n");
  mySerial.println(resp);

  mySerial.print("udp...");
  Serial.print("AT+CIPSTART=4,\"UDP\",\"");
  Serial.print(WLAN_ADDR);
  Serial.print("\",57120,");
  Serial.print(PORT);
  Serial.println(",0");
  resp = Serial.find("OK\r\n");
  mySerial.println(resp);

  Serial.setTimeout(1000);
}
void loop() {
  while (Serial.available()) {
    String abc = Serial.readStringUntil('\n');
    if (abc.startsWith("+IPD,4,16:" + tag)) {
      id = abc[22];
      on = abc[23];
      hi = abc[24];
      lo = abc[25];
      mySerial.print("id:");
      mySerial.println(id);
      mySerial.print("on:");
      mySerial.println(on);
      mySerial.print("hi:");
      mySerial.println(hi);
      mySerial.print("lo:");
      mySerial.println(lo);

      buf[15] = cnt++;
      Serial.println("AT+CIPSEND=4,16");
      Serial.find(">");
      Serial.write(buf, sizeof(buf));
      resp = Serial.find("OK\r\n");
      mySerial.print("send...");
      mySerial.println(resp);
    }
  }
}

SuperCollider test code:

(
//--call&response
var last= Main.elapsedTime;
OSCFunc({|msg, time, addr|
    [\id, msg[1]>>24, \on, (msg[1]>>16)&255, \hi, (msg[1]>>8)&255, \lo, msg[1]&255, time-last, addr].postln;
    last= time;
}, \sti);
n= NetAddr("192.168.1.50", 1112);  //ESP8266 ip address
f= {|id, on, hi, lo| (id&255<<24)|(on&255<<16)|(hi&255<<8)|(lo&255)};
r= Routine.run({
    inf.do{|i|
        n.sendMsg(\tap, f.value(4, 1, i.asInteger>>8&255, i.asInteger%256));
        0.5.wait;
    };
});
)

Note: my new and better way to do this is described in this post: /f0blog/f0led/


Bass!

2015-05-26 00:15 electronics

For a project in collaboration with Stine Janvin Motland I built this 4-channel transducer bass shaker system.

The system has four transducers (Visaton bs 130, 4Ω), two class D stereo amplifiers (2x50W, TDA7492 chip) and a powerful ATX switching power supply (Codecom pm-350c).

I modified the power supply to only give out 12V (yellow&black cables) and also made it start up automatically by shorting the green cable (PS-ON) to ground (black).

transducers photo 1

transducers photo 2

There's no volume control so better take care - the system is very hot.


«first  …3 4 5 6 7 8 9 last»