Added basic replacement for audioRecorder based in MVP+interactor

This commit is contained in:
Manuel Cortez 2019-01-04 17:32:17 -06:00
parent 9d2fc5bc6e
commit 2804400910
6 changed files with 179 additions and 2 deletions

View File

@ -6,7 +6,9 @@ from __future__ import unicode_literals
import os
import logging
import widgetUtils
from . import audioRecorder
import presenters
import interactors
from wxUI.dialogs import audioRecorder as gui2
from mutagen.id3 import ID3
from sessionmanager.utils import seconds_to_string
from wxUI.dialogs import attach as gui
@ -91,7 +93,7 @@ class attach(object):
self.dialog.remove.Enable(True)
def upload_voice_message(self, *args, **kwargs):
a = audioRecorder.audioRecorder()
a = presenters.audioRecorderPresenter(view=gui2.audioRecorderDialog(), interactor=interactors.audioRecorderInteractor())
if a.file != None and a.duration != 0:
audioInfo = {"type": "voice_message", "file": a.file, "from": "local"}
self.attachments.append(audioInfo)

View File

@ -0,0 +1 @@
from . audioRecorder import *

View File

@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import widgetUtils
from pubsub import pub
from . import base
class audioRecorderInteractor(base.baseInteractor):
def install(self, presenter, view, modulename="audiorecorder"):
super(audioRecorderInteractor, self).install(view=view, presenter=presenter, modulename=modulename)
widgetUtils.connect_event(view.play, widgetUtils.BUTTON_PRESSED, self.on_play)
widgetUtils.connect_event(view.record, widgetUtils.BUTTON_PRESSED, self.on_record)
widgetUtils.connect_event(view.discard, widgetUtils.BUTTON_PRESSED, self.on_discard)
def start(self):
result = self.view.get_response()
if result == widgetUtils.OK:
self.on_postprocess()
def on_record(self, *args, **kwargs):
self.presenter.toggle_recording()
def on_discard(self, *args, **kwargs):
self.presenter.discard_recording()
def on_play(self, *args, **kwargs):
self.presenter.play()
def on_postprocess(self):
pass

31
src/interactors/base.py Normal file
View File

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
from pubsub import pub
class baseInteractor(object):
def install(self, view, presenter, modulename):
self.modulename = modulename
self.view = view
self.presenter = presenter
pub.subscribe(self.disable_control, "{modulename}_disable_control".format(modulename=modulename))
pub.subscribe(self.enable_control, "{modulename}_enable_control".format(modulename=modulename))
pub.subscribe(self.set_label, "{modulename}_set_label".format(modulename=modulename))
pub.subscribe(self.focus_control, "{modulename}_focus_control".format(modulename=modulename))
def uninstall(self):
pub.unsubscribe(self.disable_control, "{modulename}_disable_control".format(modulename=self.modulename))
pub.unsubscribe(self.enable_control, "{modulename}_enable_control".format(modulename=self.modulename))
pub.unsubscribe(self.set_label, "{modulename}_set_label".format(modulename=self.modulename))
pub.unsubscribe(self.focus_control, "{modulename}_focus_control".format(modulename=self.modulename))
def disable_control(self, control):
self.view.disable(control)
def enable_control(self, control):
self.view.enable(control)
def focus_control(self, control):
getattr(self.view, control).SetFocus()
def set_label(self, control, label):
self.view.set(control, label)

View File

@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from .audioRecorder import *

View File

@ -0,0 +1,112 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import time
import os
import tempfile
import sound_lib
import sound
import output
from pubsub import pub
from mysc.thread_utils import call_threaded
class audioRecorderPresenter(object):
def __init__(self, view, interactor):
self.view = view
self.interactor = interactor
self.interactor.install(view=view, presenter=self)
self.recorded = False
self.recording = None
self.duration = 0
self.playing = None
self.interactor.start()
def toggle_recording(self, *args, **kwargs):
if self.recording != None:
self.stop_recording()
else:
self.start_recording()
def start_recording(self):
self.file = tempfile.mktemp(suffix='.wav')
self.recording = sound.get_recording(self.file)
self.duration = time.time()
self.recording.play()
pub.sendMessage("audiorecorder_set_label", control="record", label=_("&Stop"))
output.speak(_("Recording"))
pub.sendMessage("audiorecorder_disable_control", control="ok")
def stop_recording(self):
self.recording.stop()
self.duration = int(time.time()-self.duration)
self.recording.free()
output.speak(_("Stopped"))
self.recorded = True
pub.sendMessage("audiorecorder_set_label", control="record", label=_("&Record"))
pub.sendMessage("audiorecorder_disable_control", control="record")
pub.sendMessage("audiorecorder_enable_control", control="play")
pub.sendMessage("audiorecorder_enable_control", control="discard")
pub.sendMessage("audiorecorder_enable_control", control="ok")
pub.sendMessage("audiorecorder_focus_control", control="play")
def discard_recording(self, *args, **kwargs):
if self.playing:
self._stop()
if self.recording != None:
self.cleanup()
pub.sendMessage("audiorecorder_disable_control", control="play")
pub.sendMessage("audiorecorder_disable_control", control="ok")
self.file = None
pub.sendMessage("audiorecorder_enable_control", control="record")
pub.sendMessage("audiorecorder_focus_control", control="record")
pub.sendMessage("audiorecorder_disable_control", control="discard")
self.recording = None
output.speak(_("Discarded"))
def play(self, *args, **kwargs):
if not self.playing:
call_threaded(self._play)
else:
self._stop()
def _play(self):
output.speak(_("Playing..."))
# try:
self.playing = sound_lib.stream.FileStream(file=str(self.file), flags=sound_lib.stream.BASS_UNICODE)
self.playing.play()
pub.sendMessage("audiorecorder_set_label", control="play", label=_("&Stop"))
try:
while self.playing.is_playing:
pass
pub.sendMessage("audiorecorder_set_label", control="play", label=_("&Play"))
self.playing.free()
self.playing = None
except:
pass
def _stop(self):
output.speak(_("Stopped"))
self.playing.stop()
self.playing.free()
pub.sendMessage("audiorecorder_set_label", control="play", label=_("&Play"))
self.playing = None
def postprocess(self):
if self.file.lower().endswith('.wav'):
output.speak(_("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)