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 )
30 frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
31 frameCESound->SetEditorMode(FALSE);
32 intResult = GetLastInt(&ContactEditorData.SoundsList);
33 frameCESound->SetupPointers(&ContactEditorData.SoundsList,
34 &ContactEditorData.SoundsListAltID,
35 &ContactEditorData.SoundsListPID,
36 &ContactEditorData.SoundsListType,
37 &ContactEditorData.SoundsListAudioType,
38 &ContactEditorData.SoundsListAudioEncType,
39 &ContactEditorData.SoundsListTokens,
40 &ContactEditorData.SoundsListMediatype,
41 &ContactEditorData.SoundsListPref,
44 frameCESound->ShowModal();
50 void frmContactEditor::ModifySound( wxCommandEvent& event )
53 long longSelected = -1;
54 int intSelectedData = 0;
56 if (!GetSelectedItem(lboSounds,
62 frmContactEditorSound *frameCESound = new frmContactEditorSound ( this );
63 frameCESound->SetupPointers(&ContactEditorData.SoundsList,
64 &ContactEditorData.SoundsListAltID,
65 &ContactEditorData.SoundsListPID,
66 &ContactEditorData.SoundsListType,
67 &ContactEditorData.SoundsListAudioType,
68 &ContactEditorData.SoundsListAudioEncType,
69 &ContactEditorData.SoundsListTokens,
70 &ContactEditorData.SoundsListMediatype,
71 &ContactEditorData.SoundsListPref,
74 frameCESound->SetEditorMode(TRUE);
75 frameCESound->ShowModal();
81 void frmContactEditor::DeleteSound( wxCommandEvent& event )
84 long longSelected = -1;
85 int intSelectedData = 0;
87 if (!GetSelectedItem(lboSounds,
93 lboSounds->DeleteItem(longSelected);
95 DeleteMapData(intSelectedData, &ContactEditorData.SoundsList, &ContactEditorData.SoundsListAltID,
96 &ContactEditorData.SoundsListPID, &ContactEditorData.SoundsListType, &ContactEditorData.SoundsListAudioType,
97 &ContactEditorData.SoundsListAudioEncType, &ContactEditorData.SoundsListTokens, &ContactEditorData.SoundsListMediatype,
98 &ContactEditorData.SoundsListPref);
102 void frmContactEditor::PlaySoundDetach()
105 if (!AudioStreamPlayback.openFromMemory(base64dec.c_str(), base64declen)){
107 // Can't load file. We must flee...
108 // Also show an error message too.
110 wxMessageBox(_("This file is in an audio format that is unsupported or is not an audio file."), _("Error opening audio file"), wxICON_ERROR);
118 //AudioStreamPlayback.setbuffer(AudioBuffer);
119 AudioStreamPlayback.play();
124 n1.tv_nsec = 100000000L;*/
126 //while (TimeA != TimeB){
127 while (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
129 // We poll this every 100ms in order to conserve CPU usage.
139 void frmContactEditor::PlaySound( wxCommandEvent& event )
142 //sf::Music *AudioPlayback = new sf::Music;
143 //AudioStream *AudioStreamPlayback = new AudioStream;
144 //sf::SoundBuffer *AudioBuffer = new sf::SoundBuffer;
146 //sf::Sound *AudioPlayback = new sf::Sound;
148 //delete(AudioPlayback);
150 //delete(AudioPlayback);
151 //delete(AudioStreamPlayback);
152 //delete(AudioBuffer);
154 //AudioPlayback = NULL;
155 //AudioStreamPlayback = NULL;
156 //AudioBuffer = NULL;
160 long longSelected = -1;
161 int intSelectedData = 0;
163 // Check if sound is still being played and if it is, stop it.
165 if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
167 AudioStreamPlayback.stop();
171 if (!GetSelectedItem(lboSounds,
177 std::map<int, std::string>::iterator initier;
179 initier = ContactEditorData.SoundsList.find(intSelectedData);
181 base64dec = base64_decode(initier->second);
182 base64declen = base64dec.size();
184 std::thread AudioPlaybackThread(&frmContactEditor::PlaySoundDetach, this);
185 AudioPlaybackThread.detach();
191 void frmContactEditor::StopSound( wxCommandEvent& event )
194 if (AudioStreamPlayback.getStatus() == sf::SoundSource::Playing){
196 AudioStreamPlayback.stop();
206 void frmContactEditor::SaveSound( wxCommandEvent &event )
209 long longSelected = -1;
210 int intSelectedData = 0;
212 if (!GetSelectedItem(lboSounds,
222 wxString FinalFilename;
224 std::map<int,std::string>::iterator initier;
225 std::map<int,wxString>::iterator soundtypeiter;
227 initier = ContactEditorData.SoundsList.find(intSelectedData);
228 soundtypeiter = ContactEditorData.SoundsListAudioType.find(intSelectedData);
230 // Open up the dialog to save the picture.
232 wxFileDialog ExportDlg(this, wxT("Save Sound"), wxT(""), wxT(""),
233 FileTypes, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
235 if (ExportDlg.ShowModal() == wxID_CANCEL){
241 // Write out the picture to the file.
245 if (SoundFile.Open(ExportDlg.GetPath(), wxFile::write)){
247 std::string base64dec;
249 initier = ContactEditorData.SoundsList.find(intSelectedData);
250 base64dec = base64_decode(initier->second);
252 SoundFile.Write(base64dec.c_str(), (size_t)base64dec.size());
258 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);