]> git.saurik.com Git - wxWidgets.git/blame - contrib/samples/mmedia/mmbman.cpp
Compilation fix for Cygwin
[wxWidgets.git] / contrib / samples / mmedia / mmbman.cpp
CommitLineData
e8482f24
GL
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__
694f70fa 13 #pragma implementation "mmbman.h"
e8482f24
GL
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
be5a51fb 28// need because it includes almost all "standard" wxWidgets headers
e8482f24
GL
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"
c42b1de6 44#include "wx/mmedia/sndmsad.h"
e8482f24
GL
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
71class MMBoardSoundFile: public MMBoardFile {
72public:
73 MMBoardSoundFile(const wxString& filename);
74 ~MMBoardSoundFile();
8b523dc5 75
e8482f24 76 bool NeedWindow();
8b523dc5 77
e8482f24 78 void SetWindow(wxWindow *window);
8b523dc5 79
e8482f24
GL
80 void Play();
81 void Pause();
82 void Resume();
83 void Stop();
8b523dc5 84
e8482f24
GL
85 MMBoardTime GetPosition();
86 MMBoardTime GetLength();
87 void SetPosition(MMBoardTime btime);
8b523dc5 88
e8482f24
GL
89 bool IsStopped();
90 bool IsPaused();
8b523dc5 91
e8482f24
GL
92 wxString GetStringType();
93 wxString GetStringInformation();
8b523dc5 94
e8482f24
GL
95protected:
96 wxSoundFileStream *GetDecoder();
8b523dc5 97
e8482f24
GL
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
106class MMBoardVideoFile: public MMBoardFile {
107public:
108 MMBoardVideoFile(const wxString& filename);
109 ~MMBoardVideoFile();
8b523dc5 110
e8482f24 111 bool NeedWindow();
8b523dc5 112
e8482f24 113 void SetWindow(wxWindow *window);
8b523dc5 114
e8482f24
GL
115 void Play();
116 void Pause();
117 void Resume();
118 void Stop();
8b523dc5 119
e8482f24
GL
120 MMBoardTime GetPosition();
121 MMBoardTime GetLength();
122 void SetPosition(MMBoardTime btime);
8b523dc5 123
e8482f24
GL
124 bool IsStopped();
125 bool IsPaused();
8b523dc5 126
e8482f24
GL
127 wxString GetStringType();
128 wxString GetStringInformation();
8b523dc5 129
e8482f24
GL
130protected:
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
146MMBoardSoundFile::MMBoardSoundFile(const wxString& filename)
147 : MMBoardFile()
148{
149 m_input_stream = new wxFileInputStream(filename);
150 m_output_stream = MMBoardManager::OpenSoundStream();
8b523dc5 151
e8482f24 152 m_file_stream = GetDecoder();
8b523dc5 153
e8482f24
GL
154 if (!m_file_stream) {
155 SetError(MMBoard_UnknownFile);
156 return;
157 }
8b523dc5 158
e8482f24
GL
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
169MMBoardSoundFile::~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
177wxSoundFileStream *MMBoardSoundFile::GetDecoder()
178{
179 wxSoundFileStream *f_stream;
8b523dc5 180
e8482f24
GL
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;
8b523dc5 187
e8482f24
GL
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;
8b523dc5 194
e8482f24 195 m_file_type = MMBoard_UNKNOWNTYPE;
8b523dc5 196
e8482f24 197 // TODO: automate
8b523dc5 198
e8482f24
GL
199 return NULL;
200}
201
202MMBoardTime MMBoardSoundFile::GetLength()
203{
204 return m_length;
205}
206
207bool MMBoardSoundFile::IsStopped()
208{
209 return m_file_stream->IsStopped();
210}
211
212bool MMBoardSoundFile::IsPaused()
213{
214 return m_file_stream->IsPaused();
215}
216
217MMBoardTime MMBoardSoundFile::GetPosition()
218{
219 wxUint32 length, seconds;
220 MMBoardTime file_time;
8b523dc5 221
e8482f24
GL
222 file_time.seconds = file_time.minutes = file_time.hours = 0;
223 if (m_file_stream->IsStopped())
224 return file_time;
8b523dc5 225
e8482f24
GL
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;
8b523dc5 231
e8482f24
GL
232 return file_time;
233}
234
235void 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
246bool MMBoardSoundFile::NeedWindow()
247{
dea7e44a 248 return false;
e8482f24
GL
249}
250
2bbf230a 251void MMBoardSoundFile::SetWindow(wxWindow *WXUNUSED(window))
e8482f24
GL
252{
253}
254
255void MMBoardSoundFile::Play()
256{
257 m_file_stream->Play();
258}
259
260void MMBoardSoundFile::Pause()
261{
262 m_file_stream->Pause();
263}
264
265void MMBoardSoundFile::Resume()
266{
267 m_file_stream->Resume();
268}
269
270void MMBoardSoundFile::Stop()
271{
272 m_file_stream->Stop();
273}
274
275wxString MMBoardSoundFile::GetStringType()
276{
277 switch (m_file_type) {
278 case MMBoard_WAVE:
279 return wxString(wxT("WAVE file"));
2bbf230a
JS
280 #if 0
281 // break is not reachable after return
e8482f24 282 break;
2bbf230a 283 #endif
e8482f24
GL
284 case MMBoard_AIFF:
285 return wxString(wxT("AIFF file"));
2bbf230a
JS
286 #if 0
287 // break is not reachable after return
e8482f24 288 break;
2bbf230a
JS
289 #endif
290 #if 0
291 // default moved outside switch for those compilers
292 // which complain about lack of return in function
e8482f24
GL
293 default:
294 return wxString(wxT("Unknown file"));
295 break;
2bbf230a 296 #endif
e8482f24 297 }
2bbf230a 298 return wxString(wxT("Unknown file"));
e8482f24
GL
299}
300
301wxString MMBoardSoundFile::GetStringInformation()
302{
303 wxString info;
304 wxSoundFormatBase *format;
8b523dc5 305
e8482f24 306 format = &(m_file_stream->GetSoundFormat());
8b523dc5 307
e8482f24
GL
308 info = wxT("Data encoding: ");
309 switch (format->GetType()) {
310 case wxSOUND_PCM: {
311 wxSoundFormatPcm *pcm_format = (wxSoundFormatPcm *)format;
8b523dc5 312
dea7e44a 313 info += wxString::Format(wxT("PCM %s %s\n"),
e8482f24
GL
314 pcm_format->Signed() ? wxT("signed") : wxT("unsigned"),
315 pcm_format->GetOrder() == wxLITTLE_ENDIAN ? wxT("little endian") : wxT("big endian"));
dea7e44a
WS
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;
e8482f24 324 }
c42b1de6
GL
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 }
e8482f24
GL
335 case wxSOUND_ULAW: {
336 wxSoundFormatUlaw *ulaw_format = (wxSoundFormatUlaw *)format;
dea7e44a
WS
337 info += wxT("ULAW\n");
338 info += wxString::Format(wxT("Sampling rate: %d\n"), ulaw_format->GetSampleRate());
339 break;
e8482f24
GL
340 }
341 default:
342 info += wxT("Unknown");
dea7e44a 343 break;
e8482f24
GL
344 }
345 return info;
346}
347
348// ----------------------------------------------------------------------------
349
350
351// ----------------------------------------------------------------------------
352// MMBoardVideoFile
353
354MMBoardVideoFile::MMBoardVideoFile(const wxString& filename)
355{
356 m_output_window = NULL;
8b523dc5 357
69067986 358#if defined(__UNIX__) && !defined(__CYGWIN__)
e8482f24 359 m_video_driver = new wxVideoXANIM(filename);
69067986 360#elif defined(__WINDOWS__) && !defined(__MINGW32__) && !defined(__WATCOMC__) && !defined(__CYGWIN__)
2b3644c7
JS
361 // versions of Open Watcom and MinGW tested against this source does not
362 // deliver "digitalv.h" required in this feature
e8482f24
GL
363 m_video_driver = new wxVideoWindows(filename);
364#else
8b523dc5 365 wxUnusedVar(filename);
e8482f24
GL
366 m_video_driver = NULL;
367 SetError(MMBoard_UnknownFile);
368#endif
369}
370
371MMBoardVideoFile::~MMBoardVideoFile()
372{
373 if (m_video_driver)
374 delete m_video_driver;
375}
376
377bool MMBoardVideoFile::NeedWindow()
378{
dea7e44a 379 return true;
e8482f24
GL
380}
381
382void 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
395void MMBoardVideoFile::Play()
396{
397 m_video_driver->Play();
398}
399
400void MMBoardVideoFile::Pause()
401{
402 m_video_driver->Pause();
403}
404
405void MMBoardVideoFile::Resume()
406{
407 m_video_driver->Resume();
408}
409
410void MMBoardVideoFile::Stop()
411{
412 m_video_driver->Stop();
413}
414
415MMBoardTime MMBoardVideoFile::GetPosition()
416{
417 MMBoardTime btime;
418
419 btime.seconds = btime.minutes = btime.hours = 0;
420 return btime;
421}
422
423MMBoardTime MMBoardVideoFile::GetLength()
424{
425 MMBoardTime btime;
426 int frameTime;
427
428 frameTime = (int)( m_video_driver->GetNbFrames() / m_video_driver->GetFrameRate());
8b523dc5 429
e8482f24
GL
430 btime.seconds = frameTime % 60;
431 btime.minutes = (frameTime / 60) % 60;
432 btime.hours = frameTime / 3600;
433 return btime;
434}
435
2bbf230a 436void MMBoardVideoFile::SetPosition(MMBoardTime WXUNUSED(btime))
e8482f24
GL
437{
438}
439
440bool MMBoardVideoFile::IsStopped()
441{
442 return m_video_driver->IsStopped();
443}
444
445bool MMBoardVideoFile::IsPaused()
446{
447 return m_video_driver->IsPaused();
448}
449
450wxString MMBoardVideoFile::GetStringType()
451{
452 return wxString(wxT("Video XANIM"));
453}
454
455wxString MMBoardVideoFile::GetStringInformation()
456{
457 wxString info;
458
459 info = wxT("Video codec: ");
2bbf230a 460 info += m_video_driver->GetMovieCodec() + _T("\n");
e8482f24
GL
461 info += wxT("Audio codec: ");
462 info += m_video_driver->GetAudioCodec();
2bbf230a 463 info += wxString::Format(_T(" Sample rate: %d Channels: %d\n"), m_video_driver->GetSampleRate(),
e8482f24 464 m_video_driver->GetBPS());
2bbf230a 465 info += wxString::Format(_T(" Frame rate: %.01f"), m_video_driver->GetFrameRate());
e8482f24
GL
466 return info;
467}
468
469// ----------------------------------------------------------------------------
470
471// ----------------------------------------------------------------------------
472// MMBoardFile
473
474MMBoardFile::MMBoardFile()
475{
476 m_error = 0;
477}
478
479MMBoardFile::~MMBoardFile()
480{
481}
482
483//
484// ----------------------------------------------------------------------------
485
486// ----------------------------------------------------------------------------
487// MMBoardManager
488
489MMBoardFile *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
509DECLARE_APP(MMBoardApp)
510
511wxSoundStream *MMBoardManager::OpenSoundStream()
512{
69067986
MW
513#ifdef __WIN32__
514 if ((wxGetApp().m_caps & MM_SOUND_WIN) != 0)
515 return new wxSoundStreamWin();
516#elif __UNIX__
e8482f24
GL
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
2bbf230a 524 wxMessageBox(_T("You are trying to open a multimedia but you have not devices"), _T("Error"), wxOK | wxICON_ERROR, NULL);
e8482f24
GL
525
526 return NULL;
527}
528
529void MMBoardManager::UnrefSoundStream(wxSoundStream *stream)
530{
531 delete stream;
532}
533
534// ----------------------------------------------------------------------------
535