2014-12-29 20:58:30 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
############################################################
|
|
|
|
# Copyright (c) 2013, 2014 Manuel Eduardo Cortéz Vallejo <manuel@manuelcortez.net>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
############################################################
|
2018-11-22 13:35:19 -06:00
|
|
|
from __future__ import absolute_import
|
2014-12-29 20:58:30 -06:00
|
|
|
import widgetUtils
|
2018-11-22 13:35:19 -06:00
|
|
|
from . import wx_ui
|
|
|
|
from . import wx_transfer_dialogs
|
|
|
|
from . import transfer
|
2014-12-29 20:58:30 -06:00
|
|
|
import output
|
|
|
|
import tempfile
|
|
|
|
import sound
|
|
|
|
import os
|
|
|
|
import config
|
|
|
|
from pubsub import pub
|
|
|
|
from mysc.thread_utils import call_threaded
|
|
|
|
import sound_lib
|
2015-01-18 17:19:39 -06:00
|
|
|
import logging
|
2014-12-29 20:58:30 -06:00
|
|
|
|
2015-01-18 17:19:39 -06:00
|
|
|
log = logging.getLogger("extra.AudioUploader.audioUploader")
|
2015-06-15 16:31:00 -05:00
|
|
|
|
2014-12-29 20:58:30 -06:00
|
|
|
class audioUploader(object):
|
|
|
|
def __init__(self, configFile, completed_callback):
|
|
|
|
self.config = configFile
|
|
|
|
super(audioUploader, self).__init__()
|
|
|
|
self.dialog = wx_ui.audioDialog(services=self.get_available_services())
|
|
|
|
self.file = None
|
|
|
|
self.recorded = False
|
|
|
|
self.recording = None
|
|
|
|
self.playing = None
|
|
|
|
widgetUtils.connect_event(self.dialog.play, widgetUtils.BUTTON_PRESSED, self.on_play)
|
|
|
|
widgetUtils.connect_event(self.dialog.pause, widgetUtils.BUTTON_PRESSED, self.on_pause)
|
|
|
|
widgetUtils.connect_event(self.dialog.record, widgetUtils.BUTTON_PRESSED, self.on_record)
|
|
|
|
widgetUtils.connect_event(self.dialog.attach_exists, widgetUtils.BUTTON_PRESSED, self.on_attach_exists)
|
|
|
|
widgetUtils.connect_event(self.dialog.discard, widgetUtils.BUTTON_PRESSED, self.on_discard)
|
|
|
|
if self.dialog.get_response() == widgetUtils.OK:
|
|
|
|
self.postprocess()
|
2015-01-18 17:19:39 -06:00
|
|
|
log.debug("Uploading file %s to %s..." % (self.file, self.dialog.get("services")))
|
2014-12-29 20:58:30 -06:00
|
|
|
self.uploaderDialog = wx_transfer_dialogs.UploadDialog(self.file)
|
|
|
|
output.speak(_(u"Attaching..."))
|
2015-06-15 16:31:00 -05:00
|
|
|
if self.dialog.get("services") == "SNDUp":
|
2018-01-31 17:12:43 -06:00
|
|
|
base_url = "https://sndup.net/post.php"
|
2017-05-06 10:40:31 -05:00
|
|
|
if len(self.config["sound"]["sndup_api_key"]) > 0:
|
|
|
|
url = base_url + '?apikey=' + self.config['sound']['sndup_api_key']
|
|
|
|
else:
|
|
|
|
url = base_url
|
2016-05-06 10:38:17 -05:00
|
|
|
self.uploaderFunction = transfer.Upload(obj=self, field='file', url=url, filename=self.file, completed_callback=completed_callback)
|
2014-12-29 20:58:30 -06:00
|
|
|
pub.subscribe(self.uploaderDialog.update, "uploading")
|
2016-04-30 06:16:16 -05:00
|
|
|
self.uploaderDialog.get_response(self.uploaderFunction.perform_threaded)
|
2014-12-29 20:58:30 -06:00
|
|
|
|
|
|
|
def get_available_services(self):
|
|
|
|
services = []
|
|
|
|
services.append("SNDUp")
|
|
|
|
return services
|
|
|
|
|
|
|
|
def on_pause(self, *args, **kwargs):
|
|
|
|
if self.dialog.get("pause") == _(u"Pause"):
|
|
|
|
self.recording.pause()
|
2015-09-05 11:42:47 -05:00
|
|
|
self.dialog.set("pause", _(u"&Resume"))
|
2014-12-29 20:58:30 -06:00
|
|
|
elif self.dialog.get("pause") == _(u"Resume"):
|
|
|
|
self.recording.play()
|
2015-09-05 11:42:47 -05:00
|
|
|
self.dialog.set("pause", _(U"&Pause"))
|
2014-12-29 20:58:30 -06:00
|
|
|
|
|
|
|
def on_record(self, *args, **kwargs):
|
|
|
|
if self.recording != None:
|
|
|
|
self.stop_recording()
|
|
|
|
self.dialog.disable_control("pause")
|
|
|
|
else:
|
|
|
|
self.start_recording()
|
|
|
|
self.dialog.enable_control("pause")
|
|
|
|
|
|
|
|
def start_recording(self):
|
|
|
|
self.dialog.disable_control("attach_exists")
|
|
|
|
self.file = tempfile.mktemp(suffix='.wav')
|
|
|
|
self.recording = sound.recording(self.file)
|
|
|
|
self.recording.play()
|
2015-09-05 11:42:47 -05:00
|
|
|
self.dialog.set("record", _(u"&Stop"))
|
2014-12-29 20:58:30 -06:00
|
|
|
output.speak(_(u"Recording"))
|
|
|
|
|
|
|
|
def stop_recording(self):
|
|
|
|
self.recording.stop()
|
|
|
|
self.recording.free()
|
|
|
|
output.speak(_(u"Stopped"))
|
|
|
|
self.recorded = True
|
2015-09-05 11:42:47 -05:00
|
|
|
self.dialog.set("record", _(u"&Record"))
|
2014-12-29 20:58:30 -06:00
|
|
|
self.file_attached()
|
|
|
|
|
|
|
|
def file_attached(self):
|
2015-09-05 11:42:47 -05:00
|
|
|
self.dialog.set("pause", _(u"&Pause"))
|
2014-12-29 20:58:30 -06:00
|
|
|
self.dialog.disable_control("record")
|
|
|
|
self.dialog.enable_control("play")
|
|
|
|
self.dialog.enable_control("discard")
|
|
|
|
self.dialog.disable_control("attach_exists")
|
|
|
|
self.dialog.enable_control("attach")
|
|
|
|
self.dialog.play.SetFocus()
|
|
|
|
|
|
|
|
def on_discard(self, *args, **kwargs):
|
|
|
|
if self.playing:
|
|
|
|
self._stop()
|
|
|
|
if self.recording != None:
|
2016-05-06 10:38:17 -05:00
|
|
|
self.cleanup()
|
|
|
|
self.dialog.disable_control("attach")
|
|
|
|
self.dialog.disable_control("play")
|
2014-12-29 20:58:30 -06:00
|
|
|
self.file = None
|
|
|
|
self.dialog.enable_control("record")
|
|
|
|
self.dialog.enable_control("attach_exists")
|
|
|
|
self.dialog.record.SetFocus()
|
|
|
|
self.dialog.disable_control("discard")
|
|
|
|
self.recording = None
|
|
|
|
output.speak(_(u"Discarded"))
|
|
|
|
|
|
|
|
def on_play(self, *args, **kwargs):
|
|
|
|
if not self.playing:
|
|
|
|
call_threaded(self._play)
|
|
|
|
else:
|
|
|
|
self._stop()
|
|
|
|
|
|
|
|
def _play(self):
|
|
|
|
output.speak(_(u"Playing..."))
|
|
|
|
# try:
|
|
|
|
self.playing = sound_lib.stream.FileStream(file=unicode(self.file), flags=sound_lib.stream.BASS_UNICODE)
|
|
|
|
self.playing.play()
|
2015-09-05 11:42:47 -05:00
|
|
|
self.dialog.set("play", _(u"&Stop"))
|
2014-12-29 20:58:30 -06:00
|
|
|
try:
|
|
|
|
while self.playing.is_playing:
|
|
|
|
pass
|
2015-09-05 11:42:47 -05:00
|
|
|
self.dialog.set("play", _(u"&Play"))
|
2014-12-29 20:58:30 -06:00
|
|
|
self.playing.free()
|
|
|
|
self.playing = None
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _stop(self):
|
|
|
|
output.speak(_(u"Stopped"))
|
|
|
|
self.playing.stop()
|
|
|
|
self.playing.free()
|
2015-09-05 11:42:47 -05:00
|
|
|
self.dialog.set("play", _(u"&Play"))
|
2014-12-29 20:58:30 -06:00
|
|
|
self.playing = None
|
|
|
|
|
|
|
|
def postprocess(self):
|
|
|
|
if self.file.lower().endswith('.wav'):
|
|
|
|
output.speak(_(u"Recoding audio..."))
|
|
|
|
sound.recode_audio(self.file)
|
|
|
|
self.wav_file = self.file
|
|
|
|
self.file = '%s.ogg' % self.file[:-4]
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
if self.playing and self.playing.is_playing:
|
|
|
|
self.playing.stop()
|
|
|
|
if self.recording != None:
|
|
|
|
if self.recording.is_playing:
|
|
|
|
self.recording.stop()
|
|
|
|
try:
|
|
|
|
self.recording.free()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
os.remove(self.file)
|
|
|
|
if hasattr(self, 'wav_file'):
|
|
|
|
os.remove(self.wav_file)
|
|
|
|
|
|
|
|
def on_attach_exists(self, *args, **kwargs):
|
|
|
|
self.file = self.dialog.get_file()
|
|
|
|
if self.file != False:
|
|
|
|
self.file_attached()
|
|
|
|
|