]>
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 bool wxSoundRouterStream::StartProduction(int evt
)
98 if (m_sndio
->StartProduction(evt
))
101 m_snderror
= m_sndio
->GetError();
102 m_lastcount
= m_sndio
->GetLastAccess();
106 if (m_router
->StartProduction(evt
))
109 m_snderror
= m_router
->GetError();
110 m_lastcount
= m_router
->GetLastAccess();
114 bool wxSoundRouterStream::StopProduction()
117 if (m_sndio
->StopProduction())
120 m_snderror
= m_sndio
->GetError();
121 m_lastcount
= m_sndio
->GetLastAccess();
125 if (m_router
->StopProduction())
128 m_snderror
= m_router
->GetError();
129 m_lastcount
= m_router
->GetLastAccess();
134 // --------------------------------------------------------------------------
135 // wxSoundFileStream: generic reader
136 // --------------------------------------------------------------------------
138 wxSoundFileStream::wxSoundFileStream(wxInputStream
& stream
,
139 wxSoundStream
& io_sound
)
140 : m_codec(io_sound
), m_sndio(&io_sound
),
141 m_input(&stream
), m_output(NULL
), m_state(wxSOUND_FILE_STOPPED
)
145 wxSoundFileStream::wxSoundFileStream(wxOutputStream
& stream
,
146 wxSoundStream
& io_sound
)
147 : m_codec(io_sound
), m_sndio(&io_sound
),
148 m_input(NULL
), m_output(&stream
), m_state(wxSOUND_FILE_STOPPED
)
152 wxSoundFileStream::~wxSoundFileStream()
154 if (m_state
!= wxSOUND_FILE_STOPPED
)
158 bool wxSoundFileStream::Play()
160 if (m_state
!= wxSOUND_FILE_STOPPED
)
163 if (!PrepareToPlay())
166 m_state
= wxSOUND_FILE_PLAYING
;
168 if (!StartProduction(wxSOUND_OUTPUT
))
174 bool wxSoundFileStream::Record(unsigned long time
)
176 if (m_state
!= wxSOUND_FILE_STOPPED
)
179 if (!PrepareToRecord(time
))
182 m_len
= m_sndformat
->GetBytesFromTime(time
);
184 m_state
= wxSOUND_FILE_RECORDING
;
185 if (!StartProduction(wxSOUND_INPUT
))
191 bool wxSoundFileStream::Stop()
193 if (m_state
== wxSOUND_FILE_STOPPED
)
196 if (!StopProduction())
199 if (m_state
== wxSOUND_FILE_RECORDING
)
200 if (!FinishRecording()) {
201 m_state
= wxSOUND_FILE_STOPPED
;
205 // TODO reset counter
206 m_state
= wxSOUND_FILE_STOPPED
;
210 bool wxSoundFileStream::Pause()
212 if (m_state
== wxSOUND_FILE_PAUSED
|| m_state
== wxSOUND_FILE_STOPPED
)
215 if (!StopProduction())
218 m_oldstate
= m_state
;
219 m_state
= wxSOUND_FILE_PAUSED
;
223 bool wxSoundFileStream::Resume()
225 if (m_state
== wxSOUND_FILE_PLAYING
|| m_state
== wxSOUND_FILE_RECORDING
||
226 m_state
== wxSOUND_FILE_STOPPED
)
229 if (!StartProduction( (m_oldstate
== wxSOUND_FILE_PLAYING
) ?
230 wxSOUND_OUTPUT
: wxSOUND_INPUT
))
233 m_state
= m_oldstate
;
238 wxSoundStream
& wxSoundFileStream::Read(void *buffer
, wxUint32 len
)
240 m_lastcount
= GetData(buffer
, len
);
244 wxSoundStream
& wxSoundFileStream::Write(const void *buffer
, wxUint32 len
)
246 m_lastcount
= PutData(buffer
, len
);
250 void wxSoundFileStream::SetDuplexMode(bool duplex
)
254 bool wxSoundFileStream::StartProduction(int evt
)
256 m_sndio
->SetEventHandler(this);
258 if (!m_codec
.StartProduction(evt
))
264 bool wxSoundFileStream::StopProduction()
266 return m_codec
.StopProduction();
269 void wxSoundFileStream::OnSoundEvent(int evt
)
271 wxUint32 len
= m_sndio
->GetBestSize();
274 buffer
= new char[len
];
275 wxSoundStream::OnSoundEvent(evt
);
277 while (!m_sndio
->QueueFilled()) {
283 len
= m_codec
.Read(buffer
, len
).GetLastAccess();
284 PutData(buffer
, len
);
292 len
= GetData(buffer
, len
);
297 m_codec
.Write(buffer
, len
);
304 bool wxSoundFileStream::SetSoundFormat(const wxSoundFormatBase
& format
)
306 wxSoundStream::SetSoundFormat(format
);
307 return m_codec
.SetSoundFormat(format
);