refactoring in preparation for further changes: moved data in a private struct, let Windows load resources instead of doing it ourselves, use GlobalPtr[Lock] classes
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@34987 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
// Name: sound.h
|
// Name: wx/msw/sound.h
|
||||||
// Purpose: wxSound class
|
// Purpose: wxSound class
|
||||||
// Author: Julian Smart
|
// Author: Julian Smart
|
||||||
// Modified by:
|
// Modified by:
|
||||||
@@ -18,38 +18,39 @@
|
|||||||
|
|
||||||
#if wxUSE_SOUND
|
#if wxUSE_SOUND
|
||||||
|
|
||||||
#include "wx/object.h"
|
|
||||||
|
|
||||||
class WXDLLIMPEXP_ADV wxSound : public wxSoundBase
|
class WXDLLIMPEXP_ADV wxSound : public wxSoundBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
wxSound();
|
wxSound();
|
||||||
wxSound(const wxString& fileName, bool isResource = false);
|
wxSound(const wxString& fileName, bool isResource = false);
|
||||||
wxSound(int size, const wxByte* data);
|
wxSound(int size, const wxByte* data);
|
||||||
~wxSound();
|
virtual ~wxSound();
|
||||||
|
|
||||||
public:
|
// Create from resource or file
|
||||||
// Create from resource or file
|
bool Create(const wxString& fileName, bool isResource = false);
|
||||||
bool Create(const wxString& fileName, bool isResource = false);
|
|
||||||
// Create from data
|
|
||||||
bool Create(int size, const wxByte* data);
|
|
||||||
|
|
||||||
bool IsOk() const { return (m_waveData ? true : false); };
|
// Create from data
|
||||||
|
bool Create(int size, const wxByte* data);
|
||||||
|
|
||||||
static void Stop();
|
bool IsOk() const { return m_data != NULL; }
|
||||||
|
|
||||||
|
static void Stop();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool Free();
|
void Init() { m_data = NULL; }
|
||||||
|
bool CheckCreatedOk();
|
||||||
|
void Free();
|
||||||
|
|
||||||
bool DoPlay(unsigned flags) const;
|
virtual bool DoPlay(unsigned flags) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wxByte* m_waveData;
|
// data of this object
|
||||||
int m_waveLength;
|
class wxSoundData *m_data;
|
||||||
bool m_isResource;
|
|
||||||
|
|
||||||
DECLARE_NO_COPY_CLASS(wxSound)
|
DECLARE_NO_COPY_CLASS(wxSound)
|
||||||
};
|
};
|
||||||
#endif
|
|
||||||
#endif
|
#endif // wxUSE_SOUND
|
||||||
|
|
||||||
|
#endif // _WX_SOUND_H_
|
||||||
|
|
||||||
|
@@ -2,161 +2,212 @@
|
|||||||
// Name: sound.cpp
|
// Name: sound.cpp
|
||||||
// Purpose: wxSound
|
// Purpose: wxSound
|
||||||
// Author: Julian Smart
|
// Author: Julian Smart
|
||||||
// Modified by:
|
// Modified by: 2005-07-29: Vadim Zeitlin: redesign
|
||||||
// Created: 04/01/98
|
// Created: 04/01/98
|
||||||
// RCS-ID: $Id$
|
// RCS-ID: $Id$
|
||||||
// Copyright: (c) Julian Smart
|
// Copyright: (c) Julian Smart
|
||||||
// Licence: wxWindows licence
|
// Licence: wxWindows licence
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// declarations
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// headers
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
|
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
|
||||||
#pragma implementation "sound.h"
|
#pragma implementation "sound.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// For compilers that support precompilation, includes "wx.h".
|
// For compilers that support precompilation, includes "wx.h".
|
||||||
#include "wx/wxprec.h"
|
#include "wx/wxprec.h"
|
||||||
|
|
||||||
#if defined(__BORLANDC__)
|
#if defined(__BORLANDC__)
|
||||||
#pragma hdrstop
|
#pragma hdrstop
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if wxUSE_SOUND
|
#if wxUSE_SOUND
|
||||||
|
|
||||||
#ifndef WX_PRECOMP
|
|
||||||
#include "wx/wx.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "wx/file.h"
|
|
||||||
#include "wx/sound.h"
|
#include "wx/sound.h"
|
||||||
#include "wx/msw/private.h"
|
#include "wx/msw/private.h"
|
||||||
|
|
||||||
#include <windowsx.h>
|
|
||||||
|
|
||||||
#include <mmsystem.h>
|
#include <mmsystem.h>
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// wxSoundData
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// ABC for different sound data representations
|
||||||
|
class wxSoundData
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
wxSoundData() { }
|
||||||
|
|
||||||
|
// return true if we had been successfully initialized
|
||||||
|
virtual bool IsOk() const = 0;
|
||||||
|
|
||||||
|
// get the flag corresponding to our content for PlaySound()
|
||||||
|
virtual DWORD GetSoundFlag() const = 0;
|
||||||
|
|
||||||
|
// get the data to be passed to PlaySound()
|
||||||
|
virtual LPCTSTR GetSoundData() const = 0;
|
||||||
|
|
||||||
|
virtual ~wxSoundData() { }
|
||||||
|
};
|
||||||
|
|
||||||
|
// class for in-memory sound data
|
||||||
|
class wxSoundDataMemory : public wxSoundData
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// we copy the data
|
||||||
|
wxSoundDataMemory(int size, const wxByte *buf);
|
||||||
|
|
||||||
|
void *GetPtr() const { return m_waveDataPtr; }
|
||||||
|
|
||||||
|
virtual bool IsOk() const { return GetPtr() != NULL; }
|
||||||
|
virtual DWORD GetSoundFlag() const { return SND_MEMORY; }
|
||||||
|
virtual LPCTSTR GetSoundData() const { return (LPCTSTR)GetPtr(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
GlobalPtr m_waveData;
|
||||||
|
GlobalPtrLock m_waveDataPtr;
|
||||||
|
|
||||||
|
DECLARE_NO_COPY_CLASS(wxSoundDataMemory)
|
||||||
|
};
|
||||||
|
|
||||||
|
// class for sound files and resources
|
||||||
|
class wxSoundDataFile : public wxSoundData
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
wxSoundDataFile(const wxString& filename, bool isResource);
|
||||||
|
|
||||||
|
virtual bool IsOk() const { return !m_name.empty(); }
|
||||||
|
virtual DWORD GetSoundFlag() const
|
||||||
|
{
|
||||||
|
return m_isResource ? SND_RESOURCE : SND_FILENAME;
|
||||||
|
}
|
||||||
|
virtual LPCTSTR GetSoundData() const { return m_name.c_str(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const wxString m_name;
|
||||||
|
const bool m_isResource;
|
||||||
|
|
||||||
|
DECLARE_NO_COPY_CLASS(wxSoundDataFile)
|
||||||
|
};
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// implementation
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// wxSoundData-derived classes
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
wxSoundDataMemory::wxSoundDataMemory(int size, const wxByte *buf)
|
||||||
|
: m_waveData(size),
|
||||||
|
m_waveDataPtr(m_waveData)
|
||||||
|
{
|
||||||
|
if ( IsOk() )
|
||||||
|
::CopyMemory(m_waveDataPtr, buf, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
wxSoundDataFile::wxSoundDataFile(const wxString& filename, bool isResource)
|
||||||
|
: m_name(filename),
|
||||||
|
m_isResource(isResource)
|
||||||
|
{
|
||||||
|
// check for file/resource existence?
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// wxSound
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
wxSound::wxSound()
|
wxSound::wxSound()
|
||||||
: m_waveData(NULL), m_waveLength(0), m_isResource(false)
|
|
||||||
{
|
{
|
||||||
|
Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
wxSound::wxSound(const wxString& sFileName, bool isResource)
|
wxSound::wxSound(const wxString& filename, bool isResource)
|
||||||
: m_waveData(NULL), m_waveLength(0), m_isResource(isResource)
|
|
||||||
{
|
{
|
||||||
Create(sFileName, isResource);
|
Init();
|
||||||
|
Create(filename, isResource);
|
||||||
}
|
}
|
||||||
|
|
||||||
wxSound::wxSound(int size, const wxByte* data)
|
wxSound::wxSound(int size, const wxByte *data)
|
||||||
: m_waveData(NULL), m_waveLength(0), m_isResource(false)
|
|
||||||
{
|
{
|
||||||
Create(size, data);
|
Init();
|
||||||
|
Create(size, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
wxSound::~wxSound()
|
wxSound::~wxSound()
|
||||||
{
|
{
|
||||||
Free();
|
Free();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxSound::Create(const wxString& fileName, bool isResource)
|
void wxSound::Free()
|
||||||
{
|
{
|
||||||
Free();
|
if ( m_data )
|
||||||
|
|
||||||
if (isResource)
|
|
||||||
{
|
|
||||||
m_isResource = true;
|
|
||||||
|
|
||||||
HRSRC hresInfo;
|
|
||||||
hresInfo = ::FindResource((HMODULE) wxhInstance, fileName, wxT("WAVE"));
|
|
||||||
if (!hresInfo)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
HGLOBAL waveData = ::LoadResource((HMODULE) wxhInstance, hresInfo);
|
|
||||||
|
|
||||||
if (waveData)
|
|
||||||
{
|
{
|
||||||
m_waveData= (wxByte*)::LockResource(waveData);
|
delete m_data;
|
||||||
m_waveLength = (int) ::SizeofResource((HMODULE) wxhInstance, hresInfo);
|
m_data = NULL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (m_waveData ? true : false);
|
bool wxSound::CheckCreatedOk()
|
||||||
}
|
{
|
||||||
else
|
if ( m_data && !m_data->IsOk() )
|
||||||
{
|
Free();
|
||||||
m_isResource = false;
|
|
||||||
|
|
||||||
wxFile fileWave;
|
return m_data != NULL;
|
||||||
if (!fileWave.Open(fileName, wxFile::read))
|
}
|
||||||
return false;
|
|
||||||
|
|
||||||
m_waveLength = (int) fileWave.Length();
|
bool wxSound::Create(const wxString& filename, bool isResource)
|
||||||
|
{
|
||||||
|
Free();
|
||||||
|
|
||||||
m_waveData = (wxByte*)GlobalLock(GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength));
|
m_data = new wxSoundDataFile(filename, isResource);
|
||||||
if (!m_waveData)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
fileWave.Read(m_waveData, m_waveLength);
|
return CheckCreatedOk();
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxSound::Create(int size, const wxByte* data)
|
bool wxSound::Create(int size, const wxByte* data)
|
||||||
{
|
{
|
||||||
Free();
|
Free();
|
||||||
m_isResource = true;
|
|
||||||
m_waveLength=size;
|
|
||||||
m_waveData = (wxByte*)GlobalLock(GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength));
|
|
||||||
if (!m_waveData)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
for (int i=0; i<size; i++) m_waveData[i] = data[i];
|
m_data = new wxSoundDataMemory(size, data);
|
||||||
return true;
|
|
||||||
|
return CheckCreatedOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxSound::DoPlay(unsigned flags) const
|
bool wxSound::DoPlay(unsigned flags) const
|
||||||
{
|
{
|
||||||
if (!IsOk())
|
if ( !IsOk() || !m_data->IsOk() )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return (::PlaySound((LPCTSTR)m_waveData, NULL,
|
DWORD flagsMSW = m_data->GetSoundFlag();
|
||||||
SND_MEMORY | SND_NODEFAULT |
|
HMODULE hmod = flagsMSW == SND_RESOURCE ? wxGetInstance() : NULL;
|
||||||
((flags & wxSOUND_ASYNC) ? SND_ASYNC : SND_SYNC) |
|
|
||||||
((flags & wxSOUND_LOOP) ? (SND_LOOP | SND_ASYNC) : 0))
|
|
||||||
!= 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool wxSound::Free()
|
// we don't want replacement default sound
|
||||||
{
|
flagsMSW |= SND_NODEFAULT;
|
||||||
if (m_waveData)
|
|
||||||
|
// NB: wxSOUND_SYNC is 0, don't test for it
|
||||||
|
flagsMSW |= (flags & wxSOUND_ASYNC) ? SND_ASYNC : SND_SYNC;
|
||||||
|
if ( flags & wxSOUND_LOOP )
|
||||||
{
|
{
|
||||||
#ifdef __WXWINCE__
|
// looping only works with async flag
|
||||||
HGLOBAL waveData = (HGLOBAL) m_waveData;
|
flagsMSW |= SND_LOOP | SND_ASYNC;
|
||||||
#else
|
|
||||||
HGLOBAL waveData = GlobalHandle(m_waveData);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (waveData)
|
|
||||||
{
|
|
||||||
#ifndef __WXWINCE__
|
|
||||||
if (m_isResource)
|
|
||||||
::FreeResource(waveData);
|
|
||||||
else
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
GlobalUnlock(waveData);
|
|
||||||
GlobalFree(waveData);
|
|
||||||
}
|
|
||||||
|
|
||||||
m_waveData = NULL;
|
|
||||||
m_waveLength = 0;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
|
return ::PlaySound(m_data->GetSoundData(), hmod, flagsMSW) != FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*static*/ void wxSound::Stop()
|
/* static */
|
||||||
|
void wxSound::Stop()
|
||||||
{
|
{
|
||||||
::PlaySound(NULL, NULL, 0);
|
::PlaySound(NULL, NULL, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // wxUSE_SOUND
|
#endif // wxUSE_SOUND
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user