]>
Commit | Line | Data |
---|---|---|
4d6306eb GL |
1 | #ifdef __GNUG__ |
2 | #pragma implementation "sndpcm.h" | |
3 | #endif | |
4 | #include "sndsnd.h" | |
5 | #include "sndpcm.h" | |
4d6306eb GL |
6 | |
7 | #define WX_BIG_ENDIAN 0 | |
8 | ||
9 | wxSoundPcmCodec::wxSoundPcmCodec() | |
10 | : wxSoundCodec() | |
11 | { | |
12 | m_orig_format.SetCodecCreate(FALSE); | |
926c550d | 13 | m_orig_format.SetCodecNo(WXSOUND_PCM); |
4d6306eb GL |
14 | } |
15 | ||
16 | wxSoundPcmCodec::~wxSoundPcmCodec() | |
17 | { | |
18 | } | |
19 | ||
20 | size_t wxSoundPcmCodec::GetByteRate() const | |
21 | { | |
22 | return (m_orig_format.GetBps()/8)* | |
23 | m_orig_format.GetSampleRate()* | |
24 | m_orig_format.GetChannels(); | |
25 | } | |
26 | ||
27 | wxSoundDataFormat wxSoundPcmCodec::GetPreferredFormat(int codec) const | |
28 | { | |
29 | wxSoundDataFormat prefFormat; | |
30 | ||
31 | prefFormat = m_orig_format; | |
4d6306eb GL |
32 | return prefFormat; |
33 | } | |
34 | ||
35 | // --------------------------------------------------------------------------- | |
36 | // Main part of the decoder | |
37 | // --------------------------------------------------------------------------- | |
38 | ||
39 | void wxSoundPcmCodec::Decode() | |
40 | { | |
4d6306eb GL |
41 | if (m_io_format == m_orig_format) { |
42 | CopyToOutput(); | |
4d6306eb GL |
43 | return; |
44 | } | |
45 | ||
46 | // Swap bytes | |
47 | switch (m_io_format.GetBps()) { | |
48 | case 8: | |
49 | InputSign8(); | |
50 | break; | |
51 | case 16: | |
52 | InputSwapAndSign16(); | |
53 | break; | |
54 | case 32: | |
55 | case 64: | |
56 | default: | |
57 | break; | |
58 | } | |
4d6306eb GL |
59 | } |
60 | ||
61 | // --------------------------------------------------------------------------- | |
62 | // Change the sign of a 8-bit sample. | |
63 | ||
64 | #define GET() (m_in_sound->GetChar()) | |
65 | #define PUT(c) (m_out_sound->PutChar(c)) | |
4d6306eb GL |
66 | |
67 | void wxSoundPcmCodec::InputSign8() | |
68 | { | |
69 | unsigned char signer = 0; | |
4d6306eb GL |
70 | |
71 | if (m_io_format.GetSign() != m_orig_format.GetSign()) | |
72 | signer = 128; | |
73 | ||
926c550d | 74 | while (StreamOk()) |
4d6306eb | 75 | PUT(GET() + signer); |
4d6306eb GL |
76 | } |
77 | ||
78 | // --------------------------------------------------------------------------- | |
79 | // Swap bytes and change the sign of a 16-bit sample. | |
80 | ||
81 | void wxSoundPcmCodec::InputSwapAndSign16() | |
82 | { | |
83 | unsigned short signer1 = 0, signer2 = 0; | |
4d6306eb | 84 | bool swap = (m_io_format.GetByteOrder() != m_orig_format.GetByteOrder()); |
926c550d | 85 | register char temp, temp2; |
4d6306eb GL |
86 | |
87 | if (m_io_format.GetSign() != m_orig_format.GetSign()) { | |
88 | if (m_io_format.GetByteOrder() == wxSND_SAMPLE_LE) | |
89 | signer2 = 0x80; | |
90 | else | |
91 | signer1 = 0x80; | |
92 | } | |
93 | ||
94 | if (swap) { | |
926c550d GL |
95 | while (StreamOk()) { |
96 | temp = GET(); | |
97 | temp2 = GET(); | |
98 | PUT(temp2 ^ signer2); | |
99 | if (!StreamOk()) { | |
100 | m_in_sound->WriteBack(temp); | |
101 | m_in_sound->WriteBack(temp2); | |
4d6306eb GL |
102 | break; |
103 | } | |
926c550d | 104 | PUT(temp ^ signer1); |
4d6306eb GL |
105 | } |
106 | } else { | |
926c550d GL |
107 | while (StreamOk()) { |
108 | temp = GET(); | |
109 | PUT(temp ^ signer1); | |
110 | if (!StreamOk()) { | |
111 | m_in_sound->WriteBack(temp); | |
4d6306eb GL |
112 | break; |
113 | } | |
114 | PUT(GET() ^ signer2); | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | // --------------------------------------------------------------------------- | |
120 | // Encoder part. | |
121 | // --------------------------------------------------------------------------- | |
122 | ||
123 | void wxSoundPcmCodec::OutputSign8() | |
124 | { | |
4d6306eb GL |
125 | unsigned char signer = 0; |
126 | ||
127 | if (m_io_format.GetSign() != m_orig_format.GetSign()) | |
128 | signer = 128; | |
129 | ||
926c550d | 130 | while (StreamOk()) |
4d6306eb GL |
131 | PUT((char)(GET() + signer)); |
132 | } | |
133 | ||
134 | // --------------------------------------------------------------------------- | |
135 | ||
136 | void wxSoundPcmCodec::OutputSwapAndSign16() | |
137 | { | |
138 | bool swap = (m_io_format.GetByteOrder() != m_orig_format.GetByteOrder()); | |
139 | unsigned short signer1 = 0, signer2 = 0; | |
926c550d | 140 | register char temp, temp2; |
4d6306eb GL |
141 | |
142 | if (m_io_format.GetSign() != m_orig_format.GetSign()) | |
143 | if (m_io_format.GetByteOrder() == wxSND_SAMPLE_LE) | |
144 | signer1 = 0x80; | |
145 | else | |
146 | signer2 = 0x80; | |
147 | ||
148 | if (swap) { | |
926c550d | 149 | while (StreamOk()) { |
4d6306eb | 150 | temp = GET(); |
926c550d GL |
151 | temp2 = GET(); |
152 | PUT(temp2 ^ signer1); | |
153 | if (!StreamOk()) { | |
154 | m_in_sound->WriteBack(temp); | |
155 | m_in_sound->WriteBack(temp2); | |
4d6306eb GL |
156 | break; |
157 | } | |
158 | PUT(temp ^ signer2); | |
159 | } | |
160 | } else { | |
926c550d GL |
161 | while (StreamOk()) { |
162 | temp = GET(); | |
163 | temp2 = GET(); | |
164 | PUT(temp ^ signer1); | |
165 | if (!StreamOk()) { | |
166 | m_in_sound->WriteBack(temp); | |
4d6306eb GL |
167 | break; |
168 | } | |
926c550d | 169 | PUT(temp2 ^ signer2); |
4d6306eb GL |
170 | } |
171 | } | |
172 | ||
173 | } | |
174 | ||
175 | // --------------------------------------------------------------------------- | |
176 | ||
177 | void wxSoundPcmCodec::Encode() | |
178 | { | |
4d6306eb GL |
179 | if (m_io_format == m_orig_format) { |
180 | CopyToOutput(); | |
4d6306eb GL |
181 | return; |
182 | } | |
183 | ||
184 | // Swap bytes | |
185 | switch (m_io_format.GetBps()) { | |
186 | case 8: | |
187 | OutputSign8(); | |
188 | break; | |
189 | case 16: | |
190 | OutputSwapAndSign16(); | |
191 | break; | |
192 | case 32: | |
193 | case 64: | |
194 | default: | |
195 | break; | |
196 | } | |
4d6306eb | 197 | } |