]> git.saurik.com Git - wxWidgets.git/blob - src/msw/sound.cpp
Added licence/copyright information
[wxWidgets.git] / src / msw / sound.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sound.cpp
3 // Purpose: wxSound
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "sound.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_SOUND
24
25 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #endif
28
29 #include "wx/file.h"
30 #include "wx/sound.h"
31 #include "wx/msw/private.h"
32
33 #include <windowsx.h>
34
35 #include <mmsystem.h>
36
37 wxSound::wxSound()
38 : m_waveData(NULL), m_waveLength(0), m_isResource(false)
39 {
40 }
41
42 wxSound::wxSound(const wxString& sFileName, bool isResource)
43 : m_waveData(NULL), m_waveLength(0), m_isResource(isResource)
44 {
45 Create(sFileName, isResource);
46 }
47
48 wxSound::wxSound(int size, const wxByte* data)
49 : m_waveData(NULL), m_waveLength(0), m_isResource(false)
50 {
51 Create(size, data);
52 }
53
54 wxSound::~wxSound()
55 {
56 Free();
57 }
58
59 bool wxSound::Create(const wxString& fileName, bool isResource)
60 {
61 Free();
62
63 if (isResource)
64 {
65 m_isResource = true;
66
67 HRSRC hresInfo;
68 hresInfo = ::FindResource((HMODULE) wxhInstance, fileName, wxT("WAVE"));
69 if (!hresInfo)
70 return false;
71
72 HGLOBAL waveData = ::LoadResource((HMODULE) wxhInstance, hresInfo);
73
74 if (waveData)
75 {
76 m_waveData= (wxByte*)::LockResource(waveData);
77 m_waveLength = (int) ::SizeofResource((HMODULE) wxhInstance, hresInfo);
78 }
79
80 return (m_waveData ? true : false);
81 }
82 else
83 {
84 m_isResource = false;
85
86 wxFile fileWave;
87 if (!fileWave.Open(fileName, wxFile::read))
88 return false;
89
90 m_waveLength = (int) fileWave.Length();
91
92 m_waveData = (wxByte*)GlobalLock(GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength));
93 if (!m_waveData)
94 return false;
95
96 fileWave.Read(m_waveData, m_waveLength);
97
98 return true;
99 }
100 }
101
102 bool wxSound::Create(int size, const wxByte* data)
103 {
104 Free();
105 m_isResource = true;
106 m_waveLength=size;
107 m_waveData = (wxByte*)GlobalLock(GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength));
108 if (!m_waveData)
109 return false;
110
111 for (int i=0; i<size; i++) m_waveData[i] = data[i];
112 return true;
113 }
114
115 bool wxSound::DoPlay(unsigned flags) const
116 {
117 if (!IsOk())
118 return false;
119
120 return (::PlaySound((LPCTSTR)m_waveData, NULL,
121 SND_MEMORY | SND_NODEFAULT |
122 ((flags & wxSOUND_ASYNC) ? SND_ASYNC : SND_SYNC) |
123 ((flags & wxSOUND_LOOP) ? (SND_LOOP | SND_ASYNC) : 0))
124 != 0);
125 }
126
127 bool wxSound::Free()
128 {
129 if (m_waveData)
130 {
131 #ifdef __WXWINCE__
132 HGLOBAL waveData = (HGLOBAL) m_waveData;
133 #else
134 HGLOBAL waveData = GlobalHandle(m_waveData);
135 #endif
136
137 if (waveData)
138 {
139 #ifndef __WXWINCE__
140 if (m_isResource)
141 ::FreeResource(waveData);
142 else
143 #endif
144 {
145 GlobalUnlock(waveData);
146 GlobalFree(waveData);
147 }
148
149 m_waveData = NULL;
150 m_waveLength = 0;
151 return true;
152 }
153 }
154 return false;
155 }
156
157 /*static*/ void wxSound::Stop()
158 {
159 ::PlaySound(NULL, NULL, 0);
160 }
161
162 #endif // wxUSE_SOUND