]> git.saurik.com Git - wxWidgets.git/blame_incremental - utils/wxMMedia2/board/mmbman.cpp
menu sample added
[wxWidgets.git] / utils / wxMMedia2 / board / mmbman.cpp
... / ...
CommitLineData
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
70class MMBoardSoundFile: public MMBoardFile {
71public:
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
94protected:
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
105class MMBoardVideoFile: public MMBoardFile {
106public:
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
129protected:
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
145MMBoardSoundFile::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
168MMBoardSoundFile::~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
176wxSoundFileStream *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
201MMBoardTime MMBoardSoundFile::GetLength()
202{
203 return m_length;
204}
205
206bool MMBoardSoundFile::IsStopped()
207{
208 return m_file_stream->IsStopped();
209}
210
211bool MMBoardSoundFile::IsPaused()
212{
213 return m_file_stream->IsPaused();
214}
215
216MMBoardTime 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
234void 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
245bool MMBoardSoundFile::NeedWindow()
246{
247 return FALSE;
248}
249
250void MMBoardSoundFile::SetWindow(wxWindow *window)
251{
252}
253
254void MMBoardSoundFile::Play()
255{
256 m_file_stream->Play();
257}
258
259void MMBoardSoundFile::Pause()
260{
261 m_file_stream->Pause();
262}
263
264void MMBoardSoundFile::Resume()
265{
266 m_file_stream->Resume();
267}
268
269void MMBoardSoundFile::Stop()
270{
271 m_file_stream->Stop();
272}
273
274wxString 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
289wxString 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 += wxString::Format(wxT("PCM %s %s\n"),
302 pcm_format->Signed() ? wxT("signed") : wxT("unsigned"),
303 pcm_format->GetOrder() == wxLITTLE_ENDIAN ? wxT("little endian") : wxT("big endian"));
304 info += wxString::Format(wxT("Sampling rate: %d\n")
305 wxT("Bits per sample: %d\n")
306 wxT("Number of channels: %d\n"),
307 pcm_format->GetSampleRate(),
308 pcm_format->GetBPS(),
309 pcm_format->GetChannels());
310
311 break;
312 }
313 case wxSOUND_ULAW: {
314 wxSoundFormatUlaw *ulaw_format = (wxSoundFormatUlaw *)format;
315 info += wxT("ULAW\n");
316 info += wxString::Format(wxT("Sampling rate: %d\n"), ulaw_format->GetSampleRate());
317 break;
318 }
319 default:
320 info += wxT("Unknown");
321 break;
322 }
323 return info;
324}
325
326// ----------------------------------------------------------------------------
327
328
329// ----------------------------------------------------------------------------
330// MMBoardVideoFile
331
332MMBoardVideoFile::MMBoardVideoFile(const wxString& filename)
333{
334 m_output_window = NULL;
335
336#if defined(__UNIX__)
337 m_video_driver = new wxVideoXANIM(filename);
338#elif defined(__WIN32__)
339 m_video_driver = new wxVideoWindows(filename);
340#else
341 m_video_driver = NULL;
342 SetError(MMBoard_UnknownFile);
343#endif
344}
345
346MMBoardVideoFile::~MMBoardVideoFile()
347{
348 if (m_video_driver)
349 delete m_video_driver;
350}
351
352bool MMBoardVideoFile::NeedWindow()
353{
354 return TRUE;
355}
356
357void MMBoardVideoFile::SetWindow(wxWindow *window)
358{
359 m_output_window = window;
360 m_video_driver->AttachOutput(*window);
361
362 wxSize size;
363 m_video_driver->GetSize(size);
364 window->SetSize(size);
365 // BAD BAD
366 // and we remove
367 // window->GetParent()->GetSizer()->Fit(window->GetParent());
368}
369
370void MMBoardVideoFile::Play()
371{
372 m_video_driver->Play();
373}
374
375void MMBoardVideoFile::Pause()
376{
377 m_video_driver->Pause();
378}
379
380void MMBoardVideoFile::Resume()
381{
382 m_video_driver->Resume();
383}
384
385void MMBoardVideoFile::Stop()
386{
387 m_video_driver->Stop();
388}
389
390MMBoardTime MMBoardVideoFile::GetPosition()
391{
392 MMBoardTime btime;
393
394 btime.seconds = btime.minutes = btime.hours = 0;
395 return btime;
396}
397
398MMBoardTime MMBoardVideoFile::GetLength()
399{
400 MMBoardTime btime;
401 int frameTime;
402
403 frameTime = (int)( m_video_driver->GetNbFrames() / m_video_driver->GetFrameRate());
404
405 btime.seconds = frameTime % 60;
406 btime.minutes = (frameTime / 60) % 60;
407 btime.hours = frameTime / 3600;
408 return btime;
409}
410
411void MMBoardVideoFile::SetPosition(MMBoardTime btime)
412{
413}
414
415bool MMBoardVideoFile::IsStopped()
416{
417 return m_video_driver->IsStopped();
418}
419
420bool MMBoardVideoFile::IsPaused()
421{
422 return m_video_driver->IsPaused();
423}
424
425wxString MMBoardVideoFile::GetStringType()
426{
427 return wxString(wxT("Video XANIM"));
428}
429
430wxString MMBoardVideoFile::GetStringInformation()
431{
432 wxString info;
433
434 info = wxT("Video codec: ");
435 info += m_video_driver->GetMovieCodec() + "\n";
436 info += wxT("Audio codec: ");
437 info += m_video_driver->GetAudioCodec();
438 info += wxString::Format(" Sample rate: %d Channels: %d\n", m_video_driver->GetSampleRate(),
439 m_video_driver->GetBPS());
440 info += wxString::Format(" Frame rate: %.01f", m_video_driver->GetFrameRate());
441 return info;
442}
443
444// ----------------------------------------------------------------------------
445
446// ----------------------------------------------------------------------------
447// MMBoardFile
448
449MMBoardFile::MMBoardFile()
450{
451 m_error = 0;
452}
453
454MMBoardFile::~MMBoardFile()
455{
456}
457
458//
459// ----------------------------------------------------------------------------
460
461// ----------------------------------------------------------------------------
462// MMBoardManager
463
464MMBoardFile *MMBoardManager::Open(const wxString& filename)
465{
466 MMBoardFile *file;
467
468 // Test the audio codec
469 file = new MMBoardSoundFile(filename);
470 if (!file->GetError())
471 return file;
472 delete file;
473
474 // Test the video codec
475 file = new MMBoardVideoFile(filename);
476 if (!file->GetError())
477 return file;
478 delete file;
479
480 // Arrrgh, we just could not see what is that file ...
481 return NULL;
482}
483
484DECLARE_APP(MMBoardApp)
485
486wxSoundStream *MMBoardManager::OpenSoundStream()
487{
488#ifdef __UNIX__
489 if ((wxGetApp().m_caps & MM_SOUND_ESD) != 0)
490 return new wxSoundStreamESD();
491
492 if ((wxGetApp().m_caps & MM_SOUND_OSS) != 0)
493 return new wxSoundStreamOSS();
494#endif
495
496#ifdef __WIN32__
497 if ((wxGetApp().m_caps & MM_SOUND_WIN) != 0)
498 return new wxSoundStreamWin();
499#endif
500
501 wxMessageBox("You are trying to open a multimedia but you have not devices", "Error", wxOK | wxICON_ERROR, NULL);
502
503 return NULL;
504}
505
506void MMBoardManager::UnrefSoundStream(wxSoundStream *stream)
507{
508 delete stream;
509}
510
511// ----------------------------------------------------------------------------
512