]>
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 m_state
= wxSOUND_FILE_PLAYING
;
163 if (!StartProduction(wxSOUND_OUTPUT
))
169 bool wxSoundFileStream::Record(unsigned long time
)
171 if (m_state
!= wxSOUND_FILE_STOPPED
)
174 if (!PrepareToRecord(time
))
177 m_len
= m_sndformat
->GetBytesFromTime(time
);
179 m_state
= wxSOUND_FILE_RECORDING
;
180 if (!StartProduction(wxSOUND_INPUT
))
186 bool wxSoundFileStream::Stop()
188 if (m_state
== wxSOUND_FILE_STOPPED
)
191 if (!StopProduction())
194 if (m_state
== wxSOUND_FILE_RECORDING
)
195 if (!FinishRecording()) {
196 m_state
= wxSOUND_FILE_STOPPED
;
200 // TODO reset counter
201 m_state
= wxSOUND_FILE_STOPPED
;
205 bool wxSoundFileStream::Pause()
207 if (m_state
== wxSOUND_FILE_PAUSED
|| m_state
== wxSOUND_FILE_STOPPED
)
210 if (!StopProduction())
213 m_oldstate
= m_state
;
214 m_state
= wxSOUND_FILE_PAUSED
;
218 bool wxSoundFileStream::Resume()
220 if (m_state
== wxSOUND_FILE_PLAYING
|| m_state
== wxSOUND_FILE_RECORDING
||
221 m_state
== wxSOUND_FILE_STOPPED
)
224 if (!StartProduction( (m_oldstate
== wxSOUND_FILE_PLAYING
) ?
225 wxSOUND_OUTPUT
: wxSOUND_INPUT
))
228 m_state
= m_oldstate
;
233 wxSoundStream
& wxSoundFileStream::Read(void *buffer
, size_t len
)
235 m_lastcount
= GetData(buffer
, len
);
239 wxSoundStream
& wxSoundFileStream::Write(const void *buffer
, size_t len
)
241 m_lastcount
= PutData(buffer
, len
);
245 void wxSoundFileStream::SetDuplexMode(bool duplex
)
249 bool wxSoundFileStream::StartProduction(int evt
)
251 m_sndio
->SetEventHandler(this);
253 if (!m_codec
.StartProduction(evt
))
259 bool wxSoundFileStream::StopProduction()
261 return m_codec
.StopProduction();
264 void wxSoundFileStream::OnSoundEvent(int evt
)
266 size_t len
= m_sndio
->GetBestSize();
269 buffer
= new char[len
];
270 wxSoundStream::OnSoundEvent(evt
);
272 while (!m_sndio
->QueueFilled()) {
278 len
= m_codec
.Read(buffer
, len
).GetLastAccess();
279 PutData(buffer
, len
);
287 len
= GetData(buffer
, len
);
292 m_codec
.Write(buffer
, len
);
299 bool wxSoundFileStream::SetSoundFormat(const wxSoundFormatBase
& format
)
301 wxSoundStream::SetSoundFormat(format
);
302 return m_codec
.SetSoundFormat(format
);