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