]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/lib/sndfile.cpp
010529d142b150af8a7fdaf7f555df3526248e38
1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999, 2000
7 // --------------------------------------------------------------------------
11 #include <wx/stream.h>
21 // --------------------------------------------------------------------------
23 // A very important class: it ensures that everybody is satisfied.
24 // It is supposed to create as many codec as it is necessary to transform
25 // a signal in a specific format in an another.
26 // --------------------------------------------------------------------------
27 wxSoundRouterStream::wxSoundRouterStream(wxSoundStream
& sndio
)
28 : wxSoundStreamCodec(sndio
)
33 wxSoundRouterStream::~wxSoundRouterStream()
39 // --------------------------------------------------------------------------
40 // Read(void *buffer, wxUint32 len): It reads data synchronously. See sndbase.h
41 // for possible errors and behaviours ...
42 // --------------------------------------------------------------------------
43 wxSoundStream
& wxSoundRouterStream::Read(void *buffer
, wxUint32 len
)
46 m_router
->Read(buffer
, len
);
47 m_snderror
= m_router
->GetError();
48 m_lastcount
= m_router
->GetLastAccess();
50 m_sndio
->Read(buffer
, len
);
51 m_snderror
= m_sndio
->GetError();
52 m_lastcount
= m_sndio
->GetLastAccess();
57 // --------------------------------------------------------------------------
58 // Write(const void *buffer, wxUint32 len): It writes data synchronously
59 // --------------------------------------------------------------------------
60 wxSoundStream
& wxSoundRouterStream::Write(const void *buffer
, wxUint32 len
)
63 m_router
->Write(buffer
, len
);
64 m_snderror
= m_router
->GetError();
65 m_lastcount
= m_router
->GetLastAccess();
67 m_sndio
->Write(buffer
, len
);
68 m_snderror
= m_sndio
->GetError();
69 m_lastcount
= m_sndio
->GetLastAccess();
74 // --------------------------------------------------------------------------
75 // SetSoundFormat(const wxSoundFormatBase& format) first tries to setup the
76 // sound driver using the specified format. If this fails, it uses personnal
77 // codec converters: for the moment there is a PCM converter (PCM to PCM:
78 // with optional resampling, ...), an ULAW converter (ULAW to PCM), a G72X
79 // converter (G72X to PCM). If nothing works, it returns FALSE.
80 // --------------------------------------------------------------------------
81 bool wxSoundRouterStream::SetSoundFormat(const wxSoundFormatBase
& format
)
86 // First, we try to setup the sound device
87 if (m_sndio
->SetSoundFormat(format
)) {
88 // We are lucky, it is working.
89 wxSoundStream::SetSoundFormat(m_sndio
->GetSoundFormat());
93 switch(format
.GetType()) {
94 case wxSOUND_NOFORMAT
:
97 m_router
= new wxSoundStreamPcm(*m_sndio
);
98 m_router
->SetSoundFormat(format
);
101 m_router
= new wxSoundStreamUlaw(*m_sndio
);
102 m_router
->SetSoundFormat(format
);
105 m_router
= new wxSoundStreamG72X(*m_sndio
);
106 m_router
->SetSoundFormat(format
);
109 wxSoundStream::SetSoundFormat(m_router
->GetSoundFormat());
113 // --------------------------------------------------------------------------
114 // GetBestSize() returns the specific best buffer size a sound driver
115 // can manage. It means that it will be easier for it to manage the buffer
116 // and so it will be faster and in some case more accurate for real-time event.
117 // --------------------------------------------------------------------------
118 wxUint32
wxSoundRouterStream::GetBestSize() const
121 return m_router
->GetBestSize();
123 return m_sndio
->GetBestSize();
126 // --------------------------------------------------------------------------
127 // StartProduction(int evt). See sndbase.h
128 // --------------------------------------------------------------------------
129 bool wxSoundRouterStream::StartProduction(int evt
)
132 if (m_sndio
->StartProduction(evt
))
135 m_snderror
= m_sndio
->GetError();
136 m_lastcount
= m_sndio
->GetLastAccess();
140 if (m_router
->StartProduction(evt
))
143 m_snderror
= m_router
->GetError();
144 m_lastcount
= m_router
->GetLastAccess();
148 // --------------------------------------------------------------------------
149 // StopProduction(). See sndbase.h
150 // --------------------------------------------------------------------------
151 bool wxSoundRouterStream::StopProduction()
154 if (m_sndio
->StopProduction())
157 m_snderror
= m_sndio
->GetError();
158 m_lastcount
= m_sndio
->GetLastAccess();
162 if (m_router
->StopProduction())
165 m_snderror
= m_router
->GetError();
166 m_lastcount
= m_router
->GetLastAccess();
170 // --------------------------------------------------------------------------
171 // wxSoundFileStream: generic reader
172 // --------------------------------------------------------------------------
174 wxSoundFileStream::wxSoundFileStream(wxInputStream
& stream
,
175 wxSoundStream
& io_sound
)
176 : m_codec(io_sound
), m_sndio(&io_sound
),
177 m_input(&stream
), m_output(NULL
), m_state(wxSOUND_FILE_STOPPED
)
184 wxSoundFileStream::wxSoundFileStream(wxOutputStream
& stream
,
185 wxSoundStream
& io_sound
)
186 : m_codec(io_sound
), m_sndio(&io_sound
),
187 m_input(NULL
), m_output(&stream
), m_state(wxSOUND_FILE_STOPPED
)
194 wxSoundFileStream::~wxSoundFileStream()
196 if (m_state
!= wxSOUND_FILE_STOPPED
)
200 bool wxSoundFileStream::Play()
202 if (m_state
!= wxSOUND_FILE_STOPPED
)
206 if (!PrepareToPlay())
209 m_state
= wxSOUND_FILE_PLAYING
;
211 if (!StartProduction(wxSOUND_OUTPUT
))
217 bool wxSoundFileStream::Record(wxUint32 time
)
219 if (m_state
!= wxSOUND_FILE_STOPPED
)
222 if (!PrepareToRecord(time
))
225 FinishPreparation(m_sndformat
->GetBytesFromTime(time
));
227 m_state
= wxSOUND_FILE_RECORDING
;
228 if (!StartProduction(wxSOUND_INPUT
))
234 bool wxSoundFileStream::Stop()
236 if (m_state
== wxSOUND_FILE_STOPPED
)
239 if (!StopProduction())
244 if (m_state
== wxSOUND_FILE_RECORDING
)
245 if (!FinishRecording()) {
246 m_state
= wxSOUND_FILE_STOPPED
;
251 m_input
->SeekI(0, wxFromStart
);
254 m_output
->SeekO(0, wxFromStart
);
256 m_state
= wxSOUND_FILE_STOPPED
;
260 bool wxSoundFileStream::Pause()
262 if (m_state
== wxSOUND_FILE_PAUSED
|| m_state
== wxSOUND_FILE_STOPPED
)
265 if (!StopProduction())
268 m_oldstate
= m_state
;
269 m_state
= wxSOUND_FILE_PAUSED
;
273 bool wxSoundFileStream::Resume()
275 if (m_state
== wxSOUND_FILE_PLAYING
|| m_state
== wxSOUND_FILE_RECORDING
||
276 m_state
== wxSOUND_FILE_STOPPED
)
279 if (!StartProduction( (m_oldstate
== wxSOUND_FILE_PLAYING
) ?
280 wxSOUND_OUTPUT
: wxSOUND_INPUT
))
283 m_state
= m_oldstate
;
288 wxSoundStream
& wxSoundFileStream::Read(void *buffer
, wxUint32 len
)
290 m_lastcount
= GetData(buffer
, len
);
294 wxSoundStream
& wxSoundFileStream::Write(const void *buffer
, wxUint32 len
)
296 m_lastcount
= PutData(buffer
, len
);
300 bool wxSoundFileStream::StartProduction(int evt
)
302 m_sndio
->SetEventHandler(this);
304 if (!m_codec
.StartProduction(evt
))
310 bool wxSoundFileStream::StopProduction()
312 return m_codec
.StopProduction();
315 void wxSoundFileStream::FinishPreparation(wxUint32 len
)
317 m_bytes_left
= m_length
= len
;
321 wxString
wxSoundFileStream::GetCodecName() const
323 return wxString(wxT("wxSoundFileStream base codec"));
326 wxUint32
wxSoundFileStream::GetLength()
328 if (m_input
&& !m_prepared
&& GetError() == wxSOUND_NOERROR
)
329 return (PrepareToPlay()) ? m_length
: 0;
334 wxUint32
wxSoundFileStream::GetPosition()
336 if (!m_prepared
&& m_input
!= NULL
&& GetError() == wxSOUND_NOERROR
)
339 return m_length
-m_bytes_left
;
342 wxUint32
wxSoundFileStream::SetPosition(wxUint32 new_position
)
344 if (!m_prepared
&& m_input
!= NULL
&& GetError() == wxSOUND_NOERROR
)
350 if (!RepositionStream(new_position
))
351 return m_length
-m_bytes_left
;
353 if (new_position
>= m_length
) {
358 m_bytes_left
= m_length
-new_position
;
362 void wxSoundFileStream::OnSoundEvent(int evt
)
364 wxUint32 len
= m_codec
.GetBestSize();
367 buffer
= new char[len
];
368 wxSoundStream::OnSoundEvent(evt
);
370 while (!m_sndio
->QueueFilled()) {
373 if (len
> m_bytes_left
)
376 len
= m_codec
.Read(buffer
, len
).GetLastAccess();
377 PutData(buffer
, len
);
379 if (m_bytes_left
== 0) {
386 if (len
> m_bytes_left
)
389 len
= GetData(buffer
, len
);
391 if (m_bytes_left
== 0) {
396 m_codec
.Write(buffer
, len
);
403 bool wxSoundFileStream::SetSoundFormat(const wxSoundFormatBase
& format
)
405 wxSoundStream::SetSoundFormat(format
);
406 return m_codec
.SetSoundFormat(format
);