|
| 1 | +#include <math.h> |
1 | 2 | #include "hw/aica/aica.h" |
2 | 3 | #include "core/log.h" |
3 | 4 | #include "core/option.h" |
@@ -80,6 +81,82 @@ struct aica { |
80 | 81 | FILE *recording; |
81 | 82 | }; |
82 | 83 |
|
| 84 | +static int32_t mvol_scale[16]; |
| 85 | +static int32_t tl_scale[256]; |
| 86 | + |
| 87 | +static void aica_init_tables() { |
| 88 | + static int initialized = 0; |
| 89 | + |
| 90 | + if (initialized) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + initialized = 1; |
| 95 | + |
| 96 | + /* the MVOL register adjusts the output level based on the table: |
| 97 | +
|
| 98 | + MVOL ∆level |
| 99 | + ---------------- |
| 100 | + 0 -MAX db |
| 101 | + 1 -42 db |
| 102 | + 2 -39 db |
| 103 | + ... |
| 104 | + n -42 + (n-1) db |
| 105 | +
|
| 106 | + sound pressure level is defined as: |
| 107 | + ∆level = 20 * log10(out / in) |
| 108 | +
|
| 109 | + out can therefor be calculated as: |
| 110 | + out = in * pow(10, ∆level / 20) |
| 111 | +
|
| 112 | + this can be approximated using MVOL instead of ∆level as: |
| 113 | + out = in / pow(2, (MVOL - i) / 2) */ |
| 114 | + |
| 115 | + for (int i = 0; i < 16; i++) { |
| 116 | + /* 0 is a special case that mutes the output */ |
| 117 | + if (i == 0) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + /* a 32-bit int is used for the scale, leaving 15 bits for the fraction */ |
| 122 | + mvol_scale[i] = (1 << 15) / pow(2.0f, (15 - i) / 2.0f); |
| 123 | + } |
| 124 | + |
| 125 | + /* each channel's TL register adjusts the output level based on the table: |
| 126 | +
|
| 127 | + TL ∆level |
| 128 | + -------------- |
| 129 | + bit 0 -0.4 db |
| 130 | + bit 1 -0.8 db |
| 131 | + bit 2 -1.5 db |
| 132 | + bit 3 -3.0 db |
| 133 | + bit 4 -6.0 db |
| 134 | + bit 5 -12.0 db |
| 135 | + bit 6 -24.0 db |
| 136 | + bit 7 -48.0 db |
| 137 | +
|
| 138 | + this can be approximated using TL as: |
| 139 | + out = in / pow(2, TL / 16) */ |
| 140 | + |
| 141 | + for (int i = 0; i < 256; i++) { |
| 142 | + /* a 32-bit int is used for the scale, leaving 15 bits for the fraction */ |
| 143 | + tl_scale[i] = (1 << 15) / pow(2.0f, i / 16.0f); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +static inline int32_t aica_adjust_master_volume(struct aica *aica, int32_t in) { |
| 148 | + int32_t y = mvol_scale[aica->common_data->MVOL]; |
| 149 | + /* truncate fraction */ |
| 150 | + return (in * y) >> 15; |
| 151 | +} |
| 152 | + |
| 153 | +static inline int32_t aica_adjust_channel_volume(struct aica_channel *ch, |
| 154 | + int32_t in) { |
| 155 | + int32_t y = tl_scale[ch->data->TL]; |
| 156 | + /* truncate fraction */ |
| 157 | + return (in * y) >> 15; |
| 158 | +} |
| 159 | + |
83 | 160 | static void aica_decode_adpcm(int32_t data, int32_t prev, int32_t prev_quant, |
84 | 161 | int32_t *next, int32_t *next_quant) { |
85 | 162 | /* the decoded value (n) = (1 - 2 * l4) * (l3 + l2/2 + l1/4 + 1/8) * quantized |
@@ -381,6 +458,9 @@ static int32_t aica_channel_update(struct aica *aica, struct aica_channel *ch) { |
381 | 458 |
|
382 | 459 | uint32_t pos = AICA_OFFSET_POS(ch->offset); |
383 | 460 | uint32_t frac = AICA_OFFSET_FRAC(ch->offset); |
| 461 | + |
| 462 | + /* the data returned is a signed 16-bit PCM data, but signed 32-bit values are |
| 463 | + used intermediately to avoid dealing with overflows until the end */ |
384 | 464 | int32_t next_sample = 0; |
385 | 465 | int32_t next_quant = 0; |
386 | 466 |
|
@@ -452,10 +532,13 @@ static void aica_generate_frames(struct aica *aica, int num_frames) { |
452 | 532 | for (int j = 0; j < AICA_NUM_CHANNELS; j++) { |
453 | 533 | struct aica_channel *ch = &aica->channels[j]; |
454 | 534 | int32_t s = aica_channel_update(aica, ch); |
455 | | - l += s; |
456 | | - r += s; |
| 535 | + l += aica_adjust_channel_volume(ch, s); |
| 536 | + r += aica_adjust_channel_volume(ch, s); |
457 | 537 | } |
458 | 538 |
|
| 539 | + l = aica_adjust_master_volume(aica, l); |
| 540 | + r = aica_adjust_master_volume(aica, r); |
| 541 | + |
459 | 542 | buffer[i * 2 + 0] = CLAMP(l, INT16_MIN, INT16_MAX); |
460 | 543 | buffer[i * 2 + 1] = CLAMP(r, INT16_MIN, INT16_MAX); |
461 | 544 | } |
@@ -746,6 +829,8 @@ void aica_destroy(struct aica *aica) { |
746 | 829 | } |
747 | 830 |
|
748 | 831 | struct aica *aica_create(struct dreamcast *dc) { |
| 832 | + aica_init_tables(); |
| 833 | + |
749 | 834 | struct aica *aica = dc_create_device(dc, sizeof(struct aica), "aica", |
750 | 835 | &aica_init, &aica_debug_menu); |
751 | 836 | return aica; |
|
0 commit comments