|
| 1 | +extern "C" { |
| 2 | +#include "emuapi.h" |
| 3 | +} |
| 4 | + |
| 5 | +#include "AudioPlaySystem.h" |
| 6 | +#include "esp_system.h" |
| 7 | + |
| 8 | +#include "driver/i2s.h" |
| 9 | +#include "freertos/queue.h" |
| 10 | +#include "string.h" |
| 11 | +#define I2S_NUM ((i2s_port_t)0) |
| 12 | + |
| 13 | +volatile uint16_t *Buffer; |
| 14 | +volatile uint16_t BufferSize; |
| 15 | + |
| 16 | + |
| 17 | +static const short square[]={ |
| 18 | +32767,32767,32767,32767, |
| 19 | +32767,32767,32767,32767, |
| 20 | +32767,32767,32767,32767, |
| 21 | +32767,32767,32767,32767, |
| 22 | +32767,32767,32767,32767, |
| 23 | +32767,32767,32767,32767, |
| 24 | +32767,32767,32767,32767, |
| 25 | +32767,32767,32767,32767, |
| 26 | +-32767,-32767,-32767,-32767, |
| 27 | +-32767,-32767,-32767,-32767, |
| 28 | +-32767,-32767,-32767,-32767, |
| 29 | +-32767,-32767,-32767,-32767, |
| 30 | +-32767,-32767,-32767,-32767, |
| 31 | +-32767,-32767,-32767,-32767, |
| 32 | +-32767,-32767,-32767,-32767, |
| 33 | +-32767,-32767,-32767,-32767, |
| 34 | +}; |
| 35 | + |
| 36 | +const short noise[] { |
| 37 | +-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767, |
| 38 | +-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,32767,-32767, |
| 39 | +-32767,-32767,32767,-32767,-32767,-32767,32767,-32767,-32767,-32767,32767,-32767,-32767,-32767,32767,-32767, |
| 40 | +-32767,-32767,32767,-32767,-32767,-32767,32767,-32767,-32767,-32767,32767,-32767,-32767,32767,32767,-32767, |
| 41 | +-32767,-32767,32767,-32767,-32767,32767,32767,-32767,-32767,-32767,32767,-32767,-32767,32767,32767,-32767, |
| 42 | +-32767,-32767,32767,-32767,-32767,32767,32767,-32767,-32767,-32767,32767,-32767,32767,32767,32767,-32767, |
| 43 | +32767,-32767,32767,-32767,-32767,32767,32767,-32767,-32767,-32767,32767,-32767,32767,32767,32767,-32767, |
| 44 | +32767,-32767,32767,-32767,-32767,32767,32767,-32767,-32767,-32767,32767,32767,32767,32767,32767,-32767, |
| 45 | +32767,-32767,32767,-32767,-32767,32767,32767,-32767,-32767,-32767,32767,32767,32767,32767,32767,-32767, |
| 46 | +32767,-32767,32767,-32767,-32767,32767,32767,-32767,-32767,-32767,-32767,32767,32767,32767,-32767,-32767, |
| 47 | +32767,-32767,-32767,-32767,-32767,32767,-32767,-32767,-32767,-32767,32767,32767,32767,32767,32767,-32767, |
| 48 | +32767,-32767,32767,-32767,-32767,32767,32767,-32767,-32767,32767,-32767,32767,32767,32767,-32767,-32767, |
| 49 | +32767,32767,-32767,-32767,-32767,32767,-32767,-32767,-32767,-32767,32767,32767,32767,32767,32767,-32767, |
| 50 | +32767,-32767,32767,-32767,-32767,32767,32767,-32767,32767,32767,-32767,32767,-32767,32767,-32767,-32767, |
| 51 | +32767,32767,-32767,-32767,-32767,32767,-32767,-32767,-32767,-32767,32767,32767,32767,32767,32767,-32767, |
| 52 | +32767,-32767,32767,-32767,-32767,32767,32767,32767,32767,32767,-32767,32767,-32767,32767,-32767,-32767, |
| 53 | +}; |
| 54 | + |
| 55 | +#define NOISEBSIZE 0x100 |
| 56 | + |
| 57 | +typedef struct |
| 58 | +{ |
| 59 | + unsigned int spos; |
| 60 | + unsigned int sinc; |
| 61 | + unsigned int vol; |
| 62 | +} Channel; |
| 63 | + |
| 64 | +volatile bool playing = false; |
| 65 | + |
| 66 | + |
| 67 | +static Channel chan[6] = { |
| 68 | + {0,0,0}, |
| 69 | + {0,0,0}, |
| 70 | + {0,0,0}, |
| 71 | + {0,0,0}, |
| 72 | + {0,0,0}, |
| 73 | + {0,0,0} }; |
| 74 | + |
| 75 | + |
| 76 | +static void snd_Reset(void) |
| 77 | +{ |
| 78 | + chan[0].vol = 0; |
| 79 | + chan[1].vol = 0; |
| 80 | + chan[2].vol = 0; |
| 81 | + chan[3].vol = 0; |
| 82 | + chan[4].vol = 0; |
| 83 | + chan[5].vol = 0; |
| 84 | + chan[0].sinc = 0; |
| 85 | + chan[1].sinc = 0; |
| 86 | + chan[2].sinc = 0; |
| 87 | + chan[3].sinc = 0; |
| 88 | + chan[4].sinc = 0; |
| 89 | + chan[5].sinc = 0; |
| 90 | +} |
| 91 | + |
| 92 | +#ifdef CUSTOM_SND |
| 93 | +extern "C" { |
| 94 | +void SND_Process(void *sndbuffer, int sndn); |
| 95 | +} |
| 96 | +#endif |
| 97 | + |
| 98 | + |
| 99 | +static void snd_Mixer16(uint16_t * stream, int len ) |
| 100 | +{ |
| 101 | + if (playing) |
| 102 | + { |
| 103 | +#ifdef CUSTOM_SND |
| 104 | + SND_Process((void*)stream, len); |
| 105 | +#else |
| 106 | + int i; |
| 107 | + long s; |
| 108 | + //len = len >> 1; |
| 109 | + |
| 110 | + short v0=chan[0].vol; |
| 111 | + short v1=chan[1].vol; |
| 112 | + short v2=chan[2].vol; |
| 113 | + short v3=chan[3].vol; |
| 114 | + short v4=chan[4].vol; |
| 115 | + short v5=chan[5].vol; |
| 116 | + for (i=0;i<len;i++) |
| 117 | + { |
| 118 | + s = ( v0*(square[(chan[0].spos>>8)&0x3f]) ); |
| 119 | + s+= ( v1*(square[(chan[1].spos>>8)&0x3f]) ); |
| 120 | + s+= ( v2*(square[(chan[2].spos>>8)&0x3f]) ); |
| 121 | + s+= ( v3*(noise[(chan[3].spos>>8)&(NOISEBSIZE-1)]) ); |
| 122 | + s+= ( v4*(noise[(chan[4].spos>>8)&(NOISEBSIZE-1)]) ); |
| 123 | + s+= ( v5*(noise[(chan[5].spos>>8)&(NOISEBSIZE-1)]) ); |
| 124 | + *stream++ = int16_t((s>>11)); |
| 125 | + chan[0].spos += chan[0].sinc; |
| 126 | + chan[1].spos += chan[1].sinc; |
| 127 | + chan[2].spos += chan[2].sinc; |
| 128 | + chan[3].spos += chan[3].sinc; |
| 129 | + chan[4].spos += chan[4].sinc; |
| 130 | + chan[5].spos += chan[5].sinc; |
| 131 | + } |
| 132 | +#endif |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +void AudioPlaySystem::begin(void) |
| 137 | +{ |
| 138 | +} |
| 139 | + |
| 140 | +void AudioPlaySystem::start(void) |
| 141 | +{ |
| 142 | + playing = true; |
| 143 | +} |
| 144 | + |
| 145 | +void AudioPlaySystem::setSampleParameters(float clockfreq, float samplerate) { |
| 146 | +} |
| 147 | + |
| 148 | +void AudioPlaySystem::reset(void) |
| 149 | +{ |
| 150 | + snd_Reset(); |
| 151 | +} |
| 152 | + |
| 153 | +void AudioPlaySystem::stop(void) |
| 154 | +{ |
| 155 | + playing = false; |
| 156 | +} |
| 157 | + |
| 158 | +bool AudioPlaySystem::isPlaying(void) |
| 159 | +{ |
| 160 | + return playing; |
| 161 | +} |
| 162 | + |
| 163 | + |
| 164 | +void AudioPlaySystem::sound(int C, int F, int V) { |
| 165 | + if (C < 6) { |
| 166 | + //printf("play %d %d %d\n",C,F,V); |
| 167 | + |
| 168 | + chan[C].vol = V; |
| 169 | + chan[C].sinc = F>>1; |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +void AudioPlaySystem::step(void) |
| 174 | +{ |
| 175 | + if (playing) { |
| 176 | + int left=DEFAULT_SAMPLERATE/50; |
| 177 | + |
| 178 | + while(left) { |
| 179 | + int n=DEFAULT_SAMPLESIZE; |
| 180 | + if (n>left) n=left; |
| 181 | + snd_Mixer16((uint16_t*)Buffer, n); |
| 182 | + //16 bit mono -> 16 bit r+l |
| 183 | + for (int i=n-1; i>=0; i--) { |
| 184 | + Buffer[i*2+1]=Buffer[i]+32767; |
| 185 | + Buffer[i*2]=Buffer[i]+32767; |
| 186 | + } |
| 187 | + // TODO: update this to use hal |
| 188 | + // i2s_write_bytes(I2S_NUM, (const void*)Buffer, n*4, portMAX_DELAY); |
| 189 | + left-=n; |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments