]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/wave.cpp
no message
[wxWidgets.git] / src / msw / wave.cpp
... / ...
CommitLineData
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#ifndef WX_PRECOMP
24#include "wx/wx.h"
25#endif
26
27#include "wx/file.h"
28#include "wx/msw/wave.h"
29#include "wx/msw/private.h"
30
31#include <windows.h>
32#include <windowsx.h>
33
34#ifndef __GNUWIN32__
35#include <mmsystem.h>
36#endif
37
38#ifdef __GNUWIN32__
39#include "wx/msw/gnuwin32/extra.h"
40#endif
41
42wxWave::wxWave()
43 : m_waveLength(0), m_isResource(FALSE), m_waveData(NULL)
44{
45}
46
47wxWave::wxWave(const wxString& sFileName, bool isResource)
48 : m_waveLength(0), m_isResource(isResource), m_waveData(NULL)
49{
50 Create(sFileName, isResource);
51}
52
53
54wxWave::~wxWave()
55{
56 Free();
57}
58
59bool wxWave::Create(const wxString& fileName, bool isResource)
60{
61 Free();
62
63 if (isResource)
64 {
65 m_isResource = TRUE;
66
67 HRSRC hresInfo;
68#ifdef __WIN32__
69 hresInfo = ::FindResourceA((HMODULE) wxhInstance, fileName, "WAVE");
70#else
71 hresInfo = ::FindResource((HMODULE) wxhInstance, fileName, "WAVE");
72#endif
73 if (!hresInfo)
74 return FALSE;
75
76 HGLOBAL waveData = ::LoadResource((HMODULE) wxhInstance, hresInfo);
77
78 if (waveData)
79 {
80 m_waveData= (byte*)::LockResource(waveData);
81 m_waveLength = (int) ::SizeofResource((HMODULE) wxhInstance, hresInfo);
82 }
83
84 return (m_waveData ? TRUE : FALSE);
85 }
86 else
87 {
88 m_isResource = FALSE;
89
90 wxFile fileWave;
91 if (!fileWave.Open(fileName, wxFile::read))
92 return FALSE;
93
94 m_waveLength = (int) fileWave.Length();
95
96 m_waveData = (byte*)::GlobalLock(::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, m_waveLength));
97 if (!m_waveData)
98 return FALSE;
99
100 fileWave.Read(m_waveData, m_waveLength);
101
102 return TRUE;
103 }
104}
105
106bool wxWave::Play(bool async, bool looped) const
107{
108 if (!IsOk())
109 return FALSE;
110
111#ifdef __WIN32__
112 return ( ::PlaySound((LPCSTR)m_waveData, NULL, SND_MEMORY |
113 SND_NODEFAULT | (async ? SND_ASYNC : SND_SYNC) | (looped ? (SND_LOOP | SND_ASYNC) : 0)) != 0 );
114#else
115 return ( ::sndPlaySound((LPCSTR)m_waveData, SND_MEMORY |
116 SND_NODEFAULT | (async ? SND_ASYNC : SND_SYNC) | (looped ? (SND_LOOP | SND_ASYNC) : 0)) != 0 );
117#endif
118}
119
120bool wxWave::Free()
121{
122 if (m_waveData)
123 {
124#ifdef __WIN32__
125 HGLOBAL waveData = ::GlobalHandle(m_waveData);
126#else
127 HGLOBAL waveData = GlobalPtrHandle(m_waveData);
128#endif
129
130 if (waveData)
131 {
132 if (m_isResource)
133 ::FreeResource(waveData);
134 else
135 {
136 ::GlobalUnlock(waveData);
137 ::GlobalFree(waveData);
138 }
139
140 m_waveData = NULL;
141 m_waveLength = 0;
142 return TRUE;
143 }
144 }
145 return FALSE;
146}
147
148