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 )
29 frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
30 frameCESound->SetEditorMode(FALSE);
31 intResult = GetLastInt(&ContactEditorData.SoundsList);
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();
48 void frmContactEditor::ModifySound( wxCommandEvent& event )
50 long longSelected = -1;
51 int intSelectedData = 0;
53 if (!GetSelectedItem(lboSounds,
59 frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
60 frameCESound->SetupPointers(&ContactEditorData.SoundsList,
61 &ContactEditorData.SoundsListAltID,
62 &ContactEditorData.SoundsListPID,
63 &ContactEditorData.SoundsListType,
64 &ContactEditorData.SoundsListAudioType,
65 &ContactEditorData.SoundsListAudioEncType,
66 &ContactEditorData.SoundsListTokens,
67 &ContactEditorData.SoundsListMediatype,
68 &ContactEditorData.SoundsListPref,
71 frameCESound->SetEditorMode(TRUE);
72 frameCESound->ShowModal();
77 void frmContactEditor::DeleteSound( wxCommandEvent& event )
79 long longSelected = -1;
80 int intSelectedData = 0;
82 if (!GetSelectedItem(lboSounds,
88 lboSounds->DeleteItem(longSelected);
90 DeleteMapData(intSelectedData, &ContactEditorData.SoundsList, &ContactEditorData.SoundsListAltID,
91 &ContactEditorData.SoundsListPID, &ContactEditorData.SoundsListType, &ContactEditorData.SoundsListAudioType,
92 &ContactEditorData.SoundsListAudioEncType, &ContactEditorData.SoundsListTokens, &ContactEditorData.SoundsListMediatype,
93 &ContactEditorData.SoundsListPref);
97 void frmContactEditor::PlaySoundDetach()
100 if (!AudioStreamPlayback.openFromMemory(base64dec.c_str(), base64declen)){
102 // Can't load file. We must flee...
103 // Also show an error message too.
105 wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
113 //AudioStreamPlayback.setbuffer(AudioBuffer);
114 AudioStreamPlayback.play();
119 n1.tv_nsec = 100000000L;*/
121 //while (TimeA != TimeB){
122 while (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
124 // We poll this every 100ms in order to conserve CPU usage.
134 void frmContactEditor::PlaySound( wxCommandEvent& event )
137 //sf::Music *AudioPlayback = new sf::Music;
138 //AudioStream *AudioStreamPlayback = new AudioStream;
139 //sf::SoundBuffer *AudioBuffer = new sf::SoundBuffer;
141 //sf::Sound *AudioPlayback = new sf::Sound;
143 //delete(AudioPlayback);
145 //delete(AudioPlayback);
146 //delete(AudioStreamPlayback);
147 //delete(AudioBuffer);
149 //AudioPlayback = NULL;
150 //AudioStreamPlayback = NULL;
151 //AudioBuffer = NULL;
155 long longSelected = -1;
156 int intSelectedData = 0;
158 // Check if sound is still being played and if it is, stop it.
160 if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
162 AudioStreamPlayback.stop();
166 if (!GetSelectedItem(lboSounds,
172 std::map<int, std::string>::iterator initier;
174 initier = ContactEditorData.SoundsList.find(intSelectedData);
176 base64dec = base64_decode(initier->second);
177 base64declen = base64dec.size();
179 std::thread AudioPlaybackThread(&frmContactEditor::PlaySoundDetach, this);
180 AudioPlaybackThread.detach();
186 void frmContactEditor::StopSound( wxCommandEvent& event )
189 if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
191 AudioStreamPlayback.stop();
201 void frmContactEditor::SaveSound( wxCommandEvent &event )
204 long longSelected = -1;
205 int intSelectedData = 0;
207 if (!GetSelectedItem(lboSounds,
217 wxString FinalFilename;
219 std::map<int,std::string>::iterator initier;
220 std::map<int,wxString>::iterator soundtypeiter;
222 initier = ContactEditorData.SoundsList.find(intSelectedData);
223 soundtypeiter = ContactEditorData.SoundsListAudioType.find(intSelectedData);
225 // Open up the dialog to save the picture.
227 wxFileDialog ExportDlg(this, wxT("Save Sound"), wxT(""), wxT(""),
228 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
230 if (ExportDlg.ShowModal() == wxID_CANCEL){
236 // Write out the picture to the file.
240 if (SoundFile.Open(ExportDlg.GetPath(), wxFile::write)){
242 std::string base64dec;
244 initier = ContactEditorData.SoundsList.find(intSelectedData);
245 base64dec = base64_decode(initier->second);
247 SoundFile.Write(base64dec.c_str(), (size_t)base64dec.size());
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);