]>
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
*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(__WIN32__)
361 m_video_driver
= new wxVideoWindows(filename
);
363 m_video_driver
= NULL
;
364 SetError(MMBoard_UnknownFile
);
368 MMBoardVideoFile::~MMBoardVideoFile()
371 delete m_video_driver
;
374 bool MMBoardVideoFile::NeedWindow()
379 void MMBoardVideoFile::SetWindow(wxWindow
*window
)
381 m_output_window
= window
;
382 m_video_driver
->AttachOutput(*window
);
385 m_video_driver
->GetSize(size
);
386 window
->SetSize(size
);
389 // window->GetParent()->GetSizer()->Fit(window->GetParent());
392 void MMBoardVideoFile::Play()
394 m_video_driver
->Play();
397 void MMBoardVideoFile::Pause()
399 m_video_driver
->Pause();
402 void MMBoardVideoFile::Resume()
404 m_video_driver
->Resume();
407 void MMBoardVideoFile::Stop()
409 m_video_driver
->Stop();
412 MMBoardTime
MMBoardVideoFile::GetPosition()
416 btime
.seconds
= btime
.minutes
= btime
.hours
= 0;
420 MMBoardTime
MMBoardVideoFile::GetLength()
425 frameTime
= (int)( m_video_driver
->GetNbFrames() / m_video_driver
->GetFrameRate());
427 btime
.seconds
= frameTime
% 60;
428 btime
.minutes
= (frameTime
/ 60) % 60;
429 btime
.hours
= frameTime
/ 3600;
433 void MMBoardVideoFile::SetPosition(MMBoardTime
WXUNUSED(btime
))
437 bool MMBoardVideoFile::IsStopped()
439 return m_video_driver
->IsStopped();
442 bool MMBoardVideoFile::IsPaused()
444 return m_video_driver
->IsPaused();
447 wxString
MMBoardVideoFile::GetStringType()
449 return wxString(wxT("Video XANIM"));
452 wxString
MMBoardVideoFile::GetStringInformation()
456 info
= wxT("Video codec: ");
457 info
+= m_video_driver
->GetMovieCodec() + _T("\n");
458 info
+= wxT("Audio codec: ");
459 info
+= m_video_driver
->GetAudioCodec();
460 info
+= wxString::Format(_T(" Sample rate: %d Channels: %d\n"), m_video_driver
->GetSampleRate(),
461 m_video_driver
->GetBPS());
462 info
+= wxString::Format(_T(" Frame rate: %.01f"), m_video_driver
->GetFrameRate());
466 // ----------------------------------------------------------------------------
468 // ----------------------------------------------------------------------------
471 MMBoardFile::MMBoardFile()
476 MMBoardFile::~MMBoardFile()
481 // ----------------------------------------------------------------------------
483 // ----------------------------------------------------------------------------
486 MMBoardFile
*MMBoardManager::Open(const wxString
& filename
)
490 // Test the audio codec
491 file
= new MMBoardSoundFile(filename
);
492 if (!file
->GetError())
496 // Test the video codec
497 file
= new MMBoardVideoFile(filename
);
498 if (!file
->GetError())
502 // Arrrgh, we just could not see what is that file ...
506 DECLARE_APP(MMBoardApp
)
508 wxSoundStream
*MMBoardManager::OpenSoundStream()
511 if ((wxGetApp().m_caps
& MM_SOUND_ESD
) != 0)
512 return new wxSoundStreamESD();
514 if ((wxGetApp().m_caps
& MM_SOUND_OSS
) != 0)
515 return new wxSoundStreamOSS();
519 if ((wxGetApp().m_caps
& MM_SOUND_WIN
) != 0)
520 return new wxSoundStreamWin();
523 wxMessageBox(_T("You are trying to open a multimedia but you have not devices"), _T("Error"), wxOK
| wxICON_ERROR
, NULL
);
528 void MMBoardManager::UnrefSoundStream(wxSoundStream
*stream
)
533 // ----------------------------------------------------------------------------