Mercurial > repos > simple16
annotate src/audio.c @ 43:6e7bfe83d2b0
Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 27 Aug 2016 22:38:31 -0700 |
parents | 23bea9b9569f |
children |
rev | line source |
---|---|
24 | 1 #include <stdint.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include "audio.h" | |
5 #include "system.h" | |
6 | |
7 #define BUFFER_INC_RES 1000000000UL | |
8 | |
9 audio *alloc_audio(uint32_t master_clock, uint32_t clock_div, int sample_rate, int buffer_size) | |
10 { | |
11 size_t alloc_size = sizeof(audio) + buffer_size * sizeof(int16_t) * 2; | |
12 audio *context = malloc(alloc_size); | |
13 memset(context, 0, alloc_size); | |
14 context->writebuffer = context->buffer; | |
15 context->playbuffer = context->buffer + buffer_size; | |
16 | |
17 context->buffer_size = buffer_size; | |
18 context->clock_inc = clock_div; | |
19 context->buffer_inc = ((BUFFER_INC_RES * (uint64_t)sample_rate) / (uint64_t)master_clock) * clock_div; | |
20 for (int i = 0; i < NUM_CHANNELS; i++) | |
21 { | |
22 context->value[i] = context->volume[i] << 5; | |
23 } | |
41
23bea9b9569f
Fix silly bug in alloc_audio
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
24 return context; |
24 | 25 } |
26 | |
27 void audio_run(audio *context, uint32_t target) | |
28 { | |
29 while (context->cycles < target) | |
30 { | |
31 for (int i = 0; i < 4; i++) | |
32 { | |
33 if (context->timer_cur[i]) { | |
34 context->timer_cur[i]--; | |
35 if (!context->timer_cur[i]) { | |
36 context->value[i] = context->value[i] ? 0 : (context->volume[i] << 5); | |
37 } | |
38 } else { | |
39 context->timer_cur[i] = context->timer_load[i]; | |
40 } | |
41 } | |
42 context->buffer_fraction += context->buffer_inc; | |
43 if (context->buffer_fraction >= BUFFER_INC_RES) { | |
44 context->buffer_fraction -= BUFFER_INC_RES; | |
45 context->writebuffer[context->buffer_pos++] = | |
46 context->value[0] + context->value[1] + context->value[2] + context->value[3]; | |
47 if (context->buffer_pos == context->buffer_size) { | |
48 int16_t *tmp = context->playbuffer; | |
49 context->playbuffer = context->writebuffer; | |
50 context->writebuffer = tmp; | |
51 system_present_audio(context->playbuffer); | |
52 context->buffer_pos = 0; | |
53 } | |
54 } | |
55 context->cycles += context->clock_inc; | |
56 } | |
57 } | |
58 | |
59 void audio_write_freq(audio *context, int channel, uint16_t value) | |
60 { | |
61 context->timer_cur[channel] = context->timer_load[channel] = value; | |
62 } | |
63 | |
64 void audio_write_vol(audio *context, int pair, uint16_t value) | |
65 { | |
66 int channel = pair * 2; | |
67 context->value[channel] = context->volume[channel] = value >> 8; | |
68 channel++; | |
69 context->value[channel] = context->volume[channel] = value; | |
70 } |