]>
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" wxWidgets 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
*WXUNUSED(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"));
281 // break is not reachable after return
285 return wxString(wxT("AIFF file"));
287 // break is not reachable after return
291 // default moved outside switch for those compilers
292 // which complain about lack of return in function
294 return wxString(wxT("Unknown file"));
298 return wxString(wxT("Unknown file"));
301 wxString
MMBoardSoundFile::GetStringInformation()
304 wxSoundFormatBase
*format
;
306 format
= &(m_file_stream
->GetSoundFormat());
308 info
= wxT("Data encoding: ");
309 switch (format
->GetType()) {
311 wxSoundFormatPcm
*pcm_format
= (wxSoundFormatPcm
*)format
;
313 info
+= wxString::Format(wxT("PCM %s %s\n"),
314 pcm_format
->Signed() ? wxT("signed") : wxT("unsigned"),
315 pcm_format
->GetOrder() == wxLITTLE_ENDIAN
? wxT("little endian") : wxT("big endian"));
316 info
+= wxString::Format(wxT("Sampling rate: %d\n")
317 wxT("Bits per sample: %d\n")
318 wxT("Number of channels: %d\n"),
319 pcm_format
->GetSampleRate(),
320 pcm_format
->GetBPS(),
321 pcm_format
->GetChannels());
325 case wxSOUND_MSADPCM
: {
326 wxSoundFormatMSAdpcm
*adpcm_format
= (wxSoundFormatMSAdpcm
*)format
;
328 info
+= wxString::Format(wxT("Microsoft ADPCM\n"));
329 info
+= wxString::Format(wxT("Sampling Rate: %d\n")
330 wxT("Number of channels: %d\n"),
331 adpcm_format
->GetSampleRate(),
332 adpcm_format
->GetChannels());
336 wxSoundFormatUlaw
*ulaw_format
= (wxSoundFormatUlaw
*)format
;
337 info
+= wxT("ULAW\n");
338 info
+= wxString::Format(wxT("Sampling rate: %d\n"), ulaw_format
->GetSampleRate());
342 info
+= wxT("Unknown");
348 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
354 MMBoardVideoFile::MMBoardVideoFile(const wxString
& filename
)
356 m_output_window
= NULL
;
358 #if defined(__UNIX__)
359 m_video_driver
= new wxVideoXANIM(filename
);
360 #elif defined(__WINDOWS__) && !defined(__MINGW32__) && !defined(__WATCOMC__)
361 // versions of Open Watcom and MinGW tested against this source does not
362 // deliver "digitalv.h" required in this feature
363 m_video_driver
= new wxVideoWindows(filename
);
365 m_video_driver
= NULL
;
366 SetError(MMBoard_UnknownFile
);
370 MMBoardVideoFile::~MMBoardVideoFile()
373 delete m_video_driver
;
376 bool MMBoardVideoFile::NeedWindow()
381 void MMBoardVideoFile::SetWindow(wxWindow
*window
)
383 m_output_window
= window
;
384 m_video_driver
->AttachOutput(*window
);
387 m_video_driver
->GetSize(size
);
388 window
->SetSize(size
);
391 // window->GetParent()->GetSizer()->Fit(window->GetParent());
394 void MMBoardVideoFile::Play()
396 m_video_driver
->Play();
399 void MMBoardVideoFile::Pause()
401 m_video_driver
->Pause();
404 void MMBoardVideoFile::Resume()
406 m_video_driver
->Resume();
409 void MMBoardVideoFile::Stop()
411 m_video_driver
->Stop();
414 MMBoardTime
MMBoardVideoFile::GetPosition()
418 btime
.seconds
= btime
.minutes
= btime
.hours
= 0;
422 MMBoardTime
MMBoardVideoFile::GetLength()
427 frameTime
= (int)( m_video_driver
->GetNbFrames() / m_video_driver
->GetFrameRate());
429 btime
.seconds
= frameTime
% 60;
430 btime
.minutes
= (frameTime
/ 60) % 60;
431 btime
.hours
= frameTime
/ 3600;
435 void MMBoardVideoFile::SetPosition(MMBoardTime
WXUNUSED(btime
))
439 bool MMBoardVideoFile::IsStopped()
441 return m_video_driver
->IsStopped();
444 bool MMBoardVideoFile::IsPaused()
446 return m_video_driver
->IsPaused();
449 wxString
MMBoardVideoFile::GetStringType()
451 return wxString(wxT("Video XANIM"));
454 wxString
MMBoardVideoFile::GetStringInformation()
458 info
= wxT("Video codec: ");
459 info
+= m_video_driver
->GetMovieCodec() + _T("\n");
460 info
+= wxT("Audio codec: ");
461 info
+= m_video_driver
->GetAudioCodec();
462 info
+= wxString::Format(_T(" Sample rate: %d Channels: %d\n"), m_video_driver
->GetSampleRate(),
463 m_video_driver
->GetBPS());
464 info
+= wxString::Format(_T(" Frame rate: %.01f"), m_video_driver
->GetFrameRate());
468 // ----------------------------------------------------------------------------
470 // ----------------------------------------------------------------------------
473 MMBoardFile::MMBoardFile()
478 MMBoardFile::~MMBoardFile()
483 // ----------------------------------------------------------------------------
485 // ----------------------------------------------------------------------------
488 MMBoardFile
*MMBoardManager::Open(const wxString
& filename
)
492 // Test the audio codec
493 file
= new MMBoardSoundFile(filename
);
494 if (!file
->GetError())
498 // Test the video codec
499 file
= new MMBoardVideoFile(filename
);
500 if (!file
->GetError())
504 // Arrrgh, we just could not see what is that file ...
508 DECLARE_APP(MMBoardApp
)
510 wxSoundStream
*MMBoardManager::OpenSoundStream()
513 if ((wxGetApp().m_caps
& MM_SOUND_ESD
) != 0)
514 return new wxSoundStreamESD();
516 if ((wxGetApp().m_caps
& MM_SOUND_OSS
) != 0)
517 return new wxSoundStreamOSS();
521 if ((wxGetApp().m_caps
& MM_SOUND_WIN
) != 0)
522 return new wxSoundStreamWin();
525 wxMessageBox(_T("You are trying to open a multimedia but you have not devices"), _T("Error"), wxOK
| wxICON_ERROR
, NULL
);
530 void MMBoardManager::UnrefSoundStream(wxSoundStream
*stream
)
535 // ----------------------------------------------------------------------------