]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia2/board/mmbman.cpp
Removed unnecessary code from utilsunx.cpp
[wxWidgets.git] / utils / wxMMedia2 / board / 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 "sndbase.h"
39 #include "sndfile.h"
40 #include "sndwav.h"
41 #include "sndaiff.h"
42 #include "sndpcm.h"
43 #include "sndulaw.h"
44
45 #ifdef __UNIX__
46 #include "sndoss.h"
47 #include "sndesd.h"
48 #endif
49
50 #ifdef __WIN32__
51 #include "sndwin.h"
52 #endif
53
54 #include "vidbase.h"
55 #ifdef __UNIX__
56 #include "vidxanm.h"
57 #endif
58
59 #ifdef __WIN32__
60 #include "vidwin.h"
61 #endif
62
63 #include "mmboard.h"
64 #include "mmbman.h"
65
66 // ----------------------------------------------------------------------------
67 // Private class definitions
68 // ----------------------------------------------------------------------------
69
70 class MMBoardSoundFile: public MMBoardFile {
71 public:
72 MMBoardSoundFile(const wxString& filename);
73 ~MMBoardSoundFile();
74
75 bool NeedWindow();
76
77 void SetWindow(wxWindow *window);
78
79 void Play();
80 void Pause();
81 void Resume();
82 void Stop();
83
84 MMBoardTime GetPosition();
85 MMBoardTime GetLength();
86 void SetPosition(MMBoardTime btime);
87
88 bool IsStopped();
89 bool IsPaused();
90
91 wxString GetStringType();
92 wxString GetStringInformation();
93
94 protected:
95 wxSoundFileStream *GetDecoder();
96
97 wxSoundStream *m_output_stream;
98 wxInputStream *m_input_stream;
99 wxSoundFileStream *m_file_stream;
100
101 MMBoardTime m_length;
102 wxUint8 m_file_type;
103 };
104
105 class MMBoardVideoFile: public MMBoardFile {
106 public:
107 MMBoardVideoFile(const wxString& filename);
108 ~MMBoardVideoFile();
109
110 bool NeedWindow();
111
112 void SetWindow(wxWindow *window);
113
114 void Play();
115 void Pause();
116 void Resume();
117 void Stop();
118
119 MMBoardTime GetPosition();
120 MMBoardTime GetLength();
121 void SetPosition(MMBoardTime btime);
122
123 bool IsStopped();
124 bool IsPaused();
125
126 wxString GetStringType();
127 wxString GetStringInformation();
128
129 protected:
130 wxWindow *m_output_window;
131 wxVideoBaseDriver *m_video_driver;
132 };
133
134 // ----------------------------------------------------------------------------
135 // Implementation
136 // ----------------------------------------------------------------------------
137
138 #define MMBoard_UNKNOWNTYPE 0
139 #define MMBoard_WAVE 1
140 #define MMBoard_AIFF 2
141
142 // ----------------------------------------------------------------------------
143 // MMBoardSoundFile
144
145 MMBoardSoundFile::MMBoardSoundFile(const wxString& filename)
146 : MMBoardFile()
147 {
148 m_input_stream = new wxFileInputStream(filename);
149 m_output_stream = MMBoardManager::OpenSoundStream();
150
151 m_file_stream = GetDecoder();
152
153 if (!m_file_stream) {
154 SetError(MMBoard_UnknownFile);
155 return;
156 }
157
158 // Compute length
159 wxUint32 length, seconds;
160
161 length = m_file_stream->GetLength();
162 seconds = m_file_stream->GetSoundFormat().GetTimeFromBytes(length);
163 m_length.seconds = seconds % 60;
164 m_length.minutes = (seconds / 60) % 60;
165 m_length.hours = seconds / 3600;
166 }
167
168 MMBoardSoundFile::~MMBoardSoundFile()
169 {
170 if (m_file_stream)
171 delete m_file_stream;
172 MMBoardManager::UnrefSoundStream(m_output_stream);
173 delete m_input_stream;
174 }
175
176 wxSoundFileStream *MMBoardSoundFile::GetDecoder()
177 {
178 wxSoundFileStream *f_stream;
179
180 // First, we try a Wave decoder
181 f_stream = new wxSoundWave(*m_input_stream, *m_output_stream);
182 m_file_type = MMBoard_WAVE;
183 if (f_stream->CanRead())
184 return f_stream;
185 delete f_stream;
186
187 // Then, a AIFF decoder
188 f_stream = new wxSoundAiff(*m_input_stream, *m_output_stream);
189 m_file_type = MMBoard_AIFF;
190 if (f_stream->CanRead())
191 return f_stream;
192 delete f_stream;
193
194 m_file_type = MMBoard_UNKNOWNTYPE;
195
196 // TODO: automate
197
198 return NULL;
199 }
200
201 MMBoardTime MMBoardSoundFile::GetLength()
202 {
203 return m_length;
204 }
205
206 bool MMBoardSoundFile::IsStopped()
207 {
208 return m_file_stream->IsStopped();
209 }
210
211 bool MMBoardSoundFile::IsPaused()
212 {
213 return m_file_stream->IsPaused();
214 }
215
216 MMBoardTime MMBoardSoundFile::GetPosition()
217 {
218 wxUint32 length, seconds;
219 MMBoardTime file_time;
220
221 file_time.seconds = file_time.minutes = file_time.hours = 0;
222 if (m_file_stream->IsStopped())
223 return file_time;
224
225 length = m_file_stream->GetPosition();
226 seconds = m_file_stream->GetSoundFormat().GetTimeFromBytes(length);
227 file_time.seconds = seconds % 60;
228 file_time.minutes = (seconds / 60) % 60;
229 file_time.hours = seconds / 3600;
230
231 return file_time;
232 }
233
234 void MMBoardSoundFile::SetPosition(MMBoardTime btime)
235 {
236 wxUint32 itime;
237
238 itime = btime.seconds + btime.minutes * 60 + btime.hours;
239
240 m_file_stream->SetPosition(
241 m_file_stream->GetSoundFormat().GetBytesFromTime(itime)
242 );
243 }
244
245 bool MMBoardSoundFile::NeedWindow()
246 {
247 return FALSE;
248 }
249
250 void MMBoardSoundFile::SetWindow(wxWindow *window)
251 {
252 }
253
254 void MMBoardSoundFile::Play()
255 {
256 m_file_stream->Play();
257 }
258
259 void MMBoardSoundFile::Pause()
260 {
261 m_file_stream->Pause();
262 }
263
264 void MMBoardSoundFile::Resume()
265 {
266 m_file_stream->Resume();
267 }
268
269 void MMBoardSoundFile::Stop()
270 {
271 m_file_stream->Stop();
272 }
273
274 wxString MMBoardSoundFile::GetStringType()
275 {
276 switch (m_file_type) {
277 case MMBoard_WAVE:
278 return wxString(wxT("WAVE file"));
279 break;
280 case MMBoard_AIFF:
281 return wxString(wxT("AIFF file"));
282 break;
283 default:
284 return wxString(wxT("Unknown file"));
285 break;
286 }
287 }
288
289 wxString MMBoardSoundFile::GetStringInformation()
290 {
291 wxString info;
292 wxSoundFormatBase *format;
293
294 format = &(m_file_stream->GetSoundFormat());
295
296 info = wxT("Data encoding: ");
297 switch (format->GetType()) {
298 case wxSOUND_PCM: {
299 wxSoundFormatPcm *pcm_format = (wxSoundFormatPcm *)format;
300
301 info += wxT("PCM\n");
302 info += wxString::Format(wxT("Sampling rate: %d\n")
303 wxT("Bits per sample: %d\n")
304 wxT("Number of channels: %d\n"),
305 pcm_format->GetSampleRate(),
306 pcm_format->GetBPS(),
307 pcm_format->GetChannels());
308
309 break;
310 }
311 case wxSOUND_ULAW: {
312 wxSoundFormatUlaw *ulaw_format = (wxSoundFormatUlaw *)format;
313 info += wxT("ULAW\n");
314 info += wxString::Format(wxT("Sampling rate: %d\n"), ulaw_format->GetSampleRate());
315 break;
316 }
317 default:
318 info += wxT("Unknown");
319 break;
320 }
321 return info;
322 }
323
324 // ----------------------------------------------------------------------------
325
326
327 // ----------------------------------------------------------------------------
328 // MMBoardVideoFile
329
330 MMBoardVideoFile::MMBoardVideoFile(const wxString& filename)
331 {
332 m_output_window = NULL;
333
334 #if defined(__UNIX__)
335 m_video_driver = new wxVideoXANIM(filename);
336 #elif defined(__WIN32__)
337 m_video_driver = new wxVideoWindows(filename);
338 #else
339 m_video_driver = NULL;
340 SetError(MMBoard_UnknownFile);
341 #endif
342 }
343
344 MMBoardVideoFile::~MMBoardVideoFile()
345 {
346 if (m_video_driver)
347 delete m_video_driver;
348 }
349
350 bool MMBoardVideoFile::NeedWindow()
351 {
352 return TRUE;
353 }
354
355 void MMBoardVideoFile::SetWindow(wxWindow *window)
356 {
357 m_output_window = window;
358 m_video_driver->AttachOutput(*window);
359
360 wxSize size;
361 m_video_driver->GetSize(size);
362 window->SetSize(size);
363 // BAD BAD
364 window->GetParent()->GetSizer()->Fit(window->GetParent());
365 }
366
367 void MMBoardVideoFile::Play()
368 {
369 m_video_driver->Play();
370 }
371
372 void MMBoardVideoFile::Pause()
373 {
374 m_video_driver->Pause();
375 }
376
377 void MMBoardVideoFile::Resume()
378 {
379 m_video_driver->Resume();
380 }
381
382 void MMBoardVideoFile::Stop()
383 {
384 m_video_driver->Stop();
385 }
386
387 MMBoardTime MMBoardVideoFile::GetPosition()
388 {
389 MMBoardTime btime;
390
391 btime.seconds = btime.minutes = btime.hours = 0;
392 return btime;
393 }
394
395 MMBoardTime MMBoardVideoFile::GetLength()
396 {
397 MMBoardTime btime;
398 int frameTime;
399
400 frameTime = (int)( m_video_driver->GetNbFrames() / m_video_driver->GetFrameRate());
401
402 btime.seconds = frameTime % 60;
403 btime.minutes = (frameTime / 60) % 60;
404 btime.hours = frameTime / 3600;
405 return btime;
406 }
407
408 void MMBoardVideoFile::SetPosition(MMBoardTime btime)
409 {
410 }
411
412 bool MMBoardVideoFile::IsStopped()
413 {
414 return m_video_driver->IsStopped();
415 }
416
417 bool MMBoardVideoFile::IsPaused()
418 {
419 return m_video_driver->IsPaused();
420 }
421
422 wxString MMBoardVideoFile::GetStringType()
423 {
424 return wxString(wxT("Video XANIM"));
425 }
426
427 wxString MMBoardVideoFile::GetStringInformation()
428 {
429 wxString info;
430
431 info = wxT("Video codec: ");
432 info += m_video_driver->GetMovieCodec() + "\n";
433 info += wxT("Audio codec: ");
434 info += m_video_driver->GetAudioCodec();
435 info += wxString::Format(" Sample rate: %d Channels: %d\n", m_video_driver->GetSampleRate(),
436 m_video_driver->GetBPS());
437 info += wxString::Format(" Frame rate: %.01f", m_video_driver->GetFrameRate());
438 return info;
439 }
440
441 // ----------------------------------------------------------------------------
442
443 // ----------------------------------------------------------------------------
444 // MMBoardFile
445
446 MMBoardFile::MMBoardFile()
447 {
448 m_error = 0;
449 }
450
451 MMBoardFile::~MMBoardFile()
452 {
453 }
454
455 //
456 // ----------------------------------------------------------------------------
457
458 // ----------------------------------------------------------------------------
459 // MMBoardManager
460
461 MMBoardFile *MMBoardManager::Open(const wxString& filename)
462 {
463 MMBoardFile *file;
464
465 // Test the audio codec
466 file = new MMBoardSoundFile(filename);
467 if (!file->GetError())
468 return file;
469 delete file;
470
471 // Test the video codec
472 file = new MMBoardVideoFile(filename);
473 if (!file->GetError())
474 return file;
475 delete file;
476
477 // Arrrgh, we just could not see what is that file ...
478 return NULL;
479 }
480
481 DECLARE_APP(MMBoardApp)
482
483 wxSoundStream *MMBoardManager::OpenSoundStream()
484 {
485 #ifdef __UNIX__
486 if ((wxGetApp().m_caps & MM_SOUND_ESD) != 0)
487 return new wxSoundStreamESD();
488
489 if ((wxGetApp().m_caps & MM_SOUND_OSS) != 0)
490 return new wxSoundStreamOSS();
491 #endif
492
493 #ifdef __WIN32__
494 if ((wxGetApp().m_caps & MM_SOUND_WIN) != 0)
495 return new wxSoundStreamWin();
496 #endif
497
498 wxMessageBox("You are trying to open a multimedia but you have not devices", "Error", wxOK | wxICON_ERROR, NULL);
499
500 return NULL;
501 }
502
503 void MMBoardManager::UnrefSoundStream(wxSoundStream *stream)
504 {
505 delete stream;
506 }
507
508 // ----------------------------------------------------------------------------
509