Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / osx / sound_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/sound_osx.cpp
3 // Purpose: wxSound class common osx code
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 2009-09-01
7 // Copyright: (c) Stefan Csomor
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
16 #include "wx/sound.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/object.h"
20 #include "wx/string.h"
21 #include "wx/intl.h"
22 #include "wx/log.h"
23 #include "wx/timer.h"
24 #endif
25
26 #include "wx/file.h"
27
28 #include "wx/vector.h"
29
30 class wxSoundTimer : public wxTimer
31 {
32 public:
33 wxSoundTimer(wxSoundData* snd)
34 : m_sound(snd)
35 {
36 }
37
38 virtual ~wxSoundTimer()
39 {
40 Stop();
41 if (m_sound)
42 m_sound->DoStop();
43 }
44
45 void Notify()
46 {
47 if (m_sound)
48 m_sound->SoundTask();
49 }
50
51 protected:
52 wxSoundData* m_sound;
53 };
54
55 wxVector<wxSoundData*> s_soundsPlaying;
56
57 wxSoundData::wxSoundData()
58 {
59 m_pTimer = NULL;
60 m_markedForDeletion = false;
61 }
62
63 wxSoundData::~wxSoundData()
64 {
65 }
66
67 void wxSoundData::MarkForDeletion()
68 {
69 m_markedForDeletion = true;
70 }
71
72 void wxSoundData::Stop()
73 {
74 DoStop();
75 wxDELETE(m_pTimer);
76 }
77
78 //Time between timer calls
79 #define MOVIE_DELAY 100
80
81 void wxSoundData::SoundTask()
82 {
83 }
84
85 void wxSoundData::CreateAndStartTimer()
86 {
87 //Start timer and play movie asyncronously
88 m_pTimer = new wxSoundTimer(this);
89 m_pTimer->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
90 }
91
92 wxSound::wxSound()
93 {
94 Init();
95 }
96
97 wxSound::wxSound(const wxString& sFileName, bool isResource)
98 {
99 Init();
100 Create(sFileName, isResource);
101 }
102
103 wxSound::wxSound(size_t size, const void* data)
104 {
105 Init();
106 Create( size, data );
107 }
108
109 wxSound::~wxSound()
110 {
111 // if the sound is in a playing state, just mark it to be deleted and
112 // delete it after it plays. Otherwise, async sounds created on the stack
113 // may never get the chance to play.
114 bool isPlaying = false;
115 for ( wxVector<wxSoundData*>::reverse_iterator s = s_soundsPlaying.rbegin();
116 s != s_soundsPlaying.rend(); ++s )
117 {
118 if (*s == m_data)
119 {
120 isPlaying = true;
121 break;
122 }
123 }
124
125 if (isPlaying)
126 m_data->MarkForDeletion();
127 else
128 delete m_data;
129 }
130
131 void wxSound::Init()
132 {
133 m_data = NULL;
134 }
135
136 bool wxSound::DoPlay(unsigned flags) const
137 {
138 if ( m_data )
139 {
140 s_soundsPlaying.push_back(m_data);
141 if ( !m_data->Play(flags) )
142 s_soundsPlaying.pop_back();
143 }
144
145 return false;
146 }
147
148 bool wxSound::IsPlaying()
149 {
150 return s_soundsPlaying.size() > 0;
151 }
152
153 void wxSound::Stop()
154 {
155 for ( wxVector<wxSoundData*>::reverse_iterator s = s_soundsPlaying.rbegin();
156 s != s_soundsPlaying.rend(); ++s )
157 {
158 (*s)->Stop();
159 }
160 }
161
162 // Notification when a sound has stopped
163 void wxSound::SoundStopped(const wxSoundData* data)
164 {
165 for ( wxVector<wxSoundData*>::iterator s = s_soundsPlaying.begin();
166 s != s_soundsPlaying.end(); ++s )
167 {
168 if ( (*s) == data )
169 {
170 s_soundsPlaying.erase(s);
171 break;
172 }
173 }
174 }
175
176 #endif //wxUSE_SOUND