]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/lib/sndfile.cpp
1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
7 // --------------------------------------------------------------------------
15 // --------------------------------------------------------------------------
17 // --------------------------------------------------------------------------
19 wxSoundRouterStream::wxSoundRouterStream(wxSoundStream
& sndio
)
20 : wxSoundStreamCodec(sndio
)
25 wxSoundRouterStream::~wxSoundRouterStream()
31 wxSoundStream
& wxSoundRouterStream::Read(void *buffer
, size_t len
)
34 m_router
->Read(buffer
, len
);
35 m_snderror
= m_router
->GetError();
36 m_lastcount
= m_router
->GetLastAccess();
38 m_sndio
->Read(buffer
, len
);
39 m_snderror
= m_sndio
->GetError();
40 m_lastcount
= m_sndio
->GetLastAccess();
45 wxSoundStream
& wxSoundRouterStream::Write(const void *buffer
, size_t len
)
48 m_router
->Write(buffer
, len
);
49 m_snderror
= m_router
->GetError();
50 m_lastcount
= m_router
->GetLastAccess();
52 m_sndio
->Write(buffer
, len
);
53 m_snderror
= m_sndio
->GetError();
54 m_lastcount
= m_sndio
->GetLastAccess();
59 bool wxSoundRouterStream::SetSoundFormat(const wxSoundFormatBase
& format
)
64 if (m_sndio
->SetSoundFormat(format
)) {
65 wxSoundStream::SetSoundFormat(m_sndio
->GetSoundFormat());
69 switch(format
.GetType()) {
70 case wxSOUND_NOFORMAT
:
73 m_router
= new wxSoundStreamPcm(*m_sndio
);
74 m_router
->SetSoundFormat(format
);
77 m_router
= new wxSoundStreamUlaw(*m_sndio
);
78 m_router
->SetSoundFormat(format
);
81 wxSoundStream::SetSoundFormat(m_router
->GetSoundFormat());
85 bool wxSoundRouterStream::StartProduction(int evt
)
88 if (m_sndio
->StartProduction(evt
))
91 m_snderror
= m_sndio
->GetError();
92 m_lastcount
= m_sndio
->GetLastAccess();
96 if (m_router
->StartProduction(evt
))
99 m_snderror
= m_router
->GetError();
100 m_lastcount
= m_router
->GetLastAccess();
104 bool wxSoundRouterStream::StopProduction()
107 if (m_sndio
->StopProduction())
110 m_snderror
= m_sndio
->GetError();
111 m_lastcount
= m_sndio
->GetLastAccess();
115 if (m_router
->StopProduction())
118 m_snderror
= m_router
->GetError();
119 m_lastcount
= m_router
->GetLastAccess();
124 // --------------------------------------------------------------------------
125 // wxSoundFileStream: generic reader
126 // --------------------------------------------------------------------------
128 wxSoundFileStream::wxSoundFileStream(wxInputStream
& stream
,
129 wxSoundStream
& io_sound
)
130 : m_codec(io_sound
), m_sndio(&io_sound
),
131 m_input(&stream
), m_output(NULL
), m_state(wxSOUND_FILE_STOPPED
)
135 wxSoundFileStream::wxSoundFileStream(wxOutputStream
& stream
,
136 wxSoundStream
& io_sound
)
137 : m_codec(io_sound
), m_sndio(&io_sound
),
138 m_input(NULL
), m_output(&stream
), m_state(wxSOUND_FILE_STOPPED
)
142 wxSoundFileStream::~wxSoundFileStream()
144 if (m_state
!= wxSOUND_FILE_STOPPED
)
148 bool wxSoundFileStream::Play()
150 if (m_state
!= wxSOUND_FILE_STOPPED
)
153 if (!PrepareToPlay())
156 if (!StartProduction(wxSOUND_OUTPUT
))
159 m_state
= wxSOUND_FILE_PLAYING
;
163 bool wxSoundFileStream::Record(unsigned long time
)
165 if (m_state
!= wxSOUND_FILE_STOPPED
)
168 if (!PrepareToRecord(time
))
171 m_len
= m_sndformat
->GetByteFromTime(time
);
173 if (!StartProduction(wxSOUND_INPUT
))
176 m_state
= wxSOUND_FILE_RECORDING
;
180 bool wxSoundFileStream::Stop()
182 if (m_state
== wxSOUND_FILE_STOPPED
)
185 if (!StopProduction())
188 if (m_state
== wxSOUND_FILE_RECORDING
)
189 if (!FinishRecording()) {
190 m_state
= wxSOUND_FILE_STOPPED
;
194 // TODO reset counter
195 m_state
= wxSOUND_FILE_STOPPED
;
199 bool wxSoundFileStream::Pause()
201 if (m_state
== wxSOUND_FILE_PAUSED
|| m_state
== wxSOUND_FILE_STOPPED
)
204 if (!StopProduction())
207 m_oldstate
= m_state
;
208 m_state
= wxSOUND_FILE_PAUSED
;
212 bool wxSoundFileStream::Resume()
214 if (m_state
== wxSOUND_FILE_PLAYING
|| m_state
== wxSOUND_FILE_RECORDING
||
215 m_state
== wxSOUND_FILE_STOPPED
)
218 if (!StartProduction( (m_oldstate
== wxSOUND_FILE_PLAYING
) ?
219 wxSOUND_OUTPUT
: wxSOUND_INPUT
))
222 m_state
= m_oldstate
;
227 wxSoundStream
& wxSoundFileStream::Read(void *buffer
, size_t len
)
229 m_lastcount
= GetData(buffer
, len
);
233 wxSoundStream
& wxSoundFileStream::Write(const void *buffer
, size_t len
)
235 m_lastcount
= PutData(buffer
, len
);
239 void wxSoundFileStream::SetDuplexMode(bool duplex
)
243 bool wxSoundFileStream::StartProduction(int evt
)
245 m_sndio
->SetEventHandler(this);
247 if (!m_codec
.StartProduction(evt
))
253 bool wxSoundFileStream::StopProduction()
255 return m_codec
.StopProduction();
258 void wxSoundFileStream::OnSoundEvent(int evt
)
260 size_t len
= m_sndio
->GetBestSize();
261 char buffer
[m_sndio
->GetBestSize()];
263 wxSoundStream::OnSoundEvent(evt
);
270 len
= m_codec
.Read(buffer
, len
).GetLastAccess();
271 PutData(buffer
, len
);
279 len
= GetData(buffer
, len
);
284 m_codec
.Write(buffer
, len
);
289 bool wxSoundFileStream::SetSoundFormat(const wxSoundFormatBase
& format
)
291 wxSoundStream::SetSoundFormat(format
);
292 return m_codec
.SetSoundFormat(format
);