Skip to content

Instantly share code, notes, and snippets.

@thomassross
Forked from Eiyeron/LLT.c
Last active May 13, 2016 01:08
Show Gist options
  • Save thomassross/8c120e29e6bd0c8abcce to your computer and use it in GitHub Desktop.
Save thomassross/8c120e29e6bd0c8abcce to your computer and use it in GitHub Desktop.
Long Line Theory with the PulseAudio API. Not very nice/friendly code here.

Compile with:

g++ LongLineTheory.cpp -lpulse-simple -o LongLineTheory

Run with:

./LongLineTheory

Strip with:

strip -s LongLineTheory
#include <pulse/simple.h>
#include <math.h>
#include <stdio.h>
#define min(a, b) (a) < (b) ? (a) : (b)
#define max(a, b) (a) > (b) ? (a) : (b)
int main()
{
static const pa_sample_spec ss =
{
.format = PA_SAMPLE_U8,
.rate = 8000,
.channels = 1
};
int error;
double sb, y, h, a, d, g;
int backgroundWaveNotes[] = { 15, 15, 23, 8 };
double mainInstrumentNotes[2][16] = {
{ 15, 18, 17, 17, 17, 17, 999, 999, 22, 22, 999, 18, 999, 15,
20, 22 },
{ 20, 18, 17, 17, 10, 10, 999, 999, 20, 22, 20, 18, 17, 18, 17,
10 } };
pa_simple *s = pa_simple_new(NULL, "LongLineTheory", PA_STREAM_PLAYBACK, NULL, "LongLineTheory", &ss, NULL, NULL, &error);
for (int t = 0; ; t++)
{
sb = (t > 0xffff ? 1 : 0);
y = pow(2, backgroundWaveNotes[t >> 14 & 3] / 12.);
a = 1. - ((t & 0x7ff) / (double) 0x7ff);
d = (((int) 14. * t * t ^ t) & 0x7ff);
g = (double) (t & 0x7ff) / (double) 0x7ff;
g = 1. - (g * g);
h = pow(2.,
mainInstrumentNotes[((t >> 14 & 3) > 2 ? 1 : 0) & 1][t >> 10 & 15] / 12);
double wave = ((int) (y * t * 0.241) & 127 - 64)
+ ((int) (y * t * 0.25) & 127 - 64) * 1.2;
double drum = (
((int) (((int) (5. * t) & 0x7ff) * a) & 255 - 127)
* ((0x53232323 >> (t >> 11 & 31)) & 1) * a * 1.0
+ ((int) (d * a) & 255 - 128)
* ((0xa444c444 >> (t >> 11 & 31)) & 1) * a * 1.5 + ((int) ((a
* a * d * (t >> 9 & 1))) & 0xff - 0x80) * 0.1337)
* sb;
double instrument =
(((int) (h * t) & 31) + ((int) (h * t * 1.992) & 31)
+ ((int) (h * t * .497) & 31) + ((int) (h * t * 0.977) & 31))
* g * sb;
double a = max(min((wave + drum + instrument) / 3., 127), -128);
char b = a;
char buf[sizeof(b)];
sprintf(buf, "%c", b);
pa_simple_write(s, buf, sizeof(b), &error);
}
if (s)
{
pa_simple_free(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment