added missing destructor
[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 #include "wx/sound.h"
18
19 #if wxOSX_USE_AUDIOTOOLBOX
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 #include "wx/osx/private.h"
32
33 #include <AudioToolbox/AudioToolbox.h>
34
35 class wxOSXAudioToolboxSoundData : public wxSoundData
36 {
37 public:
38 wxOSXAudioToolboxSoundData(const wxString& fileName);
39
40 ~wxOSXAudioToolboxSoundData();
41
42 virtual bool Play(unsigned flags);
43
44 virtual void DoStop();
45 protected:
46 static void CompletionCallback(SystemSoundID mySSID, void * soundRef);
47 void SoundCompleted();
48
49 SystemSoundID m_soundID;
50 wxString m_sndname; //file path
51 };
52
53 wxOSXAudioToolboxSoundData::wxOSXAudioToolboxSoundData(const wxString& fileName) :
54 m_soundID(NULL)
55 {
56 m_sndname = fileName;
57 }
58
59 wxOSXAudioToolboxSoundData::~wxOSXAudioToolboxSoundData()
60 {
61 DoStop();
62 }
63
64 void wxOSXAudioToolboxSoundData::CompletionCallback(SystemSoundID mySSID, void * soundRef)
65 {
66 wxOSXAudioToolboxSoundData* data = (wxOSXAudioToolboxSoundData*) soundRef;
67
68 data->SoundCompleted();
69 }
70
71 void wxOSXAudioToolboxSoundData::SoundCompleted()
72 {
73 if ( m_flags & wxSOUND_ASYNC )
74 {
75 if (m_flags & wxSOUND_LOOP)
76 AudioServicesPlaySystemSound(m_soundID);
77 else
78 Stop();
79 }
80 else
81 {
82 Stop();
83 CFRunLoopStop(CFRunLoopGetCurrent());
84 }
85
86 }
87
88 void wxOSXAudioToolboxSoundData::DoStop()
89 {
90 if (m_soundID)
91 {
92 AudioServicesDisposeSystemSoundID (m_soundID);
93 m_soundID = NULL;
94
95 wxSound::SoundStopped(this);
96 }
97 }
98
99 bool wxOSXAudioToolboxSoundData::Play(unsigned flags)
100 {
101 Stop();
102
103 m_flags = flags;
104
105 wxCFRef<CFMutableStringRef> cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(m_sndname)));
106 CFStringNormalize(cfMutableString,kCFStringNormalizationFormD);
107 wxCFRef<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false));
108
109 AudioServicesCreateSystemSoundID(url, &m_soundID);
110 AudioServicesAddSystemSoundCompletion( m_soundID, NULL, NULL, wxOSXAudioToolboxSoundData::CompletionCallback, (void *) this );
111
112 bool sync = !(flags & wxSOUND_ASYNC);
113
114 AudioServicesPlaySystemSound(m_soundID);
115
116 if ( sync )
117 {
118 while( m_soundID )
119 {
120 CFRunLoopRun();
121 }
122 }
123
124 return true;
125 }
126
127 bool wxSound::Create(int size, const wxByte* data)
128 {
129 return false;
130 }
131
132 bool wxSound::Create(const wxString& fileName, bool isResource)
133 {
134 if ( isResource )
135 return false;
136
137
138 m_data = new wxOSXAudioToolboxSoundData(fileName);
139 return true;
140 }
141
142 #endif // wxOSX_USE_AUDIOTOOLBOX
143
144 #endif //wxUSE_SOUND