]>
Commit | Line | Data |
---|---|---|
3b3d8b97 | 1 | ///////////////////////////////////////////////////////////////////////////// |
134865f8 SC |
2 | // Name: src/osx/core/sound.cpp |
3 | // Purpose: wxSound class implementation using AudioToolbox | |
4 | // Author: Stefan Csomor | |
3b3d8b97 | 5 | // Modified by: Stefan Csomor |
134865f8 | 6 | // Created: 2009-01-01 |
3b3d8b97 | 7 | // RCS-ID: $Id: sound.cpp 61475 2009-07-20 16:47:54Z VZ $ |
134865f8 | 8 | // Copyright: (c) Stefan Csomor |
3b3d8b97 SC |
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 | ||
3b3d8b97 SC |
17 | #include "wx/sound.h" |
18 | ||
0770c0a0 SC |
19 | #if wxOSX_USE_AUDIOTOOLBOX |
20 | ||
3b3d8b97 SC |
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 | ||
0770c0a0 SC |
31 | #include "wx/osx/private.h" |
32 | ||
33 | #include <AudioToolbox/AudioToolbox.h> | |
34 | ||
3b3d8b97 SC |
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 | ||
02ca710b SC |
59 | wxOSXAudioToolboxSoundData::~wxOSXAudioToolboxSoundData() |
60 | { | |
61 | DoStop(); | |
62 | } | |
63 | ||
9a78f785 VZ |
64 | void |
65 | wxOSXAudioToolboxSoundData::CompletionCallback(SystemSoundID WXUNUSED(mySSID), | |
66 | void * soundRef) | |
3b3d8b97 SC |
67 | { |
68 | wxOSXAudioToolboxSoundData* data = (wxOSXAudioToolboxSoundData*) soundRef; | |
69 | ||
70 | data->SoundCompleted(); | |
509da835 KO |
71 | |
72 | if (data->IsMarkedForDeletion()) | |
73 | delete data; | |
3b3d8b97 SC |
74 | } |
75 | ||
76 | void wxOSXAudioToolboxSoundData::SoundCompleted() | |
77 | { | |
78 | if ( m_flags & wxSOUND_ASYNC ) | |
79 | { | |
80 | if (m_flags & wxSOUND_LOOP) | |
81 | AudioServicesPlaySystemSound(m_soundID); | |
82 | else | |
83 | Stop(); | |
84 | } | |
85 | else | |
86 | { | |
87 | Stop(); | |
88 | CFRunLoopStop(CFRunLoopGetCurrent()); | |
89 | } | |
90 | ||
91 | } | |
92 | ||
93 | void wxOSXAudioToolboxSoundData::DoStop() | |
94 | { | |
95 | if (m_soundID) | |
96 | { | |
97 | AudioServicesDisposeSystemSoundID (m_soundID); | |
98 | m_soundID = NULL; | |
99 | ||
100 | wxSound::SoundStopped(this); | |
101 | } | |
102 | } | |
103 | ||
0770c0a0 | 104 | bool wxOSXAudioToolboxSoundData::Play(unsigned flags) |
3b3d8b97 SC |
105 | { |
106 | Stop(); | |
107 | ||
108 | m_flags = flags; | |
109 | ||
110 | wxCFRef<CFMutableStringRef> cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(m_sndname))); | |
111 | CFStringNormalize(cfMutableString,kCFStringNormalizationFormD); | |
112 | wxCFRef<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false)); | |
113 | ||
114 | AudioServicesCreateSystemSoundID(url, &m_soundID); | |
134865f8 | 115 | AudioServicesAddSystemSoundCompletion( m_soundID, CFRunLoopGetCurrent(), NULL, wxOSXAudioToolboxSoundData::CompletionCallback, (void *) this ); |
3b3d8b97 SC |
116 | |
117 | bool sync = !(flags & wxSOUND_ASYNC); | |
118 | ||
119 | AudioServicesPlaySystemSound(m_soundID); | |
120 | ||
121 | if ( sync ) | |
122 | { | |
123 | while( m_soundID ) | |
124 | { | |
125 | CFRunLoopRun(); | |
126 | } | |
127 | } | |
128 | ||
129 | return true; | |
130 | } | |
131 | ||
9a78f785 | 132 | bool wxSound::Create(int WXUNUSED(size), const wxByte* WXUNUSED(data)) |
0770c0a0 | 133 | { |
9a78f785 VZ |
134 | wxFAIL_MSG( "not implemented" ); |
135 | ||
0770c0a0 SC |
136 | return false; |
137 | } | |
138 | ||
139 | bool wxSound::Create(const wxString& fileName, bool isResource) | |
140 | { | |
9a78f785 | 141 | wxCHECK_MSG( !isResource, false, "not implemented" ); |
0770c0a0 SC |
142 | |
143 | m_data = new wxOSXAudioToolboxSoundData(fileName); | |
144 | return true; | |
145 | } | |
146 | ||
3b3d8b97 SC |
147 | #endif // wxOSX_USE_AUDIOTOOLBOX |
148 | ||
149 | #endif //wxUSE_SOUND |