| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wave.cpp |
| 3 | // Purpose: wxWave |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart and Markus Holzem |
| 9 | // Licence: wxWindows license |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "wave.h" |
| 14 | #endif |
| 15 | |
| 16 | // For compilers that support precompilation, includes "wx.h". |
| 17 | #include "wx/wxprec.h" |
| 18 | |
| 19 | #if defined(__BORLANDC__) |
| 20 | #pragma hdrstop |
| 21 | #endif |
| 22 | |
| 23 | #if wxUSE_WAVE |
| 24 | |
| 25 | #ifndef WX_PRECOMP |
| 26 | #include "wx/wx.h" |
| 27 | #endif |
| 28 | |
| 29 | #include "wx/file.h" |
| 30 | #include "wx/msw/wave.h" |
| 31 | #include "wx/msw/private.h" |
| 32 | |
| 33 | #include <windows.h> |
| 34 | #include <windowsx.h> |
| 35 | |
| 36 | #if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__) |
| 37 | #include "wx/msw/gnuwin32/extra.h" |
| 38 | #else |
| 39 | #include <mmsystem.h> |
| 40 | #endif |
| 41 | |
| 42 | wxWave::wxWave() |
| 43 | : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | wxWave::wxWave(const wxString& sFileName, bool isResource) |
| 48 | : m_waveData(NULL), m_waveLength(0), m_isResource(isResource) |
| 49 | { |
| 50 | Create(sFileName, isResource); |
| 51 | } |
| 52 | |
| 53 | wxWave::wxWave(int size, const wxByte* data) |
| 54 | : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE) |
| 55 | { |
| 56 | Create(size, data); |
| 57 | } |
| 58 | |
| 59 | wxWave::~wxWave() |
| 60 | { |
| 61 | Free(); |
| 62 | } |
| 63 | |
| 64 | bool wxWave::Create(const wxString& fileName, bool isResource) |
| 65 | { |
| 66 | Free(); |
| 67 | |
| 68 | if (isResource) |
| 69 | { |
| 70 | m_isResource = TRUE; |
| 71 | |
| 72 | HRSRC hresInfo; |
| 73 | #if defined(__WIN32__) && !defined(__TWIN32__) |
| 74 | #ifdef _UNICODE |
| 75 | hresInfo = ::FindResourceW((HMODULE) wxhInstance, fileName, wxT("WAVE")); |
| 76 | #else |
| 77 | hresInfo = ::FindResourceA((HMODULE) wxhInstance, fileName, wxT("WAVE")); |
| 78 | #endif |
| 79 | #else |
| 80 | hresInfo = ::FindResource((HMODULE) wxhInstance, fileName, wxT("WAVE")); |
| 81 | #endif |
| 82 | if (!hresInfo) |
| 83 | return FALSE; |
| 84 | |
| 85 | HGLOBAL waveData = ::LoadResource((HMODULE) wxhInstance, hresInfo); |
| 86 | |
| 87 | if (waveData) |
| 88 | { |
| 89 | m_waveData= (wxByte*)::LockResource(waveData); |
| 90 | m_waveLength = (int) ::SizeofResource((HMODULE) wxhInstance, hresInfo); |
| 91 | } |
| 92 | |
| 93 | return (m_waveData ? TRUE : FALSE); |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | m_isResource = FALSE; |
| 98 | |
| 99 | wxFile fileWave; |
| 100 | if (!fileWave.Open(fileName, wxFile::read)) |
| 101 | return FALSE; |
| 102 | |
| 103 | m_waveLength = (int) fileWave.Length(); |
| 104 | |
| 105 | m_waveData = (wxByte*)::GlobalLock(::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength)); |
| 106 | if (!m_waveData) |
| 107 | return FALSE; |
| 108 | |
| 109 | fileWave.Read(m_waveData, m_waveLength); |
| 110 | |
| 111 | return TRUE; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | bool wxWave::Create(int size, const wxByte* data) |
| 116 | { |
| 117 | Free(); |
| 118 | m_isResource = FALSE; |
| 119 | m_waveLength=size; |
| 120 | m_waveData = (wxByte*)::GlobalLock(::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength)); |
| 121 | if (!m_waveData) |
| 122 | return FALSE; |
| 123 | |
| 124 | for (int i=0; i<size; i++) m_waveData[i] = data[i]; |
| 125 | return TRUE; |
| 126 | } |
| 127 | |
| 128 | bool wxWave::Play(bool async, bool looped) const |
| 129 | { |
| 130 | if (!IsOk()) |
| 131 | return FALSE; |
| 132 | |
| 133 | #ifdef __WIN32__ |
| 134 | return ( ::PlaySound((LPCTSTR)m_waveData, NULL, SND_MEMORY | |
| 135 | SND_NODEFAULT | (async ? SND_ASYNC : SND_SYNC) | (looped ? (SND_LOOP | SND_ASYNC) : 0)) != 0 ); |
| 136 | #else |
| 137 | return ( ::sndPlaySound((LPCSTR)m_waveData, SND_MEMORY | |
| 138 | SND_NODEFAULT | (async ? SND_ASYNC : SND_SYNC) | (looped ? (SND_LOOP | SND_ASYNC) : 0)) != 0 ); |
| 139 | #endif |
| 140 | } |
| 141 | |
| 142 | bool wxWave::Free() |
| 143 | { |
| 144 | if (m_waveData) |
| 145 | { |
| 146 | #ifdef __WIN32__ |
| 147 | HGLOBAL waveData = ::GlobalHandle(m_waveData); |
| 148 | #else |
| 149 | HGLOBAL waveData = GlobalPtrHandle(m_waveData); |
| 150 | #endif |
| 151 | |
| 152 | if (waveData) |
| 153 | { |
| 154 | if (m_isResource) |
| 155 | ::FreeResource(waveData); |
| 156 | else |
| 157 | { |
| 158 | ::GlobalUnlock(waveData); |
| 159 | ::GlobalFree(waveData); |
| 160 | } |
| 161 | |
| 162 | m_waveData = NULL; |
| 163 | m_waveLength = 0; |
| 164 | return TRUE; |
| 165 | } |
| 166 | } |
| 167 | return FALSE; |
| 168 | } |
| 169 | |
| 170 | #endif // wxUSE_WAVE |