This repository has been archived by the owner on Jun 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audiorecord.py
161 lines (136 loc) · 6.07 KB
/
audiorecord.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from gettext import gettext as _
import tempfile
import os
import shutil
import gst
import gtk
from fvumeter import FVUMeter
ui = """
<interface>
<requires lib="gtk+" version="2.16"/>
<object class="GtkButton" id="record_button">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="relief">none</property>
<child>
<object class="GtkImage" id="button_image">
<property name="visible">True</property>
<property name="stock">gtk-media-record</property>
</object>
</child>
</object>
</interface>
"""
def clamp(x, min, max):
if x < min:
return min
elif x > max:
return max
return x
class AudioRecorder(gtk.HBox):
def __init__(self, instance):
gtk.HBox.__init__(self)
self.app = instance
self.audiosrc = gst.element_factory_make("autoaudiosrc", "audiosrc")
self.level = gst.element_factory_make("level", "level")
self.encoder = gst.element_factory_make("flacenc", "encoder")
self.sink = gst.element_factory_make("fdsink", "sink")
self.level.set_property("message", True)
self.recording_bin = gst.Bin()
self.recording_bin.add(self.audiosrc, self.level, self.encoder, self.sink)
gst.element_link_many (self.audiosrc, self.level, self.encoder, self.sink)
self._createUI()
self.is_recording = False
def _createUI(self):
builder = gtk.Builder()
builder.add_from_string(ui)
builder.add_from_file(os.path.join(os.path.dirname(__file__), "audiosave.glade"))
self.audiosave_dialog = builder.get_object("audiosave_dialog")
self.audiosave_dialog.filename_entry = builder.get_object("filename")
self.audiosave_dialog.folderchooser = builder.get_object("folderchooser")
self.record_button = builder.get_object("record_button")
self.button_image = builder.get_object("button_image")
self.vumeter = FVUMeter()
self.pack_start(self.record_button, expand=False, fill=False)
eventbox = gtk.EventBox()
eventbox.add(self.vumeter)
self.pack_start(eventbox, expand=True, fill=True)
self.set_size_request(300, -1)
self.record_button.set_sensitive(True)
self.record_button.connect("clicked", self._recordCb)
def _on_message(self, bus, message):
if message.structure.get_name() == 'level':
s = message.structure
self.vumeter.freeze_notify()
decay = clamp(s['decay'][0], -90.0, 0.0)
peak = clamp(s['peak'][0], -90.0, 0.0)
self.vumeter.set_property('decay', decay)
self.vumeter.set_property('peak', peak)
return True
def _recordCb(self, widget):
if not self.is_recording:
self.start_recording()
self.is_recording = True
self.button_image.set_from_stock(gtk.STOCK_MEDIA_STOP, gtk.ICON_SIZE_LARGE_TOOLBAR)
else:
self.stop_recording()
self.is_recording = False
self.button_image.set_from_stock(gtk.STOCK_MEDIA_RECORD, gtk.ICON_SIZE_LARGE_TOOLBAR)
def start_recording(self):
self._tempfd, self._temppath = tempfile.mkstemp()
self.sink.set_property("fd", self._tempfd)
self._cb_id = self.app.current.pipeline.getBus().connect("message::element", self._on_message)
self.app.current.pipeline.addBin(self.recording_bin)
self.app.current.pipeline.play()
def stop_recording(self):
self.app.current.pipeline.getBus().disconnect(self._cb_id)
self.app.current.pipeline.stop()
self.app.current.pipeline.removeBin(self.recording_bin)
os.close(self._tempfd) # assuming gstreamer doesn't close the fd. Should check
self._save_file()
def _save_file(self):
self.audiosave_dialog.set_transient_for(self.app.gui)
response = self.audiosave_dialog.run()
if response == gtk.RESPONSE_OK:
self.file_path = os.path.join(self.audiosave_dialog.folderchooser.get_filename(),
self.audiosave_dialog.filename_entry.get_text())
if os.path.exists(self.file_path):
if self._sourceExists('file:///' + self.file_path):
iBox = gtk.MessageDialog(self.audiosave_dialog,
gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
_("File already exists in the project and cannot be overwritten. Please choose a different filename"))
iBox.run()
iBox.destroy()
self._save_file()
return
mBox = gtk.MessageDialog(self.audiosave_dialog,
gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO,
_("File already exists. Do you want to overwrite it?"))
mresponse = mBox.run()
mBox.destroy()
if mresponse == gtk.RESPONSE_NO:
self._save_file()
return
shutil.copy(self._temppath, self.file_path)
self.app.current.sources.connect("source-added", self._sourceAddedCb)
self.app.current.sources.addUris(['file:///' + self.file_path])
else:
os.remove(self._temppath)
self.audiosave_dialog.hide()
self.audiosave_dialog.filename_entry.set_text("")
def _sourceExists(self, uri):
try:
self.app.current.sources.getUri(uri)
return True
except:
return False
def _sourceAddedCb(self, unused, factory):
# Add recorded file to timeline
# TODO: Check to make sure that we're adding the correct factory
timeline_obj = self.app.current.timeline.addSourceFactory(factory)
self.app.current.sources.disconnect_by_func(self._sourceAddedCb)
def show_all(self):
HBox.show_all(self)
self.vumeter.show()