beat

Arrange audio samples into something bigger
git clone https://git.ce9e.org/beat.git

commit
26fa5272e83773d8494fafc8ca842952874dc49f
parent
c60e0226e653eba2995fa5fdff13820aa10f1b41
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2019-12-18 12:50
static BUFSIZE

Diffstat

M beat.c 27 +++++++++++----------------

1 files changed, 11 insertions, 16 deletions


diff --git a/beat.c b/beat.c

@@ -4,9 +4,10 @@
    4     4 #include <math.h>
    5     5 
    6     6 #define MIN(A, B) ((A) < (B) ? (A) : (B))
   -1     7 #define BUFSIZE (1 << 18)
    7     8 
    8     9 struct context {
    9    -1     float *buf;
   -1    10     float buf[BUFSIZE];
   10    11     struct context *next;
   11    12 };
   12    13 
@@ -14,14 +15,13 @@ int samplerate;
   14    15 int frames_per_beat;  // samplerate * 60 / bpm
   15    16 int frames;
   16    17 int buf_cur;
   17    -1 size_t buf_len;
   18    18 float factor;
   19    19 struct context *ctx;
   20    20 
   21    21 void add_file_at_beat(const char *path, int beat) {
   22    22     int ibs = 1024;
   23    23     int pos = beat * frames_per_beat;
   24    -1     int rel_pos = pos - buf_cur * buf_len;
   -1    24     int rel_pos = pos - buf_cur * BUFSIZE;
   25    25     float fbuf[ibs];
   26    26     SF_INFO sfinfo;
   27    27     SNDFILE *infile = sf_open(path, SFM_READ, &sfinfo);
@@ -38,12 +38,12 @@ void add_file_at_beat(const char *path, int beat) {
   38    38         for (int i = 0; i < count; ++i) {
   39    39             pos += 1;
   40    40             rel_pos += 1;
   41    -1             if (rel_pos >= 2 * buf_len) {
   -1    41             if (rel_pos >= 2 * BUFSIZE) {
   42    42                 printf("dropping %s at %i\n", path, pos);
   43    43                 sf_close(infile);
   44    44                 return;
   45    -1             } else if (rel_pos >= buf_len) {
   46    -1                 ctx->next->buf[rel_pos - buf_len] += fbuf[i] * factor;
   -1    45             } else if (rel_pos >= BUFSIZE) {
   -1    46                 ctx->next->buf[rel_pos - BUFSIZE] += fbuf[i] * factor;
   47    47             } else {
   48    48                 ctx->buf[rel_pos] += fbuf[i] * factor;
   49    49             }
@@ -54,7 +54,7 @@ void add_file_at_beat(const char *path, int beat) {
   54    54 }
   55    55 
   56    56 int _sf_writef_float(SNDFILE *sndfile, float *buf) {
   57    -1     int size = MIN(frames - buf_cur * buf_len, buf_len);
   -1    57     int size = MIN(frames - buf_cur * BUFSIZE, BUFSIZE);
   58    58     if (size <= 0) {
   59    59         return 0;
   60    60     }
@@ -72,18 +72,13 @@ int main(int argc, char **argv) {
   72    72     frames = atoi(argv[4]) * frames_per_beat;
   73    73     factor = 1 / sqrt(atoi(argv[5]));
   74    74 
   75    -1     buf_len = MIN(frames / 4, 1 << 18);
   76    75     buf_cur = 0;
   77    76 
   78    77     ctx = (struct context *)malloc(sizeof(struct context));
   79    -1     float buf[buf_len];
   80    -1     memset(buf, 0, buf_len * sizeof(float));
   81    -1     ctx->buf = buf;
   -1    78     memset(ctx->buf, 0, BUFSIZE * sizeof(float));
   82    79 
   83    80     ctx->next = (struct context *)malloc(sizeof(struct context));
   84    -1     float buf2[buf_len];
   85    -1     memset(buf2, 0, buf_len * sizeof(float));
   86    -1     ctx->next->buf = buf2;
   -1    81     memset(ctx->next->buf, 0, BUFSIZE * sizeof(float));
   87    82     ctx->next->next = ctx;
   88    83 
   89    84     SF_INFO sfinfo;
@@ -100,9 +95,9 @@ int main(int argc, char **argv) {
  100    95         int beat = atoi(argv[i]);
  101    96         char *path = argv[i + 1];
  102    97 
  103    -1         while (beat * frames_per_beat >= (buf_cur + 1) * buf_len) {
   -1    98         while (beat * frames_per_beat >= (buf_cur + 1) * BUFSIZE) {
  104    99             _sf_writef_float(outfile, ctx->buf);
  105    -1             memset(ctx->buf, 0, buf_len * sizeof(float));
   -1   100             memset(ctx->buf, 0, BUFSIZE * sizeof(float));
  106   101 
  107   102             ctx = ctx->next;
  108   103             buf_cur += 1;