musix 0.3.3

Music player library for esoteric audio formats (music from C64,Amiga etc)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

#include "types.h"
#include "v2mconv.h"
#include "sounddef.h"
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdint.h>

#define printf2(str, ...)

extern const char * const v2mconv_errors[] =
{
    "no error",
    "V2M file contains no patch data! (forgot program changes?)",
    "V2M file was made with unknown synth version!",
};

// nicht drĂ¼ber nachdenken.
static struct _ssbase
{
    const uint8_t   *patchmap;
    const uint8_t   *globals;
    uint32_t    timediv;
    uint32_t    timediv2;
    uint32_t    maxtime;
    const uint8_t   *gptr;
    uint32_t  gdnum;
    struct _basech
    {
        uint32_t    notenum;
        const uint8_t      *noteptr;
        uint32_t    pcnum;
        const uint8_t      *pcptr;
        uint32_t    pbnum;
        const uint8_t      *pbptr;
        struct _bcctl
        {
            uint32_t       ccnum;
            const uint8_t  *ccptr;
        } ctl[7];
    } chan[16];

    const uint8_t *mididata;
    int midisize;
    int patchsize;
    int globsize;
    int maxp;
    const uint8_t *newpatchmap;

    const uint8_t *speechdata;
    int spsize;

} base;

static void readfile(const unsigned char *inptr, const int inlen)
{
    const uint8_t *d = inptr;
    memset(&base, 0, sizeof(base));

    base.timediv  = (*((uint32_t*)(d)));
    base.timediv2 = 10000*base.timediv;
    base.maxtime  = *((uint32_t*)(d + 4));
    base.gdnum    = *((uint32_t*)(d + 8));
    d += 12;
    base.gptr = d;
    d += 10*base.gdnum;
    for (int ch = 0; ch < 16; ch++)
    {
        _ssbase::_basech &c = base.chan[ch];
        c.notenum = *((uint32_t*)d);
        d += 4;
        if (c.notenum)
        {
            c.noteptr = d;
            d += 5*c.notenum;
            c.pcnum = *((uint32_t*)d);
            d += 4;
            c.pcptr = d;
            d += 4*c.pcnum;
            c.pbnum = *((uint32_t*)d);
            d += 4;
            c.pbptr = d;
            d += 5*c.pbnum;
            for (int cn = 0; cn < 7; cn++)
            {
                _ssbase::_basech::_bcctl &cc = c.ctl[cn];
                cc.ccnum = *((uint32_t*)d);
                d += 4;
                cc.ccptr = d;
                d += 4*cc.ccnum;
            }
        }
    }
    base.midisize = d - inptr;
    base.globsize = *((uint32_t*)d);
    if (base.globsize < 0 || base.globsize > 131072) return;
    d += 4;
    base.globals = d;
    d += base.globsize;
    base.patchsize = *((uint32_t*)d);
    if (base.patchsize < 0 || base.patchsize > 1048576) return;
    d += 4;
    base.patchmap = d;
    d += base.patchsize;

    if (d - inptr < inlen)
    {
        base.spsize = *((uint32_t*)d);
        d += 4;
        base.speechdata = d;
        d += base.spsize;

        // small sanity check
        if (base.spsize < 0 || base.spsize > 8192 || (d - inptr) > inlen)
        {
            base.spsize = 0;
            base.speechdata = 0;
        }
    } else
    {
        base.spsize = 0;
        base.speechdata = 0;
    }

    printf2("after read: est %d, is %d\n", inlen, d - inptr);
    printf2("midisize: %d, globs: %d, patches: %d\n", base.midisize, base.globsize, base.patchsize);
}

int patchesused[128];

