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.
30 frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
31 frameCESound->SetEditorMode(FALSE);
32 frameCESound->SetupPointers(&ContactEditorData.SoundsList,
33 &ContactEditorData.SoundsListAltID,
34 &ContactEditorData.SoundsListPID,
35 &ContactEditorData.SoundsListType,
36 &ContactEditorData.SoundsListAudioType,
37 &ContactEditorData.SoundsListAudioEncType,
38 &ContactEditorData.SoundsListTokens,
39 &ContactEditorData.SoundsListMediatype,
40 &ContactEditorData.SoundsListPref,
43 frameCESound->ShowModal();
49 void frmContactEditor::ModifySound( wxCommandEvent& event )
52 // Bring up a form to modify sound.
54 long longSelected = -1;
55 int intSelectedData = 0;
57 if (!GetSelectedItem(lboSounds,
63 frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
64 frameCESound->SetupPointers(&ContactEditorData.SoundsList,
65 &ContactEditorData.SoundsListAltID,
66 &ContactEditorData.SoundsListPID,
67 &ContactEditorData.SoundsListType,
68 &ContactEditorData.SoundsListAudioType,
69 &ContactEditorData.SoundsListAudioEncType,
70 &ContactEditorData.SoundsListTokens,
71 &ContactEditorData.SoundsListMediatype,
72 &ContactEditorData.SoundsListPref,
75 frameCESound->SetEditorMode(TRUE);
76 frameCESound->ShowModal();
82 void frmContactEditor::DeleteSound( wxCommandEvent& event )
85 // Bring up a form to delete sound.
87 long longSelected = -1;
88 int intSelectedData = 0;
90 if (!GetSelectedItem(lboSounds,
96 lboSounds->DeleteItem(longSelected);
98 DeleteMapData(intSelectedData, &ContactEditorData.SoundsList, &ContactEditorData.SoundsListAltID,
99 &ContactEditorData.SoundsListPID, &ContactEditorData.SoundsListType, &ContactEditorData.SoundsListAudioType,
100 &ContactEditorData.SoundsListAudioEncType, &ContactEditorData.SoundsListTokens, &ContactEditorData.SoundsListMediatype,
101 &ContactEditorData.SoundsListPref);
105 void frmContactEditor::PlaySoundDetach()
108 // Play the sound and detach.
110 if (!AudioStreamPlayback.openFromMemory(base64dec.c_str(), base64declen)){
112 // Can't load file. We must flee...
113 // Also show an error message too.
115 wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
123 AudioStreamPlayback.play();
125 while (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
127 // We poll this every 100ms in order to conserve CPU usage.
137 void frmContactEditor::PlaySound( wxCommandEvent& event )
142 long longSelected = -1;
143 int intSelectedData = 0;
145 // Check if sound is still being played and if it is, stop it.
147 if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
149 AudioStreamPlayback.stop();
153 if (!GetSelectedItem(lboSounds,
159 std::map<int, std::string>::iterator initier;
161 initier = ContactEditorData.SoundsList.find(intSelectedData);
163 base64dec = base64_decode(initier->second);
164 base64declen = base64dec.size();
166 std::thread AudioPlaybackThread(&frmContactEditor::PlaySoundDetach, this);
167 AudioPlaybackThread.detach();
173 void frmContactEditor::StopSound( wxCommandEvent& event )
178 if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
180 AudioStreamPlayback.stop();
190 void frmContactEditor::SaveSound( wxCommandEvent &event )
195 long longSelected = -1;
196 int intSelectedData = 0;
198 if (!GetSelectedItem(lboSounds,
208 wxString FinalFilename;
210 std::map<int,std::string>::iterator initier;
211 std::map<int,wxString>::iterator soundtypeiter;
213 initier = ContactEditorData.SoundsList.find(intSelectedData);
214 soundtypeiter = ContactEditorData.SoundsListAudioType.find(intSelectedData);
216 // Open up the dialog to save the picture.
218 wxFileDialog ExportDlg(this, wxT("Save Sound"), wxT(""), wxT(""),
219 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
221 if (ExportDlg.ShowModal() == wxID_CANCEL){
227 // Write out the picture to the file.
231 if (SoundFile.Open(ExportDlg.GetPath(), wxFile::write)){
233 std::string base64dec;
235 initier = ContactEditorData.SoundsList.find(intSelectedData);
236 base64dec = base64_decode(initier->second);
238 SoundFile.Write(base64dec.c_str(), (size_t)base64dec.size());
244 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);