]> git.saurik.com Git - wxWidgets.git/blobdiff - src/unix/sound_sdl.cpp
Allow OS X Cocoa (or any OS X port) to override GetBestSize and provide a native...
[wxWidgets.git] / src / unix / sound_sdl.cpp
index b8c77fa00674bffad77fa121f832fdcef37f1dd2..3962e6dc722038401cca5b8ff09c1381b3673a3c 100644 (file)
@@ -1,24 +1,22 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        sound_sdl.cpp
+// Name:        src/unix/sound_sdl.cpp
 // Purpose:     wxSound backend using SDL
 // Author:      Vaclav Slavik
 // Modified by:
 // Created:     2004/01/31
 // RCS-ID:      $Id$
-// Copyright:   (c) 2004, Vaclav Slavik
-// Licence:    wxWindows licence
+// Copyright:   (c) 2004, Open Source Applications Foundation
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 // for compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
-#include "wx/setup.h"
-
 #if defined(__BORLANDC__)
-#pragma hdrstop
+    #pragma hdrstop
 #endif
 
-#if wxUSE_WAVE && wxUSE_LIBSDL
+#if wxUSE_SOUND && wxUSE_LIBSDL
 
 #include <SDL.h>
 
     #include "wx/intl.h"
     #include "wx/log.h"
     #include "wx/utils.h"
+    #include "wx/module.h"
 #endif
 
 #include "wx/thread.h"
-#include "wx/module.h"
 #include "wx/sound.h"
 
 // ----------------------------------------------------------------------------
@@ -42,26 +40,23 @@ class wxSoundBackendSDLNotification : public wxEvent
 public:
     DECLARE_DYNAMIC_CLASS(wxSoundBackendSDLNotification)
     wxSoundBackendSDLNotification();
-       wxEvent *Clone() const { return new wxSoundBackendSDLNotification(*this); }
+    wxEvent *Clone() const { return new wxSoundBackendSDLNotification(*this); }
 };
 
 typedef void (wxEvtHandler::*wxSoundBackendSDLNotificationFunction)
              (wxSoundBackendSDLNotification&);
 
-BEGIN_DECLARE_EVENT_TYPES()
-    DECLARE_LOCAL_EVENT_TYPE(wxEVT_SOUND_BACKEND_SDL_NOTIFICATION, -1)
-END_DECLARE_EVENT_TYPES()
+wxDECLARE_EVENT(wxEVT_SOUND_BACKEND_SDL_NOTIFICATION, wxSoundBackendSDLNotification);
 
 #define EVT_SOUND_BACKEND_SDL_NOTIFICATON(func) \
     DECLARE_EVENT_TABLE_ENTRY(wxEVT_SOUND_BACKEND_SDL_NOTIFICATION, \
                               -1,                       \
                               -1,                       \
-                              (wxObjectEventFunction)   \
-                              (wxSoundBackendSDLNotificationFunction)& func, \
-                              (wxObject *) NULL ),
+                              wxEVENT_HANDLER_CAST( wxSoundBackendSDLNotificationFunction, func ), \
+                              NULL ),
 
 IMPLEMENT_DYNAMIC_CLASS(wxSoundBackendSDLNotification, wxEvtHandler)
-DEFINE_EVENT_TYPE(wxEVT_SOUND_BACKEND_SDL_NOTIFICATION)
+wxDEFINE_EVENT( wxEVT_SOUND_BACKEND_SDL_NOTIFICATION, wxSoundBackendSDLNotification );
 
 wxSoundBackendSDLNotification::wxSoundBackendSDLNotification()
 {
@@ -73,25 +68,28 @@ class wxSoundBackendSDLEvtHandler;
 class wxSoundBackendSDL : public wxSoundBackend
 {
 public:
-    wxSoundBackendSDL() 
+    wxSoundBackendSDL()
         : m_initialized(false), m_playing(false), m_audioOpen(false),
           m_data(NULL), m_evtHandler(NULL) {}
     virtual ~wxSoundBackendSDL();
-    
-    wxString GetName() const { return _T("Simple DirectMedia Layer"); }
+
+    wxString GetName() const { return wxT("Simple DirectMedia Layer"); }
     int GetPriority() const { return 9; }
     bool IsAvailable() const;
     bool HasNativeAsyncPlayback() const { return true; }
-    bool Play(wxSoundData *data, unsigned flags);
+    bool Play(wxSoundData *data, unsigned flags,
+              volatile wxSoundPlaybackStatus *status);
 
     void FillAudioBuffer(Uint8 *stream, int len);
+    void FinishedPlayback();
+
     void Stop();
     bool IsPlaying() const { return m_playing; }
-    
+
 private:
     bool OpenAudio();
     void CloseAudio();
-    
+
     bool                        m_initialized;
     bool                        m_playing, m_audioOpen;
     // playback information:
@@ -111,9 +109,9 @@ public:
 private:
     void OnNotify(wxSoundBackendSDLNotification& WXUNUSED(event))
     {
-        wxLogTrace(_T("sound"),
-                   _T("received playback status change notification"));
-        m_backend->Stop();
+        wxLogTrace(wxT("sound"),
+                   wxT("received playback status change notification"));
+        m_backend->FinishedPlayback();
     }
     wxSoundBackendSDL *m_backend;
 
@@ -141,7 +139,7 @@ bool wxSoundBackendSDL::IsAvailable() const
             return false;
     }
     wxConstCast(this, wxSoundBackendSDL)->m_initialized = true;
-    wxLogTrace(_T("sound"), _T("initialized SDL audio subsystem"));
+    wxLogTrace(wxT("sound"), wxT("initialized SDL audio subsystem"));
     return true;
 }
 
