]> git.saurik.com Git - wxWidgets.git/blame - src/msw/wave.cpp
Added rules to build the regex library from the main makefile, if
[wxWidgets.git] / src / msw / wave.cpp
CommitLineData
2bda0e17
KB
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
c42404a5 9// Licence: wxWindows license
2bda0e17
KB
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
1e6feb95
VZ
23#if wxUSE_WAVE
24
2bda0e17 25#ifndef WX_PRECOMP
ee4f8c2a 26#include "wx/wx.h"
2bda0e17
KB
27#endif
28
ee4f8c2a
JS
29#include "wx/file.h"
30#include "wx/msw/wave.h"
31#include "wx/msw/private.h"
2bda0e17
KB
32
33#include <windows.h>
34#include <windowsx.h>
35
ae090fdb 36#if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__)
c42404a5
VZ
37 #include "wx/msw/gnuwin32/extra.h"
38#else
39 #include <mmsystem.h>
65fd5cb0 40#endif
2bda0e17 41
ee4f8c2a 42wxWave::wxWave()
0f358732 43 : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE)
2bda0e17
KB
44{
45}
46
47wxWave::wxWave(const wxString& sFileName, bool isResource)
0f358732 48 : m_waveData(NULL), m_waveLength(0), m_isResource(isResource)
2bda0e17
KB
49{
50 Create(sFileName, isResource);
51}
52
57c208c5 53wxWave::wxWave(int size, const wxByte* data)
0f358732 54 : m_waveData(NULL), m_waveLength(0), m_isResource(FALSE)
321db4b6
JS
55{
56 Create(size, data);
57}
2bda0e17 58
ee4f8c2a 59wxWave::~wxWave()
2bda0e17
KB
60{
61 Free();
62}
63
64bool wxWave::Create(const wxString& fileName, bool isResource)
65{
66 Free();
67
68 if (isResource)
69 {
70 m_isResource = TRUE;
71
72 HRSRC hresInfo;
57c208c5 73#if defined(__WIN32__) && !defined(__TWIN32__)
837e5743 74#ifdef _UNICODE
223d09f6 75 hresInfo = ::FindResourceW((HMODULE) wxhInstance, fileName, wxT("WAVE"));
2bda0e17 76#else
223d09f6 77 hresInfo = ::FindResourceA((HMODULE) wxhInstance, fileName, wxT("WAVE"));
837e5743
OK
78#endif
79#else
223d09f6 80 hresInfo = ::FindResource((HMODULE) wxhInstance, fileName, wxT("WAVE"));
2bda0e17
KB
81#endif
82 if (!hresInfo)
83 return FALSE;
84
85 HGLOBAL waveData = ::LoadResource((HMODULE) wxhInstance, hresInfo);
86
87 if (waveData)
88 {
57c208c5 89 m_waveData= (wxByte*)::LockResource(waveData);
2bda0e17
KB
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
57c208c5 105 m_waveData = (wxByte*)::GlobalLock(::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength));
2bda0e17
KB
106 if (!m_waveData)
107 return FALSE;
108
109 fileWave.Read(m_waveData, m_waveLength);
110
111 return TRUE;
112 }
113}
114
57c208c5 115bool wxWave::Create(int size, const wxByte* data)
321db4b6
JS
116{
117 Free();
118 m_isResource = FALSE;
119 m_waveLength=size;
57c208c5 120 m_waveData = (wxByte*)::GlobalLock(::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength));
321db4b6
JS
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
2bda0e17
KB
128bool wxWave::Play(bool async, bool looped) const
129{
130 if (!IsOk())
131 return FALSE;
132
133#ifdef __WIN32__
837e5743 134 return ( ::PlaySound((LPCTSTR)m_waveData, NULL, SND_MEMORY |
2bda0e17
KB
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
ee4f8c2a 142bool wxWave::Free()
2bda0e17
KB
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
1e6feb95 170#endif // wxUSE_WAVE