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