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