[gnome-sound-recorder] record: convert Record JS Object to GObject



commit 30a9405f755885336b7aa9e590b7f713686cbd09
Author: Kavan Mevada <kavanmevada gmail com>
Date:   Sat May 30 19:56:36 2020 +0530

    record: convert Record JS Object to GObject

 src/application.js |  7 -------
 src/mainWindow.js  | 29 +++++++++++++++++---------
 src/record.js      | 60 ++++++++++++++++++++++++++++--------------------------
 src/waveform.js    | 10 +--------
 4 files changed, 51 insertions(+), 55 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 1e1d28a..3792cfe 100644
--- a/src/application.js
+++ b/src/application.js
@@ -103,13 +103,6 @@ var Application = GObject.registerClass(class Application extends Gtk.Applicatio
             this.window.get_style_context().add_class('devel');
     }
 
-    onWindowDestroy() {
-        if (MainWindow.wave.pipeline)
-            MainWindow.wave.pipeline.set_state(Gst.State.NULL);
-        if (MainWindow._record.pipeline)
-            MainWindow._record.pipeline.set_state(Gst.State.NULL);
-    }
-
     _loadStyleSheet() {
         let provider = new Gtk.CssProvider();
         provider.load_from_resource('/org/gnome/SoundRecorder/application.css');
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 5485b15..b6db8c5 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -1,4 +1,4 @@
-/* exported MainWindow view wave */
+/* exported MainWindow view */
 /*
 * Copyright 2013 Meg Ford
 * This library is free software; you can redistribute it and/or
@@ -32,7 +32,6 @@ const Record = imports.record;
 const Waveform = imports.waveform;
 
 var view = null;
-var wave = null;
 
 var MainWindow = GObject.registerClass({
     Template: 'resource:///org/gnome/SoundRecorder/ui/window.ui',
@@ -48,7 +47,21 @@ var MainWindow = GObject.registerClass({
         this.player = new Player();
         view = this;
 
-        this.connect('destroy', () => this.player.stop());
+        this._record.connect('waveform', (_, time, peak) => {
+            if (this.wave)
+                this.wave._drawEvent(time, peak);
+        });
+
+        this._record.connect('notify::duration', _record => {
+            this._recordTimeLabel.label = Utils.Time.formatTime(_record.duration);
+        });
+
+        this.connect('destroy', () => {
+            if (this.wave)
+                this.wave.endDrawing();
+            this.player.stop();
+            this._record.stopRecording();
+        });
 
         this._recordingList = new RecordingList();
         this._refreshView();
@@ -91,7 +104,7 @@ var MainWindow = GObject.registerClass({
         this._recordGrid.show();
         this._record.startRecording();
 
-        wave = new Waveform.WaveForm(this._recordGrid, null);
+        this.wave = new Waveform.WaveForm(this._recordGrid, null);
     }
 
     _onRecordStop() {
@@ -104,7 +117,8 @@ var MainWindow = GObject.registerClass({
         let recording = new Recording(recordedFile);
         this._recordingList.insert(0, recording);
 
-        wave = null;
+        this.wave.endDrawing();
+        this.wave = null;
     }
 
     _refreshView() {
@@ -113,9 +127,4 @@ var MainWindow = GObject.registerClass({
         else
             this._mainStack.set_visible_child_name('mainView');
     }
-
-    setRecordTimeLabel(time) {
-        let timeLabelString = Utils.Time.formatTime(time);
-        this._recordTimeLabel.label = timeLabelString;
-    }
 });
diff --git a/src/record.js b/src/record.js
index a0eb4bd..8ca2478 100644
--- a/src/record.js
+++ b/src/record.js
@@ -24,10 +24,9 @@ const Gst = imports.gi.Gst;
 const GstAudio = imports.gi.GstAudio;
 const GstPbutils = imports.gi.GstPbutils;
 const Gtk = imports.gi.Gtk;
+const GObject = imports.gi.GObject;
 
-const Application = imports.application;
 const Settings = imports.preferences;
-const MainWindow = imports.mainWindow;
 
 var PipelineStates = {
     PLAYING: 0,
@@ -78,7 +77,18 @@ var EncodingProfiles = [
         mimeType: 'audio/mpeg' },
 ];
 
-var Record = class Record {
+var Record = new GObject.registerClass({
+    Properties: {
+        'duration': GObject.ParamSpec.int(
+            'duration',
+            'Recording Duration', 'Recording duration in seconds',
+            GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT,
+            0, GLib.MAXINT16, 0),
+    },
+    Signals: {
+        'waveform': { param_types: [GObject.TYPE_INT, GObject.TYPE_FLOAT] },
+    },
+}, class Record extends GObject.Object {
     _recordPipeline() {
         errorDialogState = ErrState.OFF;
         this.baseTime = 0;
@@ -120,7 +130,6 @@ var Record = class Record {
         this.recordBus.connect('message', (recordBus, message) => {
             if (message !== null)
                 this._onMessageReceived(message);
-
         });
         this.level = Gst.ElementFactory.make('level', 'level');
         this.pipeline.add(this.level);
@@ -150,19 +159,6 @@ var Record = class Record {
             errorDialogState = ErrState.ON;
             this.onEndOfStream();
         }
-
-        GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, Application.SIGINT, 
Application.application.onWindowDestroy);
-        GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, Application.SIGTERM, 
Application.application.onWindowDestroy);
-    }
-
-    _updateTime() {
-        let time = this.pipeline.query_position(Gst.Format.TIME)[1] / Gst.SECOND;
-
-        if (time >= 0)
-            MainWindow.view.setRecordTimeLabel(time, 0);
-
-
-        return true;
     }
 
     startRecording() {
@@ -180,9 +176,12 @@ var Record = class Record {
             this.volume.set_volume(GstAudio.StreamVolumeFormat.CUBIC, Settings.settings.micVolume);
         }
 
-        if (!this.timeout)
-            this.timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, _SEC_TIMEOUT, () => this._updateTime());
-
+        this.timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, _SEC_TIMEOUT, () => {
+            const pos = this.pipeline.query_position(Gst.Format.TIME)[1];
+            if (pos > 0)
+                this.duration = pos / Gst.SECOND;
+            return true;
+        });
     }
 
     stopRecording() {
@@ -193,8 +192,7 @@ var Record = class Record {
             this.timeout = null;
         }
 
-        if (MainWindow.wave !== null)
-            MainWindow.wave.endDrawing();
+        this.duration = 0;
     }
 
     onEndOfStream() {
@@ -204,8 +202,6 @@ var Record = class Record {
         if (this.recordBus)
             this.recordBus.remove_signal_watch();
 
-
-        this._updateTime();
         errorDialogState = ErrState.OFF;
     }
 
@@ -263,9 +259,7 @@ var Record = class Record {
 
                         this.runTime = this.absoluteTime - this.baseTime;
                         let approxTime = Math.round(this.runTime / _TENTH_SEC);
-
-                        if (MainWindow.wave !== null)
-                            MainWindow.wave._drawEvent(approxTime, this.peak);
+                        this.emit('waveform', approxTime, this.peak);
                     }
                 }
             }
@@ -329,7 +323,6 @@ var Record = class Record {
             errorDialog.set_transient_for(Gio.Application.get_default().get_active_window());
             errorDialog.connect('response', () => {
                 errorDialog.destroy();
-                MainWindow.view.onRecordStopClicked();
                 this.onEndOfStream();
             });
             errorDialog.show();
@@ -351,7 +344,16 @@ var Record = class Record {
         return containerProfile;
     }
 
-};
+    get duration() {
+        return this._duration;
+    }
+
+    set duration(val) {
+        this._duration = val;
+        this.notify('duration');
+    }
+
+});
 
 const BuildFileName = class BuildFileName {
     buildInitialFilename() {
diff --git a/src/waveform.js b/src/waveform.js
index 208454a..88ca63c 100644
--- a/src/waveform.js
+++ b/src/waveform.js
@@ -21,13 +21,9 @@
 // based on code from Pitivi
 
 const Cairo = imports.cairo;
-const GLib = imports.gi.GLib;
 const Gst = imports.gi.Gst;
 const Gtk = imports.gi.Gtk;
 
-
-const Application = imports.application;
-
 const INTERVAL = 100000000;
 const peaks = [];
 const pauseVal = 10;
@@ -81,16 +77,12 @@ var WaveForm = class WaveForm {
         this._level = this.pipeline.get_by_name('level');
         let bus = this.pipeline.get_bus();
         bus.add_signal_watch();
-        GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, Application.SIGINT, 
Application.application.onWindowDestroy);
-        GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, Application.SIGTERM, 
Application.application.onWindowDestroy);
 
         this.nSamples = Math.ceil(this.duration / INTERVAL);
 
         bus.connect('message', message => {
             if (message !== null)
                 this._messageCb(message);
-
-
         });
     }
 
@@ -188,12 +180,12 @@ var WaveForm = class WaveForm {
                 if (start >= 40 && xAxis === 0)
                     cr.moveTo(xAxis * pixelsPerSample, waveheight);
 
-
                 cr.lineTo(xAxis * pixelsPerSample, waveheight - peaks[i] * waveheight);
             }
 
             xAxis += 1;
         }
+
         cr.lineTo(xAxis * pixelsPerSample, waveheight);
         cr.closePath();
         cr.strokePreserve();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]