1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSound class implementation: optional
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "sound.h"
16 #include "wx/object.h"
17 #include "wx/string.h"
23 #include "wx/cocoa/autorelease.h"
24 #include "wx/cocoa/string.h"
26 #import <AppKit/AppKit.h>
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
34 WX_NSSound lastSound=NULL;
35 bool isLastSoundLooping = false;
36 bool isLastSoundInScope = false;
38 // ========================================================================
40 // ========================================================================
41 @interface wxNSSoundDelegate : NSObject
46 - (void)sound:(NSSound *)theSound didFinishPlaying:(BOOL)bOK;
47 @end // interface wxNSSoundDelegate : NSObject
49 @implementation wxNSSoundDelegate : NSObject
51 - (void)sound:(NSSound *)theSound didFinishPlaying:(BOOL)bOK
53 if (bOK && isLastSoundLooping)
55 else if (!isLastSoundInScope)
62 @end // wxNSSoundDelegate
64 const wxObjcAutoRefFromAlloc<struct objc_object*> wxSound::sm_cocoaDelegate = [[wxNSSoundDelegate alloc] init];
66 // ------------------------------------------------------------------
68 // ------------------------------------------------------------------
76 wxSound::wxSound(const wxString& sFileName, bool isResource)
80 Create(sFileName, isResource);
83 wxSound::wxSound(int size, const wxByte* data)
87 NSData* theData = [[NSData alloc] dataWithBytesNoCopy:(void*)data length:size];
88 m_cocoaNSSound = [[NSSound alloc] initWithData:theData];
94 if (m_cocoaNSSound != lastSound)
96 [m_cocoaNSSound release];
99 isLastSoundInScope = false;
102 bool wxSound::Create(const wxString& fileName, bool isResource)
104 wxAutoNSAutoreleasePool thePool;
110 //oftype could be @"snd" @"wav" or @"aiff"; nil or @"" autodetects (?)
111 m_cocoaNSSound = [[NSSound alloc]
112 initWithContentsOfFile:[[NSBundle mainBundle]
113 pathForResource:wxNSStringWithWxString(fileName)
118 m_cocoaNSSound = [[NSSound alloc] initWithContentsOfFile:wxNSStringWithWxString(fileName) byReference:YES];
120 m_sndname = fileName;
121 return m_cocoaNSSound;
124 bool wxSound::DoPlay(unsigned flags) const
126 wxASSERT_MSG(!( (flags & wxSOUND_SYNC) && (flags & wxSOUND_LOOP)),
127 wxT("Invalid flag combination passed to wxSound::Play"));
131 if (flags & wxSOUND_ASYNC)
133 lastSound = m_cocoaNSSound;
134 isLastSoundLooping = (flags & wxSOUND_LOOP) == wxSOUND_LOOP;
135 isLastSoundInScope = true;
136 [m_cocoaNSSound setDelegate:sm_cocoaDelegate];
137 return [m_cocoaNSSound play];
141 [m_cocoaNSSound setDelegate:nil];
144 bool bOK = [m_cocoaNSSound play];
146 while ([m_cocoaNSSound isPlaying])
148 wxTheApp->Yield(false);
154 bool wxSound::IsPlaying()
156 return [lastSound isPlaying];
161 if (isLastSoundInScope)
163 isLastSoundInScope = false;
165 //remember that even though we're
166 //programatically stopping it, the
167 //delegate will still be called -
168 //so it will free the memory here
169 [((NSSound*&)lastSound) stop];