]> Shamusworld >> Repos - virtualjaguar/blob - src/pcm.cpp
596d8465aaa1b63e603704d911d587ef5ae7a0aa
[virtualjaguar] / src / pcm.cpp
1 #include "include/pcm.h"
2
3 #define PCM_DUMP
4
5 #define sample_rate 44100
6 #define sample_bits 16
7 #define buffer_size 4
8 #define buffer_modulo (sample_rate * buffer_size)
9
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;
20
21 //////////////////////////////////////////////////////////////////////////////
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //
25 //
26 //
27 //
28 //
29 //
30 //////////////////////////////////////////////////////////////////////////////
31 void pcm_set_sample_rate(int rate)
32 {
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);
36 }
37 //////////////////////////////////////////////////////////////////////////////
38 //
39 //////////////////////////////////////////////////////////////////////////////
40 //
41 //
42 //
43 //
44 //
45 //
46 //////////////////////////////////////////////////////////////////////////////
47 void pcm_updateOne(int channel, int16 *data, uint32 length)
48 {
49         if (channel==0)
50         {
51                 while (length)
52                 {
53                         *data++=pcm_left[(pcm_left_playback_pos>>8)%buffer_modulo];
54                         pcm_left_playback_pos+=pcm_inc;
55                         length--;
56                 }
57         }
58         else
59         {
60                 while (length)
61                 {
62                         *data++=pcm_right[(pcm_right_playback_pos>>8)%buffer_modulo];
63                         pcm_right_playback_pos+=pcm_inc;
64                         length--;
65                 }
66         }
67 }
68 //////////////////////////////////////////////////////////////////////////////
69 //
70 //////////////////////////////////////////////////////////////////////////////
71 //
72 //
73 //
74 //
75 //
76 //
77 //////////////////////////////////////////////////////////////////////////////
78 void pcm_init(void)
79 {
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");
82         pcm_reset();
83         fp_left=fopen("leftdac.raw","wb");
84         fp_right=fopen("rightdac.raw","wb");
85
86 }
87 //////////////////////////////////////////////////////////////////////////////
88 //
89 //////////////////////////////////////////////////////////////////////////////
90 //
91 //
92 //
93 //
94 //
95 //
96 //////////////////////////////////////////////////////////////////////////////
97 void pcm_reset(void)
98 {
99         pcm_left_pos=0;
100         pcm_right_pos=0;
101         pcm_left_playback_pos=0;
102         pcm_right_playback_pos=0;
103 }
104 //////////////////////////////////////////////////////////////////////////////
105 //
106 //////////////////////////////////////////////////////////////////////////////
107 //
108 //
109 //
110 //
111 //
112 //
113 //////////////////////////////////////////////////////////////////////////////
114 void pcm_done(void)
115 {
116         fclose(fp_left);
117         fclose(fp_right);
118 }
119 //////////////////////////////////////////////////////////////////////////////
120 //
121 //////////////////////////////////////////////////////////////////////////////
122 //
123 //
124 //
125 //
126 //
127 //
128 //////////////////////////////////////////////////////////////////////////////
129 void pcm_update(void)
130 {
131 }
132 //////////////////////////////////////////////////////////////////////////////
133 //
134 //////////////////////////////////////////////////////////////////////////////
135 //
136 //
137 //
138 //
139 //
140 //
141 //////////////////////////////////////////////////////////////////////////////
142 void pcm_render_left_dac(void)
143 {
144 #ifdef PCM_DUMP
145         fwrite(pcm_left,1,sample_rate*2,fp_left);
146 #endif
147 }
148 //////////////////////////////////////////////////////////////////////////////
149 //
150 //////////////////////////////////////////////////////////////////////////////
151 //
152 //
153 //
154 //
155 //
156 //
157 //////////////////////////////////////////////////////////////////////////////
158 void pcm_render_right_dac(void)
159 {
160 #ifdef PCM_DUMP
161         fwrite(pcm_right,1,sample_rate*2,fp_right);
162 #endif
163 }
164 //////////////////////////////////////////////////////////////////////////////
165 //
166 //////////////////////////////////////////////////////////////////////////////
167 //
168 //
169 //
170 //
171 //
172 //
173 //////////////////////////////////////////////////////////////////////////////
174 void pcm_byte_write(uint32 offset, uint8 data)
175 {
176 //      fprintf(log_get(),"pcm: writing 0x%.2x at 0x%.8x\n",data,offset);
177 }
178 //////////////////////////////////////////////////////////////////////////////
179 //
180 //////////////////////////////////////////////////////////////////////////////
181 //
182 //
183 //
184 //
185 //
186 //
187 //////////////////////////////////////////////////////////////////////////////
188 void pcm_word_write(uint32 offset, uint16 data)
189 {
190         if (offset==2)
191         {
192                 pcm_left[pcm_left_pos%buffer_modulo]=data;
193                 pcm_left_pos++;
194                 if ((pcm_left_pos%buffer_modulo)==0)
195                         pcm_render_left_dac();
196         }
197         else
198         if (offset==6)
199         {
200                 pcm_right[pcm_right_pos%buffer_modulo]=data;
201                 pcm_right_pos++;
202                 if ((pcm_right_pos%buffer_modulo)==0)
203                         pcm_render_right_dac();
204         }
205 }
206 //////////////////////////////////////////////////////////////////////////////
207 //
208 //////////////////////////////////////////////////////////////////////////////
209 //
210 //
211 //
212 //
213 //
214 //
215 //////////////////////////////////////////////////////////////////////////////
216 uint8 pcm_byte_read(uint32 offset)
217 {
218 //      fprintf(log_get(),"pcm: reading byte from 0x%.8x\n",offset);
219         return(0xff);
220 }
221 //////////////////////////////////////////////////////////////////////////////
222 //
223 //////////////////////////////////////////////////////////////////////////////
224 //
225 //
226 //
227 //
228 //
229 //
230 //////////////////////////////////////////////////////////////////////////////
231 uint16 pcm_word_read(uint32 offset)
232 {
233 //      fprintf(log_get(),"pcm: reading word from 0x%.8x\n",offset);
234         return(0xffff);
235 }