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