diff options
Diffstat (limited to 'src/uxn/devices')
-rw-r--r-- | src/uxn/devices/apu.c | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/src/uxn/devices/apu.c b/src/uxn/devices/apu.c index ba9fe88..c3168c2 100644 --- a/src/uxn/devices/apu.c +++ b/src/uxn/devices/apu.c | |||
@@ -202,29 +202,40 @@ void sound_mix() { | |||
202 | if (chan->data == NULL) { | 202 | if (chan->data == NULL) { |
203 | continue; | 203 | continue; |
204 | } | 204 | } |
205 | for(size_t i = 0; i < AUDIO_BUF_LEN; i++) { | 205 | if (chan->pos + chan->inc * AUDIO_BUF_LEN >= chan->length) { |
206 | // Remember we are using fixed point values. | 206 | // Sample is going to finish, need to consider this for looping or |
207 | mix_buffer[i] += chan->data[chan->pos >> 12] * chan->vol; | 207 | // stopping. |
208 | chan->pos += chan->inc; | 208 | for(size_t i = 0; i < AUDIO_BUF_LEN; i++) { |
209 | 209 | // Remember we are using fixed point values. | |
210 | if (chan->pos >= chan->length) { | 210 | mix_buffer[i] += chan->data[chan->pos >> 12] * chan->vol; |
211 | // If looping is not active disable the channel. | 211 | chan->pos += chan->inc; |
212 | if (chan->loop_length == 0) { | 212 | |
213 | chan->data = NULL; | 213 | if (chan->pos >= chan->length) { |
214 | break; | 214 | // If looping is not active disable the channel. |
215 | } | 215 | if (chan->loop_length == 0) { |
216 | 216 | chan->data = NULL; | |
217 | // Loop the sample. | 217 | break; |
218 | while (chan->pos >= chan->length) { | 218 | } |
219 | chan->pos -= chan->loop_length; | 219 | |
220 | // Loop the sample. | ||
221 | while (chan->pos >= chan->length) { | ||
222 | chan->pos -= chan->loop_length; | ||
223 | } | ||
220 | } | 224 | } |
221 | } | 225 | } |
226 | } else { | ||
227 | // Sample still have room to go, no need to check for looping or | ||
228 | // end of sample. | ||
229 | for(size_t i = 0; i < AUDIO_BUF_LEN; i++) { | ||
230 | mix_buffer[i] += chan->data[chan->pos>>12] * chan->vol; | ||
231 | chan->pos += chan->inc; | ||
232 | } | ||
222 | } | 233 | } |
223 | } | 234 | } |
224 | 235 | ||
225 | // Downsample and copy to the playing buffer. | 236 | // Downsample and copy to the playing buffer. |
226 | for (size_t i = 0; i < AUDIO_BUF_LEN; ++i) { | 237 | for (size_t i = 0; i < AUDIO_BUF_LEN; ++i) { |
227 | // >> 8 to divide off the volume, >> 2 to divide by 4 channels to | 238 | // >> 6 to divide off the volume, >> 2 to divide by 4 channels to |
228 | // prevent overflow. | 239 | // prevent overflow. |
229 | audio.current_buffer[i] = mix_buffer[i] >> 8; | 240 | audio.current_buffer[i] = mix_buffer[i] >> 8; |
230 | } | 241 | } |