Pedro Gimeno's gists (pastes with history), each in an independent (orphan) branch

Pedro Gimeno bc61acf72d hace 3 años
README.md bc61acf72d hace 3 años
output.wav bc61acf72d hace 3 años
program.c bc61acf72d hace 3 años

README.md

Algorithmic Symphony at 16 bits, 44.1 kHz

This one-line piece from "Algorithmic Symphonies" is quite famous:

main(t){for(t=0;;t++)putchar(t*((t>>12|t>>8)&63&t>>4));}

It generates a raw audio stream that needs to be reproduced in 8 bits at an 8 kHz sample rate. It can be heard in this video: https://youtube.com/watch?v=GtQdIYUtAHg

An easy way to play it is to use SoX, with this command line:

./program | sox -t raw -r 8000 -b 8 -c 1 -e unsigned-integer -B - -d

(note: the syntax ./program applies to POSIX systems; adjust appropriately for other OSes)

I wondered how it would sound if it was not limited to that poor sample rate and bit depth, even if at the cost of adding verbosity. And came up with this program:

#include <stdio.h>
int main()
{
  double t;
  for (t = 0; ; t += .18140589569160998)
  {
    unsigned sample;
    unsigned mul = ((unsigned)(t*4096)|(unsigned)(t*65536)) & (63*16777216)
      & (unsigned)(t*1048576);

    sample = (unsigned)((mul * t) / 65536);
    putchar((sample >> 8) & 0xFF);
    putchar(sample & 0xFF);
  }
  return 0;
}

The command line that can be used to play it is the same, except for the bit depth and sample rate:

./program | sox -t raw -r 441000 -b 16 -c 1 -e unsigned-integer -B - -d

This repository contains a .wav file with about 32.7 seconds, which is the period of the piece. It can be generated with:

./program | sox -t raw -r 44100 -b 16 -c 1 -e unsigned-integer -B - output.wav trim 0 32.768