]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/mmedia/mmbman.cpp
ab4f5c8c5477e8465dfa0fbefa9ed853833292b1
[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.h"
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" wxWidgets 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__) && !defined(__CYGWIN__)
359 m_video_driver = new wxVideoXANIM(filename);
360 #elif defined(__WINDOWS__) && !defined(__MINGW32__) && !defined(__WATCOMC__) && !defined(__CYGWIN__)
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 wxUnusedVar(filename);
366 m_video_driver = NULL;
367 SetError(MMBoard_UnknownFile);
368 #endif
369 }
370
371 MMBoardVideoFile::~MMBoardVideoFile()
372 {
373 if (m_video_driver)
374 delete m_video_driver;
375 }
376
377 bool MMBoardVideoFile::NeedWindow()
378 {
379 return true;
380 }
381
382 void MMBoardVideoFile::SetWindow(wxWindow *window)
383 {
384 m_output_window = window;
385 m_video_driver->AttachOutput(*window);
386
387 wxSize size;
388 m_video_driver->GetSize(size);
389 window->SetSize(size);
390 // BAD BAD
391 // and we remove
392 // window->GetParent()->GetSizer()->Fit(window->GetParent());
393 }
394
395 void MMBoardVideoFile::Play()
396 {
397 m_video_driver->Play();
398 }
399
400 void MMBoardVideoFile::Pause()
401 {
402 m_video_driver->Pause();
403 }
404
405 void MMBoardVideoFile::Resume()
406 {
407 m_video_driver->Resume();
408 }
409
410 void MMBoardVideoFile::Stop()
411 {
412 m_video_driver->Stop();
413 }
414
415 MMBoardTime MMBoardVideoFile::GetPosition()
416 {
417 MMBoardTime btime;
418
419 btime.seconds = btime.minutes = btime.hours = 0;
420 return btime;
421 }
422
423 MMBoardTime MMBoardVideoFile::GetLength()
424 {
425 MMBoardTime btime;
426 int frameTime;
427
428 frameTime = (int)( m_video_driver->GetNbFrames() / m_video_driver->GetFrameRate());
429
430 btime.seconds = frameTime % 60;
431 btime.minutes = (frameTime / 60) % 60;
432 btime.hours = frameTime / 3600;
433 return btime;
434 }
435
436 void MMBoardVideoFile::SetPosition(MMBoardTime WXUNUSED(btime))
437 {
438 }
439
440 bool MMBoardVideoFile::IsStopped()
441 {
442 return m_video_driver->IsStopped();
443 }
444
445 bool MMBoardVideoFile::IsPaused()
446 {
447 return m_video_driver->IsPaused();
448 }
449
450 wxString MMBoardVideoFile::GetStringType()
451 {
452 return wxString(wxT("Video XANIM"));
453 }
454
455 wxString MMBoardVideoFile::GetStringInformation()
456 {
457 wxString info;
458
459 info = wxT("Video codec: ");
460 info += m_video_driver->GetMovieCodec() + _T("\n");
461 info += wxT("Audio codec: ");
462 info += m_video_driver->GetAudioCodec();
463 info += wxString::Format(_T(" Sample rate: %d Channels: %d\n"), m_video_driver->GetSampleRate(),
464 m_video_driver->GetBPS());
465 info += wxString::Format(_T(" Frame rate: %.01f"), m_video_driver->GetFrameRate());
466 return info;
467 }
468
469 // ----------------------------------------------------------------------------
470
471 // ----------------------------------------------------------------------------
472 // MMBoardFile
473
474 MMBoardFile::MMBoardFile()
475 {
476 m_error = 0;
477 }
478
479 MMBoardFile::~MMBoardFile()
480 {
481 }
482
483 //
484 // ----------------------------------------------------------------------------
485
486 // ----------------------------------------------------------------------------
487 // MMBoardManager
488
489 MMBoardFile *MMBoardManager::Open(const wxString& filename)
490 {
491 MMBoardFile *file;
492
493 // Test the audio codec
494 file = new MMBoardSoundFile(filename);
495 if (!file->GetError())
496 return file;
497 delete file;
498
499 // Test the video codec
500 file = new MMBoardVideoFile(filename);
501 if (!file->GetError())
502 return file;
503 delete file;
504
505 // Arrrgh, we just could not see what is that file ...
506 return NULL;
507 }
508
509 DECLARE_APP(MMBoardApp)
510
511 wxSoundStream *MMBoardManager::OpenSoundStream()
512 {
513 #ifdef __WIN32__
514 if ((wxGetApp().m_caps & MM_SOUND_WIN) != 0)
515 return new wxSoundStreamWin();
516 #elif __UNIX__
517 if ((wxGetApp().m_caps & MM_SOUND_ESD) != 0)
518 return new wxSoundStreamESD();
519
520 if ((wxGetApp().m_caps & MM_SOUND_OSS) != 0)
521 return new wxSoundStreamOSS();
522 #endif
523
524 wxMessageBox(_T("You are trying to open a multimedia but you have not devices"), _T("Error"), wxOK | wxICON_ERROR, NULL);
525
526 return NULL;
527 }
528
529 void MMBoardManager::UnrefSoundStream(wxSoundStream *stream)
530 {
531 delete stream;
532 }
533
534 // ----------------------------------------------------------------------------
535