1 // frmContactEditor-Sound.cpp - frmContactEditor Sound tab subroutines.
3 // (c) 2012-2016 Xestia Software Development.
5 // This file is part of Xestia Address Book.
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include "frmContactEditor.h"
20 #include "frmContactEditorSound.h"
21 #include "../common/base64.h"
22 #include <SFML/Audio.hpp>
25 void frmContactEditor::AddSound( wxCommandEvent& event )
28 // Bring up a form to add sound.
32 frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
33 frameCESound->SetEditorMode(FALSE);
34 intResult = GetLastInt(&ContactEditorData.SoundsList);
35 frameCESound->SetupPointers(&ContactEditorData.SoundsList,
36 &ContactEditorData.SoundsListAltID,
37 &ContactEditorData.SoundsListPID,
38 &ContactEditorData.SoundsListType,
39 &ContactEditorData.SoundsListAudioType,
40 &ContactEditorData.SoundsListAudioEncType,
41 &ContactEditorData.SoundsListTokens,
42 &ContactEditorData.SoundsListMediatype,
43 &ContactEditorData.SoundsListPref,
46 frameCESound->ShowModal();
52 void frmContactEditor::ModifySound( wxCommandEvent& event )
55 // Bring up a form to modify sound.
57 long longSelected = -1;
58 int intSelectedData = 0;
60 if (!GetSelectedItem(lboSounds,
66 frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
67 frameCESound->SetupPointers(&ContactEditorData.SoundsList,
68 &ContactEditorData.SoundsListAltID,
69 &ContactEditorData.SoundsListPID,
70 &ContactEditorData.SoundsListType,
71 &ContactEditorData.SoundsListAudioType,
72 &ContactEditorData.SoundsListAudioEncType,
73 &ContactEditorData.SoundsListTokens,
74 &ContactEditorData.SoundsListMediatype,
75 &ContactEditorData.SoundsListPref,
78 frameCESound->SetEditorMode(TRUE);
79 frameCESound->ShowModal();
85 void frmContactEditor::DeleteSound( wxCommandEvent& event )
88 // Bring up a form to delete sound.
90 long longSelected = -1;
91 int intSelectedData = 0;
93 if (!GetSelectedItem(lboSounds,
99 lboSounds->DeleteItem(longSelected);
101 DeleteMapData(intSelectedData, &ContactEditorData.SoundsList, &ContactEditorData.SoundsListAltID,
102 &ContactEditorData.SoundsListPID, &ContactEditorData.SoundsListType, &ContactEditorData.SoundsListAudioType,
103 &ContactEditorData.SoundsListAudioEncType, &ContactEditorData.SoundsListTokens, &ContactEditorData.SoundsListMediatype,
104 &ContactEditorData.SoundsListPref);
108 void frmContactEditor::PlaySoundDetach()
111 // Play the sound and detach.
113 if (!AudioStreamPlayback.openFromMemory(base64dec.c_str(), base64declen)){
115 // Can't load file. We must flee...
116 // Also show an error message too.
118 wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
126 AudioStreamPlayback.play();
128 while (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
130 // We poll this every 100ms in order to conserve CPU usage.
140 void frmContactEditor::PlaySound( wxCommandEvent& event )
145 long longSelected = -1;
146 int intSelectedData = 0;
148 // Check if sound is still being played and if it is, stop it.
150 if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
152 AudioStreamPlayback.stop();
156 if (!GetSelectedItem(lboSounds,
162 std::map<int, std::string>::iterator initier;
164 initier = ContactEditorData.SoundsList.find(intSelectedData);
166 base64dec = base64_decode(initier->second);
167 base64declen = base64dec.size();
169 std::thread AudioPlaybackThread(&frmContactEditor::PlaySoundDetach, this);
170 AudioPlaybackThread.detach();
176 void frmContactEditor::StopSound( wxCommandEvent& event )
181 if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
183 AudioStreamPlayback.stop();
193 void frmContactEditor::SaveSound( wxCommandEvent &event )
198 long longSelected = -1;
199 int intSelectedData = 0;
201 if (!GetSelectedItem(lboSounds,
211 wxString FinalFilename;
213 std::map<int,std::string>::iterator initier;
214 std::map<int,wxString>::iterator soundtypeiter;
216 initier = ContactEditorData.SoundsList.find(intSelectedData);
217 soundtypeiter = ContactEditorData.SoundsListAudioType.find(intSelectedData);
219 // Open up the dialog to save the picture.
221 wxFileDialog ExportDlg(this, wxT("Save Sound"), wxT(""), wxT(""),
222 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
224 if (ExportDlg.ShowModal() == wxID_CANCEL){
230 // Write out the picture to the file.
234 if (SoundFile.Open(ExportDlg.GetPath(), wxFile::write)){
236 std::string base64dec;
238 initier = ContactEditorData.SoundsList.find(intSelectedData);
239 base64dec = base64_decode(initier->second);
241 SoundFile.Write(base64dec.c_str(), (size_t)base64dec.size());
247 wxMessageBox(_("An error occured whilst saving the sound. Check that you have free space and permissions to write the picture at this location."), _("Sound save error"), wxOK, this);