audio tracks are now stored in Sound_src

master
bxwtf 2021-12-21 01:37:24 +00:00
parent 349051e4de
commit 4a5a3f1e3d
3 changed files with 32 additions and 26 deletions

View File

@ -29,7 +29,7 @@ typedef double f64;
typedef struct Sound_src Sound_src;
struct Sound_src {
s32 start; // in samples
s8 *tracks[CHANNELS];
s32 len; // in samples
f32 pos; // position in samples
f32 speed; // in samples, per sample, can be negative
@ -40,7 +40,6 @@ typedef struct Ampler_state Ampler_state;
struct Ampler_state {
u32 size; // in bytes, off this struct
SDL_AudioDeviceID playdev;
s8 tracks[CHANNELS][SAMPLE_RATE * 480]; // 8 minutes
Sound_src sounds[64];
s32 played_audio_last_frame;
};

41
audio.c
View File

@ -1,15 +1,15 @@
// audio.c
void sound_src_frame(Sound_src *s, s8 *tracks[], s8 *mix[]) {
void sound_src_frame(Sound_src *s, s8 *mix[]) {
if s -> state == LOOPING do
for int i = 0; i < FRAME_SAMPLES; i += 1 do {
for int c = 0; c < CHANNELS; c += 1 do
mix[c][i] += tracks[c][(int) (s -> start + s -> pos)];
mix[c][i] += s -> tracks[c][(int) s -> pos];
s -> pos += s -> speed;
while s -> pos < 0.0f do
s -> pos += s -> len;
while s -> pos > s -> len do
while s -> pos >= s -> len do
s -> pos -= s -> len;
}
}
@ -21,14 +21,14 @@ int audio_frame(Ampler_state *state) {
if queued > FRAME_SAMPLES * CHANNELS * 2 do return 0;
// else puts("queued audio.");
// TODO: We should use CHANNELS here
static s8 mix_l[FRAME_SAMPLES] = { 0 }, mix_r[FRAME_SAMPLES] = { 0 };
SDL_memset(mix_l, 0, sizeof mix_l);
SDL_memset(mix_r, 0, sizeof mix_r);
s8 *mix[] = { mix_l, mix_r };
s8 *tracks[] = { state -> tracks[0], state -> tracks[1] };
for int s = 0; s < arraylen(state -> sounds); s += 1 do
sound_src_frame(&(state -> sounds[s]), tracks, mix);
sound_src_frame(&(state -> sounds[s]), mix);
const int vol = 100;
static s16 frame[FRAME_SAMPLES * CHANNELS] = { 0 };
@ -55,25 +55,30 @@ void load_track(Ampler_state *state) {
}
const int chans = spec.channels;
const int len = (bytes / sizeof s16) / chans;
const int length = (bytes / sizeof s16) / chans;
// TODO: VALIDATE LENGTH + TRACKS
// TODO: Actually find free sound_src
Sound_src *snd = &(state -> sounds[0]);
snd -> len = length;
snd -> speed = 1.0f;
snd -> pos = 0.0f;
snd -> state = LOOPING; // TOOD: Should be STOPPED
printf("loading sample.wav %f seconds.\n",
((f32) len) / ((f32) SAMPLE_RATE));
for int i = 0; i < CHANNELS; i += 1 do
snd -> tracks[i] = malloc(snd -> len);
for int i = 0; i < len; i += 1 do
for int i = 0; i < CHANNELS; i += 1 do
if !snd -> tracks[i] do
puts("fffuuuck");
for int i = 0; i < snd -> len; i += 1 do
for int t = 0; t < chans; t += 1 do
state -> tracks[t][i] = (s8) (buffer[i * chans + t] >> 8);
// TODO: Find actually free sound_src
state -> sounds[0].start = 0;
state -> sounds[0].len = len;
state -> sounds[0].speed = 1.0f;
state -> sounds[0].pos = 0.0f;
state -> sounds[0].state = LOOPING;
snd -> tracks[t][i] = (s8) (buffer[i * chans + t] >> 8);
SDL_FreeWAV((void *) buffer);
printf("loaded sample.wav %f seconds.\n",
((f32) length) / ((f32) SAMPLE_RATE));
}
void load_track_f32() { // idk if we're ever gonna be loading floats

14
draw.c
View File

@ -2,22 +2,24 @@
void draw_frame(SDL_Window *w, SDL_Renderer *r, Ampler_state *state) {
SDL_SetRenderDrawColor(r, 0x1D, 0x2B, 0x53, 255);
//SDL_SetRenderDrawColor(r, 10, 10, 10, 100);
SDL_RenderClear(r);
// TODO: ACTUALLY DRAW ALL SOUNDS, BUT SMOLER
SDL_SetRenderDrawColor(r, 0xFF, 0x00, 0x4D, 255);
const space = (44100 * 8) / 512;
for int t = 0; t < arraylen(state -> tracks); t += 1 do
const int t = 0;
for int x = 0; x < 512; x += 1 do {
const int y = 128 + 256 * t;
const int val = state -> tracks[t][x * space];
SDL_RenderDrawLine(r, y + val / 8, x, y + val, x);
//SDL_RenderDrawPoint(r, y + val, x);
const int val = state -> sounds[0].tracks[t][x * space];
SDL_RenderDrawLine(r, x, y + val / 8, x, y + val);
//SDL_RenderDrawPoint(r, x, y + val);
}
if state -> played_audio_last_frame do
for int i = 0; i < arraylen(state -> sounds); i += 1 do {
const int x = (state -> sounds[i].start + state -> sounds[i].pos) / space;
SDL_RenderDrawLine(r, 0, x, 512, x);
const int x = state -> sounds[i].pos / space;
SDL_RenderDrawLine(r, x, 0, x, 512);
}
SDL_RenderPresent(r);