]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/sound_osx.cpp
sound implementation for AudioToolbox on 10.5 and iPhone
[wxWidgets.git] / src / osx / sound_osx.cpp
diff --git a/src/osx/sound_osx.cpp b/src/osx/sound_osx.cpp
new file mode 100644 (file)
index 0000000..141552e
--- /dev/null
@@ -0,0 +1,156 @@
+/////////////////////////////////////////////////////////////////////////////
+// Name:        src/osx/sound_osx.cpp
+// Purpose:     wxSound class common osx code
+// Author:      Stefan Csomor
+// Modified by: 
+// Created:     2009-09-01
+// RCS-ID:      $Id: sound.cpp 61475 2009-07-20 16:47:54Z VZ $
+// Copyright:   (c) Stefan Csomor
+// Licence:     wxWindows licence
+/////////////////////////////////////////////////////////////////////////////
+
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
+
+#if wxUSE_SOUND
+
+#include "wx/sound.h"
+
+#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"
+
+#include "wx/vector.h"
+
+class wxSoundTimer : public wxTimer
+{
+public:
+    wxSoundTimer(wxSoundData* snd)
+    : m_sound(snd)
+    {
+    }
+    
+    virtual ~wxSoundTimer()
+    {
+        Stop();
+        m_sound->DoStop();
+    }
+    
+    void Notify()
+    {
+        m_sound->SoundTask();
+    }
+    
+protected:
+    wxSoundData* m_sound;
+};
+
+wxVector<wxSoundData*> s_soundsPlaying;
+
+wxSoundData::wxSoundData()
+{
+    m_pTimer = NULL;
+}
+
+wxSoundData::~wxSoundData()
+{
+}
+
+void wxSoundData::Stop()
+{
+    DoStop();
+    if ( m_pTimer )
+    {
+        delete m_pTimer;
+        m_pTimer = NULL;
+    }
+}
+
+//Time between timer calls
+#define MOVIE_DELAY 100
+
+void wxSoundData::SoundTask()
+{
+}
+
+void wxSoundData::CreateAndStartTimer()
+{
+    //Start timer and play movie asyncronously
+    m_pTimer = new wxSoundTimer(this);
+    m_pTimer->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
+}
+
+wxSound::wxSound() 
+{
+    Init();
+}
+
+wxSound::wxSound(const wxString& sFileName, bool isResource)
+{
+    Init();
+    Create(sFileName, isResource);
+}
+
+wxSound::wxSound(int size, const wxByte* data)
+{
+    Init();
+    Create( size, data );
+}
+
+wxSound::~wxSound()
+{
+    delete m_data;
+}
+
+void wxSound::Init()
+{
+    m_data = NULL;
+}
+
+bool wxSound::DoPlay(unsigned flags) const
+{
+    if ( m_data )
+    {
+        s_soundsPlaying.push_back(m_data);
+        if ( !m_data->Play(flags) )
+            s_soundsPlaying.pop_back();
+    }
+    
+    return false;
+}
+
+bool wxSound::IsPlaying()
+{
+    return s_soundsPlaying.size() > 0;
+}
+
+void wxSound::Stop()
+{
+    for ( wxVector<wxSoundData*>::reverse_iterator s = s_soundsPlaying.rbegin();
+         s != s_soundsPlaying.rend(); ++s )
+    {
+        (*s)->Stop();
+    }
+}
+
+// Notification when a sound has stopped
+void wxSound::SoundStopped(const wxSoundData* data)
+{
+    for ( wxVector<wxSoundData*>::iterator s = s_soundsPlaying.begin();
+         s != s_soundsPlaying.end(); ++s )
+    {
+        if ( (*s) == data ) 
+        {
+            s_soundsPlaying.erase(s);
+            break;
+        }
+    }
+}
+
+#endif //wxUSE_SOUND