properly handle recorder/recording duration
- default to 0 nanoseconds
- make use of Date to get miliseconds easily
- use tnum font feature
Bilal Elmoussaoui
3 years ago
28 | 28 | <property name="visible">True</property> |
29 | 29 | <property name="can_focus">False</property> |
30 | 30 | <property name="margin_top">18</property> |
31 | <property name="use_markup">True</property> | |
32 | <attributes> | |
33 | <attribute name="font-features" value="tnum=1"/> | |
34 | </attributes> | |
31 | 35 | <style> |
32 | 36 | <class name="dim-label"/> |
33 | 37 | <class name="recorder-time-label"/> |
30 | 30 | <property name="visible">True</property> |
31 | 31 | <property name="can_focus">False</property> |
32 | 32 | <property name="halign">end</property> |
33 | <property name="use_markup">True</property> | |
34 | <attributes> | |
35 | <attribute name="font-features" value="tnum=1"/> | |
36 | </attributes> | |
33 | 37 | </object> |
34 | 38 | <packing> |
35 | 39 | <property name="pack_type">end</property> |
62 | 62 | Properties: { |
63 | 63 | 'duration': GObject.ParamSpec.int( |
64 | 64 | 'duration', |
65 | 'Recording Duration', 'Recording duration in seconds', | |
65 | 'Recording Duration', 'Recording duration in nanoseconds', | |
66 | 66 | GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT, |
67 | 67 | 0, GLib.MAXINT16, 0), |
68 | 68 | 'current-peak': GObject.ParamSpec.float( |
130 | 130 | this.timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 100, () => { |
131 | 131 | const pos = this.pipeline.query_position(Gst.Format.TIME)[1]; |
132 | 132 | if (pos > 0) |
133 | this.duration = pos / Gst.SECOND; | |
133 | this.duration = pos; | |
134 | 134 | return true; |
135 | 135 | }); |
136 | 136 | } |
146 | 146 | |
147 | 147 | stop() { |
148 | 148 | this.state = Gst.State.NULL; |
149 | ||
149 | this.duration = 0; | |
150 | 150 | if (this.timeout) { |
151 | 151 | GLib.source_remove(this.timeout); |
152 | 152 | this.timeout = null; |
34 | 34 | |
35 | 35 | this.recorder.bind_property('current-peak', this.waveform, 'peak', GObject.BindingFlags.SYNC_CREATE | GObject.BindingFlags.DEFAULT); |
36 | 36 | this.recorder.connect('notify::duration', _recorder => { |
37 | this._recorderTime.label = formatTime(_recorder.duration); | |
37 | this._recorderTime.set_markup(formatTime(_recorder.duration)); | |
38 | 38 | }); |
39 | 39 | |
40 | 40 |
11 | 11 | Properties: { |
12 | 12 | 'duration': GObject.ParamSpec.int( |
13 | 13 | 'duration', |
14 | 'Recording Duration', 'Recording duration in seconds', | |
14 | 'Recording Duration', 'Recording duration in nanoseconds', | |
15 | 15 | GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT, |
16 | 16 | 0, GLib.MAXINT16, 0), |
17 | 17 | 'name': GObject.ParamSpec.string( |
46 | 46 | discoverer.start(); |
47 | 47 | discoverer.connect('discovered', (_discoverer, audioInfo) => { |
48 | 48 | this._duration = audioInfo.get_duration(); |
49 | ||
49 | 50 | this.notify('duration'); |
50 | 51 | }); |
51 | 52 |
0 | 0 | /* exported Row */ |
1 | const { Gdk, Gio, GObject, Gst, Gtk } = imports.gi; | |
1 | const { Gdk, Gio, GObject, Gtk } = imports.gi; | |
2 | 2 | const { displayDateTime, formatTime } = imports.utils; |
3 | 3 | const { WaveForm, WaveType } = imports.waveform; |
4 | 4 | |
153 | 153 | |
154 | 154 | // Force LTR, we don't want reverse hh:mm::ss |
155 | 155 | this._duration.direction = Gtk.TextDirection.LTR; |
156 | this._duration.label = formatTime(recording.duration / Gst.SECOND); | |
156 | this._duration.markup = formatTime(recording.duration); | |
157 | 157 | recording.connect('notify::duration', () => { |
158 | this._duration.label = formatTime(recording.duration / Gst.SECOND); | |
158 | this._duration.label = formatTime(recording.duration); | |
159 | 159 | }); |
160 | 160 | } |
161 | 161 |
17 | 17 | * |
18 | 18 | */ |
19 | 19 | const Gettext = imports.gettext; |
20 | const GLib = imports.gi.GLib; | |
20 | const { GLib, Gst } = imports.gi; | |
21 | 21 | |
22 | var formatTime = totalSeconds => { | |
23 | totalSeconds = Math.floor(totalSeconds); | |
24 | const hours = parseInt(totalSeconds / Math.pow(60, 2)).toString(); | |
25 | const minutes = (parseInt(totalSeconds / 60) % 60).toString(); | |
26 | const seconds = parseInt(totalSeconds % 60).toString(); | |
22 | var formatTime = nanoSeconds => { | |
23 | const time = new Date(0, 0, 0, 0, 0, 0, parseInt(nanoSeconds / Gst.MSECOND)); | |
27 | 24 | |
28 | return `${hours.padStart(2, '0')}∶${minutes.padStart(2, '0')}∶${seconds.padStart(2, '0')}`; | |
25 | const miliseconds = time.getMilliseconds().toString().padStart(2, '0').substring(0, 2); | |
26 | const seconds = time.getSeconds().toString().padStart(2, '0'); | |
27 | const minutes = time.getMinutes().toString().padStart(2, '0'); | |
28 | const hours = time.getHours().toString().padStart(2, '0'); | |
29 | ||
30 | // eslint-disable-next-line no-irregular-whitespace | |
31 | return `${hours} ∶ ${minutes} ∶ ${seconds} . <small>${miliseconds}</small>`; | |
29 | 32 | }; |
30 | 33 | |
31 | 34 | var displayDateTime = time => { |