1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/sound.cpp
3 // Purpose: wxSound class implementation using AudioToolbox
4 // Author: Stefan Csomor
5 // Modified by: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxOSX_USE_AUDIOTOOLBOX
22 #include "wx/object.h"
23 #include "wx/string.h"
31 #include "wx/osx/private.h"
33 #include <AudioToolbox/AudioToolbox.h>
35 class wxOSXAudioToolboxSoundData
: public wxSoundData
38 wxOSXAudioToolboxSoundData(const wxString
& fileName
);
40 ~wxOSXAudioToolboxSoundData();
42 virtual bool Play(unsigned flags
);
44 virtual void DoStop();
46 static void CompletionCallback(SystemSoundID mySSID
, void * soundRef
);
47 void SoundCompleted();
49 SystemSoundID m_soundID
;
50 wxString m_sndname
; //file path
53 wxOSXAudioToolboxSoundData::wxOSXAudioToolboxSoundData(const wxString
& fileName
) :
59 wxOSXAudioToolboxSoundData::~wxOSXAudioToolboxSoundData()
65 wxOSXAudioToolboxSoundData::CompletionCallback(SystemSoundID
WXUNUSED(mySSID
),
68 wxOSXAudioToolboxSoundData
* data
= (wxOSXAudioToolboxSoundData
*) soundRef
;
70 data
->SoundCompleted();
72 if (data
->IsMarkedForDeletion())
76 void wxOSXAudioToolboxSoundData::SoundCompleted()
78 if ( m_flags
& wxSOUND_ASYNC
)
80 if (m_flags
& wxSOUND_LOOP
)
81 AudioServicesPlaySystemSound(m_soundID
);
88 CFRunLoopStop(CFRunLoopGetCurrent());
93 void wxOSXAudioToolboxSoundData::DoStop()
97 AudioServicesDisposeSystemSoundID (m_soundID
);
100 wxSound::SoundStopped(this);
104 bool wxOSXAudioToolboxSoundData::Play(unsigned flags
)
110 wxCFRef
<CFMutableStringRef
> cfMutableString(CFStringCreateMutableCopy(NULL
, 0, wxCFStringRef(m_sndname
)));
111 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormD
);
112 wxCFRef
<CFURLRef
> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, cfMutableString
, kCFURLPOSIXPathStyle
, false));
114 AudioServicesCreateSystemSoundID(url
, &m_soundID
);
115 AudioServicesAddSystemSoundCompletion( m_soundID
, CFRunLoopGetCurrent(), NULL
, wxOSXAudioToolboxSoundData::CompletionCallback
, (void *) this );
117 bool sync
= !(flags
& wxSOUND_ASYNC
);
119 AudioServicesPlaySystemSound(m_soundID
);
132 bool wxSound::Create(int WXUNUSED(size
), const wxByte
* WXUNUSED(data
))
134 wxFAIL_MSG( "not implemented" );
139 bool wxSound::Create(const wxString
& fileName
, bool isResource
)
141 wxCHECK_MSG( !isResource
, false, "not implemented" );
143 m_data
= new wxOSXAudioToolboxSoundData(fileName
);
147 #endif // wxOSX_USE_AUDIOTOOLBOX