]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/mmedia/sndulaw.cpp
and restored error checking too: this completes sequence of 3 check ins making wx...
[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$
58b9c9ba 7// wxWindows licence
e8482f24
GL
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
33wxSoundFormatUlaw::wxSoundFormatUlaw()
34 : m_srate(22050), m_channels(1)
35{
36}
37
38wxSoundFormatUlaw::~wxSoundFormatUlaw()
39{
40}
41
42void wxSoundFormatUlaw::SetSampleRate(wxUint32 srate)
43{
44 m_srate = srate;
45}
46
47wxUint32 wxSoundFormatUlaw::GetSampleRate() const
48{
49 return m_srate;
50}
51
52wxUint8 wxSoundFormatUlaw::GetChannels() const
53{
54 return m_channels;
55}
56
57void wxSoundFormatUlaw::SetChannels(wxUint8 nchannels)
58{
59 m_channels = nchannels;
60}
61
62wxSoundFormatBase *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
71wxUint32 wxSoundFormatUlaw::GetTimeFromBytes(wxUint32 bytes) const
72{
73 return (bytes / m_srate);
74}
75
76wxUint32 wxSoundFormatUlaw::GetBytesFromTime(wxUint32 time) const
77{
78 return time * m_srate;
79}
80
81bool wxSoundFormatUlaw::operator !=(const wxSoundFormatBase& frmt2) const
82{
83 wxSoundFormatUlaw *ulaw = (wxSoundFormatUlaw *)&frmt2;
84
85 if (frmt2.GetType() != wxSOUND_ULAW)
dea7e44a 86 return true;
e8482f24
GL
87
88 return (ulaw->m_srate != m_srate);
89}
90
91// --------------------------------------------------------------------------
92// wxSoundStreamUlaw
93// --------------------------------------------------------------------------
94wxSoundStreamUlaw::wxSoundStreamUlaw(wxSoundStream& sndio)
95 : wxSoundStreamCodec(sndio)
96{
97 // PCM converter
98 m_router = new wxSoundRouterStream(sndio);
99}
100
101wxSoundStreamUlaw::~wxSoundStreamUlaw()
102{
103 delete m_router;
104}
105
106wxSoundStream& 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
133wxSoundStream& 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
155wxUint32 wxSoundStreamUlaw::GetBestSize() const
156{
157 return m_sndio->GetBestSize() / 2;
158}
159
160bool wxSoundStreamUlaw::SetSoundFormat(const wxSoundFormatBase& format)
161{
162 if (format.GetType() != wxSOUND_ULAW) {
163 m_snderror = wxSOUND_INVFRMT;
dea7e44a 164 return false;
e8482f24
GL
165 }
166
c42b1de6
GL
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.
e8482f24
GL
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());
dea7e44a 181 pcm.Signed(true);
e8482f24
GL
182 pcm.SetOrder(wxBYTE_ORDER);
183
184 m_router->SetSoundFormat(pcm);
185
dea7e44a 186 return true;
e8482f24 187}