Mercurial > libervia-desktop-kivy
annotate cagou/plugins/plugin_transfer_voice.py @ 282:1b835bcfa663
date update
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 25 Mar 2019 07:11:09 +0100 |
parents | cd99f70ea592 |
children | ca4daced4638 |
rev | line source |
---|---|
95
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client |
282 | 5 # Copyright (C) 2016-2019 Jérôme Poisson (goffi@goffi.org) |
95
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core import log as logging |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 log = logging.getLogger(__name__) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.core.i18n import _ |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from kivy.uix.boxlayout import BoxLayout |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 import sys |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 import time |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from kivy.clock import Clock |
96
641678ddc26c
upload (voice): fixed crash on desktop
Goffi <goffi@goffi.org>
parents:
95
diff
changeset
|
28 from kivy import properties |
95
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 if sys.platform == "android": |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 from plyer import audio |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 PLUGIN_INFO = { |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 "name": _(u"voice"), |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 "main": "VoiceRecorder", |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 "platforms": ["android"], |
97
5d2289127bb7
menu (upload): better menu using dedicated widget:
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
37 "description": _(u"transmit a voice record"), |
5d2289127bb7
menu (upload): better menu using dedicated widget:
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
38 "icon_medium": u"{media}/icons/muchoslava/png/micro_off_50.png", |
95
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 } |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 class VoiceRecorder(BoxLayout): |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 callback = properties.ObjectProperty() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 cancel_cb = properties.ObjectProperty() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 recording = properties.BooleanProperty(False) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 playing = properties.BooleanProperty(False) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 time = properties.NumericProperty(0) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 def __init__(self, **kwargs): |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 super(VoiceRecorder, self).__init__(**kwargs) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 self._started_at = None |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 self._counter_timer = None |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 self._play_timer = None |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 self.record_time = None |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 self.audio = audio |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 self.audio.file_path = "/sdcard/cagou_record.3gp" |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 def _updateTimer(self, dt): |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 self.time = int(time.time() - self._started_at) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 def switchRecording(self): |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 if self.playing: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 self._stopPlaying() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 if self.recording: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 try: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 audio.stop() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 except Exception as e: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 # an exception can happen if record is pressed |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 # repeatedly in a short time (not a normal use) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 log.warning(u"Exception on stop: {}".format(e)) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 self._counter_timer.cancel() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 self.time = self.time + 1 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 else: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 audio.start() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 self._started_at = time.time() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 self.time = 0 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 self._counter_timer = Clock.schedule_interval(self._updateTimer, 1) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 self.recording = not self.recording |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 def _stopPlaying(self, dummy=None): |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 if self.record_time is None: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 log.error("_stopPlaying should no be called when record_time is None") |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 return |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 audio.stop() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 self.playing = False |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 self.time = self.record_time |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 if self._counter_timer is not None: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 self._counter_timer.cancel() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 def playRecord(self): |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 if self.recording: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 return |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 if self.playing: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 self._stopPlaying() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 else: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 try: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 audio.play() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 except Exception as e: |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 # an exception can happen in the same situation |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 # as for audio.stop() above (i.e. bad record) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 log.warning(u"Exception on play: {}".format(e)) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 self.time = 0 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 return |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 self.playing = True |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 self.record_time = self.time |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 Clock.schedule_once(self._stopPlaying, self.time + 1) |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 self._started_at = time.time() |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 self.time = 0 |
3e3c097b07b7
upload: added voice plugin (for Android)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 self._counter_timer = Clock.schedule_interval(self._updateTimer, 0.5) |