// gives version deltas
int CheckV2MVersion(const unsigned char *inptr, const int inlen)
{
    int i;
    readfile(inptr,inlen);

    if (!base.patchsize)
        return -1;

    // determine highest used patch from progchange commands
    // (midiconv remaps the patches so that they're a
    //  continuous block from 0 to x)
    memset(patchesused, 0, sizeof(patchesused));
    base.maxp = 0;


    for (int ch = 0; ch < 16; ch++)
    {
        unsigned char p = 0;
        unsigned char v = 0;

        //int pcp = 0;
        int pct = 0;
        int pcn = base.chan[ch].pcnum;
        int np = 0;
        int nt = 0;
        int nn = base.chan[ch].notenum;

        int td;
        const uint8_t *ptr;
        for (int pcp = 0; pcp < pcn; pcp++)
        {
            // have there been notes? add last pgm
            ptr = base.chan[ch].pcptr;
            td  = 0x10000*ptr[2*pcn + pcp] + 0x100*ptr[1*pcn + pcp] + ptr[0*pcn + pcp];
            pct += td;
            if (pct > nt)
            {
                patchesused[p] = 1;
                if (p >= base.maxp) base.maxp = p + 1;
            }
            p += ptr[3*pcn + pcp];

            // walk notes until we reach current cmd
            while (np < nn && nt <= pct)
            {
                ptr = base.chan[ch].noteptr;
                td  = 0x10000*ptr[2*nn + np] + 0x100*ptr[1*nn + np] + ptr[0*nn + np];
                v  += ptr[4*nn+np];
                nt += td;
                np++;
            }
        }

        if (np < nn)
        {
            patchesused[p] = 1;
            if (p >= base.maxp) base.maxp = p + 1;
        }

    }
    printf2("patches used: %d\n", base.maxp);

    if (!base.maxp)
        return -1;

    // offset table to ptaches
    int *poffsets = (int *)base.patchmap;

    int matches = 0, best = -1;
    // for each version check...
    for (i = 0; i <= v2version; i++)
    {
        // ... if globals size is correct...
        if (base.globsize == v2gsizes[i])
        {
            printf2("globsize match: %d\n",i);
            // ... and if the size of all patches makes sense
            int p;
            for (p = 0; p < base.maxp - 1; p++)
            {
                int d = (poffsets[p + 1] - poffsets[p]) - (v2vsizes[i] - 3*255);
                if (d % 3) break;
                d /= 3;
                if (d != base.patchmap[poffsets[p] + v2vsizes[i] - 3*255 - 1])
                    break;
            }

            if (p == base.maxp - 1)
            {
                printf2("... ok!\n");
                best = i;
                matches++;
            } else
            {
                printf2("no match!\n");
            }
        }
    }

    // if we've got exactly one match, we're quite sure
    int ret = (matches >= 1) ? v2version - best : -2;
    printf2("found version delta: %d\n", ret);
    return ret;
}

//Klicke den Narren Ă¼ber Dir und das Schauspiel wird beginnen...
/*static int transEnv(int enVal)
{
    float dv = (float)(1.0f - pow(2.0f, -enVal/128.0f*11.0f));
    dv = (float)sqrt(dv); // square root to correct value
    int nEnVal = (int)(-log(1.0f - dv)/log(2.0f)*128.0f/11.0f);
    if (nEnVal < 0)
    {
        printf2("!!clamping enval lower bound!\n");
        nEnVal = 0;
    } else if (nEnVal > 127)
    {
        printf2("!!clamping enval upper bound!\n");
        nEnVal = 127;
    }
    return nEnVal;
}*/

