diff options
Diffstat (limited to 'interface/audioview.py')
| -rw-r--r-- | interface/audioview.py | 109 | 
1 files changed, 109 insertions, 0 deletions
diff --git a/interface/audioview.py b/interface/audioview.py new file mode 100644 index 0000000..ebfe228 --- /dev/null +++ b/interface/audioview.py @@ -0,0 +1,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)  | 
