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