| 149 | return start, end |
| 150 | |
| 151 | def do_fill(self, offset, length, buf): |
| 152 | if length == -1: |
| 153 | samples = SAMPLESPERBUFFER |
| 154 | else: |
| 155 | samples = int(length / self.info.bpf) |
| 156 | |
| 157 | self.generate_samples_per_buffer = samples |
| 158 | |
| 159 | bytes_ = samples * self.info.bpf |
| 160 | |
| 161 | next_sample = self.next_sample + samples |
| 162 | next_byte = self.next_byte + bytes_ |
| 163 | next_time = Gst.util_uint64_scale_int(next_sample, Gst.SECOND, self.info.rate) |
| 164 | |
| 165 | try: |
| 166 | with buf.map(Gst.MapFlags.WRITE) as info: |
| 167 | array = np.ndarray(shape = self.info.channels * samples, dtype = np.float32, buffer = info.data) |
| 168 | if not self.mute: |
| 169 | r = np.repeat(np.arange(self.accumulator, self.accumulator + samples), |
| 170 | self.info.channels) |
| 171 | np.sin(2 * np.pi * r * self.freq / self.info.rate, out=array) |
| 172 | array *= self.volume |
| 173 | else: |
| 174 | array[:] = 0 |
| 175 | except Exception as e: |
| 176 | Gst.error("Mapping error: %s" % e) |
| 177 | return Gst.FlowReturn.ERROR |
| 178 | |
| 179 | buf.offset = self.next_sample |
| 180 | buf.offset_end = next_sample |
| 181 | buf.pts = self.next_time |
| 182 | buf.duration = next_time - self.next_time |
| 183 | |
| 184 | self.next_time = next_time |
| 185 | self.next_sample = next_sample |
| 186 | self.next_byte = next_byte |
| 187 | self.accumulator += samples |
| 188 | self.accumulator %= self.info.rate / self.freq |
| 189 | |
| 190 | return (Gst.FlowReturn.OK, buf) |
| 191 | |
| 192 | |
| 193 | __gstelementfactory__ = ("py_audiotestsrc", Gst.Rank.NONE, AudioTestSrc) |