]> git.saurik.com Git - wxWidgets.git/blame - utils/wxMMedia/sndpcm.cpp
new makefiles (part I)
[wxWidgets.git] / utils / wxMMedia / sndpcm.cpp
CommitLineData
7f42cff1
GL
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////////////////////////////////////////////////////////////////////////////////
4d6306eb
GL
10#ifdef __GNUG__
11#pragma implementation "sndpcm.h"
12#endif
13#include "sndsnd.h"
14#include "sndpcm.h"
4d6306eb
GL
15
16#define WX_BIG_ENDIAN 0
17
18wxSoundPcmCodec::wxSoundPcmCodec()
19 : wxSoundCodec()
20{
21 m_orig_format.SetCodecCreate(FALSE);
926c550d 22 m_orig_format.SetCodecNo(WXSOUND_PCM);
4d6306eb
GL
23}
24
25wxSoundPcmCodec::~wxSoundPcmCodec()
26{
27}
28
29size_t wxSoundPcmCodec::GetByteRate() const
30{
31 return (m_orig_format.GetBps()/8)*
32 m_orig_format.GetSampleRate()*
33 m_orig_format.GetChannels();
34}
35
36wxSoundDataFormat wxSoundPcmCodec::GetPreferredFormat(int codec) const
37{
38 wxSoundDataFormat prefFormat;
39
40 prefFormat = m_orig_format;
4d6306eb
GL
41 return prefFormat;
42}
43
44// ---------------------------------------------------------------------------
45// Main part of the decoder
46// ---------------------------------------------------------------------------
47
48void wxSoundPcmCodec::Decode()
49{
4d6306eb
GL
50 if (m_io_format == m_orig_format) {
51 CopyToOutput();
4d6306eb
GL
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 }
4d6306eb
GL
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))
4d6306eb
GL
75
76void wxSoundPcmCodec::InputSign8()
77{
78 unsigned char signer = 0;
4d6306eb
GL
79
80 if (m_io_format.GetSign() != m_orig_format.GetSign())
81 signer = 128;
82
926c550d 83 while (StreamOk())
4d6306eb 84 PUT(GET() + signer);
4d6306eb
GL
85}
86
87// ---------------------------------------------------------------------------
88// Swap bytes and change the sign of a 16-bit sample.
89
90void wxSoundPcmCodec::InputSwapAndSign16()
91{
92 unsigned short signer1 = 0, signer2 = 0;
4d6306eb 93 bool swap = (m_io_format.GetByteOrder() != m_orig_format.GetByteOrder());
926c550d 94 register char temp, temp2;
4d6306eb
GL
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) {
926c550d
GL
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);
4d6306eb
GL
111 break;
112 }
926c550d 113 PUT(temp ^ signer1);
4d6306eb
GL
114 }
115 } else {
926c550d
GL
116 while (StreamOk()) {
117 temp = GET();
7f42cff1 118 temp2 = GET();
926c550d
GL
119 PUT(temp ^ signer1);
120 if (!StreamOk()) {
121 m_in_sound->WriteBack(temp);
7f42cff1 122 m_in_sound->WriteBack(temp2);
4d6306eb
GL
123 break;
124 }
125 PUT(GET() ^ signer2);
126 }
127 }
128}
129
130// ---------------------------------------------------------------------------
131// Encoder part.
132// ---------------------------------------------------------------------------
133
134void wxSoundPcmCodec::OutputSign8()
135{
4d6306eb
GL
136 unsigned char signer = 0;
137
138 if (m_io_format.GetSign() != m_orig_format.GetSign())
139 signer = 128;
140
926c550d 141 while (StreamOk())
4d6306eb
GL
142 PUT((char)(GET() + signer));
143}
144
145// ---------------------------------------------------------------------------
146
147void wxSoundPcmCodec::OutputSwapAndSign16()
148{
149 bool swap = (m_io_format.GetByteOrder() != m_orig_format.GetByteOrder());
150 unsigned short signer1 = 0, signer2 = 0;
926c550d 151 register char temp, temp2;
4d6306eb
GL
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) {
926c550d 160 while (StreamOk()) {
4d6306eb 161 temp = GET();
926c550d
GL
162 temp2 = GET();
163 PUT(temp2 ^ signer1);
164 if (!StreamOk()) {
165 m_in_sound->WriteBack(temp);
166 m_in_sound->WriteBack(temp2);
4d6306eb
GL
167 break;
168 }
169 PUT(temp ^ signer2);
170 }
171 } else {
926c550d
GL
172 while (StreamOk()) {
173 temp = GET();
174 temp2 = GET();
175 PUT(temp ^ signer1);
176 if (!StreamOk()) {
177 m_in_sound->WriteBack(temp);
7f42cff1 178 m_in_sound->WriteBack(temp2);
4d6306eb
GL
179 break;
180 }
926c550d 181 PUT(temp2 ^ signer2);
4d6306eb
GL
182 }
183 }
184
185}
186
187// ---------------------------------------------------------------------------
188
189void wxSoundPcmCodec::Encode()
190{
4d6306eb
GL
191 if (m_io_format == m_orig_format) {
192 CopyToOutput();
4d6306eb
GL
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 }
4d6306eb 209}