Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
ba1a0c95b9f942aeb19e5c0e8c98a065bc4fbb0a
[xestiaab/.git] / source / contacteditor / frmContactEditor-Sound.cpp
1 // frmContactEditor-Sound.cpp - frmContactEditor Sound tab subroutines.
2 //
3 // (c) 2012-2016 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
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.
10 //
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.
15 //
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>
23 #include <thread>
25 void frmContactEditor::AddSound( wxCommandEvent& event )
26 {
28         // Bring up a form to add sound.
29             
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,
41                 lboSounds,
42                 (intValueSeek));
43         frameCESound->ShowModal();
44         delete frameCESound;
45         frameCESound = NULL;
46         
47 }
49 void frmContactEditor::ModifySound( wxCommandEvent& event )
50 {
51         
52         // Bring up a form to modify sound.
53         
54         long longSelected = -1;
55         int intSelectedData = 0;
56     
57         if (!GetSelectedItem(lboSounds,
58                 &longSelected,
59                 &intSelectedData)){
60                 return;
61         }
62     
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,
73                 lboSounds,
74                 intSelectedData);
75         frameCESound->SetEditorMode(TRUE);
76         frameCESound->ShowModal();
77         delete frameCESound;
78         frameCESound = NULL;
79         
80 }
82 void frmContactEditor::DeleteSound( wxCommandEvent& event )
83 {
84         
85         // Bring up a form to delete sound.
86         
87         long longSelected = -1;
88         int intSelectedData = 0;
89     
90         if (!GetSelectedItem(lboSounds,
91                 &longSelected,
92                 &intSelectedData)){
93                 return;
94         }
95     
96         lboSounds->DeleteItem(longSelected);
97     
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()
107     
108         // Play the sound and detach.
109         
110         if (!AudioStreamPlayback.openFromMemory(base64dec.c_str(), base64declen)){
111         
112                 // Can't load file. We must flee...
113                 // Also show an error message too.
114         
115                 wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
116         
117                 return;
118         
119         }
120     
121         btnStop->Enable();
123         AudioStreamPlayback.play();
124     
125         while (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
126         
127                 // We poll this every 100ms in order to conserve CPU usage.
128         
129                 SleepFor(100000000);
130         
131         }
132     
133         btnStop->Disable();
134     
137 void frmContactEditor::PlaySound( wxCommandEvent& event )
139     
140         // Play the sound.
141     
142         long longSelected = -1;
143         int intSelectedData = 0;
144     
145         // Check if sound is still being played and if it is, stop it.
146     
147         if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
148         
149                 AudioStreamPlayback.stop();
150         
151         }
152     
153         if (!GetSelectedItem(lboSounds,
154                 &longSelected,
155                 &intSelectedData)){
156                 return;
157         }
158     
159         std::map<int, std::string>::iterator initier;
160     
161         initier = ContactEditorData.SoundsList.find(intSelectedData);
162     
163         base64dec = base64_decode(initier->second);
164         base64declen = base64dec.size();
165     
166         std::thread AudioPlaybackThread(&frmContactEditor::PlaySoundDetach, this);
167         AudioPlaybackThread.detach();
168     
169         return;
170     
173 void frmContactEditor::StopSound( wxCommandEvent& event )
175         
176         // Stop the sound.
177     
178         if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
179         
180                 AudioStreamPlayback.stop();
181         
182         }
183     
184         btnStop->Disable();
186         return;
187     
190 void frmContactEditor::SaveSound( wxCommandEvent &event )
193         // Save the sound.
194         
195         long longSelected = -1;
196         int intSelectedData = 0;
197     
198         if (!GetSelectedItem(lboSounds,
199                          &longSelected,
200                          &intSelectedData)){
201                 
202                 return;
203                 
204         }
205         
206         wxString FileTypes;
207         
208         wxString FinalFilename;
209         
210         std::map<int,std::string>::iterator initier;
211         std::map<int,wxString>::iterator soundtypeiter;
212     
213         initier = ContactEditorData.SoundsList.find(intSelectedData);
214         soundtypeiter = ContactEditorData.SoundsListAudioType.find(intSelectedData);
215         
216         // Open up the dialog to save the picture.
217         
218         wxFileDialog ExportDlg(this, wxT("Save Sound"), wxT(""), wxT(""),
219                 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
220         
221         if (ExportDlg.ShowModal() == wxID_CANCEL){
222         
223                 return;
224         
225         }
226         
227         // Write out the picture to the file.
228         
229         wxFile SoundFile;
230         
231         if (SoundFile.Open(ExportDlg.GetPath(), wxFile::write)){
232         
233                 std::string base64dec;
234                 
235                 initier = ContactEditorData.SoundsList.find(intSelectedData);
236                 base64dec = base64_decode(initier->second);
237                 
238                 SoundFile.Write(base64dec.c_str(), (size_t)base64dec.size());
240                 SoundFile.Close();
241         
242         } else {
243         
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);
245                 return;
246         
247         }
248         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy