]>
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>
21 // --------------------------------------------------------------------------
23 // --------------------------------------------------------------------------
25 wxSoundRouterStream::wxSoundRouterStream(wxSoundStream
& sndio
)
26 : wxSoundStreamCodec(sndio
)
31 wxSoundRouterStream::~wxSoundRouterStream()
37 wxSoundStream
& wxSoundRouterStream::Read(void *buffer
, wxUint32 len
)
40 m_router
->Read(buffer
, len
);
41 m_snderror
= m_router
->GetError();
42 m_lastcount
= m_router
->GetLastAccess();
44 m_sndio
->Read(buffer
, len
);
45 m_snderror
= m_sndio
->GetError();
46 m_lastcount
= m_sndio
->GetLastAccess();
51 wxSoundStream
& wxSoundRouterStream::Write(const void *buffer
, wxUint32 len
)
54 m_router
->Write(buffer
, len
);
55 m_snderror
= m_router
->GetError();
56 m_lastcount
= m_router
->GetLastAccess();
58 m_sndio
->Write(buffer
, len
);
59 m_snderror
= m_sndio
->GetError();
60 m_lastcount
= m_sndio
->GetLastAccess();
65 bool wxSoundRouterStream::SetSoundFormat(const wxSoundFormatBase
& format
)
70 if (m_sndio
->SetSoundFormat(format
)) {
71 wxSoundStream::SetSoundFormat(m_sndio
->GetSoundFormat());
75 switch(format
.GetType()) {
76 case wxSOUND_NOFORMAT
:
79 m_router
= new wxSoundStreamPcm(*m_sndio
);
80 m_router
->SetSoundFormat(format
);
83 m_router
= new wxSoundStreamUlaw(*m_sndio
);
84 m_router
->SetSoundFormat(format
);
87 m_router
= new wxSoundStreamG72X(*m_sndio
);
88 m_router
->SetSoundFormat(format
);
91 wxSoundStream::SetSoundFormat(m_router
->GetSoundFormat());
95 wxUint32
wxSoundRouterStream::GetBestSize() const
98 return m_router
->GetBestSize();
100 return m_sndio
->GetBestSize();
103 bool wxSoundRouterStream::StartProduction(int evt
)
106 if (m_sndio
->StartProduction(evt
))
109 m_snderror
= m_sndio
->GetError();
110 m_lastcount
= m_sndio
->GetLastAccess();
114 if (m_router
->StartProduction(evt
))
117 m_snderror
= m_router
->GetError();
118 m_lastcount
= m_router
->GetLastAccess();
122 bool wxSoundRouterStream::StopProduction()
125 if (m_sndio
->StopProduction())
128 m_snderror
= m_sndio
->GetError();
129 m_lastcount
= m_sndio
->GetLastAccess();
133 if (m_router
->StopProduction())
136 m_snderror
= m_router
->GetError();
137 m_lastcount
= m_router
->GetLastAccess();
142 // --------------------------------------------------------------------------
143 // wxSoundFileStream: generic reader
144 // --------------------------------------------------------------------------
146 wxSoundFileStream::wxSoundFileStream(wxInputStream
& stream
,
147 wxSoundStream
& io_sound
)
148 : m_codec(io_sound
), m_sndio(&io_sound
),
149 m_input(&stream
), m_output(NULL
), m_state(wxSOUND_FILE_STOPPED
)
153 wxSoundFileStream::wxSoundFileStream(wxOutputStream
& stream
,
154 wxSoundStream
& io_sound
)
155 : m_codec(io_sound
), m_sndio(&io_sound
),
156 m_input(NULL
), m_output(&stream
), m_state(wxSOUND_FILE_STOPPED
)
160 wxSoundFileStream::~wxSoundFileStream()
162 if (m_state
!= wxSOUND_FILE_STOPPED
)
166 bool wxSoundFileStream::Play()
168 if (m_state
!= wxSOUND_FILE_STOPPED
)
171 if (!PrepareToPlay())
174 m_state
= wxSOUND_FILE_PLAYING
;
176 if (!StartProduction(wxSOUND_OUTPUT
))
182 bool wxSoundFileStream::Record(unsigned long time
)
184 if (m_state
!= wxSOUND_FILE_STOPPED
)
187 if (!PrepareToRecord(time
))
190 m_len
= m_sndformat
->GetBytesFromTime(time
);
192 m_state
= wxSOUND_FILE_RECORDING
;
193 if (!StartProduction(wxSOUND_INPUT
))
199 bool wxSoundFileStream::Stop()
201 if (m_state
== wxSOUND_FILE_STOPPED
)
204 if (!StopProduction())
207 if (m_state
== wxSOUND_FILE_RECORDING
)
208 if (!FinishRecording()) {
209 m_state
= wxSOUND_FILE_STOPPED
;
213 // TODO reset counter
214 m_state
= wxSOUND_FILE_STOPPED
;
218 bool wxSoundFileStream::Pause()
220 if (m_state
== wxSOUND_FILE_PAUSED
|| m_state
== wxSOUND_FILE_STOPPED
)
223 if (!StopProduction())
226 m_oldstate
= m_state
;
227 m_state
= wxSOUND_FILE_PAUSED
;
231 bool wxSoundFileStream::Resume()
233 if (m_state
== wxSOUND_FILE_PLAYING
|| m_state
== wxSOUND_FILE_RECORDING
||
234 m_state
== wxSOUND_FILE_STOPPED
)
237 if (!StartProduction( (m_oldstate
== wxSOUND_FILE_PLAYING
) ?
238 wxSOUND_OUTPUT
: wxSOUND_INPUT
))
241 m_state
= m_oldstate
;
246 wxSoundStream
& wxSoundFileStream::Read(void *buffer
, wxUint32 len
)
248 m_lastcount
= GetData(buffer
, len
);
252 wxSoundStream
& wxSoundFileStream::Write(const void *buffer
, wxUint32 len
)
254 m_lastcount
= PutData(buffer
, len
);
258 void wxSoundFileStream::SetDuplexMode(bool duplex
)
262 bool wxSoundFileStream::StartProduction(int evt
)
264 m_sndio
->SetEventHandler(this);
266 if (!m_codec
.StartProduction(evt
))
272 bool wxSoundFileStream::StopProduction()
274 return m_codec
.StopProduction();
277 void wxSoundFileStream::OnSoundEvent(int evt
)
279 wxUint32 len
= m_codec
.GetBestSize();
282 buffer
= new char[len
];
283 wxSoundStream::OnSoundEvent(evt
);
285 while (!m_sndio
->QueueFilled()) {
291 len
= m_codec
.Read(buffer
, len
).GetLastAccess();
292 PutData(buffer
, len
);
301 len
= GetData(buffer
, len
);
307 m_codec
.Write(buffer
, len
);
314 bool wxSoundFileStream::SetSoundFormat(const wxSoundFormatBase
& format
)
316 wxSoundStream::SetSoundFormat(format
);
317 return m_codec
.SetSoundFormat(format
);