sound implementation for AudioToolbox on 10.5 and iPhone
[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 // RCS-ID: $Id: sound.cpp 61475 2009-07-20 16:47:54Z VZ $
8 // Copyright: (c) Stefan Csomor
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 #ifndef WX_PRECOMP
20 #include "wx/object.h"
21 #include "wx/string.h"
22 #include "wx/intl.h"
23 #include "wx/log.h"
24 #include "wx/timer.h"
25 #endif
26
27 #include "wx/file.h"
28
29 #include "wx/vector.h"
30
31 class wxSoundTimer : public wxTimer
32 {
33 public:
34 wxSoundTimer(wxSoundData* snd)
35 : m_sound(snd)
36 {
37 }
38
39 virtual ~wxSoundTimer()
40 {
41 Stop();
42 m_sound->DoStop();
43 }
44
45 void Notify()
46 {
47 m_sound->SoundTask();
48 }
49
50 protected:
51 wxSoundData* m_sound;
52 };
53
54 wxVector<wxSoundData*> s_soundsPlaying;
55
56 wxSoundData::wxSoundData()
57 {
58 m_pTimer = NULL;
59 }
60
61 wxSoundData::~wxSoundData()
62 {
63 }
64
65 void wxSoundData::Stop()
66 {
67 DoStop();
68 if ( m_pTimer )
69 {
70 delete m_pTimer;
71 m_pTimer = NULL;
72 }
73 }
74
75 //Time between timer calls
76 #define MOVIE_DELAY 100
77
78 void wxSoundData::SoundTask()
79 {
80 }
81
82 void wxSoundData::CreateAndStartTimer()
83 {
84 //Start timer and play movie asyncronously
85 m_pTimer = new wxSoundTimer(this);
86 m_pTimer->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
87 }
88
89 wxSound::wxSound()
90 {
91 Init();
92 }
93
94 wxSound::wxSound(const wxString& sFileName, bool isResource)
95 {
96 Init();
97 Create(sFileName, isResource);
98 }
99
100 wxSound::wxSound(int size, const wxByte* data)
101 {
102 Init();
103 Create( size, data );
104 }
105
106 wxSound::~wxSound()
107 {
108 delete m_data;
109 }
110
111 void wxSound::Init()
112 {
113 m_data = NULL;
114 }
115
116 bool wxSound::DoPlay(unsigned flags) const
117 {
118 if ( m_data )
119 {
120 s_soundsPlaying.push_back(m_data);
121 if ( !m_data->Play(flags) )
122 s_soundsPlaying.pop_back();
123 }
124
125 return false;
126 }
127
128 bool wxSound::IsPlaying()
129 {
130 return s_soundsPlaying.size() > 0;
131 }
132
133 void wxSound::Stop()
134 {
135 for ( wxVector<wxSoundData*>::reverse_iterator s = s_soundsPlaying.rbegin();
136 s != s_soundsPlaying.rend(); ++s )
137 {
138 (*s)->Stop();
139 }
140 }
141
142 // Notification when a sound has stopped
143 void wxSound::SoundStopped(const wxSoundData* data)
144 {
145 for ( wxVector<wxSoundData*>::iterator s = s_soundsPlaying.begin();
146 s != s_soundsPlaying.end(); ++s )
147 {
148 if ( (*s) == data )
149 {
150 s_soundsPlaying.erase(s);
151 break;
152 }
153 }
154 }
155
156 #endif //wxUSE_SOUND