]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/mmedia/mmbman.cpp
Applied patch [ 875663 ] Warning free mmedia contrib
[wxWidgets.git] / contrib / samples / mmedia / mmbman.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mmbman.cpp
3 // Purpose: Multimedia Board manager
4 // Author: Guilhem Lavaux, <guilhem.lavaux@libertysurf.fr>
5 // Modified by:
6 // Created: 13/02/2000
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000, Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "mmbman.cpp"
14 #endif
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWindows headers
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 // Personal headers
34
35 #include "wx/stream.h"
36 #include "wx/wfstream.h"
37
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"
45
46 #ifdef __UNIX__
47 #include "wx/mmedia/sndoss.h"
48 #include "wx/mmedia/sndesd.h"
49 #endif
50
51 #ifdef __WIN32__
52 #include "wx/mmedia/sndwin.h"
53 #endif
54
55 #include "wx/mmedia/vidbase.h"
56 #ifdef __UNIX__
57 #include "wx/mmedia/vidxanm.h"
58 #endif
59
60 #ifdef __WIN32__
61 #include "wx/mmedia/vidwin.h"
62 #endif
63
64 #include "mmboard.h"
65 #include "mmbman.h"
66
67 // ----------------------------------------------------------------------------
68 // Private class definitions
69 // ----------------------------------------------------------------------------
70
71 class MMBoardSoundFile: public MMBoardFile {
72 public:
73 MMBoardSoundFile(const wxString& filename);
74 ~MMBoardSoundFile();
75
76 bool NeedWindow();
77
78 void SetWindow(wxWindow *window);
79
80 void Play();
81 void Pause();
82 void Resume();
83 void Stop();
84
85 MMBoardTime GetPosition();
86 MMBoardTime GetLength();
87 void SetPosition(MMBoardTime btime);
88
89 bool IsStopped();
90 bool IsPaused();
91
92 wxString GetStringType();
93 wxString GetStringInformation();
94
95 protected:
96 wxSoundFileStream *GetDecoder();
97
98 wxSoundStream *m_output_stream;
99 wxInputStream *m_input_stream;
100 wxSoundFileStream *m_file_stream;
101
102 MMBoardTime m_length;
103 wxUint8 m_file_type;
104 };
105
106 class MMBoardVideoFile: public MMBoardFile {
107 public:
108 MMBoardVideoFile(const wxString& filename);
109 ~MMBoardVideoFile();
110
111 bool NeedWindow();
112
113 void SetWindow(wxWindow *window);
114
115 void Play();
116 void Pause();
117 void Resume();
118 void Stop();
119
120 MMBoardTime GetPosition();
121 MMBoardTime GetLength();
122 void SetPosition(MMBoardTime btime);
123
124 bool IsStopped();
125 bool IsPaused();
126
127 wxString GetStringType();
128 wxString GetStringInformation();
129
130 protected:
131 wxWindow *m_output_window;
132 wxVideoBaseDriver *m_video_driver;
133 };
134
135 // ----------------------------------------------------------------------------
136 // Implementation
137 // ----------------------------------------------------------------------------
138
139 #define MMBoard_UNKNOWNTYPE 0
140 #define MMBoard_WAVE 1
141 #define MMBoard_AIFF 2
142
143 // ----------------------------------------------------------------------------
144 // MMBoardSoundFile
145
146 MMBoardSoundFile::MMBoardSoundFile(const wxString& filename)
147 : MMBoardFile()
148 {
149 m_input_stream = new wxFileInputStream(filename);
150 m_output_stream = MMBoardManager::OpenSoundStream();
151
152 m_file_stream = GetDecoder();
153
154 if (!m_file_stream) {
155 SetError(MMBoard_UnknownFile);
156 return;
157 }
158
159 // Compute length
160 wxUint32 length, seconds;
161
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;
167 }
168
169 MMBoardSoundFile::~MMBoardSoundFile()
170 {
171 if (m_file_stream)
172 delete m_file_stream;
173 MMBoardManager::UnrefSoundStream(m_output_stream);
174 delete m_input_stream;
175 }
176
177 wxSoundFileStream *MMBoardSoundFile::GetDecoder()
178 {
179 wxSoundFileStream *f_stream;
180
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())
185 return f_stream;
186 delete f_stream;
187
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())
192 return f_stream;
193 delete f_stream;
194
195 m_file_type = MMBoard_UNKNOWNTYPE;
196
197 // TODO: automate
198
199 return NULL;
200 }
201
202 MMBoardTime MMBoardSoundFile::GetLength()
203 {
204 return m_length;
205 }
206
207 bool MMBoardSoundFile::IsStopped()
208 {
209 return m_file_stream->IsStopped();
210 }
211
212 bool MMBoardSoundFile::IsPaused()
213 {
214 return m_file_stream->IsPaused();
215 }
216
217 MMBoardTime MMBoardSoundFile::GetPosition()
218 {
219 wxUint32 length, seconds;
220 MMBoardTime file_time;
221
222 file_time.seconds = file_time.minutes = file_time.hours = 0;
223 if (m_file_stream->IsStopped())
224 return file_time;
225
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;
231
232 return file_time;
233 }
234
235 void MMBoardSoundFile::SetPosition(MMBoardTime btime)
236 {
237 wxUint32 itime;
238
239 itime = btime.seconds + btime.minutes * 60 + btime.hours;
240
241 m_file_stream->SetPosition(
242 m_file_stream->GetSoundFormat().GetBytesFromTime(itime)
243 );
244 }
245
246 bool MMBoardSoundFile::NeedWindow()
247 {
248 return FALSE;
249 }
250
251 void MMBoardSoundFile::SetWindow(wxWindow *WXUNUSED(window))
252 {
253 }
254
255 void MMBoardSoundFile::Play()
256 {
257 m_file_stream->Play();
258 }
259
260 void MMBoardSoundFile::Pause()
261 {
262 m_file_stream->Pause();
263 }
264
265 void MMBoardSoundFile::Resume()
266 {
267 m_file_stream->Resume();
268 }
269
270 void MMBoardSoundFile::Stop()
271 {
272 m_file_stream->Stop();
273 }
274
275 wxString MMBoardSoundFile::GetStringType()
276 {
277 switch (m_file_type) {
278 case MMBoard_WAVE:
279 return wxString(wxT("WAVE file"));
280 #if 0
281 // break is not reachable after return
282 break;
283 #endif
284 case MMBoard_AIFF:
285 return wxString(wxT("AIFF file"));
286 #if 0
287 // break is not reachable after return
288 break;
289 #endif
290 #if 0
291 // default moved outside switch for those compilers
292 // which complain about lack of return in function
293 default:
294 return wxString(wxT("Unknown file"));
295 break;
296 #endif
297 }
298 return wxString(wxT("Unknown file"));
299 }
300
301 wxString MMBoardSoundFile::GetStringInformation()
302 {
303 wxString info;
304 wxSoundFormatBase *format;
305
306 format = &(m_file_stream->GetSoundFormat());
307
308 info = wxT("Data encoding: ");
309 switch (format->GetType()) {
310 case wxSOUND_PCM: {
311 wxSoundFormatPcm *pcm_format = (wxSoundFormatPcm *)format;
312
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());
322
323 break;
324 }
325 case wxSOUND_MSADPCM: {
326 wxSoundFormatMSAdpcm *adpcm_format = (wxSoundFormatMSAdpcm *)format;
327
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());
333 break;
334 }
335 case wxSOUND_ULAW: {
336 wxSoundFormatUlaw *ulaw_format = (wxSoundFormatUlaw *)format;
337 info += wxT("ULAW\n");
338 info += wxString::Format(wxT("Sampling rate: %d\n"), ulaw_format->GetSampleRate());
339 break;
340 }
341 default:
342 info += wxT("Unknown");
343 break;
344 }
345 return info;
346 }
347
348 // ----------------------------------------------------------------------------
349
350
351 // ----------------------------------------------------------------------------
352 // MMBoardVideoFile
353
354 MMBoardVideoFile::MMBoardVideoFile(const wxString& filename)
355 {
356 m_output_window = NULL;
357
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);
364 #else
365 m_video_driver = NULL;
366 SetError(MMBoard_UnknownFile);
367 #endif
368 }
369
370 MMBoardVideoFile::~MMBoardVideoFile()
371 {
372 if (m_video_driver)
373 delete m_video_driver;
374 }
375
376 bool MMBoardVideoFile::NeedWindow()
377 {
378 return TRUE;
379 }
380
381 void MMBoardVideoFile::SetWindow(wxWindow *window)
382 {
383 m_output_window = window;
384 m_video_driver->AttachOutput(*window);
385
386 wxSize size;
387 m_video_driver->GetSize(size);
388 window->SetSize(size);
389 // BAD BAD
390 // and we remove
391 // window->GetParent()->GetSizer()->Fit(window->GetParent());
392 }
393
394 void MMBoardVideoFile::Play()
395 {
396 m_video_driver->Play();
397 }
398
399 void MMBoardVideoFile::Pause()
400 {
401 m_video_driver->Pause();
402 }
403
404 void MMBoardVideoFile::Resume()
405 {
406 m_video_driver->Resume();
407 }
408
409 void MMBoardVideoFile::Stop()
410 {
411 m_video_driver->Stop();
412 }
413
414 MMBoardTime MMBoardVideoFile::GetPosition()
415 {
416 MMBoardTime btime;
417
418 btime.seconds = btime.minutes = btime.hours = 0;
419 return btime;
420 }
421
422 MMBoardTime MMBoardVideoFile::GetLength()
423 {
424 MMBoardTime btime;
425 int frameTime;
426
427 frameTime = (int)( m_video_driver->GetNbFrames() / m_video_driver->GetFrameRate());
428
429 btime.seconds = frameTime % 60;
430 btime.minutes = (frameTime / 60) % 60;
431 btime.hours = frameTime / 3600;
432 return btime;
433 }
434
435 void MMBoardVideoFile::SetPosition(MMBoardTime WXUNUSED(btime))
436 {
437 }
438
439 bool MMBoardVideoFile::IsStopped()
440 {
441 return m_video_driver->IsStopped();
442 }
443
444 bool MMBoardVideoFile::IsPaused()
445 {
446 return m_video_driver->IsPaused();
447 }
448
449 wxString MMBoardVideoFile::GetStringType()
450 {
451 return wxString(wxT("Video XANIM"));
452 }
453
454 wxString MMBoardVideoFile::GetStringInformation()
455 {
456 wxString info;
457
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());
465 return info;
466 }
467
468 // ----------------------------------------------------------------------------
469
470 // ----------------------------------------------------------------------------
471 // MMBoardFile
472
473 MMBoardFile::MMBoardFile()
474 {
475 m_error = 0;
476 }
477
478 MMBoardFile::~MMBoardFile()
479 {
480 }
481
482 //
483 // ----------------------------------------------------------------------------
484
485 // ----------------------------------------------------------------------------
486 // MMBoardManager
487
488 MMBoardFile *MMBoardManager::Open(const wxString& filename)
489 {
490 MMBoardFile *file;
491
492 // Test the audio codec
493 file = new MMBoardSoundFile(filename);
494 if (!file->GetError())
495 return file;
496 delete file;
497
498 // Test the video codec
499 file = new MMBoardVideoFile(filename);
500 if (!file->GetError())
501 return file;
502 delete file;
503
504 // Arrrgh, we just could not see what is that file ...
505 return NULL;
506 }
507
508 DECLARE_APP(MMBoardApp)
509
510 wxSoundStream *MMBoardManager::OpenSoundStream()
511 {
512 #ifdef __UNIX__
513 if ((wxGetApp().m_caps & MM_SOUND_ESD) != 0)
514 return new wxSoundStreamESD();
515
516 if ((wxGetApp().m_caps & MM_SOUND_OSS) != 0)
517 return new wxSoundStreamOSS();
518 #endif
519
520 #ifdef __WIN32__
521 if ((wxGetApp().m_caps & MM_SOUND_WIN) != 0)
522 return new wxSoundStreamWin();
523 #endif
524
525 wxMessageBox(_T("You are trying to open a multimedia but you have not devices"), _T("Error"), wxOK | wxICON_ERROR, NULL);
526
527 return NULL;
528 }
529
530 void MMBoardManager::UnrefSoundStream(wxSoundStream *stream)
531 {
532 delete stream;
533 }
534
535 // ----------------------------------------------------------------------------
536