]> Shamusworld >> Repos - virtualjaguar/blob - src/pcm.cpp
Virtual Jaguar 1.0.4 update (Shamus)
[virtualjaguar] / src / pcm.cpp
1 //
2 // PCM Handler
3 //
4 // by cal2
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups by James L. Hammons
7 //
8
9 #include "pcm.h"
10
11 //#define PCM_DUMP
12
13 #define sample_rate 44100
14 #define sample_bits 16
15 #define buffer_size 4
16 #define buffer_modulo (sample_rate * buffer_size)
17
18 static int16 * pcm_left;
19 static int16 * pcm_right;
20 static uint32 pcm_left_pos;
21 static uint32 pcm_right_pos;
22 static uint32 pcm_left_playback_pos;
23 static uint32 pcm_right_playback_pos;
24 static uint32 pcm_sample_rate = sample_rate;
25 static uint32 pcm_inc = (pcm_sample_rate << 8) / sample_rate;
26 #ifdef PCM_DUMP
27 static FILE * fp_left;
28 static FILE * fp_right;
29 #endif
30
31
32 void pcm_set_sample_rate(int rate)
33 {
34         pcm_sample_rate = rate;
35         pcm_inc = (pcm_sample_rate << 8) / sample_rate;
36 //      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 }
38
39 void pcm_updateOne(int channel, int16 * data, uint32 length)
40 {
41         if (channel == 0)
42         {
43                 while (length)
44                 {
45                         *data++ = pcm_left[(pcm_left_playback_pos >> 8) % buffer_modulo];
46                         pcm_left_playback_pos += pcm_inc;
47                         length--;
48                 }
49         }
50         else
51         {
52                 while (length)
53                 {
54                         *data++ = pcm_right[(pcm_right_playback_pos >> 8) % buffer_modulo];
55                         pcm_right_playback_pos += pcm_inc;
56                         length--;
57                 }
58         }
59 }
60
61 void pcm_init(void)
62 {
63         memory_malloc_secure((void **)&pcm_left, buffer_modulo * sizeof(int16), "Left DAC buffer");
64         memory_malloc_secure((void **)&pcm_right, buffer_modulo * sizeof(int16), "Right DAC buffer");
65         pcm_reset();
66 #ifdef PCM_DUMP
67         fp_left = fopen("leftdac.raw", "wb");
68         fp_right = fopen("rightdac.raw", "wb");
69 #endif
70 }
71
72 void pcm_reset(void)
73 {
74         pcm_left_pos = 0;
75         pcm_right_pos = 0;
76         pcm_left_playback_pos = 0;
77         pcm_right_playback_pos = 0;
78 }
79
80 void pcm_done(void)
81 {
82 #ifdef PCM_DUMP
83         fclose(fp_left);
84         fclose(fp_right);
85 #endif
86         fprintf(log_get(), "PCM: Done.\n");
87 }
88
89 void pcm_update(void)
90 {
91 }
92
93 void pcm_render_left_dac(void)
94 {
95 #ifdef PCM_DUMP
96         fwrite(pcm_left, 1, sample_rate * 2, fp_left);
97 #endif
98 }
99
100 void pcm_render_right_dac(void)
101 {
102 #ifdef PCM_DUMP
103         fwrite(pcm_right, 1, sample_rate * 2, fp_right);
104 #endif
105 }
106
107 void pcm_byte_write(uint32 offset, uint8 data)
108 {
109 //      fprintf(log_get(),"pcm: writing 0x%.2x at 0x%.8x\n",data,offset);
110 }
111
112 void pcm_word_write(uint32 offset, uint16 data)
113 {
114         if (offset == 2)
115         {
116                 pcm_left[pcm_left_pos % buffer_modulo] = data;
117                 pcm_left_pos++;
118                 if ((pcm_left_pos % buffer_modulo) == 0)
119                         pcm_render_left_dac();
120         }
121         else
122         if (offset == 6)
123         {
124                 pcm_right[pcm_right_pos % buffer_modulo] = data;
125                 pcm_right_pos++;
126                 if ((pcm_right_pos % buffer_modulo) == 0)
127                         pcm_render_right_dac();
128         }
129 }
130
131 uint8 pcm_byte_read(uint32 offset)
132 {
133 //      fprintf(log_get(),"pcm: reading byte from 0x%.8x\n",offset);
134         return 0xFF;
135 }
136
137 uint16 pcm_word_read(uint32 offset)
138 {
139 //      fprintf(log_get(),"pcm: reading word from 0x%.8x\n",offset);
140         return 0xFFFF;
141 }