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