diff options
author | Thibault Saunier <tsaunier@igalia.com> | 2019-12-11 08:14:16 -0300 |
---|---|---|
committer | Thibault Saunier <tsaunier@igalia.com> | 2019-12-11 08:32:44 -0300 |
commit | 986d6d12fb56f08f4acbdb75a0637018849418fa (patch) | |
tree | b77464776cc24d18ae44f84a21401b4e37c7f8e8 | |
parent | d365954fc0793d3d6edc12a1532682e676a753d0 (diff) |
example: Use do_fill in AudioTestSrc instead of do_create
With the new mapping API we can efficiently use the ->fill vmethod
which is sensibly better.
-rw-r--r-- | examples/plugins/python/py_audiotestsrc.py | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/examples/plugins/python/py_audiotestsrc.py b/examples/plugins/python/py_audiotestsrc.py index 6d5ff21..08e1269 100644 --- a/examples/plugins/python/py_audiotestsrc.py +++ b/examples/plugins/python/py_audiotestsrc.py @@ -148,7 +148,7 @@ class AudioTestSrc(GstBase.BaseSrc): return start, end - def do_create(self, offset, length): + def do_fill(self, offset, length, buf): if length == -1: samples = SAMPLESPERBUFFER else: @@ -162,16 +162,19 @@ class AudioTestSrc(GstBase.BaseSrc): next_byte = self.next_byte + bytes_ next_time = Gst.util_uint64_scale_int(next_sample, Gst.SECOND, self.info.rate) - if not self.mute: - r = np.repeat( - np.arange(self.accumulator, self.accumulator + samples), - self.info.channels) - data = ((np.sin(2 * np.pi * r * self.freq / self.info.rate) * self.volume) - .astype(np.float32)) - else: - data = [0] * bytes_ - - buf = Gst.Buffer.new_wrapped(bytes(data)) + try: + with buf.map(Gst.MapFlags.WRITE) as info: + array = np.ndarray(shape = self.info.channels * samples, dtype = np.float32, buffer = info.data) + if not self.mute: + r = np.repeat(np.arange(self.accumulator, self.accumulator + samples), + self.info.channels) + np.sin(2 * np.pi * r * self.freq / self.info.rate, out=array) + array *= self.volume + else: + array[:] = 0 + except Exception as e: + Gst.error("Mapping error: %s" % e) + return Gst.FlowReturn.ERROR buf.offset = self.next_sample buf.offset_end = next_sample |