/ MCUME_pico / display / AudioPlaySystem.cpp
AudioPlaySystem.cpp
  1  #include "emuapi.h"
  2  
  3  #ifdef HAS_SND
  4  
  5  #include "AudioPlaySystem.h"
  6  
  7  #define SAMPLERATE AUDIO_SAMPLE_RATE_EXACT
  8  #define CLOCKFREQ 985248
  9  
 10  #ifndef CUSTOM_SND 
 11  static const short square[]={
 12  32767,32767,32767,32767,
 13  32767,32767,32767,32767,
 14  32767,32767,32767,32767,
 15  32767,32767,32767,32767,
 16  32767,32767,32767,32767,
 17  32767,32767,32767,32767,
 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  };
 29  
 30  const short noise[] {
 31  -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,
 32  -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,32767,-32767,
 33  -32767,-32767,32767,-32767,-32767,-32767,32767,-32767,-32767,-32767,32767,-32767,-32767,-32767,32767,-32767,
 34  -32767,-32767,32767,-32767,-32767,-32767,32767,-32767,-32767,-32767,32767,-32767,-32767,32767,32767,-32767,
 35  -32767,-32767,32767,-32767,-32767,32767,32767,-32767,-32767,-32767,32767,-32767,-32767,32767,32767,-32767,
 36  -32767,-32767,32767,-32767,-32767,32767,32767,-32767,-32767,-32767,32767,-32767,32767,32767,32767,-32767,
 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  };
 48  
 49  #define NOISEBSIZE 0x100
 50  
 51  typedef struct
 52  {
 53    unsigned int spos;
 54    unsigned int sinc;
 55    unsigned int vol;
 56  } Channel;
 57  
 58  static Channel chan[6] = {
 59    {0,0,0},
 60    {0,0,0},
 61    {0,0,0},
 62    {0,0,0},
 63    {0,0,0},
 64    {0,0,0} };
 65    
 66  #endif
 67  
 68  volatile bool playing = false;
 69  
 70  
 71  static void snd_Reset(void)
 72  {
 73  #ifndef CUSTOM_SND 
 74    chan[0].vol = 0;
 75    chan[1].vol = 0;
 76    chan[2].vol = 0;
 77    chan[3].vol = 0;
 78    chan[4].vol = 0;
 79    chan[5].vol = 0;
 80    chan[0].sinc = 0;
 81    chan[1].sinc = 0;
 82    chan[2].sinc = 0;
 83    chan[3].sinc = 0;
 84    chan[4].sinc = 0;
 85    chan[5].sinc = 0;
 86  #endif
 87  }
 88  
 89  
 90  #ifdef CUSTOM_SND
 91  
 92  #ifdef CUSTOM_SND_C 
 93  extern "C" {
 94  #endif  
 95  void SND_Process(void *sndbuffer, int sndn);
 96  #ifdef CUSTOM_SND_C 
 97  }
 98  #endif  
 99  #endif
100  
101  #include <stdio.h>
102  void AudioPlaySystem::snd_Mixer(short *  stream, int len )
103  {
104    if (playing) 
105    {
106  #ifdef CUSTOM_SND
107      //printf("s\n");    
108      SND_Process((void*)stream, len);
109  #else
110      int i;
111      long s;     
112      len = len >> 1;   
113      short v0=chan[0].vol;
114      short v1=chan[1].vol;
115      short v2=chan[2].vol;
116      short v3=chan[3].vol;
117      short v4=chan[4].vol;
118      short v5=chan[5].vol;
119      for (i=0;i<len;i++)
120      {
121        s =((v0*square[(chan[0].spos>>8)&0x3f])>>11);
122        s+=((v1*square[(chan[1].spos>>8)&0x3f])>>11);
123        s+=((v2*square[(chan[2].spos>>8)&0x3f])>>11);
124        s+=((v3*noise[(chan[3].spos>>8)&(NOISEBSIZE-1)])>>11);
125        s+=((v4*noise[(chan[4].spos>>8)&(NOISEBSIZE-1)])>>11);
126        s+=((v5*noise[(chan[5].spos>>8)&(NOISEBSIZE-1)])>>11);         
127        *stream++ = (short)(s);
128        *stream++ = (short)(s);
129        chan[0].spos += chan[0].sinc;
130        chan[1].spos += chan[1].sinc;
131        chan[2].spos += chan[2].sinc;
132        chan[3].spos += chan[3].sinc;  
133        chan[4].spos += chan[4].sinc;  
134        chan[5].spos += chan[5].sinc;  
135      }
136  #endif         
137    }
138  }
139    
140  void AudioPlaySystem::begin(void)
141  {
142    this->reset();
143  }
144  
145  void AudioPlaySystem::start(void)
146  {
147    playing = true;  
148  }
149  
150  void AudioPlaySystem::setSampleParameters(float clockfreq, float samplerate) {
151  }
152  
153  void AudioPlaySystem::reset(void)
154  {
155    snd_Reset();
156  }
157  
158  void AudioPlaySystem::stop(void)
159  {
160    playing = false;  
161  }
162  
163  bool AudioPlaySystem::isPlaying(void) 
164  { 
165    return playing; 
166  }
167  
168  
169  
170  void AudioPlaySystem::sound(int C, int F, int V) {
171  #ifndef CUSTOM_SND 
172    if (C < 6) {
173      chan[C].vol = V;
174      chan[C].sinc = F>>1; 
175    }
176  #endif  
177  }
178  
179  void AudioPlaySystem::step(void) {
180  }
181  #endif