aboutsummaryrefslogtreecommitdiffstats
path: root/src/uxn/devices/apu.c
blob: 2b1c8ff50d661aef79038545546c96b6cef341a2 (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
#define SAMPLE_FREQUENCY 44100
#define NOTE_PERIOD (SAMPLE_FREQUENCY * 0x4000 / 11025)
#define ADSR_STEP (SAMPLE_FREQUENCY / 0xf)

static u16 pitch_table[] = {
    12173, 11490, 10845, 10237, 9662, 9120, 8608, 8125,
    7669, 7238, 6832, 6448, 6086, 5745, 5422, 5118,
    4831, 4560, 4304, 4062, 3834, 3619, 3416, 3224,
    3043, 2872, 2711, 2559, 2415, 2280, 2152, 2031,
    1917, 1809, 1708, 1612, 1521, 1436, 1355, 1279,
    1207, 1140, 1076, 1015, 958, 904, 854, 806,
    760, 718, 677, 639, 603, 570, 538, 507,
    479, 452, 427, 403, 380, 359, 338, 319,
    301, 285, 269, 253, 239, 226, 213, 201,
    190, 179, 169, 159, 150, 142, 134, 126,
    119, 113, 106, 100, 95, 89, 84, 79,
    75, 71, 67, 63, 59, 56, 53, 50,
    47, 44, 42, 39, 37, 35, 33, 31,
    29, 28, 26, 25, 23, 22, 21, 19,
    18, 17, 16, 15, 14, 14, 13, 12,
    2,
};

s8 square_wave[] =  {
    (s8)0x00 + (s8)0x80, (s8)0xFF + (s8)0x80
};

#include "text.h"

//
//       REG_TM0D                    frequency    buffer size
//          |                            |            |
//          V                            V            V
//
// Timer = 62610 = 65536 - (16777216 /  5734), buf = 96
// Timer = 63940 = 65536 - (16777216 / 10512), buf = 176
// Timer = 64282 = 65536 - (16777216 / 13379), buf = 224
// Timer = 64612 = 65536 - (16777216 / 18157), buf = 304
// Timer = 64738 = 65536 - (16777216 / 21024), buf = 352
// Timer = 64909 = 65536 - (16777216 / 26758), buf = 448
// Timer = 65004 = 65536 - (16777216 / 31536), buf = 528
// Timer = 65073 = 65536 - (16777216 / 36314), buf = 608
// Timer = 65118 = 65536 - (16777216 / 40137), buf = 672
// Timer = 65137 = 65536 - (16777216 / 42048), buf = 704
//
// Source: https://deku.gbadev.org/program/sound1.html
#define AUDIO_FREQ    18157
#define AUDIO_BUF_LEN 304
#define AUDIO_TIMER   64612

typedef struct Audio {
    s8 mix_buffer[AUDIO_BUF_LEN * 2];
    s8 *current_buffer;
    u8 active_buffer;
} Audio;

typedef struct AudioChannel {
    // Pointer to the raw data in the ROM.
    s8 *data;
    // Current position in the data (20.12 fixed-point).
    u32 pos;
    // Increment (20.12 fixed-point).
    u32 inc;
    // Volume (0-64, 1.6 fixed-point).
    u32 vol;
    // Sound length (20.12 fixed-point).
    u32 length;
    // Length of looped portion (20.12 fixed-point, 0 to disable looping).
    u32 loop_length;
    // TODO: this should be different?
    u8 pitch;
} AudioChannel;

static Audio audio;

#define POLYPHONY 4
static AudioChannel channels[POLYPHONY];

void
init_sound(void) {
    // Initialize audio buffers/channels.
    audio = (Audio){0};
    for (size_t i = 0; i < POLYPHONY; ++i) {
        channels[i] = (AudioChannel){0};
    }

    // DEBUG: testing channel 0 with square wave
    // channels[0].data = samples;
    // channels[0].length = (LEN(samples) - 1)  << 12;
    // channels[0].pos = 0;
    // channels[0].inc = (44100 << 12) / AUDIO_FREQ;
    // channels[0].vol = 32;
    // channels[0].loop_length = channels[0].length;
    // channels[0].loop_length = 0;

    // Enable the sound chip.
    SOUND_STATUS = SOUND_ENABLE;

    // Set max volume, left-right sound, fifo reset and use timer 0 for
    // DirectSound A.
    SOUND_DSOUND_MASTER = SOUND_DSOUND_RATIO_A
        | SOUND_DSOUND_LEFT_A
        | SOUND_DSOUND_RIGHT_A
        | SOUND_DSOUND_RESET_A;

    // TODO: No pitch selection yet.
    TIMER_DATA_0 = AUDIO_TIMER;
    TIMER_CTRL_0 = TIMER_CTRL_ENABLE;
}

void sound_vsync() {
    // buffer 1 just got over
    if(audio.active_buffer == 1) {
        // Start playing buffer 0
        dma_transfer_copy(SOUND_FIFO_A, audio.mix_buffer, 1, 1,
                DMA_DST_FIXED | DMA_CHUNK_32 | DMA_REFRESH | DMA_REPEAT | DMA_ENABLE);

        // Set the current buffer pointer to the start of buffer 1
        audio.current_buffer = audio.mix_buffer + AUDIO_BUF_LEN;
        audio.active_buffer = 0;
    } else {
        // buffer 0 just got over
        // DMA points to buffer 1 already, so don't bother stopping and resetting it
        // Set the current buffer pointer to the start of buffer 0
        audio.current_buffer = audio.mix_buffer;
        audio.active_buffer = 1;
    }
}

void sound_mix() {
    // Initialize and clear mix_buffer.
    s16 mix_buffer[AUDIO_BUF_LEN];
    u32 fill = 0;
    dma_fill(mix_buffer, fill, sizeof(mix_buffer), 3);

    //  Mix channels into the temporary buffer.
    for (size_t j = 0; j < POLYPHONY; ++j) {
        AudioChannel *ch = &channels[j];
        // Check if channel is active.
        if (ch->data == NULL || ch->pitch >= 108) {
            continue;
        }
        if (ch->pos + ch->inc * AUDIO_BUF_LEN >= ch->length) {
            // Sample is going to finish, need to consider this for looping or
            // stopping.
            for(size_t i = 0; i < AUDIO_BUF_LEN; i++) {
                // Remember we are using fixed point values.
                mix_buffer[i] += (0x80 + (u8)ch->data[ch->pos >> 12]) * ch->vol;
                ch->pos += ch->inc;

                if (ch->pos >= ch->length) {
                    // If looping is not active disable the channel.
                    if (ch->loop_length == 0) {
                        ch->data = NULL;
                        break;
                    }

                    // Loop the sample.
                    while (ch->pos >= ch->length) {
                        ch->pos -= ch->loop_length;
                    }
                }
            }
        } else {
            // Sample still have room to go, no need to check for looping or
            // end of sample.
            for(size_t i = 0; i < AUDIO_BUF_LEN; i++) {
                mix_buffer[i] +=  (0x80 + (u8)ch->data[ch->pos>>12]) * ch->vol;
                ch->pos += ch->inc;
            }
        }
    }

    // Downsample and copy to the playing buffer.
    for (size_t i = 0; i < AUDIO_BUF_LEN; ++i) {
        // >> 6 to divide off the volume, >> 2 to divide by 4 channels to
        // prevent overflow.
        audio.current_buffer[i] = mix_buffer[i] >> 8;
    }
}