]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/sound.mm
Due to the last change the delegate should not release itself.
[wxWidgets.git] / src / cocoa / sound.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sound.cpp
3 // Purpose: wxSound class implementation: optional
4 // Author: Ryan Norton
5 // Modified by:
6 // Created: 2004-10-02
7 // RCS-ID: $Id$
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "sound.h"
14 #endif
15
16 #include "wx/object.h"
17 #include "wx/string.h"
18 #include "wx/sound.h"
19
20 #if wxUSE_SOUND
21
22 #include "wx/app.h"
23 #include "wx/cocoa/autorelease.h"
24 #include "wx/cocoa/string.h"
25
26 #import <AppKit/AppKit.h>
27
28 //
29 // NB: Vaclav's new wxSound API is really tricky -
30 // Basically, we need to make sure that if the wxSound
31 // object is still in scope we don't release it's NSSound
32 //
33
34 WX_NSSound lastSound=NULL;
35 bool isLastSoundLooping = false;
36 bool isLastSoundInScope = false;
37
38 // ========================================================================
39 // wxNSSoundDelegate
40 // ========================================================================
41 @interface wxNSSoundDelegate : NSObject
42 {
43 }
44
45 // Delegate methods
46 - (void)sound:(NSSound *)theSound didFinishPlaying:(BOOL)bOK;
47 @end // interface wxNSSoundDelegate : NSObject
48
49 @implementation wxNSSoundDelegate : NSObject
50
51 - (void)sound:(NSSound *)theSound didFinishPlaying:(BOOL)bOK
52 {
53 if (bOK && isLastSoundLooping)
54 [lastSound play];
55 else if (!isLastSoundInScope)
56 {
57 [lastSound release];
58 }
59 }
60
61 @end // wxNSSoundDelegate
62
63 const wxObjcAutoRefFromAlloc<struct objc_object*> wxSound::sm_cocoaDelegate = [[wxNSSoundDelegate alloc] init];
64
65 // ------------------------------------------------------------------
66 // wxSound
67 // ------------------------------------------------------------------
68
69 wxSound::wxSound()
70 : m_cocoaNSSound(nil)
71 , m_waveLength(0)
72 {
73 }
74
75 wxSound::wxSound(const wxString& sFileName, bool isResource)
76 : m_cocoaNSSound(nil)
77 , m_waveLength(0)
78 {
79 Create(sFileName, isResource);
80 }
81
82 wxSound::wxSound(int size, const wxByte* data)
83 : m_cocoaNSSound(nil)
84 , m_waveLength(size)
85 {
86 NSData* theData = [[NSData alloc] dataWithBytesNoCopy:(void*)data length:size];
87 m_cocoaNSSound = [[NSSound alloc] initWithData:theData];
88
89 }
90
91 wxSound::~wxSound()
92 {
93 if (m_cocoaNSSound != lastSound)
94 {
95 [m_cocoaNSSound release];
96 }
97 else
98 isLastSoundInScope = false;
99 }
100
101 bool wxSound::Create(const wxString& fileName, bool isResource)
102 {
103 wxAutoNSAutoreleasePool thePool;
104
105 Stop();
106
107 if (isResource)
108 {
109 //oftype could be @"snd" @"wav" or @"aiff"; nil or @"" autodetects (?)
110 m_cocoaNSSound = [[NSSound alloc]
111 initWithContentsOfFile:[[NSBundle mainBundle]
112 pathForResource:wxNSStringWithWxString(fileName)
113 ofType:nil]
114 byReference:YES];
115 }
116 else
117 m_cocoaNSSound = [[NSSound alloc] initWithContentsOfFile:wxNSStringWithWxString(fileName) byReference:YES];
118
119 m_sndname = fileName;
120 return m_cocoaNSSound;
121 }
122
123 bool wxSound::DoPlay(unsigned flags) const
124 {
125 wxASSERT_MSG(!( (flags & wxSOUND_SYNC) && (flags & wxSOUND_LOOP)),
126 wxT("Invalid flag combination passed to wxSound::Play"));
127
128 Stop();
129
130 if (flags & wxSOUND_ASYNC)
131 {
132 lastSound = m_cocoaNSSound;
133 isLastSoundLooping = (flags & wxSOUND_LOOP) == wxSOUND_LOOP;
134 isLastSoundInScope = true;
135 [m_cocoaNSSound setDelegate:sm_cocoaDelegate];
136 return [m_cocoaNSSound play];
137 }
138 else
139 {
140 [m_cocoaNSSound setDelegate:nil];
141
142 //play until done
143 bool bOK = [m_cocoaNSSound play];
144
145 while ([m_cocoaNSSound isPlaying])
146 {
147 wxTheApp->Yield(false);
148 }
149 return bOK;
150 }
151 }
152
153 bool wxSound::IsPlaying()
154 {
155 return [lastSound isPlaying];
156 }
157
158 void wxSound::Stop()
159 {
160 if (isLastSoundInScope)
161 {
162 isLastSoundInScope = false;
163
164 //remember that even though we're
165 //programatically stopping it, the
166 //delegate will still be called -
167 //so it will free the memory here
168 [((NSSound*&)lastSound) stop];
169 }
170
171 lastSound = nil;
172 }
173
174 #endif //wxUSE_SOUND