beat

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

commit
84d9a2e54c7db084e5c9e3d6de9029fcd0c8a9fa
parent
9fe6033d924cf05dd812a5bac9171a273b5a9795
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2019-12-18 10:15
convert some context to global variables

Diffstat

M beat.c 76 ++++++++++++++++++++++++++++++------------------------------

1 files changed, 38 insertions, 38 deletions


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

@@ -6,44 +6,46 @@
    6     6 #define MIN(A, B) ((A) < (B) ? (A) : (B))
    7     7 
    8     8 struct context {
    9    -1     int samplerate;
   10    -1     int frames_per_beat;  // samplerate * 60 / bpm
   11    -1     int frames;
   12    -1     int buf_cur;
   13    -1     size_t buf_len;
   14    -1     float factor;
   15     9     float *buf;
   16    10     float *buf2;
   17    11 };
   18    12 
   19    -1 void add_file_at_beat(const char *path, int beat, struct context ctx) {
   -1    13 int samplerate;
   -1    14 int frames_per_beat;  // samplerate * 60 / bpm
   -1    15 int frames;
   -1    16 int buf_cur;
   -1    17 size_t buf_len;
   -1    18 float factor;
   -1    19 struct context ctx;
   -1    20 
   -1    21 void add_file_at_beat(const char *path, int beat) {
   20    22     int ibs = 1024;
   21    -1     int pos = beat * ctx.frames_per_beat;
   -1    23     int pos = beat * frames_per_beat;
   22    24     float fbuf[ibs];
   23    25     SF_INFO sfinfo;
   24    26     SNDFILE *sndfile = sf_open(path, SFM_READ, &sfinfo);
   25    27 
   26    -1     // assert sfinfo.samplerate == ctx.samplerate
   -1    28     // assert sfinfo.samplerate == samplerate
   27    29     // assert sfinfo.channels == 1
   28    30 
   29    31     while (1) {
   30    32         int count = sf_readf_float(sndfile, fbuf, ibs);
   31    -1         count = MIN(count, ctx.frames - pos - 1);
   32    -1         int rel_pos = pos - ctx.buf_cur * ctx.buf_len;
   -1    33         count = MIN(count, frames - pos - 1);
   -1    34         int rel_pos = pos - buf_cur * buf_len;
   33    35 
   34    36         if (count <= 0) break;
   35    37 
   36    38         for (int i = 0; i < count; ++i) {
   37    39             pos += 1;
   38    40             rel_pos += 1;
   39    -1             if (rel_pos >= 2 * ctx.buf_len) {
   -1    41             if (rel_pos >= 2 * buf_len) {
   40    42                 printf("dropping %s at %i\n", path, pos);
   41    43                 sf_close(sndfile);
   42    44                 return;
   43    -1             } else if (rel_pos >= ctx.buf_len) {
   44    -1                 ctx.buf2[rel_pos - ctx.buf_len] += fbuf[i] * ctx.factor;
   -1    45             } else if (rel_pos >= buf_len) {
   -1    46                 ctx.buf2[rel_pos - buf_len] += fbuf[i] * factor;
   45    47             } else {
   46    -1                 ctx.buf[rel_pos] += fbuf[i] * ctx.factor;
   -1    48                 ctx.buf[rel_pos] += fbuf[i] * factor;
   47    49             }
   48    50         }
   49    51     }
@@ -57,29 +59,27 @@ int main(int argc, char **argv) {
   57    59         exit(1);
   58    60     }
   59    61 
   60    -1     struct context ctx;
   61    -1 
   62    -1     ctx.samplerate = atoi(argv[2]);
   63    -1     ctx.frames_per_beat = ctx.samplerate * 60 / atoi(argv[3]);
   64    -1     ctx.frames = atoi(argv[4]) * ctx.frames_per_beat;
   65    -1     ctx.factor = 1 / sqrt(atoi(argv[5]));
   -1    62     samplerate = atoi(argv[2]);
   -1    63     frames_per_beat = samplerate * 60 / atoi(argv[3]);
   -1    64     frames = atoi(argv[4]) * frames_per_beat;
   -1    65     factor = 1 / sqrt(atoi(argv[5]));
   66    66 
   67    -1     ctx.buf_len = MIN(ctx.frames / 4, 1 << 18);
   68    -1     ctx.buf_cur = 0;
   -1    67     buf_len = MIN(frames / 4, 1 << 18);
   -1    68     buf_cur = 0;
   69    69 
   70    -1     float buf[ctx.buf_len];
   71    -1     memset(buf, 0, ctx.buf_len * sizeof(float));
   -1    70     float buf[buf_len];
   -1    71     memset(buf, 0, buf_len * sizeof(float));
   72    72     ctx.buf = buf;
   73    73 
   74    -1     float buf2[ctx.buf_len];
   75    -1     memset(buf2, 0, ctx.buf_len * sizeof(float));
   -1    74     float buf2[buf_len];
   -1    75     memset(buf2, 0, buf_len * sizeof(float));
   76    76     ctx.buf2 = buf2;
   77    77 
   78    78     SF_INFO sfinfo;
   79    79     sfinfo.channels = 1;
   80    80     sfinfo.format = SF_FORMAT_FLAC | SF_FORMAT_PCM_16;
   81    -1     sfinfo.frames = ctx.frames;
   82    -1     sfinfo.samplerate = ctx.samplerate;
   -1    81     sfinfo.frames = frames;
   -1    82     sfinfo.samplerate = samplerate;
   83    83     sfinfo.sections = 1;
   84    84     sfinfo.seekable = 1;
   85    85 
@@ -89,23 +89,23 @@ int main(int argc, char **argv) {
   89    89         int beat = atoi(argv[i]);
   90    90         char *path = argv[i + 1];
   91    91 
   92    -1         if (beat * ctx.frames_per_beat >= (ctx.buf_cur + 1) * ctx.buf_len) {
   93    -1             sf_writef_float(sndfile, ctx.buf, ctx.buf_len);
   94    -1             memset(ctx.buf, 0, ctx.buf_len * sizeof(float));
   -1    92         if (beat * frames_per_beat >= (buf_cur + 1) * buf_len) {
   -1    93             sf_writef_float(sndfile, ctx.buf, buf_len);
   -1    94             memset(ctx.buf, 0, buf_len * sizeof(float));
   95    95 
   96    96             float *tmp = ctx.buf;
   97    97             ctx.buf = ctx.buf2;
   98    98             ctx.buf2 = tmp;
   99    -1             ctx.buf_cur += 1;
   -1    99             buf_cur += 1;
  100   100         }
  101   101 
  102    -1         add_file_at_beat(path, beat, ctx);
   -1   102         add_file_at_beat(path, beat);
  103   103     }
  104   104 
  105    -1     int rest = ctx.frames - ctx.buf_cur * ctx.buf_len;
  106    -1     if (rest > ctx.buf_len) {
  107    -1         sf_writef_float(sndfile, ctx.buf, ctx.buf_len);
  108    -1         sf_writef_float(sndfile, ctx.buf2, rest - ctx.buf_len);
   -1   105     int rest = frames - buf_cur * buf_len;
   -1   106     if (rest > buf_len) {
   -1   107         sf_writef_float(sndfile, ctx.buf, buf_len);
   -1   108         sf_writef_float(sndfile, ctx.buf2, rest - buf_len);
  109   109     } else {
  110   110         sf_writef_float(sndfile, ctx.buf, rest);
  111   111     }