2004-02-01 05:53:59 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: sound.cpp
|
2004-05-25 07:20:37 -04:00
|
|
|
// Purpose: Example of sound playing in wxWidgets
|
2004-02-01 05:53:59 -05:00
|
|
|
// Author: Vaclav Slavik
|
|
|
|
// Modified by:
|
|
|
|
// Created: 2004/01/29
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2004 Vaclav Salvik
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// for all others, include the necessary headers (this file is usually all you
|
2004-05-25 07:20:37 -04:00
|
|
|
// need because it includes almost all "standard" wxWidgets headers)
|
2004-02-01 05:53:59 -05:00
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/frame.h"
|
|
|
|
#include "wx/menu.h"
|
|
|
|
#include "wx/msgdlg.h"
|
2004-02-29 10:21:11 -05:00
|
|
|
#include "wx/icon.h"
|
2004-03-11 14:31:33 -05:00
|
|
|
#include "wx/textctrl.h"
|
|
|
|
#include "wx/filedlg.h"
|
2004-02-01 05:53:59 -05:00
|
|
|
#endif
|
|
|
|
|
2004-02-02 08:03:23 -05:00
|
|
|
#include "wx/sound.h"
|
2004-02-29 10:21:11 -05:00
|
|
|
#include "wx/numdlg.h"
|
2004-02-01 05:53:59 -05:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// resources
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// the application icon (under Windows and OS/2 it is in resources)
|
|
|
|
#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
|
|
|
|
#include "../sample.xpm"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define WAV_FILE _T("doggrowl.wav")
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// private classes
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool OnInit();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class MyFrame : public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctor(s)
|
|
|
|
MyFrame(const wxString& title);
|
|
|
|
~MyFrame() { delete m_sound; }
|
|
|
|
|
|
|
|
// event handlers (these functions should _not_ be virtual)
|
|
|
|
void OnPlaySync(wxCommandEvent& event);
|
|
|
|
void OnPlayAsync(wxCommandEvent& event);
|
|
|
|
void OnPlayAsyncOnStack(wxCommandEvent& event);
|
|
|
|
void OnPlayLoop(wxCommandEvent& event);
|
2004-03-11 14:31:33 -05:00
|
|
|
|
|
|
|
void OnSelectFile(wxCommandEvent& event);
|
2004-02-01 05:53:59 -05:00
|
|
|
void OnQuit(wxCommandEvent& event);
|
|
|
|
void OnAbout(wxCommandEvent& event);
|
|
|
|
|
2004-03-11 14:31:33 -05:00
|
|
|
void NotifyUsingFile(const wxString& name);
|
|
|
|
|
|
|
|
|
2004-02-01 05:53:59 -05:00
|
|
|
private:
|
2004-03-11 14:31:33 -05:00
|
|
|
wxSound* m_sound;
|
|
|
|
wxString m_soundFile;
|
|
|
|
wxTextCtrl* m_tc;
|
2004-02-01 05:53:59 -05:00
|
|
|
|
2004-05-25 07:20:37 -04:00
|
|
|
// any class wishing to process wxWidgets events must use this macro
|
2004-02-01 05:53:59 -05:00
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// IDs for the controls and the menu commands
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
// menu items
|
|
|
|
Sound_Quit = wxID_EXIT,
|
|
|
|
Sound_About = wxID_ABOUT,
|
|
|
|
Sound_PlaySync = wxID_HIGHEST + 1,
|
|
|
|
Sound_PlayAsync,
|
|
|
|
Sound_PlayAsyncOnStack,
|
2004-03-11 14:31:33 -05:00
|
|
|
Sound_PlayLoop,
|
|
|
|
Sound_SelectFile
|
2004-02-01 05:53:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2004-05-25 07:20:37 -04:00
|
|
|
// event tables and other macros for wxWidgets
|
2004-02-01 05:53:59 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2004-05-25 07:20:37 -04:00
|
|
|
// the event tables connect the wxWidgets events with the functions (event
|
2004-02-01 05:53:59 -05:00
|
|
|
// handlers) which process them. It can be also done at run-time, but for the
|
|
|
|
// simple menu events like this the static method is much simpler.
|
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2004-03-11 14:31:33 -05:00
|
|
|
EVT_MENU(Sound_SelectFile, MyFrame::OnSelectFile)
|
2004-02-01 05:53:59 -05:00
|
|
|
EVT_MENU(Sound_Quit, MyFrame::OnQuit)
|
|
|
|
EVT_MENU(Sound_About, MyFrame::OnAbout)
|
|
|
|
EVT_MENU(Sound_PlaySync, MyFrame::OnPlaySync)
|
|
|
|
EVT_MENU(Sound_PlayAsync, MyFrame::OnPlayAsync)
|
2004-07-21 20:43:58 -04:00
|
|
|
EVT_MENU(Sound_PlayAsyncOnStack, MyFrame::OnPlayAsyncOnStack)
|
2004-02-01 05:53:59 -05:00
|
|
|
EVT_MENU(Sound_PlayLoop, MyFrame::OnPlayLoop)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
2004-05-25 07:20:37 -04:00
|
|
|
// Create a new application object: this macro will allow wxWidgets to create
|
2004-02-01 05:53:59 -05:00
|
|
|
// the application object during program execution (it's better than using a
|
|
|
|
// static object for many reasons) and also implements the accessor function
|
|
|
|
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
|
|
|
|
// not wxApp)
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// the application class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// 'Main program' equivalent: the program execution "starts" here
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
|
|
|
// create the main application window
|
2004-05-25 07:20:37 -04:00
|
|
|
MyFrame *frame = new MyFrame(_T("wxWidgets Sound Sample"));
|
2004-02-01 05:53:59 -05:00
|
|
|
|
|
|
|
// and show it (the frames, unlike simple controls, are not shown when
|
|
|
|
// created initially)
|
|
|
|
frame->Show(true);
|
|
|
|
|
|
|
|
// success: wxApp::OnRun() will be called which will enter the main message
|
|
|
|
// loop and the application will run. If we returned false here, the
|
|
|
|
// application would exit immediately.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// main frame
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// frame constructor
|
|
|
|
MyFrame::MyFrame(const wxString& title)
|
|
|
|
: wxFrame(NULL, wxID_ANY, title)
|
|
|
|
{
|
|
|
|
m_sound = NULL;
|
2004-03-11 14:31:33 -05:00
|
|
|
m_soundFile = WAV_FILE;
|
2004-02-01 05:53:59 -05:00
|
|
|
|
|
|
|
// set the frame icon
|
|
|
|
SetIcon(wxICON(sample));
|
|
|
|
|
|
|
|
wxMenu *menuFile = new wxMenu;
|
|
|
|
wxMenu *helpMenu = new wxMenu;
|
|
|
|
wxMenu *playMenu = new wxMenu;
|
|
|
|
helpMenu->Append(Sound_About, _T("&About...\tF1"), _T("Show about dialog"));
|
2004-03-11 14:31:33 -05:00
|
|
|
menuFile->Append(Sound_SelectFile, _T("&Select WAV file"), _T("Select a new wav file to play"));
|
2004-02-01 05:53:59 -05:00
|
|
|
menuFile->Append(Sound_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
|
|
|
|
playMenu->Append(Sound_PlaySync, _T("Play sound &synchronously"));
|
|
|
|
playMenu->Append(Sound_PlayAsync, _T("Play sound &asynchronously"));
|
2004-07-21 20:43:58 -04:00
|
|
|
playMenu->Append(Sound_PlayAsyncOnStack, _T("Play sound asynchronously (&object on stack)"));
|
2004-02-01 05:53:59 -05:00
|
|
|
playMenu->Append(Sound_PlayLoop, _T("&Loop sound"));
|
|
|
|
|
|
|
|
// now append the freshly created menu to the menu bar...
|
|
|
|
wxMenuBar *menuBar = new wxMenuBar();
|
|
|
|
menuBar->Append(menuFile, _T("&File"));
|
|
|
|
menuBar->Append(playMenu, _T("&Play"));
|
|
|
|
menuBar->Append(helpMenu, _T("&Help"));
|
|
|
|
|
|
|
|
// ... and attach this menu bar to the frame
|
|
|
|
SetMenuBar(menuBar);
|
2004-03-11 14:31:33 -05:00
|
|
|
|
2004-05-25 15:35:38 -04:00
|
|
|
m_tc = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
|
2004-03-18 12:24:06 -05:00
|
|
|
wxDefaultPosition, wxDefaultSize,
|
2004-03-11 14:31:33 -05:00
|
|
|
wxTE_MULTILINE|wxTE_READONLY);
|
|
|
|
NotifyUsingFile(m_soundFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MyFrame::NotifyUsingFile(const wxString& name)
|
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
msg << _T("Using sound file: ") << name << _T("\n");
|
|
|
|
m_tc->AppendText(msg);
|
2004-02-01 05:53:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// event handlers
|
|
|
|
|
2004-03-11 14:31:33 -05:00
|
|
|
|
|
|
|
void MyFrame::OnSelectFile(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxFileDialog dlg(this, _T("Choose a sound file"),
|
|
|
|
wxEmptyString, wxEmptyString,
|
|
|
|
_T("WAV files (*.wav)|*.wav"), wxOPEN|wxCHANGE_DIR);
|
|
|
|
if ( dlg.ShowModal() == wxID_OK )
|
|
|
|
{
|
|
|
|
m_soundFile = dlg.GetPath();
|
|
|
|
delete m_sound;
|
|
|
|
m_sound = NULL;
|
|
|
|
NotifyUsingFile(m_soundFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-01 05:53:59 -05:00
|
|
|
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
// true is to force the frame to close
|
|
|
|
Close(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnPlaySync(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxBusyCursor busy;
|
|
|
|
if (!m_sound)
|
2004-03-11 14:31:33 -05:00
|
|
|
m_sound = new wxSound(m_soundFile);
|
2004-02-01 05:53:59 -05:00
|
|
|
if (m_sound->IsOk())
|
2004-05-26 04:47:53 -04:00
|
|
|
m_sound->Play(wxSOUND_SYNC);
|
2004-02-01 05:53:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnPlayAsync(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxBusyCursor busy;
|
|
|
|
if (!m_sound)
|
2004-03-11 14:31:33 -05:00
|
|
|
m_sound = new wxSound(m_soundFile);
|
2004-02-01 05:53:59 -05:00
|
|
|
if (m_sound->IsOk())
|
2004-05-26 04:47:53 -04:00
|
|
|
m_sound->Play(wxSOUND_ASYNC);
|
2004-02-01 05:53:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnPlayAsyncOnStack(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxBusyCursor busy;
|
2004-03-11 14:31:33 -05:00
|
|
|
wxSound snd(m_soundFile);
|
2004-02-01 05:53:59 -05:00
|
|
|
if (snd.IsOk())
|
2004-05-26 04:47:53 -04:00
|
|
|
snd.Play(wxSOUND_ASYNC);
|
2004-02-01 05:53:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnPlayLoop(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxBusyCursor busy;
|
|
|
|
if (!m_sound)
|
2004-03-11 14:31:33 -05:00
|
|
|
m_sound = new wxSound(m_soundFile);
|
2004-02-01 05:53:59 -05:00
|
|
|
if (m_sound->IsOk())
|
2004-05-26 04:47:53 -04:00
|
|
|
m_sound->Play(wxSOUND_ASYNC | wxSOUND_LOOP);
|
2004-02-01 05:53:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
msg.Printf( _T("This is the About dialog of the sound sample.\n")
|
|
|
|
_T("Welcome to %s"), wxVERSION_STRING);
|
|
|
|
|
|
|
|
wxMessageBox(msg, _T("About"), wxOK | wxICON_INFORMATION, this);
|
|
|
|
}
|