beat

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

commit
37c9da9eccca24a5daef7c15a618733f6e4fea78
parent
b91b7f0a76103f7f581b11dd3391bbc724534073
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2019-12-18 13:23
rename context

Diffstat

M beat.c 42 +++++++++++++++++++++---------------------

1 files changed, 21 insertions, 21 deletions


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

@@ -6,9 +6,9 @@
    6     6 #define MIN(A, B) ((A) < (B) ? (A) : (B))
    7     7 #define BUFSIZE (1 << 12)
    8     8 
    9    -1 struct context {
   -1     9 struct ring {
   10    10     float buf[BUFSIZE];
   11    -1     struct context *next;
   -1    11     struct ring *next;
   12    12 };
   13    13 
   14    14 int samplerate;
@@ -16,10 +16,10 @@ int frames_per_beat;  // samplerate * 60 / bpm
   16    16 int frames;
   17    17 int buf_cur;
   18    18 float factor;
   19    -1 struct context *ctx;
   -1    19 struct ring *first;
   20    20 
   21    -1 struct context *create_context(void) {
   22    -1     struct context *c = (struct context *)malloc(sizeof(struct context));
   -1    21 struct ring *create_ring(void) {
   -1    22     struct ring *c = (struct ring *)malloc(sizeof(struct ring));
   23    23     memset(c->buf, 0, BUFSIZE * sizeof(float));
   24    24     return c;
   25    25 }
@@ -32,7 +32,7 @@ void add_file_at_beat(const char *path, int beat) {
   32    32     SF_INFO sfinfo;
   33    33     SNDFILE *infile = sf_open(path, SFM_READ, &sfinfo);
   34    34 
   35    -1     struct context *curctx = ctx;
   -1    35     struct ring *cur = first;
   36    36 
   37    37     // assert sfinfo.samplerate == samplerate
   38    38     // assert sfinfo.channels == 1
@@ -48,13 +48,13 @@ void add_file_at_beat(const char *path, int beat) {
   48    48             rel_pos += 1;
   49    49             while (rel_pos >= BUFSIZE) {
   50    50                 rel_pos -= BUFSIZE;
   51    -1                 if (curctx->next == ctx) {
   52    -1                     curctx->next = create_context();
   53    -1                     curctx->next->next = ctx;
   -1    51                 if (cur->next == first) {
   -1    52                     cur->next = create_ring();
   -1    53                     cur->next->next = first;
   54    54                 }
   55    -1                 curctx = curctx->next;
   -1    55                 cur = cur->next;
   56    56             }
   57    -1             curctx->buf[rel_pos] += fbuf[i] * factor;
   -1    57             cur->buf[rel_pos] += fbuf[i] * factor;
   58    58         }
   59    59     }
   60    60 
@@ -82,8 +82,8 @@ int main(int argc, char **argv) {
   82    82 
   83    83     buf_cur = 0;
   84    84 
   85    -1     ctx = create_context();
   86    -1     ctx->next = ctx;
   -1    85     first = create_ring();
   -1    86     first->next = first;
   87    87 
   88    88     SF_INFO sfinfo;
   89    89     sfinfo.channels = 1;
@@ -100,26 +100,26 @@ int main(int argc, char **argv) {
  100   100         char *path = argv[i + 1];
  101   101 
  102   102         while (beat * frames_per_beat >= (buf_cur + 1) * BUFSIZE) {
  103    -1             _sf_writef_float(outfile, ctx->buf);
  104    -1             memset(ctx->buf, 0, BUFSIZE * sizeof(float));
   -1   103             _sf_writef_float(outfile, first->buf);
   -1   104             memset(first->buf, 0, BUFSIZE * sizeof(float));
  105   105 
  106    -1             ctx = ctx->next;
   -1   106             first = first->next;
  107   107             buf_cur += 1;
  108   108         }
  109   109 
  110   110         add_file_at_beat(path, beat);
  111   111     }
  112   112 
  113    -1     struct context *last = ctx;
  114    -1     while (last->next != ctx) {
   -1   113     struct ring *last = first;
   -1   114     while (last->next != first) {
  115   115         last = last->next;
  116   116     }
  117   117     last->next = NULL;
  118   118 
  119    -1     while (ctx) {
  120    -1         struct context *tmp = ctx;
   -1   119     while (first) {
   -1   120         struct ring *tmp = first;
  121   121         _sf_writef_float(outfile, tmp->buf);
  122    -1         ctx = tmp->next;
   -1   122         first = tmp->next;
  123   123         free(tmp);
  124   124     }
  125   125