def extract_channel_fragment(channel, fromtime, totime, silence=0.): """ Extract a fragment of a channel in the interval [fromtime,totime]. Eventually, surround it by silences. @param channel (Channel - IN) @param fromtime (float - IN) From time value in seconds. @param totime (float - IN) To time value in seconds. @param silence (float - IN) Duration value in seconds. """ framerate = channel.get_framerate() # Extract the fragment of the channel startframe = int(fromtime*framerate) toframe = int(totime*framerate) fragmentchannel = channel.extract_fragment(begin=startframe, end=toframe) # Get all the frames of this fragment nbframes = fragmentchannel.get_nframes() cf = ChannelFrames( fragmentchannel.get_frames( nbframes ) ) # surround by silences if silence > 0.: cf.prepend_silence( silence*framerate ) cf.append_silence( silence*framerate ) return Channel( 16000, 2, cf.get_frames() )
def test_CreateSilence(self): self._sample_1.extract_channel(0) self._sample_2.extract_channel(0) channel = self._sample_1.get_channel(0) monofrag = ChannelFrames(channel.frames) monofrag.append_silence(1000) self.assertEqual(channel.get_nframes()+1000, len(monofrag.get_frames())/channel.get_sampwidth())
def norm_length(self): """ Normalize the number of frames of all the channels by appending silence at the end. """ nframes = 0 for i in xrange(len(self.channels)): nframes = max(nframes, self.channels[i].get_nframes()) for i in xrange(len(self.channels)): if self.channels[i].get_nframes() < nframes: fragment = ChannelFrames(self.channels[i].frames) fragment.append_silence(nframes - self.channels[i].get_nframes()) self.channels[i] = Channel(self.channels[i].get_framerate(), self.channels[i].get_sampwidth(), fragment.get_frames())