File: timer.vala

package info (click to toggle)
gnome-metronome 0.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 388 kB
  • sloc: xml: 22; python: 12; makefile: 3
file content (77 lines) | stat: -rw-r--r-- 2,486 bytes parent folder | download
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
/* timer.vala
 *
 * Copyright 2020 João Vieira <jvieira.tpt@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */


public class Metronome.Timer : GLib.Object {
    private uint timeout_id;

    public double volume { get; set; default = 1; }
    public uint beat { get; set; default = 0; }
    public uint bpm { get; set; default = 100; }
    public uint bpb { get; set; default = 4; }
    public uint cpb { get; set; default = 1; }
    private Gst.Element pipeline;

    public void start_timer () {
        beat = (beat % bpb) + 1;
        var i = 1;

        play (volume, beat == 1 && i == 1);
        timeout_id = GLib.Timeout.add (60000 / bpm / cpb, () => {
            if (i < cpb) {
                pipeline.set_state (Gst.State.NULL);
                i++;
                play (volume, beat == 1 && i == 1);
            }
            else {
                pipeline.set_state (Gst.State.NULL);
                beat = (beat % bpb) + 1;
                i = 1;
                play (volume, beat == 1 && i == 1);
            }
            return GLib.Source.CONTINUE;
        });
    }

    public void stop_timer () {
        pipeline.set_state (Gst.State.NULL);
        GLib.Source.remove (timeout_id);
    }

    public void play (double volume, bool first_beep) {
        string command;

        if (first_beep) {
            command = "playbin uri=resource:///com/jvieira/tpt/Metronome/sounds/beep_high.ogg volume=%g".printf (volume);
        }
        else {
            command = "playbin uri=resource:///com/jvieira/tpt/Metronome/sounds/beep_low.ogg volume=%g".printf (volume);
        }

        try {
            pipeline = Gst.parse_launch (command);
        }
        catch (Error e) {
            stderr.printf ("Error: %s\n", e.message);
        }

        pipeline.set_state (Gst.State.PLAYING);
    }
}