Skip to content

Instantly share code, notes, and snippets.

@JeffreyBPetersen
Created August 17, 2016 01:34
Show Gist options
  • Save JeffreyBPetersen/2f23bfe11182e5c39ea13a52b6fa2e57 to your computer and use it in GitHub Desktop.
Save JeffreyBPetersen/2f23bfe11182e5c39ea13a52b6fa2e57 to your computer and use it in GitHub Desktop.
A rudimentary binary clock sonification
let config = {
ticksPerSecond: 2.4,
volume: 0.02
};
let rawNote = steps => 440 * Math.pow(Math.pow(2, 1/12), steps);
let sqr = (time, freq) => Math.floor(time * freq % 2) * 2 - 1;
let sin = (time, freq) => Math.sin(time * freq * Math.PI * 2);
let roundlog = num => {
let power = 0;
while(num > 0 && num % 2 === 0){
power++;
num /= 2;
}
return power;
};
export function dsp(t) {
return config.volume * sqr(t, rawNote(-24 + 2 * roundlog(Math.floor(t*config.ticksPerSecond))));
}
@JeffreyBPetersen
Copy link
Author

Use wavepot.com to play.

One thing I wish I knew is how to easily transition smoothly between frequencies, which becomes an issue when using sine waves for example. If you try playing sine waves then you'll often get clicking sounds as the frequency changes. The problem is much less noticeable when using square waves.

Thinking about possible solutions makes me curious to experiment with bijective wave functions made to take their most recent sample as input. Creating a smooth sine wave frequency transition with that setup would require first making a sine wave by multiplying half of a regular sine wave with a square wave as a full sine wave isn't bijective. From there it would be easy to feed the generated samples seamlessly into a half-sine * square wave combo with a different frequency. The drawback of this approach is potentially having to develop a lot of other logic from scratch in order to accommodate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment