]> git.saurik.com Git - wxWidgets.git/blobdiff - src/mac/carbon/sound.cpp
Performance optimization
[wxWidgets.git] / src / mac / carbon / sound.cpp
index 3c4920a0cbb3b2151862179db165aeeac04874bd..b3ff384fc86d9544d2b81cede45cca6357e8de31 100644 (file)
@@ -1,26 +1,30 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        sound.cpp
+// Name:        src/mac/carbon/sound.cpp
 // Purpose:     wxSound class implementation: optional
 // Author:      Ryan Norton
-// Modified by:
+// Modified by: Stefan Csomor
 // Created:     1998-01-01
 // RCS-ID:      $Id$
-// Copyright:   (c) Ryan Norton, Stefan Csomor
-// Licence:       wxWindows licence
+// Copyright:   (c) Ryan Norton
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
-#ifdef __GNUG__
-#pragma implementation "sound.h"
-#endif
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
+
+#if wxUSE_SOUND
 
-#include "wx/object.h"
-#include "wx/string.h"
-#include "wx/log.h"
-#include "wx/file.h"
 #include "wx/sound.h"
-#include "wx/timer.h"
 
-#if wxUSE_SOUND
+#ifndef WX_PRECOMP
+    #include "wx/object.h"
+    #include "wx/string.h"
+    #include "wx/intl.h"
+    #include "wx/log.h"
+    #include "wx/timer.h"
+#endif
+
+#include "wx/file.h"
 
 // Carbon QT Implementation Details -
 //
 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
 //
 // File:
-// 1) Obtain FSSpec
-// 2) Call OpenMovieFile
-// 3) Call NewMovieFromFile
+// 1) Path as CFString
+// 2) Call QTNewDataReferenceFromFullPathCFString
+// 3) Call NewMovieFromDataRef
 // 4) Call CloseMovieFile
 // 4) PlayMovie();
 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
 //
 
 #ifdef __WXMAC__
-#include "wx/mac/private.h"
+#include "wx/mac/uma.h"
+#ifndef __DARWIN__
 #include <Movies.h>
 #include <Gestalt.h>
 #endif
+#endif
 
-#if defined __WXMAC__ && defined __DARWIN__/*TARGET_CARBON*/
-#ifdef __APPLE_CC__
 #include <Carbon/Carbon.h>
-#else
-#include <Carbon.h>
-#endif
-#else
-#include <Sound.h>
-#endif
 
 //quicktime media layer only required for mac emulation on pc
 #ifndef __WXMAC__
 #include <qtml.h>
 #endif
 
+#ifndef __DARWIN__
 #include <QuickTimeComponents.h>
+#else
+#include <QuickTime/QuickTimeComponents.h>
+#endif
 
-//Time inbetween timer calls
+//Time between timer calls
 #define MOVIE_DELAY 100
 
