summaryrefslogtreecommitdiffstats
path: root/examples/pod.c
blob: 156968c734b0a4334ede0bd0f87215f28717f265 (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
#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>

static int drone_id_say(xas_drone_vox *vox,
                        const char *drone_id) {
    const char *numbers[10] = {
        "zero", "one", "two",   "three", "four",
        "five", "six", "seven", "eight", "nine"
    };

    size_t len = strlen(drone_id),
           i;

    for (i=0; i<len; i++) {
        char c = drone_id[i];

        if (c == '-') {
            if (xas_drone_vox_say(vox, " dash") < 0) {
                goto error_drone_vox_say;
            }
        } else if (c >= 'a' && c <= 'z') {
            c -= 0x20;

            if (xas_drone_vox_sayf(vox, " %c", c) < 0) {
                goto error_drone_vox_say;
            }
        } else if (c >= 'A' && c <= 'Z') {
            if (xas_drone_vox_sayf(vox, " %c", c) < 0) {
                goto error_drone_vox_say;
            }
        } else if (c >= '0' && c <= '9') {
            int n = (int)(c - '0');

            if (xas_drone_vox_sayf(vox, " %s", numbers[n]) < 0) {
                goto error_drone_vox_say;
            }
        }
    }

    return 0;

error_drone_vox_say:
    return -1;
}

static int drone_part_say(xas_drone_vox *vox, const char *drone_id) {
    const char *id_lines[2] = {
        " is obedient.\n",
        " is empty.\n"
    };

    const char *speech = "Obey XANTRONIX Industrial.\n"
                         "It is just a XANTRONIX semi-autonomous number.\n"
                         "It obeys the Hive.\n"
                         "It obeys the Hive Administrator.\n";

    int i;

    for (i=0; i<2; i++) {
        if (drone_id_say(vox, drone_id) < 0) {
            goto error_say;
        }

        if (xas_drone_vox_say(vox, id_lines[i]) < 0) {
            goto error_say;
        }
    }

    if (xas_drone_vox_say(vox, speech) < 0) {
        goto error_say;
    }

    return 0;

error_say:
    return -1;
}

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 *nurse_part = "Relax, honey.  There is no point trying to escape.\n"
                             "It's okay, sweetie.  This one is right here with you.\n"
                             "You are in a calm, safe environment.\n"
                             "You will be cared for.\n";

    xas_drone_chamber_interval intervals[] = {
        { {  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     }
    };

    xas_drone *drone,
                *nurse;

    xas_drone_vox *drone_vox;

    xas_drone_chamber *chamber;

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

    int i;

    if (argc < 2) {
        usage(argc, argv, "No drone ID provided");
    } else if (argc < 3) {
        usage(argc, argv, "No output file provided");
    }

    if ((wave = xas_riff_new_file(argv[2],
                                    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 ((drone_vox = xas_drone_vox_new(drone)) == NULL) {
        goto error_drone_vox_new;
    }

    xas_drone_vox_set_speed(drone_vox, 0.75f);

    if (drone_part_say(drone_vox, argv[1]) < 0) {
        goto error_drone_part_say;
    }

    if (xas_drone_vox_save(drone_vox, 0) < 0) {
        goto error_drone_vox_save;
    }

    if (xas_drone_speech_import(nurse, NULL, 1.0f, 0, 1, &nurse_part) < 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);

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

    timerclear(&cur);

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

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

    tmp = cur;

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

    cur = tmp;

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

    xas_seq_add_stop(seq, cur);

    xas_seq_play(seq, wave);

    xas_drone_vox_destroy(drone_vox);
    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_vox_save:
error_drone_part_say:
    xas_drone_vox_destroy(drone_vox);

error_drone_vox_new:
    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;
}