Commit f3db6641d3db14a935ea169f7a693e0f1b2e5d79

Thomas de Grivel 2020-05-08T12:42:22

implement struct alignment

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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
diff --git a/lib/include/rtbuf/glfw3.h b/lib/include/rtbuf/glfw3.h
index ddb1f81..a2199bc 100644
--- a/lib/include/rtbuf/glfw3.h
+++ b/lib/include/rtbuf/glfw3.h
@@ -20,13 +20,11 @@
 #include <rtbuf/rtbuf.h>
 #include <rtbuf/music.h>
 
-#pragma pack(push,1)
 typedef struct rtbuf_glfw3_keyboard_data {
   s_rtbuf_music_notes notes;
   GLFWwindow *window;
   unsigned int octave;
 } s_rtbuf_glfw3_keyboard_data;
-#pragma pack(pop)
 
 enum {
   RTBUF_GLFW3_OSCILLOSCOPE_IN_BLACK,
@@ -36,10 +34,8 @@ enum {
   RTBUF_GLFW3_OSCILLOSCOPE_INS
 };
 
-#pragma pack(push,1)
 typedef struct rtbuf_glfw3_oscilloscope_data {
   GLFWwindow *window;
 } s_rtbuf_glfw3_oscilloscope_data;
-#pragma pack(pop)
 
 #endif
diff --git a/lib/include/rtbuf/music.h b/lib/include/rtbuf/music.h
index 91604c3..c370e37 100644
--- a/lib/include/rtbuf/music.h
+++ b/lib/include/rtbuf/music.h
@@ -26,14 +26,12 @@
 #define RTBUF_MUSIC_NOTE_SIZE RTBUF_SIGNAL_SAMPLE_SIZE
 #define RTBUF_MUSIC_NOTE_TYPE "music_note"
 
-#pragma pack(push,1)
 typedef struct rtbuf_music_note {
   t_rtbuf_signal_sample velocity; /* between 0.0 and 1.0 */
   t_rtbuf_signal_sample freq;     /* above 0.0 */
   t_rtbuf_signal_sample start;    /* time since start */
   t_rtbuf_signal_sample stop;     /* time since stop, or -1.0 */
 } s_rtbuf_music_note;
-#pragma pack(pop)
 
 #define RTBUF_MUSIC_NOTE_IN(note)                                       \
   { note "velocity",  RTBUF_SIGNAL_SAMPLE_TYPE, 1.0, 0.0, 1.0 },        \
@@ -69,12 +67,10 @@ enum {
 
 #define RTBUF_MUSIC_NOTE_MAX 32
 
-#pragma pack(push,1)
 typedef struct rtbuf_music_notes {
   unsigned int note_n;
   s_rtbuf_music_note note[RTBUF_MUSIC_NOTE_MAX];
 } s_rtbuf_music_notes;
-#pragma pack(pop)
 
 #define RTBUF_MUSIC_NOTES_SIZE sizeof(unsigned int)
 #define RTBUF_MUSIC_NOTES_TYPE "music_notes"
diff --git a/lib/include/rtbuf/portaudio.h b/lib/include/rtbuf/portaudio.h
index 35dd846..74a7a61 100644
--- a/lib/include/rtbuf/portaudio.h
+++ b/lib/include/rtbuf/portaudio.h
@@ -32,12 +32,10 @@ enum {
 typedef short                    t_rtbuf_portaudio_sample;
 typedef t_rtbuf_portaudio_sample t_rtbuf_portaudio_samples[RTBUF_PORTAUDIO_SAMPLES];
 
-#pragma pack(push,1)
 typedef struct rtbuf_portaudio_input_data {
   t_rtbuf_signal signal[RTBUF_PORTAUDIO_CHANNELS];
   t_rtbuf_portaudio_samples samples;
 } s_rtbuf_portaudio_input_data;
-#pragma pack(pop)
 
 int rtbuf_portaudio_input (s_rtbuf *rtb);
 int rtbuf_portaudio_input_start (s_rtbuf *rtb);
@@ -47,12 +45,10 @@ typedef struct rtbuf_portaudio_output_reserved {
   PaStream *stream;
 } s_rtbuf_portaudio_output_reserved;
 
-#pragma pack(push,1)
 typedef struct rtbuf_portaudio_output_data {
   t_rtbuf_portaudio_samples samples;
   s_rtbuf_portaudio_output_reserved reserved;
 } s_rtbuf_portaudio_output_data;
-#pragma pack(pop)
 
 #define RTBUF_PORTAUDIO_OUTPUT_RESERVED_SIZE \
   sizeof(s_rtbuf_portaudio_output_reserved)
diff --git a/lib/include/rtbuf/reverb.h b/lib/include/rtbuf/reverb.h
index 7e7fad1..a2f9f1f 100644
--- a/lib/include/rtbuf/reverb.h
+++ b/lib/include/rtbuf/reverb.h
@@ -25,7 +25,6 @@
 #define RTBUF_REVERB_FDN4_DELAY_SAMPLES_MAX \
   RTBUF_SIGNAL_SAMPLERATE
 
-#pragma pack(push,1)
 typedef struct rtbuf_reverb_fdn4_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal_sample d0[RTBUF_REVERB_FDN4_DELAY_SAMPLES_MAX];
@@ -34,7 +33,6 @@ typedef struct rtbuf_reverb_fdn4_data {
         t_rtbuf_signal_sample d3[RTBUF_REVERB_FDN4_DELAY_SAMPLES_MAX];
         unsigned int pos;
 } s_rtbuf_reverb_fdn4_data;
-#pragma pack(pop)
 
 enum {
         RTBUF_REVERB_FDN4_IN_SIGNAL = 0,
diff --git a/lib/include/rtbuf/signal.h b/lib/include/rtbuf/signal.h
index 784cb3a..cf2986b 100644
--- a/lib/include/rtbuf/signal.h
+++ b/lib/include/rtbuf/signal.h
@@ -61,7 +61,6 @@ const double g_rtbuf_signal_default_frequency;
 
 /* butterworth bandpass filter, second order */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_bandpass2_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal_sample x1;
@@ -73,7 +72,6 @@ typedef struct rtbuf_signal_bandpass2_data {
         t_rtbuf_signal_sample y3;
         t_rtbuf_signal_sample y4;
 } s_rtbuf_signal_bandpass2_data;
-#pragma pack(pop)
 
 enum {
   RTBUF_SIGNAL_BANDPASS_IN_SIGNAL = 0,
@@ -91,13 +89,11 @@ int rtbuf_signal_bandpass2_start (s_rtbuf *rtb);
 #define RTBUF_SIGNAL_DELAY_SAMPLES_MAX (RTBUF_SIGNAL_DELAY_MAX *       \
                                          RTBUF_SIGNAL_SAMPLERATE)
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_delay_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal_sample in[RTBUF_SIGNAL_DELAY_SAMPLES_MAX];
         unsigned int pos;
 } s_rtbuf_signal_delay_data;
-#pragma pack(pop)
 
 enum {
         RTBUF_SIGNAL_DELAY_IN_SIGNAL = 0,
@@ -111,7 +107,6 @@ int rtbuf_signal_delay_start (s_rtbuf *rtb);
 
 /* butterworth second order 10 band equalizer */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_equalizer10_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal signal32;
@@ -169,7 +164,6 @@ typedef struct rtbuf_signal_equalizer10_data {
         t_rtbuf_signal_sample y9_3;
         t_rtbuf_signal_sample y9_4;
 } s_rtbuf_signal_equalizer10_data;
-#pragma pack(pop)
 
 enum {
   RTBUF_SIGNAL_EQUALIZER10_IN_SIGNAL = 0,
@@ -195,7 +189,6 @@ int rtbuf_signal_equalizer10_start (s_rtbuf *rtb);
 #define RTBUF_SIGNAL_FLANGER_SAMPLES_MAX (RTBUF_SIGNAL_FLANGER_MAX * \
                                           RTBUF_SIGNAL_SAMPLERATE)
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_flanger_data {
   t_rtbuf_signal signal;
   double phase;
@@ -203,7 +196,6 @@ typedef struct rtbuf_signal_flanger_data {
   unsigned int pos;
   unsigned int ds;
 } s_rtbuf_signal_flanger_data;
-#pragma pack(pop)
 
 enum {
   RTBUF_SIGNAL_FLANGER_IN_SIGNAL = 0,
@@ -219,13 +211,11 @@ int rtbuf_signal_flanger_start (s_rtbuf *rtb);
 
 /* butterworth hipass filter, first order */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_hipass_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal_sample x1;
         t_rtbuf_signal_sample y1;
 } s_rtbuf_signal_hipass_data;
-#pragma pack(pop)
 
 enum {
   RTBUF_SIGNAL_HIPASS_IN_SIGNAL = 0,
@@ -238,7 +228,6 @@ int rtbuf_signal_hipass_start (s_rtbuf *rtb);
 
 /* butterworth hipass filter, second order */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_hipass2_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal_sample x1;
@@ -246,14 +235,12 @@ typedef struct rtbuf_signal_hipass2_data {
         t_rtbuf_signal_sample y1;
         t_rtbuf_signal_sample y2;
 } s_rtbuf_signal_hipass2_data;
-#pragma pack(pop)
 
 int rtbuf_signal_hipass2 (s_rtbuf *rtb);
 int rtbuf_signal_hipass2_start (s_rtbuf *rtb);
 
 /* butterworth hipass filter, third order */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_hipass3_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal_sample x1;
@@ -263,14 +250,12 @@ typedef struct rtbuf_signal_hipass3_data {
         t_rtbuf_signal_sample y2;
         t_rtbuf_signal_sample y3;
 } s_rtbuf_signal_hipass3_data;
-#pragma pack(pop)
 
 int rtbuf_signal_hipass3 (s_rtbuf *rtb);
 int rtbuf_signal_hipass3_start (s_rtbuf *rtb);
 
 /* butterworth hipass filter, fourth order */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_hipass4_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal_sample x1;
@@ -282,14 +267,12 @@ typedef struct rtbuf_signal_hipass4_data {
         t_rtbuf_signal_sample y3;
         t_rtbuf_signal_sample y4;
 } s_rtbuf_signal_hipass4_data;
-#pragma pack(pop)
 
 int rtbuf_signal_hipass4 (s_rtbuf *rtb);
 int rtbuf_signal_hipass4_start (s_rtbuf *rtb);
 
 /* butterworth hipass filter, fifth order */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_hipass5_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal_sample x1;
@@ -303,20 +286,17 @@ typedef struct rtbuf_signal_hipass5_data {
         t_rtbuf_signal_sample y4;
         t_rtbuf_signal_sample y5;
 } s_rtbuf_signal_hipass5_data;
-#pragma pack(pop)
 
 int rtbuf_signal_hipass5 (s_rtbuf *rtb);
 int rtbuf_signal_hipass5_start (s_rtbuf *rtb);
 
 /* butterworth lowpass filter, first order */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_lowpass_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal_sample x1;
         t_rtbuf_signal_sample y1;
 } s_rtbuf_signal_lowpass_data;
-#pragma pack(pop)
 
 enum {
   RTBUF_SIGNAL_LOWPASS_IN_SIGNAL = 0,
@@ -329,7 +309,6 @@ int rtbuf_signal_lowpass_start (s_rtbuf *rtb);
 
 /* butterworth lowpass filter, second order */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_lowpass2_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal_sample x1;
@@ -337,14 +316,12 @@ typedef struct rtbuf_signal_lowpass2_data {
         t_rtbuf_signal_sample y1;
         t_rtbuf_signal_sample y2;
 } s_rtbuf_signal_lowpass2_data;
-#pragma pack(pop)
 
 int rtbuf_signal_lowpass2 (s_rtbuf *rtb);
 int rtbuf_signal_lowpass2_start (s_rtbuf *rtb);
 
 /* butterworth lowpass filter, third order */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_lowpass3_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal_sample x1;
@@ -354,14 +331,12 @@ typedef struct rtbuf_signal_lowpass3_data {
         t_rtbuf_signal_sample y2;
         t_rtbuf_signal_sample y3;
 } s_rtbuf_signal_lowpass3_data;
-#pragma pack(pop)
 
 int rtbuf_signal_lowpass3 (s_rtbuf *rtb);
 int rtbuf_signal_lowpass3_start (s_rtbuf *rtb);
 
 /* butterworth lowpass filter, fourth order */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_lowpass4_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal_sample x1;
@@ -373,14 +348,12 @@ typedef struct rtbuf_signal_lowpass4_data {
         t_rtbuf_signal_sample y3;
         t_rtbuf_signal_sample y4;
 } s_rtbuf_signal_lowpass4_data;
-#pragma pack(pop)
 
 int rtbuf_signal_lowpass4 (s_rtbuf *rtb);
 int rtbuf_signal_lowpass4_start (s_rtbuf *rtb);
 
 /* butterworth lowpass filter, fifth order */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_lowpass5_data {
         t_rtbuf_signal signal;
         t_rtbuf_signal_sample x1;
@@ -394,19 +367,16 @@ typedef struct rtbuf_signal_lowpass5_data {
         t_rtbuf_signal_sample y4;
         t_rtbuf_signal_sample y5;
 } s_rtbuf_signal_lowpass5_data;
-#pragma pack(pop)
 
 int rtbuf_signal_lowpass5 (s_rtbuf *rtb);
 int rtbuf_signal_lowpass5_start (s_rtbuf *rtb);
 
 /* sawtooth */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_sawtooth_data {
   t_rtbuf_signal signal;
   double phase;
 } s_rtbuf_signal_sawtooth_data;
-#pragma pack(pop)
 
 enum {
   RTBUF_SIGNAL_SAWTOOTH_IN_FREQUENCY = 0,
@@ -419,12 +389,10 @@ int rtbuf_signal_sawtooth_start (s_rtbuf *rtb);
 
 /* sinus */
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_sinus_data {
   t_rtbuf_signal signal;
   double phase;
 } s_rtbuf_signal_sinus_data;
-#pragma pack(pop)
 
 enum {
   RTBUF_SIGNAL_SINUS_IN_FREQUENCY = 0,
@@ -444,23 +412,14 @@ enum {
   RTBUF_SIGNAL_SQUARE_IN_N
 };
 
-#pragma pack(push,1)
 typedef struct rtbuf_signal_square_data {
   t_rtbuf_signal signal;
   double phase;
 } s_rtbuf_signal_square_data;
-#pragma pack(pop)
 
 int rtbuf_signal_square (s_rtbuf *rtb);
 int rtbuf_signal_square_start (s_rtbuf *rtb);
 
 const double sqrt2_2;
 
-#ifndef M_SQRT2
-#define M_SQRT2 1.41421356237309504880
-#endif
-#ifndef M_PI
-#define M_PI 3.14159265358979323846
-#endif
-
 #endif /* RTBUF_SIGNAL_H */
diff --git a/lib/include/rtbuf/synth.h b/lib/include/rtbuf/synth.h
index 2a8616a..1d6147e 100644
--- a/lib/include/rtbuf/synth.h
+++ b/lib/include/rtbuf/synth.h
@@ -39,12 +39,10 @@ enum {
   RTBUF_SYNTH_ENVELOPE_STATE_ENDED
 };
 
-#pragma pack(push,1)
 typedef struct rtbuf_synth_adsr_data {
   t_rtbuf_signal signal;
   int state;
 } s_rtbuf_synth_adsr_data;
-#pragma pack(pop)
 
 int rtbuf_synth_adsr (s_rtbuf *rtb);
 int rtbuf_synth_adsr_start (s_rtbuf *rtb);
@@ -65,13 +63,11 @@ enum {
 #define RTBUF_SYNTH_SYNTH_IN_NOTE_OSCILLATOR(i) \
   (RTBUF_SYNTH_SYNTH_IN_NOTE_0_OSCILLATOR + 2 * i)
 
-#pragma pack(push,1)
 typedef struct rtbuf_synth_synth_data
 {
   t_rtbuf_signal signal;
   unsigned int note_n;
 } s_rtbuf_synth_synth_data;
-#pragma pack(pop)
 
 int rtbuf_synth_synth (s_rtbuf *rtb);
 int rtbuf_synth_synth_start (s_rtbuf *rtb);
diff --git a/librtbuf/rtbuf_lib.c b/librtbuf/rtbuf_lib.c
index bc9f3a9..3288b90 100644
--- a/librtbuf/rtbuf_lib.c
+++ b/librtbuf/rtbuf_lib.c
@@ -201,6 +201,20 @@ void rtbuf_lib_proc_in_init_proc (s_rtbuf_proc *proc,
   proc->in_n = i;
 }
 
+unsigned int add_padding (unsigned int offset, unsigned int size)
+{
+  unsigned int align = 1;
+  if (size == 2)
+    align = 2;
+  else if (size == 4)
+    align = 4;
+  else if (size == 8)
+    align = 8;
+  else if (size == 16)
+    align = 16;
+  return (offset + align - 1) / align * align;
+}
+
 void rtbuf_lib_proc_out_init_proc (s_rtbuf_proc *proc,
                                    s_rtbuf_lib_proc_out *out)
 {
@@ -210,15 +224,19 @@ void rtbuf_lib_proc_out_init_proc (s_rtbuf_proc *proc,
     unsigned int offset = 0;
     while (out->name && i < RTBUF_PROC_OUT_MAX) {
       s_rtbuf_proc_out *o = &proc->out[i];
+      size_t size;
       o->name = symbol_intern(out->name);
       o->type = rtbuf_type(out->type);
       assert(o->type);
+      size = (o->type->t.bits + 7) / 8;
+      offset = add_padding(offset, size);
       o->offset = offset;
-      offset += (o->type->t.bits + 7) / 8;
+      offset += size;
       out++;
       i++;
     }
     assert(i < RTBUF_PROC_OUT_MAX);
+    offset = add_padding(offset, 8);
     proc->out_bytes = offset;
     proc->type.bits = offset * 8;
     proc->type.type = DATA_TYPE_BITS;
diff --git a/librtbuf/rtbuf_type.c b/librtbuf/rtbuf_type.c
index c00904a..8bfb46c 100644
--- a/librtbuf/rtbuf_type.c
+++ b/librtbuf/rtbuf_type.c
@@ -106,7 +106,7 @@ s_rtbuf_type * rtbuf_type_parse_array (const char *name)
       return 0;
     buf[rb] = 0;
     size = atoi(&buf[lb + 1]);
-    return rtbuf_type_new(name, element_type->t.bits / 8 * size);
+    return rtbuf_type_new(name, (element_type->t.bits + 7) / 8 * size);
   }
   return 0;
 }