- commit
- 0fdb0471e2e8afaa30f3c51f207a28c8eff31baf
- parent
- 400aebaa8e6b0942aad55146b1f79008aa368be2
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2019-12-17 14:35
avoid clipping
Diffstat
| M | Makefile | 6 | +++--- |
| M | beat.c | 13 | ++++++++----- |
2 files changed, 11 insertions, 8 deletions
diff --git a/Makefile b/Makefile
@@ -3,7 +3,7 @@ SAMPLERATE = 44100 3 3 FPB = $(shell echo $(SAMPLERATE) \* 60 / $(BPM) | bc) 4 4 5 5 build/beat.flac: Makefile beat src/bumm.flac src/tack.flac6 -1 ./beat $@ $(SAMPLERATE) $$(($(FPB) / 2)) 8 0 src/bumm.flac 2 src/tack.flac 3 src/bumm.flac 4 src/bumm.flac 6 src/tack.flac-1 6 ./beat $@ $(SAMPLERATE) $$(($(FPB) / 2)) 8 2 0 src/bumm.flac 2 src/tack.flac 3 src/bumm.flac 4 src/bumm.flac 6 src/tack.flac 7 78 -1 beat: beat.c9 -1 gcc -lsndfile $< -o $@-1 8 beat: beat.c Makefile -1 9 gcc -lm -lsndfile $< -o $@
diff --git a/beat.c b/beat.c
@@ -1,6 +1,7 @@ 1 1 #include <stdlib.h> 2 2 #include <string.h> 3 3 #include <sndfile.h> -1 4 #include <math.h> 4 5 5 6 #define MIN(A, B) ((A) < (B) ? (A) : (B)) 6 7 @@ -10,6 +11,7 @@ struct context { 10 11 int frames; 11 12 int buf_cur; 12 13 size_t buf_len; -1 14 float factor; 13 15 float *buf; 14 16 float *buf2; 15 17 }; @@ -39,9 +41,9 @@ void add_file_at_beat(const char *path, int beat, struct context ctx) { 39 41 sf_close(sndfile); 40 42 return; 41 43 } else if (rel_pos >= ctx.buf_len) {42 -1 ctx.buf2[rel_pos - ctx.buf_len] += fbuf[i];-1 44 ctx.buf2[rel_pos - ctx.buf_len] += fbuf[i] * ctx.factor; 43 45 } else {44 -1 ctx.buf[rel_pos] += fbuf[i];-1 46 ctx.buf[rel_pos] += fbuf[i] * ctx.factor; 45 47 } 46 48 } 47 49 } @@ -50,8 +52,8 @@ void add_file_at_beat(const char *path, int beat, struct context ctx) { 50 52 } 51 53 52 54 int main(int argc, char **argv) {53 -1 if (argc < 6 || argc % 2 != 1) {54 -1 printf("Usage: beat OUTFILE SAMPLERATE FRAMES_PER_BEAT BEATS BEAT INFILE [BEAT INFILE…]\n");-1 55 if (argc < 6 || argc % 2 != 0) { -1 56 printf("Usage: beat OUTFILE SAMPLERATE FRAMES_PER_BEAT BEATS TRACKS BEAT INFILE [BEAT INFILE…]\n"); 55 57 exit(1); 56 58 } 57 59 @@ -60,6 +62,7 @@ int main(int argc, char **argv) { 60 62 ctx.samplerate = atoi(argv[2]); 61 63 ctx.frames_per_beat = atoi(argv[3]); 62 64 ctx.frames = atoi(argv[4]) * ctx.frames_per_beat; -1 65 ctx.factor = 1 / sqrt(atoi(argv[5])); 63 66 64 67 ctx.buf_len = MIN(ctx.frames / 4, 1 << 18); 65 68 ctx.buf_cur = 0; @@ -82,7 +85,7 @@ int main(int argc, char **argv) { 82 85 83 86 SNDFILE *sndfile = sf_open(argv[1], SFM_WRITE, &sfinfo); 84 8785 -1 for (int i = 5; i + 1 < argc; i += 2) {-1 88 for (int i = 6; i + 1 < argc; i += 2) { 86 89 int beat = atoi(argv[i]); 87 90 char *path = argv[i + 1]; 88 91