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