]> git.saurik.com Git - wxWidgets.git/blame - src/msw/sound.cpp
adding implementation for osx
[wxWidgets.git] / src / msw / sound.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/msw/sound.cpp
315ebf68 3// Purpose: wxSound
2bda0e17 4// Author: Julian Smart
ddc5c471 5// Modified by: 2005-07-29: Vadim Zeitlin: redesign
2bda0e17
KB
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
ddc5c471
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#if defined(__BORLANDC__)
ddc5c471 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
315ebf68 27#if wxUSE_SOUND
1e6feb95 28
315ebf68 29#include "wx/sound.h"
ee4f8c2a 30#include "wx/msw/private.h"
2bda0e17 31
2cef71bc 32#include <mmsystem.h>
2bda0e17 33
ddc5c471
VZ
34// ----------------------------------------------------------------------------
35// wxSoundData
36// ----------------------------------------------------------------------------
37
38// ABC for different sound data representations
39class wxSoundData
2bda0e17 40{
ddc5c471
VZ
41public:
42 wxSoundData() { }
2bda0e17 43
ddc5c471
VZ
44 // return true if we had been successfully initialized
45 virtual bool IsOk() const = 0;
46
47 // get the flag corresponding to our content for PlaySound()
48 virtual DWORD GetSoundFlag() const = 0;
49
50 // get the data to be passed to PlaySound()
51 virtual LPCTSTR GetSoundData() const = 0;
52
53 virtual ~wxSoundData() { }
54};
55
56// class for in-memory sound data
57class wxSoundDataMemory : public wxSoundData
2bda0e17 58{
ddc5c471
VZ
59public:
60 // we copy the data
61 wxSoundDataMemory(int size, const wxByte *buf);
62
63 void *GetPtr() const { return m_waveDataPtr; }
64
65 virtual bool IsOk() const { return GetPtr() != NULL; }
66 virtual DWORD GetSoundFlag() const { return SND_MEMORY; }
67 virtual LPCTSTR GetSoundData() const { return (LPCTSTR)GetPtr(); }
2bda0e17 68
ddc5c471
VZ
69private:
70 GlobalPtr m_waveData;
71 GlobalPtrLock m_waveDataPtr;
72
c0c133e1 73 wxDECLARE_NO_COPY_CLASS(wxSoundDataMemory);
ddc5c471
VZ
74};
75
76// class for sound files and resources
77class wxSoundDataFile : public wxSoundData
78{
79public:
80 wxSoundDataFile(const wxString& filename, bool isResource);
81
82 virtual bool IsOk() const { return !m_name.empty(); }
83 virtual DWORD GetSoundFlag() const
84 {
85 return m_isResource ? SND_RESOURCE : SND_FILENAME;
86 }
87 virtual LPCTSTR GetSoundData() const { return m_name.c_str(); }
88
89private:
90 const wxString m_name;
91 const bool m_isResource;
92
c0c133e1 93 wxDECLARE_NO_COPY_CLASS(wxSoundDataFile);
ddc5c471
VZ
94};
95
96// ============================================================================
97// implementation
98// ============================================================================
99
100// ----------------------------------------------------------------------------
101// wxSoundData-derived classes
102// ----------------------------------------------------------------------------
103
104wxSoundDataMemory::wxSoundDataMemory(int size, const wxByte *buf)
105 : m_waveData(size),
106 m_waveDataPtr(m_waveData)
321db4b6 107{
ddc5c471
VZ
108 if ( IsOk() )
109 ::CopyMemory(m_waveDataPtr, buf, size);
321db4b6 110}
2bda0e17 111
ddc5c471
VZ
112wxSoundDataFile::wxSoundDataFile(const wxString& filename, bool isResource)
113 : m_name(filename),
114 m_isResource(isResource)
2bda0e17 115{
ddc5c471 116 // check for file/resource existence?
2bda0e17
KB
117}
118
ddc5c471
VZ
119// ----------------------------------------------------------------------------
120// wxSound
121// ----------------------------------------------------------------------------
122
123wxSound::wxSound()
2bda0e17 124{
ddc5c471
VZ
125 Init();
126}
2bda0e17 127
ddc5c471
VZ
128wxSound::wxSound(const wxString& filename, bool isResource)
129{
130 Init();
131 Create(filename, isResource);
132}
2bda0e17 133
ddc5c471
VZ
134wxSound::wxSound(int size, const wxByte *data)
135{
136 Init();
137 Create(size, data);
138}
2bda0e17 139
ddc5c471
VZ
140wxSound::~wxSound()
141{
142 Free();
143}
2bda0e17 144
ddc5c471
VZ
145void wxSound::Free()
146{
5276b0a5 147 wxDELETE(m_data);
ddc5c471 148}
2bda0e17 149
ddc5c471
VZ
150bool wxSound::CheckCreatedOk()
151{
152 if ( m_data && !m_data->IsOk() )
153 Free();
2bda0e17 154
ddc5c471
VZ
155 return m_data != NULL;
156}
2bda0e17 157
ddc5c471
VZ
158bool wxSound::Create(const wxString& filename, bool isResource)
159{
160 Free();
2bda0e17 161
ddc5c471 162 m_data = new wxSoundDataFile(filename, isResource);
2bda0e17 163
ddc5c471 164 return CheckCreatedOk();
2bda0e17
KB
165}
166
315ebf68 167bool wxSound::Create(int size, const wxByte* data)
321db4b6 168{
ddc5c471
VZ
169 Free();
170
171 m_data = new wxSoundDataMemory(size, data);
172
173 return CheckCreatedOk();
321db4b6
JS
174}
175
315ebf68 176bool wxSound::DoPlay(unsigned flags) const
2bda0e17 177{
ddc5c471
VZ
178 if ( !IsOk() || !m_data->IsOk() )
179 return false;
2bda0e17 180
ddc5c471
VZ
181 DWORD flagsMSW = m_data->GetSoundFlag();
182 HMODULE hmod = flagsMSW == SND_RESOURCE ? wxGetInstance() : NULL;
2bda0e17 183
ddc5c471
VZ
184 // we don't want replacement default sound
185 flagsMSW |= SND_NODEFAULT;
186
187 // NB: wxSOUND_SYNC is 0, don't test for it
188 flagsMSW |= (flags & wxSOUND_ASYNC) ? SND_ASYNC : SND_SYNC;
189 if ( flags & wxSOUND_LOOP )
190 {
191 // looping only works with async flag
192 flagsMSW |= SND_LOOP | SND_ASYNC;
2bda0e17 193 }
ddc5c471
VZ
194
195 return ::PlaySound(m_data->GetSoundData(), hmod, flagsMSW) != FALSE;
315ebf68
VS
196}
197
ddc5c471
VZ
198/* static */
199void wxSound::Stop()
315ebf68
VS
200{
201 ::PlaySound(NULL, NULL, 0);
2bda0e17
KB
202}
203
315ebf68 204#endif // wxUSE_SOUND
ddc5c471 205