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