Thursday, September 1, 2016

Pleasant Random Jingle Generator

Beeping Computer

Way back during the time of the dinosaurs, circa 1975, when one turned on a desktop computer, it would go Beep!  That fell out of favour once Microsoft figured out how to make a computer take 3 minutes to boot up, before finally being able to emit a simple beep.   However, it is still common practice to test a new little embedded controller by flashing a LED.

Music vs Noise

Now for those tinkerers who are a little more adventurous:
How about pleasant sounding random noise? 

There are two things that help to make noise sound acceptable:
  • Use a tonal scale that everyone is used to.
  • Avoid obvious dissonance.

Scales

We could use a Pythagorian scale with 7 notes per octave and perfect harmony, but then it will sound weird - like a Scottish bag-pipe and I don't have enough Scottish genes in my ears to prevent them from bleeding.

The equal tempered (logarithmic) scale of Johan Bach (Das Wohltemperirte Clavier, 1722) ), with concert pitch (1939), is used in modern pianos and synthesizers.  Everyone in the western world is used to it - except maybe the Scots - and it is easy to calculate on the fly, using the formula:
  • fn = A * 12th root of 2 ** n
where A = 440 Hz for concert pitch.

Dissonance and Consonance

According to my namesake O'l Hermann von Helmholtz, maximum dissonance occurs when a beat between two tones is 33 Hz.  So avoid that and it should be less annoying.  This is effectively what is done in musical 'chords', which are designed for best consonance.


video

My old piano teacher will spin in her grave...

Here is a simple Arduino random jingle generator door bell where I tried to exercise the above rules.

// Teensy2 LED, Serial, Muzak
// Herman Oosthuysen 2016
// To enable the debug serial port:
// Go to Tools, Port and select cu.usbmodem12341

#include <math.h>

// A pleasant sounding random noise generator
// using the equally tempered scale and a simple test to reduce dissonance.

// Helmholtz: Maximum dissonance occurs when a beat = 33 Hz
// In a chord, one should watch the 2nd, 3rd and 5th harmonics also - most power

// Concert pitch: A4 = 440Hz (55, 110, 220, 440, 880, 1760...)
// CENT = 12th root of 2
// fn = A * CENT ** n

#define A1    55
#define CENT  1.059463094359
#define SPKR  8
#define LED   11
#define BAUD  9600

#define FMIN  200
#define FMAX  1200
#define TMIN  4
#define TMAX  8

int flsh = 0;
int tim = 0;
int fold = 0;
int fnew = 0;

void setup()  
{               
  Serial.begin(BAUD);
  pinMode(LED, OUTPUT);

  Serial.println("Teensy2, Muzak, eh.");
}

void loop()                    
{
  // A Pololu IRS05A proximity switch, makes it a funky door bell

  // or pet detector/terrorizer
  prox = analogRead(A0);  

  if (prox < 500)
  { 
    digitalWrite(LED, flsh);
    flsh ^= 1;

    // Helmholtz: Max dissonant if beat = 33 Hz
    // So avoid consecutive notes that are 'too close'
    // and since 42 is the answer to everything...
    while (abs(fnew - fold) < 42)
    {
      fnew = random(FMIN, FMAX);
    }

    tim = random(TMIN, TMAX);

    tone(SPKR, fnew);
    delay(1000/tim);
    noTone(SPKR);

    fold = fnew;

  }
}

// Bach's Equal Tempered frequency calculator
// 12 intonations per octave
// A1 = 55 Hz: n=1
// A4 = 440 Hz: n=12*4
int freq(int n)
{
  double t;

  t = A1 * pow(CENT, n);
 
  return (int)t;
}



Well, that actually sounds better than most of the stuff on Nights with Alice Cooper!

Sensors

The Arduinos are very easy to interface with little sensors.  In this example, I used a proximity switch to make it into a door bell of sorts.  I actually added it as a simple way to turn the silly thing on and off while experimenting.

Similarly, one could use a Sonar or IR Range sensor and modify the tune depending on the range of someone approaching your front door.  Sonar is sensitive to wind, so it may give false alarms if you use Sonar as the main detector, but you could aim it at a tree and listen to the wind sing.

It would also be fun to make a wacky proximity sensor Xilophone with Sonar tone or rhythm control, which could lead to children bouncing around your door playing - good for Halloween:  Twick or Tweet!

Pseudo Polyphonic

I can leave this toy running for a couple minutes, without getting annoyed by it - bored yes - but it isn't too grating on the ears, which was the whole intent of the exercise.

The weird thing is that while the program is obviously monophonic, it sounds somehow polyphonic, probably because the program also changes the metrum of the tones, which the brain then interprets as two or three melodies playing simultaneously.

I have not encountered anything in the literature describing this pseudo polyphonic effect.  Maybe it is indeed a new discovery.  It sounds monophonic when I slow it down only.

Music is not simple applied mathematics, it is psychological too.

If you are interested in computer generated music and want to be wowed beyond belief, then install a MIDI plugin in your browser and go the Wolfram Tones web site.  Dr Wolfram, is the creator of Mathematica - a real genius.  His music generator is based on Cellular Automata.  Others have used Fractals to much the same effect.

Elevator Muzak

Please just don't install this muzak generator in a 100 floor elevator, eh...

Have fun,

Herman

No comments:

Post a Comment

On topic comments are welcome. Junk will be deleted.