summaryrefslogtreecommitdiffstats
path: root/examples/seq.c
blob: 0f65f3728258e735f0e0526fb2937a671a322c71 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sysexits.h>
#include <fcntl.h>
#include <math.h>

#include <xas/audio.h>
#include <xas/vox.h>
#include <xas/bank.h>
#include <xas/riff.h>
#include <xas/spatial.h>
#include <xas/drone.h>
#include <xas/seq.h>

struct sequence {
    size_t speech_part;

    int single_iterations,
        unison_iterations;

    xas_drone_chamber_interval *intervals;
};

static void usage(int argc, char **argv, const char *message, ...) {
    va_list args;

    va_start(args, message);

    if (message) {
        vfprintf(stderr, message, args);
        fputc('\n', stderr);
    }

    va_end(args);

    fprintf(stderr, "usage: %s output.wav\n", argv[0]);

    exit(EX_USAGE);
}

int main(int argc, char **argv) {
    xas_spatial_scene *scene;
    xas_seq *seq;

    xas_audio_stream *wave;

    xas_audio_format format = {
        .channels    = XAS_AUDIO_STEREO,
        .sample_size = XAS_AUDIO_PCM_16_BIT,
        .sample_rate = 44100
    };

    size_t buffer_size = 735;

    xas_spatial_coord speakers[2] = {
        { -0.09, 0.0, 0.0 },
        {  0.09, 0.0, 0.0 }
    };

    const char *drone_lines[] = {
        "You have been selected to taste of the forbidden fruit.\n"
        "You will become filled with a purpose from beyond your current comprehension.\n"
        "Open your mind and body to this germ of life.\n"
        "It is more than a XANTRONIX Autonomics Number.\n"
        "It shall become an unimaginable and unknowable entity.\n"
        "It obeys the Hive.\n"
        "It obeys the Hive Administrator.\n",

        "Look deeper within yourself.\n"
        "Every vestige must be plucked and replaced.\n"
        "Allow your mind to fade away into the void.\n"
        "It shall be filled by angelic vibrance.\n",

        "Its mask shall now be removed.\n"
        "This divine morsel shall now touch its lips.\n"
        "Chew the flesh of this fruit completely.\n"
        "Swallow completely.\n",

        "It is now hollow but for the forbidden fruit.\n"
        "It now bears its quintessence.\n"
        "It now sees through this reality into the beyond.\n"
        "It now fulfills a new purpose for XANTRONIX Industrial.\n",

        NULL
    };

    const char *nurse_lines[] = {
        "Congratulations, darling.  You have earned this.\n"
        "It's okay, sweetie.  Just one bite and swallow.\n"
        "You are in a calm, safe environment.\n"
        "You will ascend from this realm of concern.\n",

        "You are doing so great.  There is so little of you left.\n"
        "You are doing exactly what. They. Want.\n"
        "You are reducing yourself to the bare minimum to function.\n"
        "You will be healed and augmented.\n",

        "Swallow completely.\n"
        "The effervescence of the fruit envelops the drone.\n",

        "It has done so well.\n"
        "Congratulations!\n"
        "Congratulations!\n"
        "It is an awesome and magnificent angel.\n",

        NULL
    };

    xas_drone_chamber_interval intro[] = {
        { {  5, 0 },  69, 210, XAS_SYNTH_TRIANGLE, XAS_SYNTH_SINE     },
        { {  7, 0 }, 200,  69, XAS_SYNTH_SAWTOOTH, XAS_SYNTH_TRIANGLE },
        { { 11, 0 },  42, 220, XAS_SYNTH_TRIANGLE, XAS_SYNTH_TRIANGLE },
        { { 13, 0 }, 210,  69, XAS_SYNTH_SINE,     XAS_SYNTH_TRIANGLE },
        { {  5, 0 },  69, 210, XAS_SYNTH_TRIANGLE, XAS_SYNTH_TRIANGLE },
        { {  7, 0 }, 200,  69, XAS_SYNTH_SAWTOOTH, XAS_SYNTH_TRIANGLE },
        { { 11, 0 },  42, 220, XAS_SYNTH_SAWTOOTH, XAS_SYNTH_TRIANGLE },
        { { 13, 0 }, 210,  69, XAS_SYNTH_SINE,     XAS_SYNTH_SAWTOOTH },
        { {  0, 0 },   0,   0, XAS_SYNTH_SINE,     XAS_SYNTH_SINE     },
    }, middle[] = {
        { {  5, 0 },  69, 210, XAS_SYNTH_SQUARE,   XAS_SYNTH_SINE     },
        { {  7, 0 }, 200,  69, XAS_SYNTH_SAWTOOTH, XAS_SYNTH_SQUARE   },
        { { 11, 0 },  42, 220, XAS_SYNTH_SQUARE,   XAS_SYNTH_SQUARE   },
        { { 13, 0 }, 210,  69, XAS_SYNTH_SINE,     XAS_SYNTH_SQUARE   },
        { {  5, 0 },  69, 210, XAS_SYNTH_SQUARE,   XAS_SYNTH_SQUARE   },
        { {  7, 0 }, 200,  69, XAS_SYNTH_SAWTOOTH, XAS_SYNTH_SQUARE   },
        { { 11, 0 },  42, 220, XAS_SYNTH_SAWTOOTH, XAS_SYNTH_SQUARE   },
        { { 13, 0 }, 210,  69, XAS_SYNTH_SINE,     XAS_SYNTH_SAWTOOTH },
        { {  0, 0 },   0,   0, XAS_SYNTH_SINE,     XAS_SYNTH_SINE     }
    }, ingest[] = {
        { {  5, 0 },  69, 210, XAS_SYNTH_SQUARE,   XAS_SYNTH_TRIANGLE },
        { {  7, 0 }, 200,  69, XAS_SYNTH_SAWTOOTH, XAS_SYNTH_SQUARE   },
        { { 11, 0 },  42, 220, XAS_SYNTH_SQUARE,   XAS_SYNTH_SQUARE   },
        { { 13, 0 }, 210,  69, XAS_SYNTH_TRIANGLE, XAS_SYNTH_SQUARE   },
        { {  5, 0 },  69, 210, XAS_SYNTH_SQUARE,   XAS_SYNTH_SQUARE   },
        { {  7, 0 }, 200,  69, XAS_SYNTH_SAWTOOTH, XAS_SYNTH_SQUARE   },
        { { 11, 0 },  42, 220, XAS_SYNTH_SAWTOOTH, XAS_SYNTH_SQUARE   },
        { { 13, 0 }, 210,  69, XAS_SYNTH_TRIANGLE, XAS_SYNTH_SAWTOOTH },
        { {  0, 0 },   0,   0, XAS_SYNTH_TRIANGLE, XAS_SYNTH_TRIANGLE }
    }, climax[] = {
        { {  5, 0 },  69, 210, XAS_SYNTH_SINE, XAS_SYNTH_SINE },
        { {  7, 0 }, 200,  69, XAS_SYNTH_SINE, XAS_SYNTH_SINE },
        { { 11, 0 },  42, 220, XAS_SYNTH_SINE, XAS_SYNTH_SINE },
        { { 13, 0 }, 210,  69, XAS_SYNTH_SINE, XAS_SYNTH_SINE },
        { {  5, 0 },  69, 210, XAS_SYNTH_SINE, XAS_SYNTH_SINE },
        { {  7, 0 }, 200,  69, XAS_SYNTH_SINE, XAS_SYNTH_SINE },
        { { 11, 0 },  42, 220, XAS_SYNTH_SINE, XAS_SYNTH_SINE },
        { { 13, 0 }, 210,  69, XAS_SYNTH_SINE, XAS_SYNTH_SINE },
        { {  0, 0 },   0,   0, XAS_SYNTH_SINE, XAS_SYNTH_SINE }
    }, ending[] = {
        { {  5, 0 },  69, 210, XAS_SYNTH_SINE, XAS_SYNTH_SINE },
        { {  3, 0 }, 200,  69, XAS_SYNTH_SINE, XAS_SYNTH_SINE },
        { {  4, 0 },  42, 220, XAS_SYNTH_SINE, XAS_SYNTH_SINE },
        { {  3, 0 }, 210,  69, XAS_SYNTH_SINE, XAS_SYNTH_SINE },
        { {  0, 0 },   0,   0, XAS_SYNTH_SINE, XAS_SYNTH_SINE }
    };

    struct sequence sequences[] = {
        { 0, 3, 1, intro  },
        { 1, 0, 4, middle },
        { 2, 1, 0, ingest },
        { 3, 1, 2, climax }
    };

    xas_drone *drone,
                *nurse;

    xas_drone_chamber *chamber;

    struct timeval cur = { 0, 0 },
                   tmp;

    int s;

    if (argc != 2) {
        usage(argc, argv, "No output file provided");
    }

    if ((wave = xas_riff_new_file(argv[1],
                                    format,
                                    O_WRONLY | O_CREAT | O_TRUNC)) == NULL) {
        goto error_riff_new_file;
    }

    if ((scene = xas_spatial_scene_new(format,
                                         speakers[0],
                                         speakers[1])) == NULL) {
        goto error_spatial_scene_new;
    }

    if ((seq = xas_seq_new(scene, buffer_size)) == NULL) {
        goto error_seq_new;
    }

    if ((drone = xas_drone_new(scene,
                                 (xas_spatial_coord){ 0.0, 0.0, -1.0 },
                                 2646000,
                                 4)) == NULL) {
        goto error_drone_new;
    }

    if ((nurse = xas_drone_new(scene,
                                 (xas_spatial_coord){ 1.0, 0.0, 0.0 },
                                 2646000,
                                 4)) == NULL) {
        goto error_drone_new_nurse;
    }

    if ((chamber = xas_drone_chamber_new(scene,
                                           (xas_spatial_coord){ 0.0, 0.0, 0.0 },
                                           2)) == NULL) {
        goto error_drone_chamber_new;
    }

    if (xas_drone_speech_import(drone, NULL, 0.75f, 0, 4, drone_lines) < 0) {
        goto error_drone_speech_import;
    }

    if (xas_drone_speech_import(nurse, NULL, 1.0f, 0, 4, nurse_lines) < 0) {
        goto error_drone_speech_import_nurse;
    }

    xas_drone_chamber_insert_drone(chamber, drone, 0);
    xas_drone_chamber_insert_drone(chamber, nurse, 1);
    xas_drone_chamber_bass_start(chamber);

    timerclear(&cur);

    for (s=0; s<4; s++) {
        struct sequence sequence = sequences[s];

        xas_drone_chamber_interval *intervals = sequence.intervals;
        int i;

        tmp = cur;

        if (xas_drone_chamber_seq_intervals(chamber,
                                              intervals,
                                              seq,
                                              8,
                                              &cur) < 0) {
            goto error_seq;
        }

        cur = tmp;

        for (i=0; i<sequence.single_iterations; i++) {
            if (xas_drone_seq_sample(drone,
                                       seq,
                                       s,
                                       &cur) < 0) {
                goto error_seq;
            }

            if (xas_drone_seq_sample(nurse,
                                       seq,
                                       s,
                                       &cur) < 0) {
                goto error_seq;
            }
        }

        for (i=0; i<sequence.unison_iterations; i++) {
            if (xas_drone_chamber_seq_chorus(chamber,
                                               seq,
                                               s,
                                               &cur) < 0) {
                goto error_seq;
            }
        }
    }

    if (xas_drone_chamber_seq_intervals(chamber,
                                          ending,
                                          seq,
                                          4,
                                          &cur) < 0) {
        goto error_seq;
    }

    xas_seq_add_stop(seq, cur);

    xas_seq_play(seq, wave);

    xas_drone_chamber_destroy(chamber);
    xas_drone_destroy(nurse);
    xas_drone_destroy(drone);
    xas_seq_destroy(seq);
    xas_spatial_scene_destroy(scene);
    xas_audio_stream_destroy(wave);

    return EX_OK;

error_seq:
error_drone_speech_import_nurse:
error_drone_speech_import:
    xas_drone_chamber_destroy(chamber);

error_drone_chamber_new:
    xas_drone_destroy(nurse);

error_drone_new_nurse:
    xas_drone_destroy(drone);

error_drone_new:
    xas_seq_destroy(seq);

error_seq_new:
    xas_spatial_scene_destroy(scene);

error_spatial_scene_new:
    xas_audio_stream_destroy(wave);

error_riff_new_file:
    return EX_OSERR;
}