1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/sound.cpp
3 // Purpose: wxSound class implementation using AudioToolbox
4 // Author: Stefan Csomor
5 // Modified by: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxOSX_USE_AUDIOTOOLBOX
21 #include "wx/object.h"
22 #include "wx/string.h"
30 #include "wx/osx/private.h"
32 #include <AudioToolbox/AudioToolbox.h>
34 class wxOSXAudioToolboxSoundData
: public wxSoundData
37 wxOSXAudioToolboxSoundData(const wxString
& fileName
);
39 ~wxOSXAudioToolboxSoundData();
41 virtual bool Play(unsigned flags
);
43 virtual void DoStop();
45 static void CompletionCallback(SystemSoundID mySSID
, void * soundRef
);
46 void SoundCompleted();
48 SystemSoundID m_soundID
;
49 wxString m_sndname
; //file path
52 wxOSXAudioToolboxSoundData::wxOSXAudioToolboxSoundData(const wxString
& fileName
) :
58 wxOSXAudioToolboxSoundData::~wxOSXAudioToolboxSoundData()
64 wxOSXAudioToolboxSoundData::CompletionCallback(SystemSoundID
WXUNUSED(mySSID
),
67 wxOSXAudioToolboxSoundData
* data
= (wxOSXAudioToolboxSoundData
*) soundRef
;
69 data
->SoundCompleted();
71 if (data
->IsMarkedForDeletion())
75 void wxOSXAudioToolboxSoundData::SoundCompleted()
77 if ( m_flags
& wxSOUND_ASYNC
)
79 if (m_flags
& wxSOUND_LOOP
)
80 AudioServicesPlaySystemSound(m_soundID
);
87 CFRunLoopStop(CFRunLoopGetCurrent());
92 void wxOSXAudioToolboxSoundData::DoStop()
96 AudioServicesDisposeSystemSoundID (m_soundID
);
99 wxSound::SoundStopped(this);
103 bool wxOSXAudioToolboxSoundData::Play(unsigned flags
)
109 wxCFRef
<CFMutableStringRef
> cfMutableString(CFStringCreateMutableCopy(NULL
, 0, wxCFStringRef(m_sndname
)));
110 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormD
);
111 wxCFRef
<CFURLRef
> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, cfMutableString
, kCFURLPOSIXPathStyle
, false));
113 AudioServicesCreateSystemSoundID(url
, &m_soundID
);
114 AudioServicesAddSystemSoundCompletion( m_soundID
, CFRunLoopGetCurrent(), NULL
, wxOSXAudioToolboxSoundData::CompletionCallback
, (void *) this );
116 bool sync
= !(flags
& wxSOUND_ASYNC
);
118 AudioServicesPlaySystemSound(m_soundID
);
131 bool wxSound::Create(size_t WXUNUSED(size
), const void* WXUNUSED(data
))
133 wxFAIL_MSG( "not implemented" );
138 bool wxSound::Create(const wxString
& fileName
, bool isResource
)
140 wxCHECK_MSG( !isResource
, false, "not implemented" );
142 m_data
= new wxOSXAudioToolboxSoundData(fileName
);
146 #endif // wxOSX_USE_AUDIOTOOLBOX