sound implementation for AudioToolbox on 10.5 and iPhone
[wxWidgets.git] / src / osx / core / sound.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/sound.cpp
3 // Purpose: wxSound class implementation: optional
4 // Author: Ryan Norton
5 // Modified by: Stefan Csomor
6 // Created: 1998-01-01
7 // RCS-ID: $Id: sound.cpp 61475 2009-07-20 16:47:54Z VZ $
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_SOUND
16
17 #if wxOSX_USE_AUDIOTOOLBOX
18
19 #include "wx/sound.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/object.h"
23 #include "wx/string.h"
24 #include "wx/intl.h"
25 #include "wx/log.h"
26 #include "wx/timer.h"
27 #endif
28
29 #include "wx/file.h"
30
31 class wxOSXAudioToolboxSoundData : public wxSoundData
32 {
33 public:
34 wxOSXAudioToolboxSoundData(const wxString& fileName);
35
36 ~wxOSXAudioToolboxSoundData();
37
38 virtual bool Play(unsigned flags);
39
40 virtual void DoStop();
41 protected:
42 static void CompletionCallback(SystemSoundID mySSID, void * soundRef);
43 void SoundCompleted();
44
45 SystemSoundID m_soundID;
46 wxString m_sndname; //file path
47 };
48
49 wxOSXAudioToolboxSoundData::wxOSXAudioToolboxSoundData(const wxString& fileName) :
50 m_soundID(NULL)
51 {
52 m_sndname = fileName;
53 }
54
55 void wxOSXAudioToolboxSoundData::CompletionCallback(SystemSoundID mySSID, void * soundRef)
56 {
57 wxOSXAudioToolboxSoundData* data = (wxOSXAudioToolboxSoundData*) soundRef;
58
59 data->SoundCompleted();
60 }
61
62 void wxOSXAudioToolboxSoundData::SoundCompleted()
63 {
64 if ( m_flags & wxSOUND_ASYNC )
65 {
66 if (m_flags & wxSOUND_LOOP)
67 AudioServicesPlaySystemSound(m_soundID);
68 else
69 Stop();
70 }
71 else
72 {
73 Stop();
74 CFRunLoopStop(CFRunLoopGetCurrent());
75 }
76
77 }
78
79 void wxOSXAudioToolboxSoundData::DoStop()
80 {
81 if (m_soundID)
82 {
83 AudioServicesDisposeSystemSoundID (m_soundID);
84 m_soundID = NULL;
85
86 wxSound::SoundStopped(this);
87 }
88 }
89
90 bool wxOSXAudioToolboxSoundData::DoPlay(unsigned flags) const
91 {
92 Stop();
93
94 m_flags = flags;
95
96 wxCFRef<CFMutableStringRef> cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(m_sndname)));
97 CFStringNormalize(cfMutableString,kCFStringNormalizationFormD);
98 wxCFRef<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false));
99
100 AudioServicesCreateSystemSoundID(url, &m_soundID);
101 AudioServicesAddSystemSoundCompletion( m_soundID, NULL, NULL, wxOSXAudioToolboxSoundData::CompletionCallback, (void *) this );
102
103 bool sync = !(flags & wxSOUND_ASYNC);
104
105 AudioServicesPlaySystemSound(m_soundID);
106
107 if ( sync )
108 {
109 while( m_soundID )
110 {
111 CFRunLoopRun();
112 }
113 }
114
115 return true;
116 }
117
118 #endif // wxOSX_USE_AUDIOTOOLBOX
119
120 #endif //wxUSE_SOUND