Compare commits

...

4 Commits

7 changed files with 253 additions and 60 deletions

View File

@ -2,6 +2,7 @@
#include "ampler.h"
#include "random.c"
#include "audio.c"
#include "draw.c"
@ -41,8 +42,9 @@ Ampler_state *init() {
// TODO: iterate a directory to load samples
//load_sample(state, "./mix.wav");
//load_sample(state, "./haunted.wav");
//load_sample(state, "./loop2.wav");
//load_sample(state, "./loop.wav");
load_sample(state, "./loop.wav");
//load_sample(state, "./chord.wav");
puts("init.");

View File

@ -13,6 +13,7 @@ typedef int32_t s32;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef float f32;
typedef double f64;
@ -37,8 +38,12 @@ for ; N##_i < arraylen(ARRAY); N##_i += 1, N = &((ARRAY)[N##_i]) do
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define SAMPLE_RATE (44100)
#define CHANNELS (2)
#define CHANNELS (1)
#define FRAME_SAMPLES (SAMPLE_RATE / 60)
#define GRAIN_SAMPLES (SAMPLE_RATE / 36)
#define GRID_WIDTH (36)
#define GRID_HEIGHT (36)
typedef struct Sound_src Sound_src;
struct Sound_src {
@ -53,6 +58,15 @@ struct Sound_src {
enum { SND_FREE = 0, SND_STOPPED, SND_PLAYING, SND_LOOPING, } state;
};
typedef struct Grain Grain;
struct Grain {
s8 samples[GRAIN_SAMPLES];
// not sure what else to store here?
// maybe file origin?
// speed multiplier?
// ending?
};
typedef struct Udp_msg Udp_msg;
struct Udp_msg {
s8 text[32]; // includes null terminator
@ -69,6 +83,7 @@ struct Ampler_state {
Udp_msg messages[64];
s8 frame_mix[CHANNELS][FRAME_SAMPLES];
s8 frame_rec[CHANNELS][FRAME_SAMPLES];
Grain grains[GRID_WIDTH][GRID_HEIGHT];
};
@ -76,4 +91,12 @@ void udp_init(Ampler_state *);
void udp_frame(Ampler_state *);
void udp_quit(Ampler_state *);
/*
typedef struct Markov_4 Markov_4;
struct Markov_4 {
u8 start;
u8 chain[256][256];
u8 lens[256];
};
*/

11
audio.c
View File

@ -95,11 +95,11 @@ int audio_frame(Ampler_state *state) {
// TODO: We should use CHANNELS here
// static s8 mix_l[FRAME_SAMPLES] = { 0 }, mix_r[FRAME_SAMPLES] = { 0 };
s8 *mix_l = state->frame_mix[0], *mix_r = state->frame_mix[1];
s8 *mix_l = state->frame_mix[0]; // s8 *mix_l = state->frame_mix[0], *mix_r = state->frame_mix[1];
SDL_memset(mix_l, 0, FRAME_SAMPLES / sizeof (s8));
SDL_memset(mix_r, 0, FRAME_SAMPLES / sizeof (s8));
//SDL_memset(mix_r, 0, FRAME_SAMPLES / sizeof (s8));
s8 *mix[] = { mix_l, mix_r };
s8 *mix[] = { mix_l }; //s8 *mix[] = { mix_l, mix_r };
foreach_ptr(Sound_src, snd, state -> sounds)
sound_src_frame(snd, mix);
@ -132,6 +132,7 @@ void load_sample(Ampler_state *state, const char *file_name) {
u32 bytes = 0;
SDL_LoadWAV(file_name, &spec, (u8 **) &buffer, &bytes);
// TODO: Check for error from LoadWAV
if spec.format != AUDIO_S16 do {
puts("error: sample.wav is not s16");
@ -167,8 +168,10 @@ void load_sample(Ampler_state *state, const char *file_name) {
puts("fffuuuck");
for int i = 0; i < snd -> track_len; i += 1 do
for int t = 0; t < chans; t += 1 do
for int t = 0; t < chans; t += 1 do {
if t >= CHANNELS do continue; // THIS GETS US MONO
snd -> tracks[t][i] = (s8) (buffer[i * chans + t] >> 8);
}
SDL_FreeWAV((void *) buffer);

94
draw.c
View File

@ -1,15 +1,6 @@
// draw.c
void draw_frame(SDL_Window *w, SDL_Renderer *r, Ampler_state *state) {
SDL_SetRenderDrawColor(r, 0x1D, 0x2B, 0x53, 255);
SDL_SetRenderDrawBlendMode(r, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawBlendMode(r, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(r, 10, 10, 10, 40);
SDL_Rect screen = { 0, 0, 512, 512 };
SDL_RenderFillRect(r, &screen);
// SDL_RenderClear(r);
void draw_snd_waves(SDL_Window *w, SDL_Renderer *r, Ampler_state *state) {
// TODO: DRAW WITH X SCALE FACTOR
// TODO: DRAW SMOLER
SDL_SetRenderDrawColor(r, 0xFF, 0x00, 0x4D, 255);
@ -34,29 +25,59 @@ void draw_frame(SDL_Window *w, SDL_Renderer *r, Ampler_state *state) {
SDL_RenderDrawLine(r, snd->start / space, top, snd->start / space, bot);
SDL_RenderDrawLine(r, snd->end / space, top, snd->end / space, bot);
}
}
static u8 markov[256][256];
SDL_memset(markov, 0, sizeof markov);
for int x = 0; x < FRAME_SAMPLES - 1; x += 1 do {
u8 cur = state->frame_mix[0][x] + 128;
u8 next = state->frame_mix[0][x + 1] + 128;
void draw_frame(SDL_Window *w, SDL_Renderer *r, Ampler_state *state) {
SDL_SetRenderDrawColor(r, 0x1D, 0x2B, 0x53, 255);
cur = state->frame_rec[0][x] + 128;
next = state->frame_rec[0][x + 1] + 128;
SDL_SetRenderDrawBlendMode(r, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawBlendMode(r, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(r, 10, 10, 10, 40);
SDL_Rect screen = { 0, 0, /*512*/ FRAME_SAMPLES, 512 };
SDL_RenderFillRect(r, &screen);
// SDL_RenderClear(r);
markov[cur][next] += 1;
//markov_draw(&(state->markov), r);
//SDL_memset(&(state->markov), 0, sizeof state->markov);
//markov_gen(&(state->markov), state->frame_mix[1]);
//markov_draw(&(state->markov), r);
SDL_RenderPresent(r);
}
/*
for int x = 0; x < 256; x += 1 do {
u8 to = 0;
for int y = 0; y < 255; y += 1 do to += markov[x][y];
markov[x][255] = to;
for int x = 0; x < 256; x += 1 do
// for int y = 0; y < markov_lens[x]; y += 1 do {
for int y = 0; y < 256; y += 1 do {
s32 m = markov[x + y * 256];
SDL_SetRenderDrawColor(r,
m >= 10 ? m : 0,
MIN(255, m < 4 ? m * 100 : 0),
MIN(255, m < 10 and m >= 4 ? m * 100 : 0), 255);
if m != 0 do {
const int y2 = 255 - y;
SDL_RenderDrawPoint(r, x * 2, y2 * 2);
SDL_RenderDrawPoint(r, x * 2 + 1, y2 * 2);
SDL_RenderDrawPoint(r, x * 2, y2 * 2 + 1);
SDL_RenderDrawPoint(r, x * 2 + 1, y2 * 2 + 1);
}
}
*/
int start_x = 255, end_x = 0;
/*
for int x = 0; x < FRAME_SAMPLES - 1; x += 1 do {
// playing
u8 cur = state->frame_mix[0][x] + 128;
u8 next = state->frame_mix[0][x + 1] + 128;
// recing
//cur = state->frame_rec[0][x] + 128;
//next = state->frame_rec[0][x + 1] + 128;
markov[cur + next * 256] += 1;
}
int start_x = 0, end_x = 255;
// converts from graph plot to prob table
for int x = 0; x < 256; x += 1 do {
// for each colum
// copy into temp col, store how filed, col is
@ -64,32 +85,15 @@ void draw_frame(SDL_Window *w, SDL_Renderer *r, Ampler_state *state) {
// for remainder of col fill with re-roll value
u8 temp[256] = { 0 }; // amount of each dest, by index
for int i = 0; i < 256; i++
do temp[i] = markov[x][i];
do temp[i] = markov[x + i * 256];
s32 next = 0;
for int i = 0; i < 256; i++ do
for int i = 0; i < 256; i++ do {
for int j = 0; j < temp[i] and next < 256; j++ do {
markov[x][next++] = i;
markov[x + (next++) * 256] = i;
if x < start_x do start_x = x;
if x > end_x do end_x = x;
}
}
//for int x = 0; x < 256; x++ do markov[x][0] = x, markov[0][x] = x;
for int x = start_x; x < end_x; x += 1 do
for int y = 0; y < 256; y += 1 do {
s32 m = markov[x][255 - y];
SDL_SetRenderDrawColor(r,
m >= 10 ? m : 0,
MIN(255, m < 4 ? m * 100 : 0),
MIN(255, m < 10 and m >= 4 ? m * 100 : 0), 255);
if m != 0 do {
SDL_RenderDrawPoint(r, x * 2, y * 2);
SDL_RenderDrawPoint(r, x * 2 + 1, y * 2);
SDL_RenderDrawPoint(r, x * 2, y * 2 + 1);
SDL_RenderDrawPoint(r, x * 2 + 1, y * 2 + 1);
markov_lens[x] = next;
}
}
SDL_RenderPresent(r);
}
*/

102
markov.c 100644
View File

@ -0,0 +1,102 @@
// markov.c
/* TODO:
* remove hardcoded FRAME_SAMPLES
*/
// !!!YOU WERE HERE!!!! //
// write a proper markov chain, only run for 256 samples per chain
// - make sure we DONT EVER fall into a zero len state
// - test with sine wave, should work on any freq
// s8 to u8 conversion macros ?
// lo / hi nibble extracion macros
void markov_gen(Markov_4 *m, s8 *audio) {
const u8 cur_1st = (u8)(((s32) audio[0]) + 128);
const u8 cur_2nd = (u8)(((s32) audio[1]) + 128);
// 1st goes in low bits, 2nd in high
m -> start = (cur_1st >> 4) | (cur_2nd & 0xf0);
for int sam = 0; sam < 512; sam += 2 do {
const u8 cur_1st = (u8)(((s32) audio[sam]) + 128);
const u8 cur_2nd = (u8)(((s32) audio[sam + 1]) + 128);
// 1st goes in low bits, 2nd in high
const u8 cur = (cur_1st >> 4) | (cur_2nd & 0xf0);
const int nex_sam = sam == 510 ?
0 : // loop back to first sample
sam + 2;
const u8 nex_1st = (u8)(((s32) audio[nex_sam]) + 128);
const u8 nex_2nd = (u8)(((s32) audio[nex_sam + 1]) + 128);
// 1st goes in low bits, 2nd in high
const u8 nex = (nex_1st >> 4) | (nex_2nd & 0xf0);
m->chain[cur][nex] = MIN(m->chain[cur][nex] + 1, 255);
}
for int x = 0; x < 256; x++ do {
static u8 x_copy[256];
for int i = 0; i < 256; i++ do
x_copy[i] = m->chain[x][i];
u8 len = 0;
for int y = 0; y < 256; y++ do
for ; len < x_copy[y]; len++ do
m -> chain[x][len] = y;
m -> lens[x] = len;
}
}
void markov_synth(Markov_4 *m, s8 *audio_out) {
if SDL_GetKeyboardState(NULL)[SDL_SCANCODE_SPACE] do return;
u8 x = m -> start;
for int sam = 0; sam < FRAME_SAMPLES; sam += 2 do {
if m->lens[x] == 0 do {
puts("asdsdgsdgKLJASDUGEAKLJASKLJSDILKWJASDKLJ, WHY IS THERE A ZERO");
break;
}
// split x into low + hi
s8 lo = (x & 0xf), hi = ((x & 0xf0) >> 4);
//audio_out[sam] = (lo - 8) << 4;
//audio_out[sam + 1] = (hi - 8) << 4;
if 1 do { // bit crush
const int b = 4;
audio_out[sam] = (audio_out[sam] >> b) << b;
audio_out[sam + 1] = (audio_out[sam + 1] >> b) << b;
}
u8 rnd_y = rnd_u8() % m->lens[x];
x = m->chain[x][rnd_y];
}
}
void markov_draw(Markov_4 *mar, SDL_Renderer *r) {
const int w = 256;
for int x = 0; x < 256; x += 1 do
//for int y = 0; y < mar->lens[x]; y += 1 do {
for int y = 0; y < 256; y += 1 do {
s32 m = mar->chain[x][y];
SDL_SetRenderDrawColor(r,
m >= 10 ? m : 0,
MIN(255, m < 4 ? m * 100 : 0),
MIN(255, m < 10 and m >= 4 ? m * 100 : 0), 255);
//SDL_SetRenderDrawColor(r, m * 32, 0, 0, 255);
if m != 0 do {
const int y2 = 255 - y;
SDL_RenderDrawPoint(r, x * 2, y2 * 2);
SDL_RenderDrawPoint(r, x * 2 + 1, y2 * 2);
SDL_RenderDrawPoint(r, x * 2, y2 * 2 + 1);
SDL_RenderDrawPoint(r, x * 2 + 1, y2 * 2 + 1);
}
}
}

59
random.c 100644
View File

@ -0,0 +1,59 @@
// random.c
// !!!!YOU WERE HERE!!!!!//
// MAKE A DOOM STYLE SHUFFLED RANDOMIZER, FOR JUST 8 BITS
//
// original uses unsigned long, tcc says that it's 4 bytes
// public domain impl from:
// http://lomont.org/papers/2008/Lomont_PRNG_2008.pdf
u32 well_512() {
// TODO: Make this more random / make sure it's correct ?
/* new Uint32Array([2 << 30]).map(x => Math.random() * x).join(",\n") */
static u32 state[16] = {
1096601398, 347948986, 707021053, 1924450882,
1184298871, 1860618357, 1301703596, 86165936,
160838326, 1276044826, 98793404, 1014941842,
1604941344,1520346171, 726203645, 1872316350,
};
static u32 index = 0;
u32 a, b, c, d;
a = state[index];
c = state[(index + 13) & 15];
b = a ^ c ^ (a << 16) ^ (c << 15);
c = state[(index + 9) & 15];
c ^= (c >> 11);
a = state[index] = b ^ c;
d = a ^ ((a << 5) & 0xda442d24UL);
index = (index + 15) & 15;
a = state[index];
state[index] = a ^ b ^ d ^ (a << 2) ^ (b << 18) ^ (c << 28);
return state[index];
}
u8 rnd_u8() {
return well_512() & 0xff;
}

12
udp.c
View File

@ -8,7 +8,7 @@
* store from ip + port in message ?
*/
#ifdef __WIN32__
#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
@ -26,13 +26,13 @@
#undef while
// G l o b a L E v i L //
#ifdef __WIN32__
#ifdef _WIN32
static SOCKET udp_sock;
#else
static int udp_sock;
#endif
#ifdef __WIN32__
#ifdef _WIN32
void udp_init(Ampler_state *state) {
// https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsastartup
WSADATA wsa;
@ -71,7 +71,7 @@ void udp_init(Ampler_state *state) {
}
#endif
#ifdef __WIN32__
#ifdef _WIN32
void udp_quit(Ampler_state *state) {
closesocket(udp_sock);
WSACleanup();
@ -82,7 +82,7 @@ void udp_quit(Ampler_state *state) {
}
#endif
#ifdef __WIN32__
#ifdef _WIN32
void recv_packet(Ampler_state *state) {
char discard_buf[32] = { 0 }; // for dropped packets
char *buf = discard_buf;
@ -128,7 +128,7 @@ void recv_packet(Ampler_state *state) {
}
#endif
#ifdef __WIN32__
#ifdef _WIN32
void udp_frame(Ampler_state *state) {
// https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-select
fd_set recv_fd_set;