void ConvertV2M(const unsigned char *inptr, const int inlen, unsigned char **outptr, int *outlen)
{
    int i, p;
    // check version
    int vdelta = CheckV2MVersion(inptr, inlen);
    if (!vdelta) // if same, simply clone
    {
        *outptr = new uint8_t[inlen + 4];
        memset(*outptr, 0, inlen + 4);
        *outlen = inlen + 4;
        memcpy(*outptr, inptr, inlen);
        return;
    }
    else if (vdelta < 0) // if invalid...
    {
        *outptr = 0;
        *outlen = 0;
        return;
    }
    vdelta = v2version - vdelta;

    // fake base.maxp
    int maxp2 = ((int*)base.patchmap)[0]/4;
    if (maxp2 != base.maxp)
    {
        printf2("warning: patch count inconsistency: we:%d, they:%d\n", base.maxp, maxp2);
        base.maxp = maxp2;
    }

    // calc new size
    int gdiff = v2gsizes[v2version] - v2gsizes[vdelta];
    int pdiff = v2vsizes[v2version] - v2vsizes[vdelta];
    int newsize = inlen + gdiff + base.maxp*pdiff;
    printf2("old size: %d, new size: %d\n", inlen, newsize);

    // init new v2m
    *outlen = newsize + 4;
    uint8_t *newptr = *outptr = new uint8_t[newsize + 4];
    memset(newptr, 0, newsize + 4);

    // copy midi data
    memcpy(newptr, inptr, base.midisize);

    // new globals length...
    newptr += base.midisize;
    *(int *)newptr = v2ngparms;
    printf2("glob size: old %d, new %d\n", base.globsize, *(int *)newptr);
    newptr += 4;

    // copy/remap globals
    memcpy(newptr, v2initglobs, v2ngparms);
    const uint8_t *oldgptr = base.globals;
    for (i = 0; i < v2ngparms; i++)
    {
        if (v2gparms[i].version <= vdelta)
            newptr[i] = *oldgptr++;
    }
    newptr += v2ngparms;

    // new patch data length
    *(int *)newptr = base.patchsize + base.maxp*pdiff;
    printf2("patch size: old %d, new %d\n", base.patchsize, *(int *)newptr);
    newptr += 4;

    base.newpatchmap = newptr;

    int *poffsets = (int *)base.patchmap;
    int *noffsets = (int *)newptr;
    //const int ros = v2vsizes[vdelta] - 255*3 - 1;

    //uint8_t *nptr2 = newptr;

    // copy patch table...
    // gcc 5.3 seems to vectorize the loop, but
    // the loop is not correct for base.maxp == 10 at least
    // adding volatile to the iterator breaks the auto-vec
    for (volatile int q = 0; q < base.maxp; q++)
    {
        noffsets[q] = poffsets[q] + (q * pdiff);
    }

    newptr += 4*base.maxp;

    // for each patch...
    for (p = 0; p < base.maxp; p++)
    {
        const uint8_t *src = base.patchmap + poffsets[p];

        //const uint8_t *dest_soll = nptr2 + noffsets[p];
        printf2("p%d ist:%08x soll:%08x\n", p, newptr, dest_soll);

        // fill patch with default values
        memcpy(newptr, v2initsnd, v2nparms);

        // copy/remap patch data
        for (i = 0; i < v2nparms; i++)
        {
            if (v2parms[i].version <= vdelta)
            {
                *newptr = *src++;
                /*
                if (vdelta < 2 && (i == 33 || i == 36 || i == 39 || i == 42)) // fix envelopes
                    *newptr=transEnv(*newptr); */
            }
            newptr++;
        }

        // copy mod number
        const int modnum = *newptr ++= *src++;
        //        printf2("patch %d: %d mods\n",p,modnum);

        // copy/remap modulations
        for (i = 0; i < modnum; i++)
        {
            newptr[0] = src[0];
            newptr[1] = src[1];
            newptr[2] = src[2];
            for (int k = 0; k <= newptr[2]; k++)
                if (v2parms[k].version > vdelta) newptr[2]++;
            newptr += 3;
            src += 3;
        }
    }

    // copy speech
    *(uint32_t*)newptr = base.spsize;
    newptr += 4;
    memcpy(newptr, base.speechdata, base.spsize);
    newptr += base.spsize;

    printf2("est size: %d, real size: %d\n", newsize, newptr - *outptr);
}

unsigned long GetV2MPatchData(const unsigned char *inptr, const int inlen,
    unsigned char **outptr, const unsigned char **patchmap)
{
    int outlen;
    ConvertV2M(inptr, inlen, outptr, &outlen);

    const uint8_t *pm = base.newpatchmap;
    if (!pm) pm = base.patchmap;

    int *poffsets = (int*)pm;
    for (int i = 0; i < base.maxp; i++)
    {
        patchmap[i] = pm + poffsets[i];
    }
    return base.maxp;
}