added wxWave sample
[wxWidgets.git] / samples / sound / sound.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sound.cpp
3 // Purpose: Example of sound playing in wxWindows
4 // Author: Vaclav Slavik
5 // Modified by:
6 // Created: 2004/01/29
7 // RCS-ID: $Id$
8 // Copyright: (c) 2004 Vaclav Salvik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 // for all others, include the necessary headers (this file is usually all you
19 // need because it includes almost all "standard" wxWindows headers)
20 #ifndef WX_PRECOMP
21 #include "wx/app.h"
22 #include "wx/frame.h"
23 #include "wx/menu.h"
24 #include "wx/msgdlg.h"
25 #endif
26
27 #include "wx/wave.h"
28
29 // ----------------------------------------------------------------------------
30 // resources
31 // ----------------------------------------------------------------------------
32
33 // the application icon (under Windows and OS/2 it is in resources)
34 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
35 #include "../sample.xpm"
36 #endif
37
38 #define WAV_FILE _T("doggrowl.wav")
39
40 // ----------------------------------------------------------------------------
41 // private classes
42 // ----------------------------------------------------------------------------
43
44 class MyApp : public wxApp
45 {
46 public:
47 virtual bool OnInit();
48 };
49
50
51 class MyFrame : public wxFrame
52 {
53 public:
54 // ctor(s)
55 MyFrame(const wxString& title);
56 ~MyFrame() { delete m_sound; }
57
58 // event handlers (these functions should _not_ be virtual)
59 void OnPlaySync(wxCommandEvent& event);
60 void OnPlayAsync(wxCommandEvent& event);
61 void OnPlayAsyncOnStack(wxCommandEvent& event);
62 void OnPlayLoop(wxCommandEvent& event);
63
64 void OnQuit(wxCommandEvent& event);
65 void OnAbout(wxCommandEvent& event);
66
67 private:
68 wxWave *m_sound;
69
70 // any class wishing to process wxWindows events must use this macro
71 DECLARE_EVENT_TABLE()
72 };
73
74 // ----------------------------------------------------------------------------
75 // constants
76 // ----------------------------------------------------------------------------
77
78 // IDs for the controls and the menu commands
79 enum
80 {
81 // menu items
82 Sound_Quit = wxID_EXIT,
83 Sound_About = wxID_ABOUT,
84 Sound_PlaySync = wxID_HIGHEST + 1,
85 Sound_PlayAsync,
86 Sound_PlayAsyncOnStack,
87 Sound_PlayLoop
88 };
89
90 // ----------------------------------------------------------------------------
91 // event tables and other macros for wxWindows
92 // ----------------------------------------------------------------------------
93
94 // the event tables connect the wxWindows events with the functions (event
95 // handlers) which process them. It can be also done at run-time, but for the
96 // simple menu events like this the static method is much simpler.
97 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
98 EVT_MENU(Sound_Quit, MyFrame::OnQuit)
99 EVT_MENU(Sound_About, MyFrame::OnAbout)
100 EVT_MENU(Sound_PlaySync, MyFrame::OnPlaySync)
101 EVT_MENU(Sound_PlayAsync, MyFrame::OnPlayAsync)
102 EVT_MENU(Sound_PlayAsyncOnStack, MyFrame::OnPlayAsync)
103 EVT_MENU(Sound_PlayLoop, MyFrame::OnPlayLoop)
104 END_EVENT_TABLE()
105
106 // Create a new application object: this macro will allow wxWindows to create
107 // the application object during program execution (it's better than using a
108 // static object for many reasons) and also implements the accessor function
109 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
110 // not wxApp)
111 IMPLEMENT_APP(MyApp)
112
113 // ============================================================================
114 // implementation
115 // ============================================================================
116
117 // ----------------------------------------------------------------------------
118 // the application class
119 // ----------------------------------------------------------------------------
120
121 // 'Main program' equivalent: the program execution "starts" here
122 bool MyApp::OnInit()
123 {
124 // create the main application window
125 MyFrame *frame = new MyFrame(_T("wxWindows Sound Sample"));
126
127 // and show it (the frames, unlike simple controls, are not shown when
128 // created initially)
129 frame->Show(true);
130
131 // success: wxApp::OnRun() will be called which will enter the main message
132 // loop and the application will run. If we returned false here, the
133 // application would exit immediately.
134 return true;
135 }
136
137 // ----------------------------------------------------------------------------
138 // main frame
139 // ----------------------------------------------------------------------------
140
141 // frame constructor
142 MyFrame::MyFrame(const wxString& title)
143 : wxFrame(NULL, wxID_ANY, title)
144 {
145 m_sound = NULL;
146
147 // set the frame icon
148 SetIcon(wxICON(sample));
149
150 wxMenu *menuFile = new wxMenu;
151 wxMenu *helpMenu = new wxMenu;
152 wxMenu *playMenu = new wxMenu;
153 helpMenu->Append(Sound_About, _T("&About...\tF1"), _T("Show about dialog"));
154 menuFile->Append(Sound_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
155 playMenu->Append(Sound_PlaySync, _T("Play sound &synchronously"));
156 playMenu->Append(Sound_PlayAsync, _T("Play sound &asynchronously"));
157 playMenu->Append(Sound_PlayAsync, _T("Play sound asynchronously (&object on stack)"));
158 playMenu->Append(Sound_PlayLoop, _T("&Loop sound"));
159
160 // now append the freshly created menu to the menu bar...
161 wxMenuBar *menuBar = new wxMenuBar();
162 menuBar->Append(menuFile, _T("&File"));
163 menuBar->Append(playMenu, _T("&Play"));
164 menuBar->Append(helpMenu, _T("&Help"));
165
166 // ... and attach this menu bar to the frame
167 SetMenuBar(menuBar);
168 }
169
170
171 // event handlers
172
173 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
174 {
175 // true is to force the frame to close
176 Close(true);
177 }
178
179 void MyFrame::OnPlaySync(wxCommandEvent& WXUNUSED(event))
180 {
181 wxBusyCursor busy;
182 if (!m_sound)
183 m_sound = new wxWave(WAV_FILE);
184 if (m_sound->IsOk())
185 m_sound->Play(false/*async*/);
186 }
187
188 void MyFrame::OnPlayAsync(wxCommandEvent& WXUNUSED(event))
189 {
190 wxBusyCursor busy;
191 if (!m_sound)
192 m_sound = new wxWave(WAV_FILE);
193 if (m_sound->IsOk())
194 m_sound->Play(true/*async*/);
195 }
196
197 void MyFrame::OnPlayAsyncOnStack(wxCommandEvent& WXUNUSED(event))
198 {
199 wxBusyCursor busy;
200 wxWave snd(WAV_FILE);
201 if (snd.IsOk())
202 snd.Play(true/*async*/);
203 }
204
205 void MyFrame::OnPlayLoop(wxCommandEvent& WXUNUSED(event))
206 {
207 wxBusyCursor busy;
208 if (!m_sound)
209 m_sound = new wxWave(WAV_FILE);
210 if (m_sound->IsOk())
211 m_sound->Play(true/*async*/, true/*loop*/);
212 }
213
214 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
215 {
216 wxString msg;
217 msg.Printf( _T("This is the About dialog of the sound sample.\n")
218 _T("Welcome to %s"), wxVERSION_STRING);
219
220 wxMessageBox(msg, _T("About"), wxOK | wxICON_INFORMATION, this);
221 }