git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@28557
c3d73ce0-8a6f-49c7-b76d-
6d57e0e08775
// Name: sound.h
// Purpose: wxSound class (loads and plays short Windows .wav files).
// Optional on non-Windows platforms.
// Name: sound.h
// Purpose: wxSound class (loads and plays short Windows .wav files).
// Optional on non-Windows platforms.
-// Author: Stefan Csomor
+// Author: Ryan Norton, Stefan Csomor
// Modified by:
// Created: 1998-01-01
// RCS-ID: $Id$
// Modified by:
// Created: 1998-01-01
// RCS-ID: $Id$
-// Copyright: (c) Stefan Csomor
+// Copyright: (c) Ryan Norton, Stefan Csomor
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
public:
bool Create(const wxString& fileName, bool isResource = FALSE);
bool IsOk() const { return !m_sndname.IsEmpty(); }
public:
bool Create(const wxString& fileName, bool isResource = FALSE);
bool IsOk() const { return !m_sndname.IsEmpty(); }
+ static void Stop();
+ static bool IsPlaying();
-protected:
- // prevent collision with some BSD definitions of macro Free()
- bool FreeData();
-
bool DoPlay(unsigned flags) const;
private:
bool DoPlay(unsigned flags) const;
private:
/////////////////////////////////////////////////////////////////////////////
// Name: sound.cpp
// Purpose: wxSound class implementation: optional
/////////////////////////////////////////////////////////////////////////////
// Name: sound.cpp
// Purpose: wxSound class implementation: optional
-// Author: Ryan Norton, Stefan Csomor
// Modified by:
// Created: 1998-01-01
// RCS-ID: $Id$
// Modified by:
// Created: 1998-01-01
// RCS-ID: $Id$
class wxQTTimer : public wxTimer
{
public:
class wxQTTimer : public wxTimer
{
public:
- wxQTTimer(Movie movie, bool bLoop) :
- m_movie(movie), m_bLoop(bLoop)
+ wxQTTimer(Movie movie, bool bLoop, bool& playing) :
+ m_movie(movie), m_bLoop(bLoop), m_pbPlaying(&playing)
+ if(m_pbPlaying)
+ *m_pbPlaying = false;
StopMovie(m_movie);
DisposeMovie(m_movie);
StopMovie(m_movie);
DisposeMovie(m_movie);
Stop();
//Note that ExitMovies() is not neccessary, but
//the docs are fuzzy on whether or not TerminateQTML is
ExitMovies();
Stop();
//Note that ExitMovies() is not neccessary, but
//the docs are fuzzy on whether or not TerminateQTML is
ExitMovies();
+#endif
+ }
+
+ void Shutdown()
+ {
+ delete this;
+ if (m_pbPlaying && !*m_pbPlaying)
+ {
+ Shutdown();
+ }
+
if(IsMovieDone(m_movie))
{
if (!m_bLoop)
if(IsMovieDone(m_movie))
{
if (!m_bLoop)
else
{
StopMovie(m_movie);
else
{
StopMovie(m_movie);
- GoToBeginningOfMovie(m_movie);
+ GoToBeginningOfMovie(m_movie);
}
else
MoviesTask(m_movie, MOVIE_DELAY); //Give QT time to play movie
}
else
MoviesTask(m_movie, MOVIE_DELAY); //Give QT time to play movie
Movie& GetMovie() {return m_movie;}
Movie& GetMovie() {return m_movie;}
protected:
Movie m_movie;
bool m_bLoop;
protected:
Movie m_movie;
bool m_bLoop;
+
+public:
+ bool* m_pbPlaying;
+
};
class wxSMTimer : public wxTimer
{
public:
};
class wxSMTimer : public wxTimer
{
public:
- wxSMTimer(void* hSnd, void* pSndChannel, const bool& bLoop)
- : m_hSnd(hSnd), m_pSndChannel(pSndChannel), m_bLoop(bLoop)
- {
- }
+ wxSMTimer(void* hSnd, void* pSndChannel, const bool& bLoop, bool& playing)
+ : m_hSnd(hSnd), m_pSndChannel(pSndChannel), m_bLoop(bLoop), m_pbPlaying(&playing)
+ {
+ }
+ {
+ if(m_pbPlaying)
+ *m_pbPlaying = false;
+ SndDisposeChannel((SndChannelPtr)m_pSndChannel, TRUE);
+ Stop();
+ }
+ {
+ if (m_pbPlaying && !*m_pbPlaying)
+ Shutdown();
+ }
+
+ SCStatus stat;
if (SndChannelStatus((SndChannelPtr)m_pSndChannel, sizeof(SCStatus), &stat) != 0)
if (SndChannelStatus((SndChannelPtr)m_pSndChannel, sizeof(SCStatus), &stat) != 0)
- Shutdown();
-
- //if the sound isn't playing anymore, see if it's looped,
- //and if so play it again, otherwise close things up
- if (stat.scChannelBusy == FALSE)
- {
- if (m_bLoop)
- {
- if(SndPlay((SndChannelPtr)m_pSndChannel, (SndListHandle) m_hSnd, true) != noErr)
- Shutdown();
- }
- else
- Shutdown();
+ Shutdown();
+
+ //if the sound isn't playing anymore, see if it's looped,
+ //and if so play it again, otherwise close things up
+ if (stat.scChannelBusy == FALSE)
+ {
+ if (m_bLoop)
+ {
+ if(SndPlay((SndChannelPtr)m_pSndChannel, (SndListHandle) m_hSnd, true) != noErr)
+ Shutdown();
+ }
+ else
+ Shutdown();
- SndDisposeChannel((SndChannelPtr)m_pSndChannel, TRUE);
- Stop();
+
+ void* GetChannel() {return m_pSndChannel;}
+
protected:
void* m_hSnd;
void* m_pSndChannel;
bool m_bLoop;
protected:
void* m_hSnd;
void* m_pSndChannel;
bool m_bLoop;
+
+public:
+ bool* m_pbPlaying;
};
// ------------------------------------------------------------------
// wxSound
// ------------------------------------------------------------------
};
// ------------------------------------------------------------------
// wxSound
// ------------------------------------------------------------------
+wxTimer* lastSoundTimer=NULL;
+bool lastSoundIsPlaying=false;
//Determines whether version 4 of QT is installed
Boolean wxIsQuickTime4Installed (void)
//Determines whether version 4 of QT is installed
Boolean wxIsQuickTime4Installed (void)
//-2093 no dll
if ((nError = InitializeQTML(0)) != noErr)
wxLogSysError(wxString::Format("Couldn't Initialize Quicktime-%i", nError));
//-2093 no dll
if ((nError = InitializeQTML(0)) != noErr)
wxLogSysError(wxString::Format("Couldn't Initialize Quicktime-%i", nError));
EnterMovies();
return true;
}
else
EnterMovies();
return true;
}
else
wxLogSysError("Quicktime is not installed, or Your Version of Quicktime is <= 4.");
wxLogSysError("Quicktime is not installed, or Your Version of Quicktime is <= 4.");
wxSound::wxSound(int size, const wxByte* data)
: m_hSnd((char*)data), m_waveLength(size), m_pTimer(NULL), m_type(wxSound_MEMORY)
{
wxSound::wxSound(int size, const wxByte* data)
: m_hSnd((char*)data), m_waveLength(size), m_pTimer(NULL), m_type(wxSound_MEMORY)
{
- if (!wxInitQT())
- m_type = wxSound_NONE;
+ if(lastSoundIsPlaying)
+ {
+ if(m_type == wxSound_RESOURCE)
+ ((wxSMTimer*)lastSoundTimer)->m_pbPlaying = NULL;
+ else
+ ((wxQTTimer*)lastSoundTimer)->m_pbPlaying = NULL;
+ }
}
bool wxSound::Create(const wxString& fileName, bool isResource)
{
}
bool wxSound::Create(const wxString& fileName, bool isResource)
{
- if(!wxInitQT())
- return false;
-
- FreeData();
wxMacStringToPascal( fileName , lpSnd ) ;
wxMacStringToPascal( fileName , lpSnd ) ;
m_hSnd = (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd);
#else
return false;
#endif
m_hSnd = (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd);
#else
return false;
#endif
m_type = wxSound_FILE;
m_sndname = fileName;
}
m_type = wxSound_FILE;
m_sndname = fileName;
}
bool wxSound::DoPlay(unsigned flags) const
{
bool wxSound::DoPlay(unsigned flags) const
{
-// wxASSERT(m_pTimer == NULL || !((wxTimer*)m_pTimer)->IsRunning() );
- FreeData();
+ if (!wxInitQT())
+ return false;
Handle myHandle, dataRef = nil;
Handle myHandle, dataRef = nil;
- MovieImportComponent miComponent;
+ MovieImportComponent miComponent;
Track targetTrack = nil;
TimeValue addedDuration = 0;
long outFlags = 0;
Track targetTrack = nil;
TimeValue addedDuration = 0;
long outFlags = 0;
ComponentResult result;
myHandle = NewHandleClear((Size)m_waveLength);
ComponentResult result;
myHandle = NewHandleClear((Size)m_waveLength);
BlockMove(m_hSnd, *myHandle, m_waveLength);
err = PtrToHand(&myHandle, &dataRef, sizeof(Handle));
BlockMove(m_hSnd, *myHandle, m_waveLength);
err = PtrToHand(&myHandle, &dataRef, sizeof(Handle));
else if (memcmp(&m_hSnd[8], "AIFC", 4) == 0)
miComponent = OpenDefaultComponent(MovieImportType, kQTFileTypeAIFC);
else
else if (memcmp(&m_hSnd[8], "AIFC", 4) == 0)
miComponent = OpenDefaultComponent(MovieImportType, kQTFileTypeAIFC);
else
wxLogSysError("wxSound - Location in memory does not contain valid data");
return false;
}
wxLogSysError("wxSound - Location in memory does not contain valid data");
return false;
}
if (result != noErr)
{
wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result));
if (result != noErr)
{
wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result));
SetMovieVolume(movie, kFullVolume);
GoToBeginningOfMovie(movie);
SetMovieVolume(movie, kFullVolume);
GoToBeginningOfMovie(movie);
SndChannelPtr pSndChannel;
SndNewChannel(&pSndChannel, sampledSynth,
SndChannelPtr pSndChannel;
SndNewChannel(&pSndChannel, sampledSynth,
- initNoInterp +
- (data.numChannels == 1 ? initMono : initStereo), NULL);
+ initNoInterp
+ + (data.numChannels == 1 ? initMono : initStereo), NULL);
if(SndPlay(pSndChannel, (SndListHandle) m_hSnd, flags & wxSOUND_ASYNC ? 1 : 0) != noErr)
if(SndPlay(pSndChannel, (SndListHandle) m_hSnd, flags & wxSOUND_ASYNC ? 1 : 0) != noErr)
if (flags & wxSOUND_ASYNC)
if (flags & wxSOUND_ASYNC)
- {
- ((wxSMTimer*&)m_pTimer) = new wxSMTimer(pSndChannel, m_hSnd, flags & wxSOUND_LOOP ? 1 : 0);
-
- ((wxTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
- }
- else
+ {
+ lastSoundTimer = ((wxSMTimer*&)m_pTimer)
+ = new wxSMTimer(pSndChannel, m_hSnd, flags & wxSOUND_LOOP ? 1 : 0,
+ lastSoundIsPlaying=true);
+
+ ((wxTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
+ }
+ else
SndDisposeChannel(pSndChannel, TRUE);
return true;
SndDisposeChannel(pSndChannel, TRUE);
return true;
break;
case wxSound_FILE:
{
break;
case wxSound_FILE:
{
+ if (!wxInitQT())
+ return false;
+
short movieResFile;
FSSpec sfFile;
short movieResFile;
FSSpec sfFile;
#else
int nError;
if ((nError = NativePathNameToFSSpec ((char*) m_sndname.c_str(), &sfFile, 0)) != noErr)
#else
int nError;
if ((nError = NativePathNameToFSSpec ((char*) m_sndname.c_str(), &sfFile, 0)) != noErr)
- {
- wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
+ {
+ wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
m_sndname.c_str(), nError));
return false;
m_sndname.c_str(), nError));
return false;
#endif
if (OpenMovieFile (&sfFile, &movieResFile, fsRdPerm) != noErr)
#endif
if (OpenMovieFile (&sfFile, &movieResFile, fsRdPerm) != noErr)
CloseMovieFile (movieResFile);
if (err != noErr)
CloseMovieFile (movieResFile);
if (err != noErr)
wxLogSysError(
wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname.c_str(), err )
);
return false;
wxLogSysError(
wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname.c_str(), err )
);
return false;
//Start the movie!
StartMovie(movie);
//Start the movie!
StartMovie(movie);
- if (flags & wxSOUND_ASYNC)
- {
- //Start timer and play movie asyncronously
- ((wxQTTimer*&)m_pTimer) = new wxQTTimer(movie, flags & wxSOUND_LOOP ? 1 : 0);
- ((wxQTTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
- }
- else
+ if (flags & wxSOUND_SYNC)
{
wxASSERT_MSG(!(flags & wxSOUND_LOOP), "Can't loop and play syncronously at the same time");
{
wxASSERT_MSG(!(flags & wxSOUND_LOOP), "Can't loop and play syncronously at the same time");
+ else
+ {
+ //Start timer and play movie asyncronously
+ lastSoundTimer = ((wxQTTimer*&)m_pTimer) = new wxQTTimer(movie, flags & wxSOUND_LOOP ? 1 : 0,lastSoundIsPlaying=true);
+ ((wxQTTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
+ }
-void* wxSound::GetHandle()
+bool wxSound::IsPlaying()
- return (void*) ((wxQTTimer*) m_pTimer)->GetMovie();
+ return lastSoundIsPlaying;
-bool wxSound::FreeData()
- delete (wxQTTimer*) m_pTimer;
- m_pTimer = NULL;
+ delete (wxTimer*&) lastSoundTimer;
+ lastSoundIsPlaying = false;
+void* wxSound::GetHandle()
+{
+ if(m_type == wxSound_RESOURCE)
+ return (void*) ((wxSMTimer*)m_pTimer)->GetChannel();
+ return (void*) ((wxQTTimer*) m_pTimer)->GetMovie();
+}