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