def __set_minvolume(self): """ Min volume for the whole file is the min rms of each frame. """ # Min volume was already estimated if self.minvolume is not None: return self.minvolume # Remember current position in the speech file pos = self.tell() # Rewind to the begining of the file self.rewind() # Find the min rms value (explore all frames) self.minvolume = 0 while self.tell() < self.get_nframes(): frames = self.read_frames(self.nbreadframes) rms = audioutils.get_rms(frames, self.get_sampwidth(), self.get_nchannels()) if rms < self.minvolume or self.minvolume == 0: self.minvolume = rms # Returns to the position where the file was before self.set_pos(pos)
def __set_meanvolume(self): """ Calculate the mean volume of the adio file (the mean of rms of the whole file). """ # Median volume was already estimated if self.meanvolume is not None: return self.meanvolume # Remember current position in the speech file pos = self.tell() # Rewind to the begining of the file self.rewind() # Get the mean value: the rms of the whole file ####mean_volume = self.rms(self.speech.readframes(self.nframes)) self.meanvolume = 0 sumrms = 0 nb = 0 while self.tell() < self.get_nframes(): frames = self.read_frames(self.nbreadframes) rms = audioutils.get_rms(frames, self.get_sampwidth(), self.get_nchannels()) sumrms += rms nb += 1 self.meanvolume = sumrms / nb # Returns to the position where the file was before self.set_pos(pos)
def extract_channels( settings, factor=0, channel=0, p=None ): """ Extract the first channel for each sound file of the settings. @param settings: a list of dictionaries with extracted information from a settings file. @param factor: coefficient to apply to the volume @param channel: index of the channel to extract @param p is a progress dialog """ total = len( settings ) for i,channelparameter in enumerate(settings): if p: p.update(float(i)/total, "Channel " + str(i+1) + " of " + str(total)) audio = signals.open( channelparameter['file'] ) # CHANNEL EXTRACTION idx = audio.extract_channel( channel ) channelparameter['channel'] = audio.get_channel(idx) # RMS EXTRACTION if factor > 0: rms = audioutils.get_rms(channelparameter['channel'].frames, audio.get_sampwidth()) rmswanted = audioutils.mel2db(audioutils.db2mel(rms)*factor) channelparameter['factor'] = rmswanted/rms audio.close() del audio
def get_rms(self): """ Return the root mean square of the whole file @return the root mean square of the audio file """ return audioutils.get_rms(self.read_frames(self.get_nframes()), self.get_sampwidth(), self.get_nchannels())
def get_rms(self): """ Return the root mean square of the channel. @return the root mean square of the channel """ return audioutils.get_rms( self.frames, self.sampwidth )
def get_silence(self, p=0.250, v=150, s=0.0): """ Get silences from an audio file. @return a set of frames corresponding to silences. """ self.silence = [] # Once silence has been found, continue searching in this interval afterloop_frames = int((self.channel.get_frameduration()/2) * self.channel.get_framerate()) initpos = i = self.channel.tell() self.silence = [] # This scans the file in steps of frames whether a section's volume # is lower than silence_cap, if it is it is written to silence. while i < self.channel.get_nframes(): curframe = self.channel.get_frames(self.channel.nbreadframes) volume = audioutils.get_rms(curframe, self.channel.get_sampwidth()) if volume < v: # Continue searching in smaller steps whether the silence is # longer than readed frames but smaller than readed frames * 2. while volume < v and self.channel.tell() < self.channel.get_nframes(): curframe = self.channel.get_frames(afterloop_frames) volume = audioutils.get_rms(curframe, self.channel.get_sampwidth()) # If the last sequence of silence ends where the new one starts # it's a continuous range. if self.silence and self.silence[-1][1] == i: self.silence[-1][1] = self.channel.tell() else: # append if silence is long enough duree = self.channel.tell() - i nbmin = int( (p+s) * self.channel.get_framerate()) if duree > nbmin: # Adjust silence start-pos __startpos = i + ( s * self.channel.get_framerate() ) # Adjust silence end-pos __endpos = self.channel.tell() - ( s * self.channel.get_framerate() ) self.silence.append([__startpos, __endpos]) i = self.channel.tell() # Return the position in the file to where it was when we got it. self.channel.seek(initpos)
def get_volumes(self): """ Return an array containing the volume of each frame of the Audio file pointer. @return an array containing the volume of each frame of the audio file """ if not self.audiofp: raise Exception(NO_AUDIO_MSG) vol = [] nb = 0 while self.tell() < self.nframes: frames = self.read_frames(self.nbreadframes) vol[nb] = audioutils.get_rms(frames, self.get_sampwidth(), self.get_nchannels()) nb += 1 return vol