+static wxTimer* lastSoundTimer=NULL;
+static bool lastSoundIsPlaying=false;
+
+#if !defined(__LP64__)
+#define USE_QUICKTIME 1
+#else
+#define USE_QUICKTIME 0
+#endif
+
+#if USE_QUICKTIME
 // ------------------------------------------------------------------
 //          wxQTTimer - Handle Asyncronous Playing
 // ------------------------------------------------------------------
 class wxQTTimer : public wxTimer
 {
 public:
-    wxQTTimer(Movie movie, bool bLoop, bool& playing) :
-        m_movie(movie), m_bLoop(bLoop), m_pbPlaying(&playing)
+    wxQTTimer(Movie movie, bool bLoop, bool* playing) :
+        m_movie(movie), m_bLoop(bLoop), m_pbPlaying(playing)
     {
     }
 
-    ~wxQTTimer()
+    virtual ~wxQTTimer()
     {
         if(m_pbPlaying)
             *m_pbPlaying = false;
@@ -86,7 +98,7 @@ public:
         DisposeMovie(m_movie);
         Stop();
 
-        //Note that ExitMovies() is not neccessary, but
+        //Note that ExitMovies() is not necessary, but
         //the docs are fuzzy on whether or not TerminateQTML is
         ExitMovies();
 
@@ -138,12 +150,12 @@ public:
 class wxSMTimer : public wxTimer
 {
 public:
-    wxSMTimer(void* hSnd, void* pSndChannel, const bool& bLoop, bool& playing)
-        : m_hSnd(hSnd), m_pSndChannel(pSndChannel), m_bLoop(bLoop), m_pbPlaying(&playing)
+    wxSMTimer(void* hSnd, void* pSndChannel, bool bLoop, bool* playing)
+        : m_hSnd(hSnd), m_pSndChannel(pSndChannel), m_bLoop(bLoop), m_pbPlaying(playing)
     {
     }
 
-    ~wxSMTimer()
+    virtual ~wxSMTimer()
     {
         if(m_pbPlaying)
             *m_pbPlaying = false;
@@ -196,8 +208,6 @@ public:
 // ------------------------------------------------------------------
 //          wxSound
 // ------------------------------------------------------------------
-wxTimer* lastSoundTimer=NULL;
-bool lastSoundIsPlaying=false;
 
 //Determines whether version 4 of QT is installed
 Boolean wxIsQuickTime4Installed (void)
@@ -221,7 +231,7 @@ inline bool wxInitQT ()
         int nError;
         //-2093 no dll
             if ((nError = InitializeQTML(0)) != noErr)
-                wxLogSysError(wxString::Format("Couldn't Initialize Quicktime-%i", nError));
+                wxLogSysError(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError));
         #endif
         EnterMovies();
         return true;
@@ -233,6 +243,8 @@ inline bool wxInitQT ()
     }
 }
 
+#endif
+
 wxSound::wxSound()
 : m_hSnd(NULL), m_waveLength(0), m_pTimer(NULL), m_type(wxSound_NONE)
 {
@@ -251,13 +263,6 @@ wxSound::wxSound(int size, const wxByte* data)
 
 wxSound::~wxSound()
 {
-    if(lastSoundIsPlaying)
-    {
-        if(m_type == wxSound_RESOURCE)
-            ((wxSMTimer*)lastSoundTimer)->m_pbPlaying = NULL;
-        else
-            ((wxQTTimer*)lastSoundTimer)->m_pbPlaying = NULL;
-    }
 }
 
 bool wxSound::Create(const wxString& fileName, bool isResource)
@@ -292,6 +297,8 @@ bool wxSound::DoPlay(unsigned flags) const
 {
     Stop();
 
+#if USE_QUICKTIME
+
     Movie movie;
 
     switch(m_type)
@@ -365,7 +372,8 @@ bool wxSound::DoPlay(unsigned flags) const
             {
                 lastSoundTimer = ((wxSMTimer*&)m_pTimer)
                     = new wxSMTimer(pSndChannel, m_hSnd, flags & wxSOUND_LOOP ? 1 : 0,
-                        lastSoundIsPlaying=true);
+                                    &lastSoundIsPlaying);
+                lastSoundIsPlaying = true;
 
                 ((wxTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
             }
@@ -380,41 +388,22 @@ bool wxSound::DoPlay(unsigned flags) const
             if (!wxInitQT())
                 return false;
 
-            short movieResFile;
-            FSSpec sfFile;
-
-#ifdef __WXMAC__
-            wxMacFilename2FSSpec( m_sndname , &sfFile ) ;
-#else
-            int nError;
-            if ((nError = NativePathNameToFSSpec ((char*) m_sndname.c_str(), &sfFile, 0)) != noErr)
-            {
-                wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
-                                m_sndname.c_str(), nError));
-                return false;
-            }
-#endif
-
-            if (OpenMovieFile (&sfFile, &movieResFile, fsRdPerm) != noErr)
-            {
-                wxLogSysError(wxT("Quicktime couldn't open the file"));
-                return false;
-            }
+            OSErr err = noErr ;
 
+            Handle dataRef = NULL;
+            OSType dataRefType;
 
-            short movieResID = 0;
-            Str255 movieName;
-            OSErr err;
+            err = QTNewDataReferenceFromFullPathCFString(wxCFStringRef(m_sndname,wxLocale::GetSystemEncoding()),
+                (UInt32)kQTNativeDefaultPathStyle, 0, &dataRef, &dataRefType);
 
-            err = NewMovieFromFile (
-            &movie,
-            movieResFile,
-            &movieResID,
-            movieName,
-            newMovieActive,
-            NULL); //wasChanged
+            wxASSERT(err == noErr);
 
-            CloseMovieFile (movieResFile);
+            if (NULL != dataRef || err != noErr)
+            {
+                err = NewMovieFromDataRef( &movie, newMovieDontAskUnresolvedDataRefs , NULL, dataRef, dataRefType );
+                wxASSERT(err == noErr);
+                DisposeHandle(dataRef);
+            }
 
             if (err != noErr)
             {
@@ -429,26 +418,31 @@ bool wxSound::DoPlay(unsigned flags) const
         return false;
     }//end switch(m_type)
 
-
     //Start the movie!
     StartMovie(movie);
 
-    if (flags & wxSOUND_SYNC)
+    if (flags & wxSOUND_ASYNC)
+    {
+        //Start timer and play movie asyncronously
+        lastSoundTimer = ((wxQTTimer*&)m_pTimer) =
+            new wxQTTimer(movie, flags & wxSOUND_LOOP ? 1 : 0,
+                          &lastSoundIsPlaying);
+        lastSoundIsPlaying = true;
+        ((wxQTTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
+    }
+    else
     {
         wxASSERT_MSG(!(flags & wxSOUND_LOOP), wxT("Can't loop and play syncronously at the same time"));
 
         //Play movie until it ends, then exit
+        //Note that due to quicktime caching this may not always
+        //work 100% correctly
         while (!IsMovieDone(movie))
-            MoviesTask(movie, 0);
+            MoviesTask(movie, 1);
 
         DisposeMovie(movie);
     }
-    else
-    {
-        //Start timer and play movie asyncronously
-        lastSoundTimer = ((wxQTTimer*&)m_pTimer) = new wxQTTimer(movie, flags & wxSOUND_LOOP ? 1 : 0,lastSoundIsPlaying=true);
-        ((wxQTTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
-    }
+#endif
 
     return true;
 }
@@ -460,19 +454,23 @@ bool wxSound::IsPlaying()
 
 void wxSound::Stop()
 {
-    if(lastSoundIsPlaying)
+    if (lastSoundIsPlaying)
     {
         delete (wxTimer*&) lastSoundTimer;
         lastSoundIsPlaying = false;
+        lastSoundTimer = NULL;
     }
 }
 
 void* wxSound::GetHandle()
 {
+#if USE_QUICKTIME
     if(m_type == wxSound_RESOURCE)
         return (void*)  ((wxSMTimer*)m_pTimer)->GetChannel();
 
     return (void*) ((wxQTTimer*) m_pTimer)->GetMovie();
+#endif
+    return NULL;
 }
 
 #endif //wxUSE_SOUND