]>
git.saurik.com Git - wxWidgets.git/blob - contrib/samples/mmedia/mmbman.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Multimedia Board manager
4 // Author: Guilhem Lavaux, <guilhem.lavaux@libertysurf.fr>
8 // Copyright: (c) 2000, Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "mmbman.cpp"
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWindows headers
35 #include "wx/stream.h"
36 #include "wx/wfstream.h"
38 #include "wx/mmedia/sndbase.h"
39 #include "wx/mmedia/sndfile.h"
40 #include "wx/mmedia/sndwav.h"
41 #include "wx/mmedia/sndaiff.h"
42 #include "wx/mmedia/sndpcm.h"
43 #include "wx/mmedia/sndulaw.h"
44 #include "wx/mmedia/sndmsad.h"
47 #include "wx/mmedia/sndoss.h"
48 #include "wx/mmedia/sndesd.h"
52 #include "wx/mmedia/sndwin.h"
55 #include "wx/mmedia/vidbase.h"
57 #include "wx/mmedia/vidxanm.h"
61 #include "wx/mmedia/vidwin.h"
67 // ----------------------------------------------------------------------------
68 // Private class definitions
69 // ----------------------------------------------------------------------------
71 class MMBoardSoundFile
: public MMBoardFile
{
73 MMBoardSoundFile(const wxString
& filename
);
78 void SetWindow(wxWindow
*window
);
85 MMBoardTime
GetPosition();
86 MMBoardTime
GetLength();
87 void SetPosition(MMBoardTime btime
);
92 wxString
GetStringType();
93 wxString
GetStringInformation();
96 wxSoundFileStream
*GetDecoder();
98 wxSoundStream
*m_output_stream
;
99 wxInputStream
*m_input_stream
;
100 wxSoundFileStream
*m_file_stream
;
102 MMBoardTime m_length
;
106 class MMBoardVideoFile
: public MMBoardFile
{
108 MMBoardVideoFile(const wxString
& filename
);
113 void SetWindow(wxWindow
*window
);
120 MMBoardTime
GetPosition();
121 MMBoardTime
GetLength();
122 void SetPosition(MMBoardTime btime
);
127 wxString
GetStringType();
128 wxString
GetStringInformation();
131 wxWindow
*m_output_window
;
132 wxVideoBaseDriver
*m_video_driver
;
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 #define MMBoard_UNKNOWNTYPE 0
140 #define MMBoard_WAVE 1
141 #define MMBoard_AIFF 2
143 // ----------------------------------------------------------------------------
146 MMBoardSoundFile::MMBoardSoundFile(const wxString
& filename
)
149 m_input_stream
= new wxFileInputStream(filename
);
150 m_output_stream
= MMBoardManager::OpenSoundStream();
152 m_file_stream
= GetDecoder();
154 if (!m_file_stream
) {
155 SetError(MMBoard_UnknownFile
);
160 wxUint32 length
, seconds
;
162 length
= m_file_stream
->GetLength();
163 seconds
= m_file_stream
->GetSoundFormat().GetTimeFromBytes(length
);
164 m_length
.seconds
= seconds
% 60;
165 m_length
.minutes
= (seconds
/ 60) % 60;
166 m_length
.hours
= seconds
/ 3600;
169 MMBoardSoundFile::~MMBoardSoundFile()
172 delete m_file_stream
;
173 MMBoardManager::UnrefSoundStream(m_output_stream
);
174 delete m_input_stream
;
177 wxSoundFileStream
*MMBoardSoundFile::GetDecoder()
179 wxSoundFileStream
*f_stream
;
181 // First, we try a Wave decoder
182 f_stream
= new wxSoundWave(*m_input_stream
, *m_output_stream
);
183 m_file_type
= MMBoard_WAVE
;
184 if (f_stream
->CanRead())
188 // Then, a AIFF decoder
189 f_stream
= new wxSoundAiff(*m_input_stream
, *m_output_stream
);
190 m_file_type
= MMBoard_AIFF
;
191 if (f_stream
->CanRead())
195 m_file_type
= MMBoard_UNKNOWNTYPE
;
202 MMBoardTime
MMBoardSoundFile::GetLength()
207 bool MMBoardSoundFile::IsStopped()
209 return m_file_stream
->IsStopped();
212 bool MMBoardSoundFile::IsPaused()
214 return m_file_stream
->IsPaused();
217 MMBoardTime
MMBoardSoundFile::GetPosition()
219 wxUint32 length
, seconds
;
220 MMBoardTime file_time
;
222 file_time
.seconds
= file_time
.minutes
= file_time
.hours
= 0;
223 if (m_file_stream
->IsStopped())
226 length
= m_file_stream
->GetPosition();
227 seconds
= m_file_stream
->GetSoundFormat().GetTimeFromBytes(length
);
228 file_time
.seconds
= seconds
% 60;
229 file_time
.minutes
= (seconds
/ 60) % 60;
230 file_time
.hours
= seconds
/ 3600;
235 void MMBoardSoundFile::SetPosition(MMBoardTime btime
)
239 itime
= btime
.seconds
+ btime
.minutes
* 60 + btime
.hours
;
241 m_file_stream
->SetPosition(
242 m_file_stream
->GetSoundFormat().GetBytesFromTime(itime
)
246 bool MMBoardSoundFile::NeedWindow()
251 void MMBoardSoundFile::SetWindow(wxWindow
*window
)
255 void MMBoardSoundFile::Play()
257 m_file_stream
->Play();
260 void MMBoardSoundFile::Pause()
262 m_file_stream
->Pause();
265 void MMBoardSoundFile::Resume()
267 m_file_stream
->Resume();
270 void MMBoardSoundFile::Stop()
272 m_file_stream
->Stop();
275 wxString
MMBoardSoundFile::GetStringType()
277 switch (m_file_type
) {
279 return wxString(wxT("WAVE file"));
282 return wxString(wxT("AIFF file"));
285 return wxString(wxT("Unknown file"));
290 wxString
MMBoardSoundFile::GetStringInformation()
293 wxSoundFormatBase
*format
;
295 format
= &(m_file_stream
->GetSoundFormat());
297 info
= wxT("Data encoding: ");
298 switch (format
->GetType()) {
300 wxSoundFormatPcm
*pcm_format
= (wxSoundFormatPcm
*)format
;
302 info
+= wxString::Format(wxT("PCM %s %s\n"),
303 pcm_format
->Signed() ? wxT("signed") : wxT("unsigned"),
304 pcm_format
->GetOrder() == wxLITTLE_ENDIAN
? wxT("little endian") : wxT("big endian"));
305 info
+= wxString::Format(wxT("Sampling rate: %d\n")
306 wxT("Bits per sample: %d\n")
307 wxT("Number of channels: %d\n"),
308 pcm_format
->GetSampleRate(),
309 pcm_format
->GetBPS(),
310 pcm_format
->GetChannels());
314 case wxSOUND_MSADPCM
: {
315 wxSoundFormatMSAdpcm
*adpcm_format
= (wxSoundFormatMSAdpcm
*)format
;
317 info
+= wxString::Format(wxT("Microsoft ADPCM\n"));
318 info
+= wxString::Format(wxT("Sampling Rate: %d\n")
319 wxT("Number of channels: %d\n"),
320 adpcm_format
->GetSampleRate(),
321 adpcm_format
->GetChannels());
325 wxSoundFormatUlaw
*ulaw_format
= (wxSoundFormatUlaw
*)format
;
326 info
+= wxT("ULAW\n");
327 info
+= wxString::Format(wxT("Sampling rate: %d\n"), ulaw_format
->GetSampleRate());
331 info
+= wxT("Unknown");
337 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
343 MMBoardVideoFile::MMBoardVideoFile(const wxString
& filename
)
345 m_output_window
= NULL
;
347 #if defined(__UNIX__)
348 m_video_driver
= new wxVideoXANIM(filename
);
349 #elif defined(__WIN32__)
350 m_video_driver
= new wxVideoWindows(filename
);
352 m_video_driver
= NULL
;
353 SetError(MMBoard_UnknownFile
);
357 MMBoardVideoFile::~MMBoardVideoFile()
360 delete m_video_driver
;
363 bool MMBoardVideoFile::NeedWindow()
368 void MMBoardVideoFile::SetWindow(wxWindow
*window
)
370 m_output_window
= window
;
371 m_video_driver
->AttachOutput(*window
);
374 m_video_driver
->GetSize(size
);
375 window
->SetSize(size
);
378 // window->GetParent()->GetSizer()->Fit(window->GetParent());
381 void MMBoardVideoFile::Play()
383 m_video_driver
->Play();
386 void MMBoardVideoFile::Pause()
388 m_video_driver
->Pause();
391 void MMBoardVideoFile::Resume()
393 m_video_driver
->Resume();
396 void MMBoardVideoFile::Stop()
398 m_video_driver
->Stop();
401 MMBoardTime
MMBoardVideoFile::GetPosition()
405 btime
.seconds
= btime
.minutes
= btime
.hours
= 0;
409 MMBoardTime
MMBoardVideoFile::GetLength()
414 frameTime
= (int)( m_video_driver
->GetNbFrames() / m_video_driver
->GetFrameRate());
416 btime
.seconds
= frameTime
% 60;
417 btime
.minutes
= (frameTime
/ 60) % 60;
418 btime
.hours
= frameTime
/ 3600;
422 void MMBoardVideoFile::SetPosition(MMBoardTime btime
)
426 bool MMBoardVideoFile::IsStopped()
428 return m_video_driver
->IsStopped();
431 bool MMBoardVideoFile::IsPaused()
433 return m_video_driver
->IsPaused();
436 wxString
MMBoardVideoFile::GetStringType()
438 return wxString(wxT("Video XANIM"));
441 wxString
MMBoardVideoFile::GetStringInformation()
445 info
= wxT("Video codec: ");
446 info
+= m_video_driver
->GetMovieCodec() + "\n";
447 info
+= wxT("Audio codec: ");
448 info
+= m_video_driver
->GetAudioCodec();
449 info
+= wxString::Format(" Sample rate: %d Channels: %d\n", m_video_driver
->GetSampleRate(),
450 m_video_driver
->GetBPS());
451 info
+= wxString::Format(" Frame rate: %.01f", m_video_driver
->GetFrameRate());
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
460 MMBoardFile::MMBoardFile()
465 MMBoardFile::~MMBoardFile()
470 // ----------------------------------------------------------------------------
472 // ----------------------------------------------------------------------------
475 MMBoardFile
*MMBoardManager::Open(const wxString
& filename
)
479 // Test the audio codec
480 file
= new MMBoardSoundFile(filename
);
481 if (!file
->GetError())
485 // Test the video codec
486 file
= new MMBoardVideoFile(filename
);
487 if (!file
->GetError())
491 // Arrrgh, we just could not see what is that file ...
495 DECLARE_APP(MMBoardApp
)
497 wxSoundStream
*MMBoardManager::OpenSoundStream()
500 if ((wxGetApp().m_caps
& MM_SOUND_ESD
) != 0)
501 return new wxSoundStreamESD();
503 if ((wxGetApp().m_caps
& MM_SOUND_OSS
) != 0)
504 return new wxSoundStreamOSS();
508 if ((wxGetApp().m_caps
& MM_SOUND_WIN
) != 0)
509 return new wxSoundStreamWin();
512 wxMessageBox("You are trying to open a multimedia but you have not devices", "Error", wxOK
| wxICON_ERROR
, NULL
);
517 void MMBoardManager::UnrefSoundStream(wxSoundStream
*stream
)
522 // ----------------------------------------------------------------------------