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