@@ -191,26 +189,32 @@ void wxSoundBackendSDL::FillAudioBuffer(Uint8 *stream, int len)
     }
 }
 
+void wxSoundBackendSDL::FinishedPlayback()
+{
+    if (!m_playing)
+        Stop();
+}
+
 bool wxSoundBackendSDL::OpenAudio()
 {
     if (!m_audioOpen)
     {
         if (!m_evtHandler)
             m_evtHandler = new wxSoundBackendSDLEvtHandler(this);
-        
+
         m_spec.silence = 0;
         m_spec.samples = 4096;
         m_spec.size = 0;
         m_spec.callback = wx_sdl_audio_callback;
         m_spec.userdata = (void*)this;
-                
-        wxLogTrace(_T("sound"), _T("opening SDL audio..."));
+
+        wxLogTrace(wxT("sound"), wxT("opening SDL audio..."));
         if (SDL_OpenAudio(&m_spec, NULL) >= 0)
         {
 #if wxUSE_LOG_DEBUG
             char driver[256];
-            SDL_AudioDriverName(driver, 256);                    
-            wxLogTrace(_T("sound"), _T("opened audio, driver '%s'"),
+            SDL_AudioDriverName(driver, 256);
+            wxLogTrace(wxT("sound"), wxT("opened audio, driver '%s'"),
                        wxString(driver, wxConvLocal).c_str());
 #endif
             m_audioOpen = true;
@@ -231,13 +235,16 @@ void wxSoundBackendSDL::CloseAudio()
     if (m_audioOpen)
     {
         SDL_CloseAudio();
-        wxLogTrace(_T("sound"), _T("closed audio"));
+        wxLogTrace(wxT("sound"), wxT("closed audio"));
         m_audioOpen = false;
     }
 }
 
-bool wxSoundBackendSDL::Play(wxSoundData *data, unsigned flags)
+bool wxSoundBackendSDL::Play(wxSoundData *data, unsigned flags,
+                             volatile wxSoundPlaybackStatus *WXUNUSED(status))
 {
+    Stop();
+
     int format;
     if (data->m_bitsPerSample == 8)
         format = AUDIO_U8;
@@ -245,8 +252,6 @@ bool wxSoundBackendSDL::Play(wxSoundData *data, unsigned flags)
         format = AUDIO_S16LSB;
     else
         return false;
-    
-    SDL_LockAudio();
 
     bool needsOpen = true;
     if (m_audioOpen)
@@ -262,35 +267,32 @@ bool wxSoundBackendSDL::Play(wxSoundData *data, unsigned flags)
             CloseAudio();
         }
     }
-    
-    Stop();
-    
+
     if (needsOpen)
     {
         m_spec.format = format;
         m_spec.freq = data->m_samplingRate;
         m_spec.channels = data->m_channels;
         if (!OpenAudio())
-        {
-            SDL_UnlockAudio();
             return false;
-        }
     }
 
+    SDL_LockAudio();
+    wxLogTrace(wxT("sound"), wxT("playing new sound"));
     m_playing = true;
     m_pos = 0;
     m_loop = (flags & wxSOUND_LOOP);
     m_data = data;
     data->IncRef();
+    SDL_UnlockAudio();
 
     SDL_PauseAudio(0);
-    SDL_UnlockAudio();
 
     // wait until playback finishes if called in sync mode:
     if (!(flags & wxSOUND_ASYNC))
     {
-        wxLogTrace(_T("sound"), _T("waiting for sample to finish"));
-        while (m_playing)
+        wxLogTrace(wxT("sound"), wxT("waiting for sample to finish"));
+        while (m_playing && m_data == data)
         {
 #if wxUSE_THREADS
             // give the playback thread a chance to add event to pending
@@ -298,13 +300,13 @@ bool wxSoundBackendSDL::Play(wxSoundData *data, unsigned flags)
             if (wxThread::IsMain())
                 wxMutexGuiLeave();
 #endif
-            wxUsleep(10);
+            wxMilliSleep(10);
 #if wxUSE_THREADS
             if (wxThread::IsMain())
                 wxMutexGuiEnter();
 #endif
         }
-        wxLogTrace(_T("sound"), _T("sample finished"));
+        wxLogTrace(wxT("sound"), wxT("sample finished"));
     }
 
     return true;
@@ -314,6 +316,7 @@ void wxSoundBackendSDL::Stop()
 {
     SDL_LockAudio();
     SDL_PauseAudio(1);
+    m_playing = false;
     if (m_data)
     {
         m_data->DecRef();
@@ -327,4 +330,4 @@ extern "C" wxSoundBackend *wxCreateSoundBackendSDL()
     return new wxSoundBackendSDL();
 }
 
-#endif // wxUSE_WAVE && wxUSE_LIBSDL
+#endif // wxUSE_SOUND && wxUSE_LIBSDL