Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Implemented option to save photos, logos and sounds.
[xestiaab/.git] / source / contacteditor / frmContactEditor-Sound.cpp
1 // frmContactEditor-Sound.cpp - frmContactEditor Sound tab subroutines.
2 //
3 // (c) 2012-2015 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 {
27     int intResult = 0;
28     
29     frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
30     frameCESound->SetEditorMode(FALSE);
31     intResult = GetLastInt(&SoundsList);
32     frameCESound->SetupPointers(&SoundsList,
33                                 &SoundsListAltID,
34                                 &SoundsListPID,
35                                 &SoundsListType,
36                                 &SoundsListAudioType,
37                                 &SoundsListAudioEncType,
38                                 &SoundsListTokens,
39                                 &SoundsListMediatype,
40                                 &SoundsListPref,
41                                 lboSounds,
42                                 (intValueSeek));
43     frameCESound->ShowModal();
44     delete frameCESound;
45     frameCESound = NULL;
46 }
48 void frmContactEditor::ModifySound( wxCommandEvent& event )
49 {
50     long longSelected = -1;
51     int intSelectedData = 0;
52     
53     if (!GetSelectedItem(lboSounds,
54                          &longSelected,
55                          &intSelectedData)){
56         return;
57     }
58     
59     frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
60     frameCESound->SetupPointers(&SoundsList,
61                                 &SoundsListAltID,
62                                 &SoundsListPID,
63                                 &SoundsListType,
64                                 &SoundsListAudioType,
65                                 &SoundsListAudioEncType,
66                                 &SoundsListTokens,
67                                 &SoundsListMediatype,
68                                 &SoundsListPref,
69                                 lboSounds,
70                                 intSelectedData);
71     frameCESound->SetEditorMode(TRUE);
72     frameCESound->ShowModal();
73     delete frameCESound;
74     frameCESound = NULL;
75 }
77 void frmContactEditor::DeleteSound( wxCommandEvent& event )
78 {
79     long longSelected = -1;
80     int intSelectedData = 0;
81     
82     if (!GetSelectedItem(lboSounds,
83                          &longSelected,
84                          &intSelectedData)){
85         return;
86     }
87     
88     lboSounds->DeleteItem(longSelected);
89     
90     DeleteMapData(intSelectedData, &SoundsList, &SoundsListAltID,
91                   &SoundsListPID, &SoundsListType, &SoundsListAudioType,
92                   &SoundsListAudioEncType, &SoundsListTokens, &SoundsListMediatype,
93                   &SoundsListPref);
94     
95 }
97 void frmContactEditor::PlaySoundDetach()
98 {
99     
100     if (!AudioStreamPlayback.openFromMemory(base64dec.c_str(), base64declen)){
101         
102         // Can't load file. We must flee...
103         // Also show an error message too.
104         
105         wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
106         
107         return;
108         
109     }
110     
111     btnStop->Enable();
112     
113     //AudioStreamPlayback.setbuffer(AudioBuffer);
114     AudioStreamPlayback.play();
115     
116     /*timespec n1, n2;
117      
118      n1.tv_sec = 0;
119      n1.tv_nsec = 100000000L;*/
120     
121     //while (TimeA != TimeB){
122     while (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
123         
124         // We poll this every 100ms in order to conserve CPU usage.
125         
126         SleepFor(100000000);
127         
128     }
129     
130     btnStop->Disable();
131     
134 void frmContactEditor::PlaySound( wxCommandEvent& event )
136     
137     //sf::Music *AudioPlayback = new sf::Music;
138     //AudioStream *AudioStreamPlayback = new AudioStream;
139     //sf::SoundBuffer *AudioBuffer = new sf::SoundBuffer;
140     
141     //sf::Sound *AudioPlayback = new sf::Sound;
142     
143     //delete(AudioPlayback);
144     
145     //delete(AudioPlayback);
146     //delete(AudioStreamPlayback);
147     //delete(AudioBuffer);
148     
149     //AudioPlayback = NULL;
150     //AudioStreamPlayback = NULL;
151     //AudioBuffer = NULL;
152     
153     //return;
154     
155     long longSelected = -1;
156     int intSelectedData = 0;
157     
158     // Check if sound is still being played and if it is, stop it.
159     
160     if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
161         
162         AudioStreamPlayback.stop();
163         
164     }
165     
166     if (!GetSelectedItem(lboSounds,
167                          &longSelected,
168                          &intSelectedData)){
169         return;
170     }
171     
172     std::map<int, std::string>::iterator initier;
173     
174     initier = SoundsList.find(intSelectedData);
175     
176     base64dec = base64_decode(initier->second);
177     base64declen = base64dec.size();
178     
179     std::thread AudioPlaybackThread(&frmContactEditor::PlaySoundDetach, this);
180     AudioPlaybackThread.detach();
181     
182     return;
183     
186 void frmContactEditor::StopSound( wxCommandEvent& event )
188     
189     if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
190         
191         AudioStreamPlayback.stop();
192         
193     }
194     
195     btnStop->Disable();
196     
197     return;
198     
201 void frmContactEditor::SaveSound( wxCommandEvent &event )
204         long longSelected = -1;
205         int intSelectedData = 0;
206     
207         if (!GetSelectedItem(lboSounds,
208                          &longSelected,
209                          &intSelectedData)){
210                 
211                 return;
212                 
213         }
214         
215         wxString FileTypes;
216         
217         wxString FinalFilename;
218         
219         std::map<int,std::string>::iterator initier;
220         std::map<int,wxString>::iterator soundtypeiter;
221     
222         initier = SoundsList.find(intSelectedData);
223         soundtypeiter = SoundsListAudioType.find(intSelectedData);
224         
225         // Open up the dialog to save the picture.
226         
227         wxFileDialog ExportDlg(this, wxT("Save Sound"), wxT(""), wxT(""),
228                 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
229         
230         if (ExportDlg.ShowModal() == wxID_CANCEL){
231         
232                 return;
233         
234         }
235         
236         // Write out the picture to the file.
237         
238         wxFile SoundFile;
239         
240         if (SoundFile.Open(ExportDlg.GetPath(), wxFile::write)){
241         
242                 std::string base64dec;
243                 
244                 initier = SoundsList.find(intSelectedData);
245                 base64dec = base64_decode(initier->second);
246                 
247                 SoundFile.Write(base64dec.c_str(), (size_t)base64dec.size());
249                 SoundFile.Close();
250         
251         } else {
252         
253                 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);
254                 return;
255         
256         }
257         
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