]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/mmedia/sndulaw.cpp
implemented (untested) work around for wxScrolledWindow painting bug
[wxWidgets.git] / contrib / src / mmedia / sndulaw.cpp
CommitLineData
e8482f24
GL
1// --------------------------------------------------------------------------
2// Name: sndulaw.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 "sndulaw.cpp"
10#endif
11
12#include "wx/wxprec.h"
13
14#ifndef WX_PRECOMP
15 #include "wx/defs.h"
16#endif
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
22#include "wx/mmedia/sndbase.h"
23#include "wx/mmedia/sndfile.h"
24#include "wx/mmedia/sndpcm.h"
25#include "wx/mmedia/sndulaw.h"
26#include "wx/mmedia/internal/g72x.h"
27
28// --------------------------------------------------------------------------
29// wxSoundFormatUlaw
30// --------------------------------------------------------------------------
31
32wxSoundFormatUlaw::wxSoundFormatUlaw()
33 : m_srate(22050), m_channels(1)
34{
35}
36
37wxSoundFormatUlaw::~wxSoundFormatUlaw()
38{
39}
40
41void wxSoundFormatUlaw::SetSampleRate(wxUint32 srate)
42{
43 m_srate = srate;
44}
45
46wxUint32 wxSoundFormatUlaw::GetSampleRate() const
47{
48 return m_srate;
49}
50
51wxUint8 wxSoundFormatUlaw::GetChannels() const
52{
53 return m_channels;
54}
55
56void wxSoundFormatUlaw::SetChannels(wxUint8 nchannels)
57{
58 m_channels = nchannels;
59}
60
61wxSoundFormatBase *wxSoundFormatUlaw::Clone() const
62{
63 wxSoundFormatUlaw *ulaw = new wxSoundFormatUlaw();
64
65 ulaw->m_srate = m_srate;
66 ulaw->m_channels = m_channels;
67 return ulaw;
68}
69
70wxUint32 wxSoundFormatUlaw::GetTimeFromBytes(wxUint32 bytes) const
71{
72 return (bytes / m_srate);
73}
74
75wxUint32 wxSoundFormatUlaw::GetBytesFromTime(wxUint32 time) const
76{
77 return time * m_srate;
78}
79
80bool wxSoundFormatUlaw::operator !=(const wxSoundFormatBase& frmt2) const
81{
82 wxSoundFormatUlaw *ulaw = (wxSoundFormatUlaw *)&frmt2;
83
84 if (frmt2.GetType() != wxSOUND_ULAW)
85 return TRUE;
86
87 return (ulaw->m_srate != m_srate);
88}
89
90// --------------------------------------------------------------------------
91// wxSoundStreamUlaw
92// --------------------------------------------------------------------------
93wxSoundStreamUlaw::wxSoundStreamUlaw(wxSoundStream& sndio)
94 : wxSoundStreamCodec(sndio)
95{
96 // PCM converter
97 m_router = new wxSoundRouterStream(sndio);
98}
99
100wxSoundStreamUlaw::~wxSoundStreamUlaw()
101{
102 delete m_router;
103}
104
105wxSoundStream& wxSoundStreamUlaw::Read(void *buffer, wxUint32 len)
106{
107 wxUint16 *old_linear;
108 register wxUint16 *linear_buffer;
109 register const wxUint8 *ulaw_buffer;
110 register wxUint32 countdown;
111
112 old_linear = linear_buffer = new wxUint16[len*2];
113 ulaw_buffer = (const wxUint8 *)buffer;
114
115 m_router->Read(linear_buffer, len * 2);
116
117 m_lastcount = countdown = m_router->GetLastAccess() / 2;
118 m_snderror = m_router->GetError();
119 if (m_snderror != wxSOUND_NOERROR)
120 return *this;
121
122 while (countdown > 0) {
123 *linear_buffer++ = ulaw2linear(*ulaw_buffer++);
124 countdown--;
125 }
126
127 delete[] old_linear;
128
129 return *m_router;
130}
131
132wxSoundStream& wxSoundStreamUlaw::Write(const void *buffer, wxUint32 len)
133{
134 wxUint16 *old_linear;
135 register wxUint16 *linear_buffer;
136 register const wxUint8 *ulaw_buffer;
137 register wxUint32 countdown = len;
138
139 old_linear = linear_buffer = new wxUint16[len*2];
140 ulaw_buffer = (const wxUint8 *)buffer;
141
142 while (countdown > 0) {
143 *linear_buffer++ = ulaw2linear(*ulaw_buffer++);
144 countdown--;
145 }
146
147 m_router->Write(old_linear, len * 2);
148
149 delete[] old_linear;
150
151 return *m_router;
152}
153
154wxUint32 wxSoundStreamUlaw::GetBestSize() const
155{
156 return m_sndio->GetBestSize() / 2;
157}
158
159bool wxSoundStreamUlaw::SetSoundFormat(const wxSoundFormatBase& format)
160{
161 if (format.GetType() != wxSOUND_ULAW) {
162 m_snderror = wxSOUND_INVFRMT;
163 return FALSE;
164 }
165
c42b1de6
GL
166 // As the codec only support 16 bits, Mono we must use a wxSoundRouter
167 // to filter the data and to translate them to a format supported
168 // by the sound card.
e8482f24
GL
169
170 wxSoundFormatPcm pcm;
171 wxSoundFormatUlaw *ulaw;
172
173 wxSoundStreamCodec::SetSoundFormat(format);
174
175 ulaw = (wxSoundFormatUlaw *)m_sndformat;
176
177 pcm.SetSampleRate(ulaw->GetSampleRate());
178 pcm.SetBPS(16);
179 pcm.SetChannels(ulaw->GetChannels());
180 pcm.Signed(TRUE);
181 pcm.SetOrder(wxBYTE_ORDER);
182
183 m_router->SetSoundFormat(pcm);
184
185 return TRUE;
186}