loading audio works right now, drawing basic waveform

master
bxwtf 2021-12-20 00:03:14 +00:00
parent 412d2588cb
commit 596dfd49e2
4 changed files with 36 additions and 20 deletions

View File

@ -35,6 +35,9 @@ Ampler_state *init() {
if !(state -> playdev) do
puts(SDL_GetError());
// TODO: iterate a directory to load samples
load_track(state);
puts("init.");
return state;
}
@ -55,8 +58,6 @@ int ampler_main(SDL_Window *w, SDL_Renderer *r, Ampler_state **u_data) {
Ampler_state *state = *u_data;
load_track(state);
SDL_PauseAudioDevice(state -> playdev, 0); // unpause audio
while 1 do {
SDL_Event e;
@ -67,7 +68,6 @@ int ampler_main(SDL_Window *w, SDL_Renderer *r, Ampler_state **u_data) {
audio_frame(state);
draw_frame(w, r, state);
SDL_RenderPresent(r);
SDL_Delay(1);
if !remove("reload-trigger") do {

View File

@ -27,9 +27,8 @@ typedef double f64;
typedef struct Ampler_state Ampler_state;
struct Ampler_state {
u32 size;
u32 size; // in bytes, off this struct
SDL_AudioDeviceID playdev;
s8 track_l[SAMPLE_RATE * 120]; // 2 minutes
s8 track_r[SAMPLE_RATE * 120]; // 2 minutes
s8 tracks[CHANNELS][SAMPLE_RATE * 480]; // 8 minutes
};

30
audio.c
View File

@ -7,12 +7,15 @@ int audio_frame(Ampler_state *state) {
if queued > FRAME_SAMPLES * CHANNELS * 2 do return 0;
// else puts("queued audio.");
// TODO: ITERATE SOUNDS AND MIX IN SEP CHANNELS
// TODO: THEN, CONVERT TO INTERLEAVED AND OUTPUT
static s16 frame[SAMPLE_RATE * CHANNELS * 7] = { 0 };
for int i = 0; i < arraylen(frame); i += CHANNELS do {
int vol = 64;
frame[i] = ((s16) (state -> track_l[i])) * vol;
frame[i + 1] = ((s16) (state -> track_r[i])) * vol;
}
const int vol = 64;
for int i = 0; i < arraylen(frame) / CHANNELS; i += 1 do
for int t = 0; t < CHANNELS; t += 1 do
frame[i * CHANNELS + t] = ((s16) (state -> tracks[t][i])) * vol;
if SDL_QueueAudio(state -> playdev, frame, sizeof frame) do
puts(SDL_GetError());
@ -31,16 +34,17 @@ void load_track(Ampler_state *state) {
return;
}
const int len = bytes / sizeof s16;
const int chans = spec.channels;
const int len = (bytes / sizeof s16) / chans;
// TODO: Use floats to print out total number of seconds
printf("loading sample.wav\n");
// TODO: VALIDATE LENGTH + TRACKS
s8 *tracks[] = { state -> track_l, state -> track_r, };
for int i = 0; i < len; i += spec.channels do
for int c = 0; c < spec.channels; c++ do {
tracks[c][i] = (s8) (buffer[i + c] >> 8);
}
printf("loading sample.wav %f seconds.\n",
((f32) len) / ((f32) SAMPLE_RATE));
for int i = 0; i < len; i += 1 do
for int t = 0; t < chans; t += 1 do
state -> tracks[t][i] = (s8) (buffer[i * chans + t] >> 8);
SDL_FreeWAV((void *) buffer);
}

15
draw.c
View File

@ -1,5 +1,18 @@
// draw.c
void draw_frame(SDL_Window *w, SDL_Renderer *r, Ampler_state *state) {
// todo: draw track l / r wave forms
SDL_SetRenderDrawColor(r, 0x1D, 0x2B, 0x53, 255);
SDL_RenderClear(r);
SDL_SetRenderDrawColor(r, 0xFF, 0x00, 0x4D, 255);
const space = (44100 * 8) / 512;
for int t = 0; t < arraylen(state -> tracks); t += 1 do
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, x, y + val / 8, x, y + val);
//SDL_RenderDrawPoint(r, x, y + val);
}
SDL_RenderPresent(r);
}