updated on Sat Jan 14 04:03:48 UTC 2012
[aur-mirror.git] / audiere / DeviceFrame.cpp
blob33a1559f5942825c0b2096862acab5d9f44675c0
1 #ifdef _MSC_VER
2 #pragma warning(disable : 4786)
3 #endif
6 #include <set>
7 #include <string>
8 #include <vector>
9 #include "Commands.h"
10 #include "DeviceFrame.h"
11 #include "SoundEffectFrame.h"
12 #include "StreamFrame.h"
13 #include "wxPlayer.h"
15 #if wxCHECK_VERSION(2,5,3)
16 #include "wx/generic/numdlgg.h"
17 #endif
19 template<typename T>
20 std::string Join(
21 T cont,
22 const std::string& joiner,
23 const std::string& prefix = "")
25 std::string result;
27 typename T::iterator i = cont.begin();
28 for (;;) {
29 result += prefix + *i++;
30 if (i == cont.end()) {
31 break;
32 } else {
33 result += joiner;
37 return result;
41 BEGIN_EVENT_TABLE(DeviceFrame, wxMDIParentFrame)
42 EVT_MENU(DEVICE_NEW_DEVICE, DeviceFrame::OnDeviceNewDevice)
43 EVT_MENU(DEVICE_NEW_CDDEVICE, DeviceFrame::OnDeviceNewCDDevice)
44 EVT_MENU(DEVICE_NEW_MIDIDEVICE, DeviceFrame::OnDeviceNewMIDIDevice)
45 EVT_MENU(DEVICE_OPEN_STREAM, DeviceFrame::OnDeviceOpenStream)
46 EVT_MENU(DEVICE_OPEN_SOUND, DeviceFrame::OnDeviceOpenSound)
47 EVT_MENU(DEVICE_CREATE_TONE, DeviceFrame::OnDeviceCreateTone)
48 EVT_MENU(DEVICE_CREATE_SQUARE_WAVE, DeviceFrame::OnDeviceCreateSquareWave)
49 EVT_MENU(DEVICE_CREATE_WHITE_NOISE, DeviceFrame::OnDeviceCreateWhiteNoise)
50 EVT_MENU(DEVICE_CREATE_PINK_NOISE, DeviceFrame::OnDeviceCreatePinkNoise)
51 EVT_MENU(DEVICE_OPEN_SINGLE_EFFECT, DeviceFrame::OnDeviceOpenSingleEffect)
52 EVT_MENU(DEVICE_OPEN_MULTIPLE_EFFECT, DeviceFrame::OnDeviceOpenMultipleEffect)
53 EVT_MENU(DEVICE_CLOSE_WINDOW, DeviceFrame::OnDeviceCloseCurrentWindow)
54 EVT_MENU(DEVICE_CLOSE, DeviceFrame::OnDeviceClose)
55 EVT_MENU(HELP_ABOUT, DeviceFrame::OnHelpAbout)
56 END_EVENT_TABLE()
59 DeviceFrame::DeviceFrame(audiere::AudioDevicePtr device)
60 : wxMDIParentFrame(0, -1, wxT("Audio Device - " + CStr2wxString( device->getName() )),
61 wxDefaultPosition, wxSize(400, 500))
63 m_device = device;
65 wxMenu* deviceMenu = new wxMenu();
66 deviceMenu->Append(DEVICE_NEW_DEVICE, wxT("&New Device..."));
67 deviceMenu->Append(DEVICE_NEW_CDDEVICE, wxT("New C&D Device..."));
68 deviceMenu->Append(DEVICE_NEW_MIDIDEVICE, wxT("New &MIDI Device..."));
69 deviceMenu->AppendSeparator();
70 deviceMenu->Append(DEVICE_OPEN_STREAM, wxT("&Open Stream..."));
71 deviceMenu->Append(DEVICE_OPEN_SOUND, wxT("Open &Sound..."));
72 deviceMenu->AppendSeparator();
73 deviceMenu->Append(DEVICE_CREATE_TONE, wxT("Create &Tone..."));
74 deviceMenu->Append(DEVICE_CREATE_SQUARE_WAVE, wxT("Create S&quare Wave..."));
75 deviceMenu->Append(DEVICE_CREATE_WHITE_NOISE, wxT("Create &White Noise"));
76 deviceMenu->Append(DEVICE_CREATE_PINK_NOISE, wxT("Create &Pink Noise"));
77 deviceMenu->AppendSeparator();
78 deviceMenu->Append(DEVICE_OPEN_SINGLE_EFFECT, wxT("Open &Effect (Single)..."));
79 deviceMenu->Append(DEVICE_OPEN_MULTIPLE_EFFECT, wxT("Open Effect (&Multiple)..."));
80 deviceMenu->AppendSeparator();
81 deviceMenu->Append(DEVICE_CLOSE_WINDOW, wxT("Close C&urrent Window"));
82 deviceMenu->Append(DEVICE_CLOSE, wxT("&Close Device"));
84 wxMenu* helpMenu = new wxMenu();
85 helpMenu->Append(HELP_ABOUT, wxT("&About..."));
87 wxMenuBar* menuBar = new wxMenuBar();
88 menuBar->Append(deviceMenu, wxT("&Device"));
89 menuBar->Append(helpMenu, wxT("&Help"));
90 SetMenuBar(menuBar);
92 SetFocus();
96 void DeviceFrame::OnDeviceNewDevice(wxCommandEvent&) {
97 wxGetApp().OnNewDevice(this);
101 void DeviceFrame::OnDeviceNewCDDevice(wxCommandEvent&) {
102 wxGetApp().OnNewCDDevice(this);
106 void DeviceFrame::OnDeviceNewMIDIDevice(wxCommandEvent&) {
107 wxGetApp().OnNewMIDIDevice(this);
111 wxString DeviceFrame::GetSoundFile() {
112 std::vector<audiere::FileFormatDesc> formats;
113 audiere::GetSupportedFileFormats(formats);
115 // combine all of the supported extensions into one collection
116 std::set<std::string> all_extensions;
118 for (unsigned i = 0; i < formats.size(); ++i) {
119 for (unsigned j = 0; j < formats[i].extensions.size(); ++j) {
120 all_extensions.insert("*." + formats[i].extensions[j]);
125 // build a string of wildcards for wxWindows
126 std::string wildcards;
127 wildcards = "Sound Files (" + Join(all_extensions, ",") + ")|";
128 wildcards += Join(all_extensions, ";") + "|";
131 for (unsigned i = 0; i < formats.size(); ++i) {
132 audiere::FileFormatDesc& ffd = formats[i];
133 wildcards += ffd.description + " ";
134 wildcards += "(" + Join(ffd.extensions, ",", "*.") + ")|";
135 wildcards += Join(ffd.extensions, ";", "*.") + "|";
139 wildcards += "All Files (*.*)|*.*";
141 return wxFileSelector(
142 wxT("Select a sound file"), wxT(""), wxT(""), wxT(""),
143 CStr2wxString(wildcards.c_str()), wxOPEN, this);
147 void DeviceFrame::OnDeviceOpenStream(wxCommandEvent &) {
148 wxString filename(GetSoundFile());
149 if (filename.empty()) {
150 return;
153 #if wxUSE_UNICODE
154 wxCharBuffer buffFilename = filename.mb_str(wxConvUTF8);
155 audiere::SampleSourcePtr source = audiere::OpenSampleSource(buffFilename.data());
156 #else
157 audiere::SampleSourcePtr source = audiere::OpenSampleSource(filename);
158 #endif
159 if (!source) {
160 wxMessageBox(
161 wxT("Could not open sample source: ") + filename,
162 wxT("Open Stream"), wxOK | wxCENTRE, this);
163 return;
166 audiere::LoopPointSourcePtr loop_source =
167 audiere::CreateLoopPointSource(source);
168 if (loop_source) {
169 source = loop_source.get();
172 audiere::OutputStreamPtr stream = audiere::OpenSound(
173 m_device,
174 source,
175 true);
176 if (!stream) {
177 wxMessageBox(
178 wxT("Could not open output stream: ") + filename,
179 wxT("Open Stream"), wxOK | wxCENTRE, this);
180 return;
183 // get the basename of the path for the window title
184 wxString basename = wxFileNameFromPath(filename);
185 new StreamFrame(this, wxT("Stream: ") + basename, stream.get(), source.get(), loop_source.get());
189 void DeviceFrame::OnDeviceOpenSound(wxCommandEvent &) {
190 wxString filename(GetSoundFile());
191 if (filename.empty()) {
192 return;
195 audiere::SampleSourcePtr source = audiere::OpenSampleSource(wxString2CStr(filename));
196 if (!source) {
197 wxMessageBox(
198 wxT("Could not open source: ") + filename,
199 wxT("Open Sound"), wxOK | wxCENTRE, this);
200 return;
203 audiere::OutputStreamPtr stream = audiere::OpenSound(m_device, source);
204 if (!stream) {
205 wxMessageBox(
206 wxT("Could not open sound: ") + filename,
207 wxT("Open Sound"), wxOK | wxCENTRE, this);
208 return;
211 // get the basename of the path for the window title
212 wxString basename = wxFileNameFromPath(filename);
213 new StreamFrame(this, wxT("Sound: ") + basename, stream.get(), source.get());
217 void DeviceFrame::OnDeviceCreateTone(wxCommandEvent &) {
218 int frequency = ::wxGetNumberFromUser(
219 wxT("Value must be between 1 and 30000."), wxT("Enter frequency in Hz"),
220 wxT("Create Tone"), 256, 1, 30000, this);
221 if (frequency != -1) {
222 audiere::SampleSourcePtr source = audiere::CreateTone(frequency);
223 if (!source) {
224 wxMessageBox(
225 wxT("Could not create tone"),
226 wxT("Create Tone"), wxOK | wxCENTRE, this);
227 return;
230 audiere::OutputStreamPtr stream = m_device->openStream(source.get());
231 if (!stream) {
232 wxMessageBox(
233 wxT("Could not open output stream"),
234 wxT("Create Tone"), wxOK | wxCENTRE, this);
235 return;
239 wxString title;
240 title.sprintf(wxT("Tone: %d Hz"), frequency);
241 new StreamFrame(this, title, stream.get(), source.get());
246 void DeviceFrame::OnDeviceCreateSquareWave(wxCommandEvent &) {
247 int frequency = ::wxGetNumberFromUser(
248 wxT("Value must be between 1 and 30000."), wxT("Enter frequency in Hz"),
249 wxT("Create Square Wave"), 256, 1, 30000, this);
250 if (frequency != -1) {
251 audiere::SampleSourcePtr source = audiere::CreateSquareWave(frequency);
252 if (!source) {
253 wxMessageBox(
254 wxT("Could not create square wave"),
255 wxT("Create Square Wave"), wxOK | wxCENTRE, this);
256 return;
259 audiere::OutputStreamPtr stream = m_device->openStream(source.get());
260 if (!stream) {
261 wxMessageBox(
262 wxT("Could not open output stream"),
263 wxT("Create Square Wave"), wxOK | wxCENTRE, this);
264 return;
267 wxString title;
268 title.sprintf(wxT("Square Wave: %d Hz"), frequency);
269 new StreamFrame(this, title, stream.get(), source.get());
274 void DeviceFrame::OnDeviceCreateWhiteNoise(wxCommandEvent &) {
275 audiere::SampleSourcePtr source = audiere::CreateWhiteNoise();
276 if (!source) {
277 wxMessageBox(
278 wxT("Could not create white noise"),
279 wxT("Create White Noise"), wxOK | wxCENTRE, this);
280 return;
283 audiere::OutputStreamPtr stream = m_device->openStream(source.get());
284 if (!stream) {
285 wxMessageBox(
286 wxT("Could not open output stream"),
287 wxT("Create White Noise"), wxOK | wxCENTRE, this);
288 return;
291 new StreamFrame(this, wxT("White Noise"), stream.get(), source.get());
295 void DeviceFrame::OnDeviceCreatePinkNoise(wxCommandEvent &) {
296 audiere::SampleSourcePtr source = audiere::CreatePinkNoise();
297 if (!source) {
298 wxMessageBox(
299 wxT("Could not create white noise"),
300 wxT("Create Pink Noise"), wxOK | wxCENTRE, this);
301 return;
304 audiere::OutputStreamPtr stream = m_device->openStream(source.get());
305 if (!stream) {
306 wxMessageBox(
307 wxT("Could not open output stream"),
308 wxT("Create Pink Noise"), wxOK | wxCENTRE, this);
309 return;
312 new StreamFrame(this, wxT("Pink Noise"), stream.get(), source.get());
316 void DeviceFrame::OnDeviceOpenSingleEffect(wxCommandEvent &) {
317 DoOpenEffect(audiere::SINGLE, wxT("Single"));
321 void DeviceFrame::OnDeviceOpenMultipleEffect(wxCommandEvent &) {
322 DoOpenEffect(audiere::MULTIPLE, wxT("Multiple"));
326 void DeviceFrame::DoOpenEffect(audiere::SoundEffectType type, wxString typestring) {
327 wxString filename(GetSoundFile());
328 if (filename.empty()) {
329 return;
332 audiere::SoundEffectPtr effect = audiere::OpenSoundEffect(m_device, wxString2CStr(filename), type);
333 if (effect) {
334 wxString basename = wxFileNameFromPath(filename);
335 wxString title;
336 title.sprintf(wxT("Sound Effect (%s): %s"),
337 typestring.c_str(), basename.c_str());
338 new SoundEffectFrame(this, title, effect);
339 } else {
340 wxMessageBox(
341 wxT("Could not open sound effect: ") + filename,
342 wxT("Open Sound Effect"), wxOK | wxCENTRE, this);
347 void DeviceFrame::OnDeviceCloseCurrentWindow(wxCommandEvent&) {
348 wxMDIChildFrame* frame = GetActiveChild();
349 if (frame) {
350 frame->Close();
355 void DeviceFrame::OnDeviceClose(wxCommandEvent &) {
356 Close();
360 void DeviceFrame::OnHelpAbout(wxCommandEvent &) {
361 wxGetApp().ShowAboutDialog(this);