1 #include "include/pcm.h"
5 #define sample_rate 44100
8 #define buffer_modulo (sample_rate * buffer_size)
10 static int16 * pcm_left;
11 static int16 * pcm_right;
12 static uint32 pcm_left_pos;
13 static uint32 pcm_right_pos;
14 static uint32 pcm_left_playback_pos;
15 static uint32 pcm_right_playback_pos;
16 static FILE * fp_left;
17 static FILE * fp_right;
18 static uint32 pcm_sample_rate = sample_rate;
19 static uint32 pcm_inc = (pcm_sample_rate << 8) / sample_rate;
21 //////////////////////////////////////////////////////////////////////////////
23 //////////////////////////////////////////////////////////////////////////////
30 //////////////////////////////////////////////////////////////////////////////
31 void pcm_set_sample_rate(int rate)
33 pcm_sample_rate = rate;
34 pcm_inc = (pcm_sample_rate << 8) / sample_rate;
35 // fprintf(log_get(),"pcm: sample rate is %i hz, sample increment is %i (%f)\n",pcm_sample_rate,pcm_inc,((float)pcm_inc)/256.0f);
37 //////////////////////////////////////////////////////////////////////////////
39 //////////////////////////////////////////////////////////////////////////////
46 //////////////////////////////////////////////////////////////////////////////
47 void pcm_updateOne(int channel, int16 *data, uint32 length)
53 *data++=pcm_left[(pcm_left_playback_pos>>8)%buffer_modulo];
54 pcm_left_playback_pos+=pcm_inc;
62 *data++=pcm_right[(pcm_right_playback_pos>>8)%buffer_modulo];
63 pcm_right_playback_pos+=pcm_inc;
68 //////////////////////////////////////////////////////////////////////////////
70 //////////////////////////////////////////////////////////////////////////////
77 //////////////////////////////////////////////////////////////////////////////
80 memory_malloc_secure((void**)&pcm_left,buffer_modulo*sizeof(int16),"Left dac buffer");
81 memory_malloc_secure((void**)&pcm_right,buffer_modulo*sizeof(int16),"Right dac buffer");
83 fp_left=fopen("leftdac.raw","wb");
84 fp_right=fopen("rightdac.raw","wb");
87 //////////////////////////////////////////////////////////////////////////////
89 //////////////////////////////////////////////////////////////////////////////
96 //////////////////////////////////////////////////////////////////////////////
101 pcm_left_playback_pos=0;
102 pcm_right_playback_pos=0;
104 //////////////////////////////////////////////////////////////////////////////
106 //////////////////////////////////////////////////////////////////////////////
113 //////////////////////////////////////////////////////////////////////////////
119 //////////////////////////////////////////////////////////////////////////////
121 //////////////////////////////////////////////////////////////////////////////
128 //////////////////////////////////////////////////////////////////////////////
129 void pcm_update(void)
132 //////////////////////////////////////////////////////////////////////////////
134 //////////////////////////////////////////////////////////////////////////////
141 //////////////////////////////////////////////////////////////////////////////
142 void pcm_render_left_dac(void)
145 fwrite(pcm_left,1,sample_rate*2,fp_left);
148 //////////////////////////////////////////////////////////////////////////////
150 //////////////////////////////////////////////////////////////////////////////
157 //////////////////////////////////////////////////////////////////////////////
158 void pcm_render_right_dac(void)
161 fwrite(pcm_right,1,sample_rate*2,fp_right);
164 //////////////////////////////////////////////////////////////////////////////
166 //////////////////////////////////////////////////////////////////////////////
173 //////////////////////////////////////////////////////////////////////////////
174 void pcm_byte_write(uint32 offset, uint8 data)
176 // fprintf(log_get(),"pcm: writing 0x%.2x at 0x%.8x\n",data,offset);
178 //////////////////////////////////////////////////////////////////////////////
180 //////////////////////////////////////////////////////////////////////////////
187 //////////////////////////////////////////////////////////////////////////////
188 void pcm_word_write(uint32 offset, uint16 data)
192 pcm_left[pcm_left_pos%buffer_modulo]=data;
194 if ((pcm_left_pos%buffer_modulo)==0)
195 pcm_render_left_dac();
200 pcm_right[pcm_right_pos%buffer_modulo]=data;
202 if ((pcm_right_pos%buffer_modulo)==0)
203 pcm_render_right_dac();
206 //////////////////////////////////////////////////////////////////////////////
208 //////////////////////////////////////////////////////////////////////////////
215 //////////////////////////////////////////////////////////////////////////////
216 uint8 pcm_byte_read(uint32 offset)
218 // fprintf(log_get(),"pcm: reading byte from 0x%.8x\n",offset);
221 //////////////////////////////////////////////////////////////////////////////
223 //////////////////////////////////////////////////////////////////////////////
230 //////////////////////////////////////////////////////////////////////////////
231 uint16 pcm_word_read(uint32 offset)
233 // fprintf(log_get(),"pcm: reading word from 0x%.8x\n",offset);