pentabug/synth/main.c

206 lines
4.4 KiB
C
Raw Normal View History

2012-08-06 03:05:52 +02:00
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
2012-08-06 03:05:52 +02:00
#include <SDL/SDL.h>
#include "synth.h"
enum { MIXRATE = 8000000 / (6 * 64) };
static SDL_AudioSpec spec = { MIXRATE, AUDIO_U16SYS, 1, 0, 1024, };
2012-08-06 03:05:52 +02:00
static void fill_buffer(void* userdata, Uint8* stream, int len) {
for(int i = 0; i < len / 2; i++) {
uint16_t m = synth_mix();
assert(m < 0x100);
((uint16_t*)stream)[i] = m * 50;
}
2012-08-06 03:05:52 +02:00
}
// this parser is a complete hack
// put i can't care less right now
static int parse_tune(const char* filename) {
FILE* file = fopen(filename, "r");
if(!file) return -1;
char wave_names[256][256];
memset(wave_names, 0, sizeof(wave_names));
int wave_counter = 0;
char inst_names[256][256];
memset(inst_names, 0, sizeof(inst_names));
int inst_counter = 0;
char pattern_names[256][256];
memset(pattern_names, 0, sizeof(pattern_names));
int pattern_counter = 0;
2012-08-08 03:23:52 +02:00
tune_length = 0;
int state = 0;
int line_nr = 0;
2012-08-08 03:23:52 +02:00
char line[256];
while(fgets(line, 256, file)) {
line_nr++;
// skip empty line
if(line[0] == '\n') continue;
// comments
if(line[0] == '#') continue;
if(state == 0) {
if(strcmp("[WAVETABLE]\n", line)) return line_nr;
state = 1;
}
else if(state == 1) {
if(line[0] == '[') {
if(strcmp("[INSTRUMENTS]\n", line)) return line_nr;
state = 2;
}
2012-08-08 20:37:26 +02:00
else if(!isspace(line[0])) {
sscanf(line, "%s", wave_names[wave_counter]);
}
else if(line[0] == '\t') {
int a, b;
if(sscanf(line + 1, "%u %u", &a, &b) != 2) return line_nr;
wave_table[wave_counter][0] = a;
wave_table[wave_counter][1] = b;
wave_counter++;
}
else return line_nr;
}
else if(state == 2) {
if(line[0] == '[') {
if(strcmp("[PATTERNS]\n", line)) return line_nr;
state = 3;
}
else {
2012-08-08 03:23:52 +02:00
char wave[256];
int pw, ps, d;
if(sscanf(line, "%s %u %u %u %s",
inst_names[inst_counter], &pw, &ps, &d,
2012-08-08 03:23:52 +02:00
wave) != 5) return line_nr;
instruments[inst_counter].pulse_width = pw;
instruments[inst_counter].pulse_sweep = ps;
instruments[inst_counter].decay = d;
int i;
for(i = 0; i < 256; i++)
2012-08-08 03:23:52 +02:00
if(strcmp(wave_names[i], wave) == 0) break;
if(i == 256) return line_nr;
instruments[inst_counter].wave_table_pos = i;
inst_counter++;
}
}
else if(state == 3) {
if(line[0] == '[') {
if(strcmp("[PATTERNTABLE]\n", line)) return line_nr;
state = 4;
}
else {
2012-08-08 20:37:26 +02:00
if(isspace(line[0])) return line_nr;
2012-08-08 03:23:52 +02:00
sscanf(line, "%s", pattern_names[pattern_counter]);
for(int i = 0; i < pattern_length && fgets(line, 256, file); i++) {
2012-08-08 03:23:52 +02:00
line_nr++;
if(line[0] != '\t') return line_nr;
2012-08-08 03:23:52 +02:00
char note[256];
char inst[256];
int m = sscanf(line + 1, "%s %s", note, inst);
if(m == 0) return line_nr;
char* s = "ccddeffggaab";
char* p = strchr(s, line[1]);
if(p) {
2012-08-08 03:23:52 +02:00
patterns[pattern_counter][i][0] = (p - s) +
(note[1] == '#') + (note[2] - '0') * 12;
int j;
for(j = 0; j < 256; j++)
if(strcmp(inst_names[j], inst) == 0) break;
if(j == 256) return line_nr;
patterns[pattern_counter][i][1] = j;
}
2012-08-08 03:23:52 +02:00
else if(m == 1) {
2012-08-08 20:37:26 +02:00
if(strcmp("---", note) == 0)
2012-08-08 03:23:52 +02:00
patterns[pattern_counter][i][0] = 0xff;
else if(strcmp(".", note) == 0)
patterns[pattern_counter][i][0] = 0;
else return line_nr;
}
else return line_nr;
}
pattern_counter++;
}
2012-08-06 03:05:52 +02:00
}
else {
2012-08-08 03:23:52 +02:00
char* p = line;
char pat[256];
for(int i = 0; i < channel_count; i ++) {
sscanf(p, "%s", pat);
int j;
for(j = 0; j < 256; j++)
if(strcmp(pattern_names[j], pat) == 0) break;
if(j == 256) return line_nr;
pattern_table[tune_length][i] = j;
while(*p && !isspace(*p)) p++;
while(*p && isspace(*p)) p++;
}
tune_length++;
}
}
if(state =! 4) return line_nr;
fclose(file);
2012-08-09 15:30:39 +02:00
int size = wave_counter * 2 +
inst_counter * sizeof(synth_instrument_t) +
pattern_counter * pattern_length * 2 +
tune_length * channel_count;
printf("tune size: %d bytes\n", size);
return 0;
}
int main(int argc, char** argv) {
if(argc != 2) {
printf("usage: %s tunefile\n", argv[0]);
return 0;
}
2012-08-09 15:30:39 +02:00
puts("parsing...");
int error = parse_tune(argv[1]);
if(error != 0) {
fprintf(stderr, "%d: parsing error\n", error);
return 1;
}
2012-08-06 03:05:52 +02:00
2012-08-09 15:30:39 +02:00
2012-08-06 03:05:52 +02:00
spec.callback = &fill_buffer;
if(SDL_OpenAudio(&spec, &spec) < 0) {
fprintf(stderr, "ERROR");
exit(1);
}
SDL_PauseAudio(0);
puts("playing...");
2012-08-06 03:05:52 +02:00
getchar();
return 0;
}