40 gi.require_version(
'Gst',
'1.0')
41 gi.require_version(
'GstAudio',
'1.0')
42 from gi.repository
import GObject
43 from gi.repository
import Gst
44 from gi.repository
import GstAudio
45 GObject.threads_init()
52 __author__ =
"Kipp Cannon <kipp.cannon@ligo.org>, Chad Hanna <chad.hanna@ligo.org>, Drew Keppel <drew.keppel@ligo.org>" 66 def repack_complex_array_to_real(arr):
68 Repack a complex-valued array into a real-valued array with twice 69 as many columns. Used to set complex arrays as values on elements 70 that expose them as real-valued array properties (gobject doesn't 71 understand complex numbers). The return value is a view into the 76 if arr.dtype.kind !=
"c":
78 assert arr.dtype.itemsize % 2 == 0
79 return arr.view(dtype = numpy.dtype(
"f%d" % (arr.dtype.itemsize // 2)))
82 def repack_real_array_to_complex(arr):
84 Repack a real-valued array into a complex-valued array with half as 85 many columns. Used to retrieve complex arrays from elements that 86 expose them as real-valued array properties (gobject doesn't 87 understand complex numbers). The return value is a view into the 92 if arr.dtype.kind !=
"f":
94 return arr.view(dtype = numpy.dtype(
"c%d" % (arr.dtype.itemsize * 2)))
106 def get_unit_size(caps):
108 name = struct.get_name()
109 if name ==
"audio/x-raw":
110 info = GstAudio.AudioInfo()
113 elif name ==
"video/x-raw" and struct[
"format"]
in (
"RGB",
"RGBA",
"ARGB",
"ABGR"):
114 return struct[
"width"] * struct[
"height"] * (3
if struct[
"format"] ==
"RGB" else 4)
115 raise ValueError(caps)
118 def numpy_dtype_from_caps(caps):
120 GstAudio.AudioFormat.F32: numpy.dtype(
"float32"),
121 GstAudio.AudioFormat.F64: numpy.dtype(
"float64"),
122 GstAudio.AudioFormat.S8: numpy.dtype(
"int8"),
123 GstAudio.AudioFormat.U8: numpy.dtype(
"uint8"),
124 GstAudio.AudioFormat.S16: numpy.dtype(
"int16"),
125 GstAudio.AudioFormat.U16: numpy.dtype(
"uint16"),
126 GstAudio.AudioFormat.S32: numpy.dtype(
"int32"),
127 GstAudio.AudioFormat.U32: numpy.dtype(
"uint32")
129 info = GstAudio.AudioInfo()
131 return formats_dict[info.finfo.format]
134 def format_string_from_numpy_dtype(dtype):
136 numpy.dtype(
"float32"): GstAudio.AudioFormat.F32,
137 numpy.dtype(
"float64"): GstAudio.AudioFormat.F64,
138 numpy.dtype(
"int8"): GstAudio.AudioFormat.S8,
139 numpy.dtype(
"uint8"): GstAudio.AudioFormat.U8,
140 numpy.dtype(
"int16"): GstAudio.AudioFormat.S16,
141 numpy.dtype(
"uint16"): GstAudio.AudioFormat.U16,
142 numpy.dtype(
"int32"): GstAudio.AudioFormat.S32,
143 numpy.dtype(
"uint32"): GstAudio.AudioFormat.U32
145 return GstAudio.AudioFormat.to_string(formats_dict[dtype])
148 def caps_from_array(arr, rate = None):
149 return Gst.Caps.from_string(
"audio/x-raw, format=(string)%s, rate=(int)%d, channels=(int)%d, layout=(string)interleaved, channel-mask=(bitmask)0" % (format_string_from_numpy_dtype(arr.dtype), rate, arr.shape[1]))
152 def array_from_audio_sample(sample):
153 caps = sample.get_caps()
154 bufmap = sample.get_buffer().map(Gst.MapFlags.READ)[1]
155 (success, channels) = caps.get_structure(0).get_int(
"channels")
160 a = numpy.frombuffer(bufmap.data, dtype = numpy_dtype_from_caps(caps))
162 a = numpy.array((), dtype = numpy_dtype_from_caps(caps))
163 return numpy.reshape(a, (len(a) // channels, channels))
166 def audio_buffer_from_array(arr, timestamp, offset, rate):
167 buf = Gst.Buffer.new_wrapped(numpy.getbuffer(arr))
169 buf.duration = (Gst.SECOND * arr.shape[0] + rate // 2) // rate
171 buf.offset_end = offset + arr.shape[0]
184 def parse_spectrum_message(message):
186 Parse a "spectrum" message from the lal_whiten element, return a 187 LAL REAL8FrequencySeries containing the strain spectral density. 189 s = message.get_structure()
190 psd = lal.CreateREAL8FrequencySeries(
191 name = s[
"instrument"]
if s.has_field(
"instrument")
else "",
192 epoch = lal.LIGOTimeGPS(0, message.timestamp),
194 deltaF = s[
"delta-f"],
195 sampleUnits = lal.Unit(s[
"sample-units"].strip()),
196 length = len(s[
"magnitude"])
198 psd.data.data = numpy.array(s[
"magnitude"])
211 def parse_framesrc_tags(taglist):
213 instrument = taglist[
"instrument"]
217 channel_name = taglist[
"channel-name"]
220 if "units" in taglist:
221 sample_units = lal.Unit(taglist[
"units"].strip())
225 "instrument": instrument,
226 "channel-name": channel_name,
227 "sample-units": sample_units