]> git.saurik.com Git - wxWidgets.git/blame - src/osx/core/sound.cpp
No real changes, just make wxWindow::CanScroll() virtual.
[wxWidgets.git] / src / osx / core / sound.cpp
CommitLineData
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
134865f8 7// Copyright: (c) Stefan Csomor
3b3d8b97
SC
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#if wxUSE_SOUND
15
3b3d8b97
SC
16#include "wx/sound.h"
17
0770c0a0
SC
18#if wxOSX_USE_AUDIOTOOLBOX
19
3b3d8b97
SC
20#ifndef WX_PRECOMP
21 #include "wx/object.h"
22 #include "wx/string.h"
23 #include "wx/intl.h"
24 #include "wx/log.h"
25 #include "wx/timer.h"
26#endif
27
28#include "wx/file.h"
29
0770c0a0
SC
30#include "wx/osx/private.h"
31
32#include <AudioToolbox/AudioToolbox.h>
33
3b3d8b97
SC
34class wxOSXAudioToolboxSoundData : public wxSoundData
35{
36public:
37 wxOSXAudioToolboxSoundData(const wxString& fileName);
38
39 ~wxOSXAudioToolboxSoundData();
ce00f59b 40
3b3d8b97
SC
41 virtual bool Play(unsigned flags);
42
43 virtual void DoStop();
44protected:
45 static void CompletionCallback(SystemSoundID mySSID, void * soundRef);
46 void SoundCompleted();
ce00f59b
VZ
47
48 SystemSoundID m_soundID;
3b3d8b97
SC
49 wxString m_sndname; //file path
50};
51
52wxOSXAudioToolboxSoundData::wxOSXAudioToolboxSoundData(const wxString& fileName) :
53 m_soundID(NULL)
ce00f59b 54{
3b3d8b97
SC
55 m_sndname = fileName;
56}
57
02ca710b
SC
58wxOSXAudioToolboxSoundData::~wxOSXAudioToolboxSoundData()
59{
60 DoStop();
61}
62
9a78f785
VZ
63void
64wxOSXAudioToolboxSoundData::CompletionCallback(SystemSoundID WXUNUSED(mySSID),
65 void * soundRef)
3b3d8b97
SC
66{
67 wxOSXAudioToolboxSoundData* data = (wxOSXAudioToolboxSoundData*) soundRef;
ce00f59b 68
3b3d8b97 69 data->SoundCompleted();
ce00f59b 70
509da835
KO
71 if (data->IsMarkedForDeletion())
72 delete data;
3b3d8b97
SC
73}
74
75void wxOSXAudioToolboxSoundData::SoundCompleted()
76{
77 if ( m_flags & wxSOUND_ASYNC )
78 {
79 if (m_flags & wxSOUND_LOOP)
80 AudioServicesPlaySystemSound(m_soundID);
ce00f59b 81 else
3b3d8b97
SC
82 Stop();
83 }
ce00f59b 84 else
3b3d8b97
SC
85 {
86 Stop();
87 CFRunLoopStop(CFRunLoopGetCurrent());
88 }
89
90}
91
92void wxOSXAudioToolboxSoundData::DoStop()
93{
94 if (m_soundID)
95 {
96 AudioServicesDisposeSystemSoundID (m_soundID);
97 m_soundID = NULL;
ce00f59b 98
3b3d8b97
SC
99 wxSound::SoundStopped(this);
100 }
101}
102
0770c0a0 103bool wxOSXAudioToolboxSoundData::Play(unsigned flags)
3b3d8b97
SC
104{
105 Stop();
106
107 m_flags = flags;
ce00f59b 108
3b3d8b97
SC
109 wxCFRef<CFMutableStringRef> cfMutableString(CFStringCreateMutableCopy(NULL, 0, wxCFStringRef(m_sndname)));
110 CFStringNormalize(cfMutableString,kCFStringNormalizationFormD);
111 wxCFRef<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kCFURLPOSIXPathStyle, false));
112
113 AudioServicesCreateSystemSoundID(url, &m_soundID);
134865f8 114 AudioServicesAddSystemSoundCompletion( m_soundID, CFRunLoopGetCurrent(), NULL, wxOSXAudioToolboxSoundData::CompletionCallback, (void *) this );
3b3d8b97
SC
115
116 bool sync = !(flags & wxSOUND_ASYNC);
117
118 AudioServicesPlaySystemSound(m_soundID);
119
120 if ( sync )
121 {
ce00f59b 122 while( m_soundID )
3b3d8b97
SC
123 {
124 CFRunLoopRun();
125 }
126 }
ce00f59b 127
3b3d8b97
SC
128 return true;
129}
130
c559c4b3 131bool wxSound::Create(size_t WXUNUSED(size), const void* WXUNUSED(data))
0770c0a0 132{
9a78f785
VZ
133 wxFAIL_MSG( "not implemented" );
134
0770c0a0
SC
135 return false;
136}
137
138bool wxSound::Create(const wxString& fileName, bool isResource)
139{
9a78f785 140 wxCHECK_MSG( !isResource, false, "not implemented" );
ce00f59b 141
0770c0a0
SC
142 m_data = new wxOSXAudioToolboxSoundData(fileName);
143 return true;
144}
145
3b3d8b97
SC
146#endif // wxOSX_USE_AUDIOTOOLBOX
147
148#endif //wxUSE_SOUND