]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/lib/sndfile.cpp
1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
7 // --------------------------------------------------------------------------
11 #include <wx/stream.h>
20 // --------------------------------------------------------------------------
22 // --------------------------------------------------------------------------
24 wxSoundRouterStream::wxSoundRouterStream(wxSoundStream
& sndio
)
25 : wxSoundStreamCodec(sndio
)
30 wxSoundRouterStream::~wxSoundRouterStream()
36 wxSoundStream
& wxSoundRouterStream::Read(void *buffer
, size_t len
)
39 m_router
->Read(buffer
, len
);
40 m_snderror
= m_router
->GetError();
41 m_lastcount
= m_router
->GetLastAccess();
43 m_sndio
->Read(buffer
, len
);
44 m_snderror
= m_sndio
->GetError();
45 m_lastcount
= m_sndio
->GetLastAccess();
50 wxSoundStream
& wxSoundRouterStream::Write(const void *buffer
, size_t len
)
53 m_router
->Write(buffer
, len
);
54 m_snderror
= m_router
->GetError();
55 m_lastcount
= m_router
->GetLastAccess();
57 m_sndio
->Write(buffer
, len
);
58 m_snderror
= m_sndio
->GetError();
59 m_lastcount
= m_sndio
->GetLastAccess();
64 bool wxSoundRouterStream::SetSoundFormat(const wxSoundFormatBase
& format
)
69 if (m_sndio
->SetSoundFormat(format
)) {
70 wxSoundStream::SetSoundFormat(m_sndio
->GetSoundFormat());
74 switch(format
.GetType()) {
75 case wxSOUND_NOFORMAT
:
78 m_router
= new wxSoundStreamPcm(*m_sndio
);
79 m_router
->SetSoundFormat(format
);
82 m_router
= new wxSoundStreamUlaw(*m_sndio
);
83 m_router
->SetSoundFormat(format
);
86 wxSoundStream::SetSoundFormat(m_router
->GetSoundFormat());
90 bool wxSoundRouterStream::StartProduction(int evt
)
93 if (m_sndio
->StartProduction(evt
))
96 m_snderror
= m_sndio
->GetError();
97 m_lastcount
= m_sndio
->GetLastAccess();
101 if (m_router
->StartProduction(evt
))
104 m_snderror
= m_router
->GetError();
105 m_lastcount
= m_router
->GetLastAccess();
109 bool wxSoundRouterStream::StopProduction()
112 if (m_sndio
->StopProduction())
115 m_snderror
= m_sndio
->GetError();
116 m_lastcount
= m_sndio
->GetLastAccess();
120 if (m_router
->StopProduction())
123 m_snderror
= m_router
->GetError();
124 m_lastcount
= m_router
->GetLastAccess();
129 // --------------------------------------------------------------------------
130 // wxSoundFileStream: generic reader
131 // --------------------------------------------------------------------------
133 wxSoundFileStream::wxSoundFileStream(wxInputStream
& stream
,
134 wxSoundStream
& io_sound
)
135 : m_codec(io_sound
), m_sndio(&io_sound
),
136 m_input(&stream
), m_output(NULL
), m_state(wxSOUND_FILE_STOPPED
)
140 wxSoundFileStream::wxSoundFileStream(wxOutputStream
& stream
,
141 wxSoundStream
& io_sound
)
142 : m_codec(io_sound
), m_sndio(&io_sound
),
143 m_input(NULL
), m_output(&stream
), m_state(wxSOUND_FILE_STOPPED
)
147 wxSoundFileStream::~wxSoundFileStream()
149 if (m_state
!= wxSOUND_FILE_STOPPED
)
153 bool wxSoundFileStream::Play()
155 if (m_state
!= wxSOUND_FILE_STOPPED
)
158 if (!PrepareToPlay())
161 if (!StartProduction(wxSOUND_OUTPUT
))
164 m_state
= wxSOUND_FILE_PLAYING
;
168 bool wxSoundFileStream::Record(unsigned long time
)
170 if (m_state
!= wxSOUND_FILE_STOPPED
)
173 if (!PrepareToRecord(time
))
176 m_len
= m_sndformat
->GetBytesFromTime(time
);
178 if (!StartProduction(wxSOUND_INPUT
))
181 m_state
= wxSOUND_FILE_RECORDING
;
185 bool wxSoundFileStream::Stop()
187 if (m_state
== wxSOUND_FILE_STOPPED
)
190 if (!StopProduction())
193 if (m_state
== wxSOUND_FILE_RECORDING
)
194 if (!FinishRecording()) {
195 m_state
= wxSOUND_FILE_STOPPED
;
199 // TODO reset counter
200 m_state
= wxSOUND_FILE_STOPPED
;
204 bool wxSoundFileStream::Pause()
206 if (m_state
== wxSOUND_FILE_PAUSED
|| m_state
== wxSOUND_FILE_STOPPED
)
209 if (!StopProduction())
212 m_oldstate
= m_state
;
213 m_state
= wxSOUND_FILE_PAUSED
;
217 bool wxSoundFileStream::Resume()
219 if (m_state
== wxSOUND_FILE_PLAYING
|| m_state
== wxSOUND_FILE_RECORDING
||
220 m_state
== wxSOUND_FILE_STOPPED
)
223 if (!StartProduction( (m_oldstate
== wxSOUND_FILE_PLAYING
) ?
224 wxSOUND_OUTPUT
: wxSOUND_INPUT
))
227 m_state
= m_oldstate
;
232 wxSoundStream
& wxSoundFileStream::Read(void *buffer
, size_t len
)
234 m_lastcount
= GetData(buffer
, len
);
238 wxSoundStream
& wxSoundFileStream::Write(const void *buffer
, size_t len
)
240 m_lastcount
= PutData(buffer
, len
);
244 void wxSoundFileStream::SetDuplexMode(bool duplex
)
248 bool wxSoundFileStream::StartProduction(int evt
)
250 m_sndio
->SetEventHandler(this);
252 if (!m_codec
.StartProduction(evt
))
258 bool wxSoundFileStream::StopProduction()
260 return m_codec
.StopProduction();
263 void wxSoundFileStream::OnSoundEvent(int evt
)
265 size_t len
= m_sndio
->GetBestSize();
268 buffer
= new char[m_sndio
->GetBestSize()];
269 wxSoundStream::OnSoundEvent(evt
);
276 len
= m_codec
.Read(buffer
, len
).GetLastAccess();
277 PutData(buffer
, len
);
285 len
= GetData(buffer
, len
);
290 m_codec
.Write(buffer
, len
);
296 bool wxSoundFileStream::SetSoundFormat(const wxSoundFormatBase
& format
)
298 wxSoundStream::SetSoundFormat(format
);
299 return m_codec
.SetSoundFormat(format
);