summaryrefslogtreecommitdiff
path: root/interface/audioview.py
blob: ebfe22838fc80f45e9c91600071679fe0f0841a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import curses
import threading

from .globals import CURSES_LOCK

def format_time(t):
    t = int(t/1000 + 0.5)
    h = (t // 3600)
    m = t % 3600

    s = m % 60
    m = m // 60

    if not m and not h:
        return "%2ds" % s
    elif not h:
        return "%2dm%2ds" % (m, s)
    return "%dh%2dm%2ds" % (h, m, s)

class AudioView:
    def set_dim(self, height, width):
        with CURSES_LOCK, self._lock:
            if (height, width) != (self._height, self._width):
                self.win.erase()
                self.win.refresh()

                self.win.resize(height, width)
                self.win.redrawwin()

            self._height = height
            self._width = width
            self._refresh()
            self.win.refresh()

    def set_pos(self, y, x):
        with self._lock:
            if (y, x) != (self._y, self._x):
                with CURSES_LOCK:
                    self.win.mvwin(y, x)
                    self._put_title()
                    self.win.refresh()

    @property
    def audio(self):
        return self._audio

    @audio.setter 
    def audio(self, v):
        with self._lock:
            self._audio = v
            self._refresh()

    @property
    def title(self):
        return self._title

    @title.setter 
    def title(self, value):
        with self._lock:
            self._title = value
            self._put_title()
            self.win.refresh()

    @property
    def highlight(self):
        return self._highlight

    @highlight.setter
    def highlight(self, value):
        with self._lock:
            self._highlight = value
            self._put_title()
            self.win.refresh()

    def _put_title(self):
        self.win.border()
        pos = min(self._width-2-len(self._title), (3*self._width)//4 - (len(self._title) // 2))
        self.win.addstr(self._height-1, pos, self._title, curses.A_REVERSE if self._highlight else 0)

    def _refresh(self):
        with CURSES_LOCK:
            self.win.erase()

            if self._audio is not None:
                self.win.addstr(1, 1, "Filename: "+str(self._audio.filename))
                self.win.addstr(2, 1, " Fade in: "+str(self._audio.fade_in)+"ms")
                self.win.addstr(3, 1, "Duration: "+str(format_time(self._audio.duration)))
                self.win.addstr(4, 1, "Fade out: "+str(self._audio.fade_out)+"ms")

            self._put_title()
            self.win.refresh()

    def __init__(self, y, x, height, width):
        with CURSES_LOCK:
            self.win = curses.newwin(height, width, y, x)
            self.win.keypad(True)

        self._lock = threading.RLock()
        self._highlight = False
        self._title = "Audio"
        self._height = height
        self._width = width
        self._y = -1
        self._x = -1

        self._audio = None

        self.set_pos(y, x)
        self.set_dim(height, width)