]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/sound.mm
Removed some extraneous whitespace.
[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 [self release];
59 }
60 }
61
62 @end // wxNSSoundDelegate
63
64 // ------------------------------------------------------------------
65 // wxSound
66 // ------------------------------------------------------------------
67
68 wxSound::wxSound()
69 : m_hSnd(NULL)
70 , m_waveLength(0)
71 {
72 }
73
74 wxSound::wxSound(const wxString& sFileName, bool isResource)
75 : m_hSnd(NULL)
76 , m_waveLength(0)
77 {
78 Create(sFileName, isResource);
79 }
80
81 wxSound::wxSound(int size, const wxByte* data)
82 : m_hSnd(NULL)
83 , m_waveLength(size)
84 {
85 NSData* theData = [[NSData alloc] dataWithBytesNoCopy:(void*)data length:size];
86 m_hSnd = [[NSSound alloc] initWithData:theData];
87
88 m_cocoaSoundDelegate = [[wxNSSoundDelegate alloc] init];
89 }
90
91 wxSound::~wxSound()
92 {
93 if (m_hSnd != lastSound)
94 {
95 [m_hSnd release];
96 [m_cocoaSoundDelegate release];
97 }
98 else
99 isLastSoundInScope = false;
100 }
101
102 bool wxSound::Create(const wxString& fileName, bool isResource)
103 {
104 wxAutoNSAutoreleasePool thePool;
105
106 Stop();
107
108 if (isResource)
109 {
110 //oftype could be @"snd" @"wav" or @"aiff"; nil or @"" autodetects (?)
111 m_hSnd = [[NSSound alloc]
112 initWithContentsOfFile:[[NSBundle mainBundle]
113 pathForResource:wxNSStringWithWxString(fileName)
114 ofType:nil]
115 byReference:YES];
116 }
117 else
118 m_hSnd = [[NSSound alloc] initWithContentsOfFile:wxNSStringWithWxString(fileName) byReference:YES];
119
120 m_cocoaSoundDelegate = [[wxNSSoundDelegate alloc] init];
121
122 m_sndname = fileName;
123 return m_hSnd != nil;
124 }
125
126 bool wxSound::DoPlay(unsigned flags) const
127 {
128 wxASSERT_MSG(!( (flags & wxSOUND_SYNC) && (flags & wxSOUND_LOOP)),
129 wxT("Invalid flag combination passed to wxSound::Play"));
130
131 Stop();
132
133 if (flags & wxSOUND_ASYNC)
134 {
135 lastSound = m_hSnd;
136 isLastSoundLooping = (flags & wxSOUND_LOOP) == wxSOUND_LOOP;
137 isLastSoundInScope = true;
138 [m_hSnd setDelegate:m_cocoaSoundDelegate];
139 return [m_hSnd play];
140 }
141 else
142 {
143 [m_hSnd setDelegate:nil];
144
145 //play until done
146 bool bOK = [m_hSnd play];
147
148 while ([m_hSnd isPlaying])
149 {
150 wxTheApp->Yield(false);
151 }
152 return bOK;
153 }
154 }
155
156 bool wxSound::IsPlaying()
157 {
158 return [lastSound isPlaying];
159 }
160
161 void wxSound::Stop()
162 {
163 if (isLastSoundInScope)
164 {
165 isLastSoundInScope = false;
166
167 //remember that even though we're
168 //programatically stopping it, the
169 //delegate will still be called -
170 //so it will free the memory here
171 [((NSSound*&)lastSound) stop];
172 }
173
174 lastSound = nil;
175 }
176
177 #endif //wxUSE_SOUND