From: Stefan Csomor Date: Sun, 13 Sep 2009 17:23:05 +0000 (+0000) Subject: sound implementation for AudioToolbox on 10.5 and iPhone X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/3b3d8b979b9c8cb76a550cf3e0c0c42fa0dc3fa2 sound implementation for AudioToolbox on 10.5 and iPhone git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61909 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/osx/core/sound.cpp b/src/osx/core/sound.cpp new file mode 100644 index 0000000000..79ba5ffd62 --- /dev/null +++ b/src/osx/core/sound.cpp @@ -0,0 +1,120 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: src/osx/carbon/sound.cpp +// Purpose: wxSound class implementation: optional +// Author: Ryan Norton +// Modified by: Stefan Csomor +// Created: 1998-01-01 +// RCS-ID: $Id: sound.cpp 61475 2009-07-20 16:47:54Z VZ $ +// Copyright: (c) Ryan Norton +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +// For compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + +#if wxUSE_SOUND + +#if wxOSX_USE_AUDIOTOOLBOX + +#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" + +class wxOSXAudioToolboxSoundData : public wxSoundData +{ +public: + wxOSXAudioToolboxSoundData(const wxString& fileName); + + ~wxOSXAudioToolboxSoundData(); + + virtual bool Play(unsigned flags); + + virtual void DoStop(); +protected: + static void CompletionCallback(SystemSoundID mySSID, void * soundRef); + void SoundCompleted(); + + SystemSoundID m_soundID; + wxString m_sndname; //file path +}; + +wxOSXAudioToolboxSoundData::wxOSXAudioToolboxSoundData(const wxString& fileName) : + m_soundID(NULL) +{ + m_sndname = fileName; +} + +void wxOSXAudioToolboxSoundData::CompletionCallback(SystemSoundID mySSID, void * soundRef) +{ + wxOSXAudioToolboxSoundData* data = (wxOSXAudioToolboxSoundData*) soundRef; + + data->SoundCompleted(); +} + +void wxOSXAudioToolboxSoundData::SoundCompleted() +{ + if ( m_flags & wxSOUND_ASYNC ) + { + if (m_flags & wxSOUND_LOOP) + AudioServicesPlaySystemSound(m_soundID); + else + Stop(); + } + else + { + Stop(); + CFRunLoopStop(CFRunLoopGetCurrent()); + } + +} + +void wxOSXAudioToolboxSoundData::DoStop() +{ + if (m_soundID) + { + AudioServicesDisposeSystemSoundID (m_soundID); + m_soundID = NULL; + + wxSound::SoundStopped(this); + } +} + +bool wxOSXAudioToolboxSoundData::DoPlay(unsigned flags) const +{ + Stop(); + + m_flags = flags; + + wxCFRef cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(m_sndname))); + CFStringNormalize(cfMutableString,kCFStringNormalizationFormD); + wxCFRef url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false)); + + AudioServicesCreateSystemSoundID(url, &m_soundID); + AudioServicesAddSystemSoundCompletion( m_soundID, NULL, NULL, wxOSXAudioToolboxSoundData::CompletionCallback, (void *) this ); + + bool sync = !(flags & wxSOUND_ASYNC); + + AudioServicesPlaySystemSound(m_soundID); + + if ( sync ) + { + while( m_soundID ) + { + CFRunLoopRun(); + } + } + + return true; +} + +#endif // wxOSX_USE_AUDIOTOOLBOX + +#endif //wxUSE_SOUND diff --git a/src/osx/sound_osx.cpp b/src/osx/sound_osx.cpp new file mode 100644 index 0000000000..141552e29a --- /dev/null +++ b/src/osx/sound_osx.cpp @@ -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 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::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::iterator s = s_soundsPlaying.begin(); + s != s_soundsPlaying.end(); ++s ) + { + if ( (*s) == data ) + { + s_soundsPlaying.erase(s); + break; + } + } +} + +#endif //wxUSE_SOUND