aboutsummaryrefslogtreecommitdiff
path: root/sfx.c
blob: 2008de54e09b0cee1b1c468f4e2b928faf7912d9 (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
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
// sfxed for beeper engine
// Copyright (C) 2021 by Juan J. Martinez <jjm@usebox.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include "SDL.h"

#include "zymosis.h"

#define LOCAL
#include "player.h"
#undef LOCAL

#include "sfx.h"

#define PLAYER_ADDR 20480
#define EFX_TABLE_ADDR 32000
#define EFX_IN_ADDR (EFX_TABLE_ADDR - 1)

// 48K timings
#define TSTATES_PER_FRAME 69888
#define TSTATE_STEP 16

uint8_t memory[65536];
uint8_t sound_state;
uint8_t exit_state;

#define MAX_SAMPLES (44100*5)
uint16_t samples[MAX_SAMPLES];
uint32_t nsamples;

int load_sfx(char *filename, BeeperSfx *table, uint8_t n)
{
    FILE *fd;
    char header[8];
    uint8_t entries = 0;
    uint8_t type;

    fd = fopen(filename, "rt");
    if (!fd)
    {
        fprintf(stderr, "ERROR: failed to load %s\n", filename);
        return -1;
    }

    if (fgets(header, 7, fd) == NULL || strcmp(header, ";SFXv1"))
    {
        fprintf(stderr, "ERROR: %s doesn't look like a valid SFX file\n", filename);
        return -1;
    }

    memset(table, 0, sizeof(struct beeper_sfx) * n);

    while (!feof(fd))
    {
        if (fscanf(fd, "%8s %hhd %hhd %hhd %hhd %hhd\n",
                   table[entries].name,
                   &type,
                   &table[entries].frames,
                   &table[entries].freq,
                   &table[entries].slide,
                   &table[entries].next
                  ) != 6)
        {
            fprintf(stderr, "ERROR: failed to load %s\n", filename);
            fclose(fd);
            return -1;
        }
        // prevent invalid frequency
        table[entries].freq = table[entries].freq ? table[entries].freq : 1;
        // move to int
        table[entries].type = type < 3 ? type : 0;

        entries++;
        if (entries == n)
        {
            fprintf(stderr, "WARN: read max %d entries\n", entries);
            break;
        }
    }

    fclose(fd);

    return entries;
}

int save_sfx(char *filename, BeeperSfx *table, uint8_t n)
{
    FILE *fd;

    fd = fopen(filename, "wt");
    if (!fd)
    {
        fprintf(stderr, "ERROR: failed to save %s\n", filename);
        return -1;
    }

    if (fprintf(fd, ";SFXv1\n") != 7)
    {
        fprintf(stderr, "ERROR: failed to write to %s\n", filename);
        return -1;
    }

    for (int i = 0; i < n; i++)
    {
        if (fprintf(fd, "%s %hhd %hhd %hhd %hhd %hhd\n",
                    table[i].name,
                    table[i].type,
                    table[i].frames,
                    table[i].freq,
                    table[i].slide,
                    table[i].next
                   ) == -1)
        {
            fprintf(stderr, "ERROR: failed to write %s\n", filename);
            fclose(fd);
            return -1;
        }
    }

    fclose(fd);

    return n;
}

int export_c(char *filename, BeeperSfx *table, uint8_t n)
{
    FILE *fd;

    fd = fopen(filename, "wt");
    if (!fd)
    {
        fprintf(stderr, "ERROR: failed to save %s\n", filename);
        return -1;
    }

    fprintf(fd, "#ifndef _SFX_H\n#define _SFX_H\n\n");

    fprintf(fd, "enum sfx_enum {\n");
    for (int  i = 0; i < n; i++)
    {
        fprintf(fd, "\t// %s\n", table[i].name);
        if (i == 0)
            fprintf(fd, "\tSFX%d = 1,\n", i + 1);
        else
            fprintf(fd, "\tSFX%d,\n", i + 1);
    }
    fprintf(fd, "};\n\n");

    fprintf(fd, "const struct beeper_sfx sfx_table[] = {\n");
    for (int i = 0; i < n; i++)
    {
        if (fprintf(fd, "\t{ %hhu, %hhu, %hhu, %hhu, %hhu },\n",
                    table[i].type,
                    table[i].frames,
                    table[i].freq,
                    table[i].slide,
                    table[i].next
                   ) == -1)
        {
            fprintf(stderr, "ERROR: failed to write %s\n", filename);
            fclose(fd);
            return -1;
        }
    }
    fprintf(fd, "};\n\n");

    fprintf(fd, "#endif /* _SFX_H */\n");

    fclose(fd);

    return n;
}

int export_bin(char *filename, BeeperSfx *table, uint8_t n)
{
    FILE *fd;

    fd = fopen(filename, "wb");
    if (!fd)
    {
        fprintf(stderr, "ERROR: failed to save %s\n", filename);
        return -1;
    }

    for (int i = 0; i < n; i++)
    {
        if (fwrite(&table[i].type, 1, 1, fd) != 1
                || fwrite(&table[i].frames, 1, 1, fd) != 1
                || fwrite(&table[i].freq, 1, 1, fd) != 1
                || fwrite(&table[i].slide, 1, 1, fd) != 1
                || fwrite(&table[i].next, 1, 1, fd) != 1)
        {
            fclose(fd);
            fprintf(stderr, "ERROR: failed to write %s\n", filename);
            return -1;
        }
    }

    fclose(fd);

    return n;
}

int export_sfx(char *filename, BeeperSfx *table, uint8_t n)
{
    if (filename[strlen(filename) - 1] == 'h')
        return export_c(filename, table, n);
    else
        return export_bin(filename, table, n);
}

void z80_mem_write(Z80Info *z80, uint16_t addr, uint8_t value, Z80MemIOType mio)
{
    memory[addr] = value;
}

uint8_t z80_mem_read(Z80Info *z80, uint16_t addr, Z80MemIOType mio)
{
    return memory[addr];
}


uint8_t z80_port_in(Z80Info *z80, uint16_t port, Z80PIOType pio)
{
    return 0;
}

void z80_port_out(Z80Info *z80, uint16_t port, uint8_t value, Z80PIOType pio)
{
    switch (port & 0xff)
    {
        case 0xfe:
            sound_state = (value & 16);
            break;

        default:
            exit_state = 1;
            break;
    }
}

SDL_AudioDeviceID dev = 0;

uint8_t play_samples()
{
    SDL_AudioSpec want, have;

    SDL_memset(&want, 0, sizeof(want));
    want.freq = 44100;
    want.format = AUDIO_S16;
    want.channels = 1;
    want.samples = 4096;

    if (!dev) {
        dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
        if (dev == 0) {
            fprintf(stderr, "ERROR: failed to open audio: %s", SDL_GetError());
            return 1;
        }
        SDL_PauseAudioDevice(dev, 0);
    }
    else
        SDL_ClearQueuedAudio(dev);

    if (SDL_QueueAudio(dev, samples, nsamples * 2) != 0)
        fprintf(stderr, "ERROR: playback error: %s\n", SDL_GetError());

    return 0;
}

int play_sfx(uint8_t index, BeeperSfx *table, uint8_t n)
{
    int i;
    uint32_t states, real, out;
    int32_t next_int;
    Z80Info z80;
    uint8_t *p;

    if (index > n)
        return 1;

    memset(&z80, 0, sizeof(Z80Info));
    memset(memory, 0, 65536);
    memcpy(memory + PLAYER_ADDR, player, PLAYER_LEN);

    p = &memory[EFX_TABLE_ADDR];
    for (int i = 0; i < n; i++)
    {
        *p++ = table[i].type & 0xff;
        *p++ = table[i].frames;
        *p++ = table[i].freq;
        *p++ = table[i].slide;
        *p++ = table[i].next;
    }
    memory[EFX_IN_ADDR] = index;

    Z80_ResetCallbacks(&z80);

    z80.memReadFn = z80_mem_read;
    z80.memWriteFn = z80_mem_write;
    z80.portInFn = z80_port_in;
    z80.portOutFn = z80_port_out;

    Z80_Reset(&z80);

    sound_state = 0;
    exit_state = 0;
    nsamples = 0;
    next_int = TSTATES_PER_FRAME;
    states = TSTATE_STEP;
    while (!exit_state)
    {
        for (i = 0, out = 0; i < 4; i++)
        {
            real = Z80_ExecuteTS(&z80, states);
            next_int -= real;

            states = TSTATE_STEP + (TSTATE_STEP - real);
            out += sound_state * 16384 / 16;

            if (next_int < TSTATE_STEP)
            {
                next_int += TSTATES_PER_FRAME;
                Z80_Interrupt(&z80);
            }
        }

        samples[nsamples++] = out >> 2;

        if (nsamples > MAX_SAMPLES)
            break;
    }

    return play_samples();
}