Sound Bytes

A coding competition


Project maintained by coding-competitions Hosted on GitHub Pages — Theme by mattgraham

Raising scales

by pswg

Listen

Practicing the scales on a Karplus-Strong piano.

This is an example submission written in Python 3.

Note: this example uses a pseudo-random number generator for white noise.

main.py


import math, os, random, struct
rate = 44100
for k in range(9, 89):
  f = int(rate / (math.pow(2, (k - 49) / 12.0) * 440))
  a = [.9 * math.sin(2 * math.pi * x / rate) + .1 * random.random() for x in range(0, f)]
  for t in range(0, rate // 4):
    os.write(1, struct.pack("f", a[t % f]))
    a[t % f] = (a[t % f] + a[(t + 1) % f]) / 2 * 0.998

Language

Python

Golf Score

350

Parameters

Sample Rate: 44100 Hz
Sample Size: 32 bits
Channels: 1 (mono)
Encoding: floating-point

Commands

Run

python ./main.py

Spectrogram

Spectrogram