]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/lib/sndcpcm.cpp
Removed wxMMedia
[wxWidgets.git] / utils / wxMMedia2 / lib / sndcpcm.cpp
1 // --------------------------------------------------------------------------
2 // Name: sndcpcm.cpp
3 // Purpose:
4 // Date: 08/11/1999
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
6 // CVSID: $Id$
7 // --------------------------------------------------------------------------
8 #ifdef __GNUG__
9 #pragma implementation "sndcpcm.cpp"
10 #endif
11
12 #include "sndbase.h"
13 #include "sndpcm.h"
14 #include "sndcpcm.h"
15
16 wxSoundStreamPcm::wxSoundStreamPcm(wxSoundStream& sndio)
17 : wxSoundStreamCodec(sndio)
18 {
19 m_function_in = NULL;
20 m_function_out = NULL;
21 }
22
23 wxSoundStreamPcm::~wxSoundStreamPcm()
24 {
25 }
26
27
28 #define SWAP_BYTES 0
29 #include "converter.def"
30 #undef SWAP_BYTES
31
32 #define SWAP_BYTES 1
33 #include "converter.def"
34 #undef SWAP_BYTES
35
36 wxSoundStreamPcm::ConverterType s_convert_16_to_8[] = {
37 Convert_16to8_16_no,
38 Convert_16to8_U2S_16_no,
39 NULL,
40 NULL,
41 Convert_16to8_U2S_16_yes,
42 Convert_16to8_16_yes,
43 };
44
45 wxSoundStreamPcm::ConverterType s_convert_16[] = {
46 NULL,
47 Convert_U2S_16_no,
48 Convert_U2S_SWAP_16_no,
49 Convert_U2S_SWAP_16_yes,
50 Convert_U2S_16_yes,
51 Convert_SWAP_16_no
52 };
53
54 wxSoundStreamPcm::ConverterType s_convert_8[] = {
55 NULL,
56 Convert_U2S_8,
57 Convert_U2S_8,
58 Convert_U2S_8,
59 Convert_U2S_8,
60 NULL
61 };
62
63 #define CONVERTER 0
64 #define CONVERTER_SIGN 1
65 #define CONVERTER_SIGN_SWAP 2
66 #define CONVERTER_SWAP_SIGN_SWAP 3
67 #define CONVERTER_SWAP_SIGN 4
68 #define CONVERTER_SWAP 5
69
70 wxSoundStream& wxSoundStreamPcm::Read(void *buffer, size_t len)
71 {
72 if (!m_function_in) {
73 m_sndio->Read(buffer, len);
74 m_lastcount = m_sndio->GetLastAccess();
75 m_snderror = m_sndio->GetError();
76 return *this;
77 }
78
79 // TODO
80 m_sndio->Read(buffer, len);
81 m_lastcount = m_sndio->GetLastAccess();
82 m_snderror = m_sndio->GetError();
83 return *this;
84 }
85
86 wxSoundStream& wxSoundStreamPcm::Write(const void *buffer, size_t len)
87 {
88 char *tmp_buf;
89 size_t len2;
90
91 if (!m_function_out)
92 return m_sndio->Write(buffer, len);
93
94 len2 = (m_16_to_8) ? len / 2 : len;
95
96 tmp_buf = new char[len2];
97 m_function_out((const char *)buffer, tmp_buf, len);
98 m_sndio->Write(tmp_buf, len);
99 delete[] tmp_buf;
100
101 m_lastcount = (m_16_to_8) ?
102 (m_sndio->GetLastAccess() * 2) : m_sndio->GetLastAccess();
103
104 return *this;
105 }
106
107 bool wxSoundStreamPcm::SetSoundFormat(const wxSoundFormatBase& format)
108 {
109 wxSoundFormatBase *new_format;
110 wxSoundFormatPcm *pcm_format, *pcm_format2;
111 ConverterType *current_table;
112 int index;
113 bool change_sign;
114
115 if (m_sndio->SetSoundFormat(format)) {
116 m_function_out = NULL;
117 m_function_in = NULL;
118 return TRUE;
119 }
120 if (format.GetType() != wxSOUND_PCM) {
121 m_snderror = wxSOUND_INVFRMT;
122 return FALSE;
123 }
124 if (m_sndformat)
125 delete m_sndformat;
126
127 new_format = m_sndio->GetSoundFormat().Clone();
128 pcm_format = (wxSoundFormatPcm *)&format;
129 pcm_format2 = (wxSoundFormatPcm *)new_format;
130
131 m_16_to_8 = FALSE;
132 if (pcm_format->GetBPS() == 16 && pcm_format2->GetBPS() == 8) {
133 m_16_to_8 = TRUE;
134 current_table = s_convert_16_to_8;
135 } else if (pcm_format->GetBPS() == 16)
136 current_table = s_convert_16;
137 else
138 current_table = s_convert_8;
139
140 change_sign = (pcm_format2->Signed() != pcm_format->Signed());
141
142 #define MY_ORDER wxBYTE_ORDER
143 #if wxBYTE_ORDER == wxLITTLE_ENDIAN
144 #define OTHER_ORDER wxBIG_ENDIAN
145 #else
146 #define OTHER_ORDER wxLITTLE_ENDIAN
147 #endif
148
149 if (pcm_format->GetOrder() == OTHER_ORDER &&
150 pcm_format2->GetOrder() == OTHER_ORDER && change_sign)
151 index = CONVERTER_SWAP_SIGN_SWAP;
152
153 else if (pcm_format->GetOrder() == OTHER_ORDER &&
154 pcm_format2->GetOrder() == MY_ORDER && change_sign)
155 index = CONVERTER_SWAP_SIGN;
156
157 else if (pcm_format->GetOrder() == MY_ORDER &&
158 pcm_format->GetOrder() == OTHER_ORDER && change_sign)
159 index = CONVERTER_SIGN_SWAP;
160
161 else if (!change_sign &&
162 pcm_format->GetOrder() != pcm_format2->GetOrder())
163 index = CONVERTER_SWAP;
164
165 else
166 index = CONVERTER;
167
168 m_function_out = current_table[index];
169 // m_function_in = current_table[index+1];
170
171 m_sndio->SetSoundFormat(*new_format);
172 m_sndformat = new_format;
173 return TRUE;
174 }