def __init__(self, path): ''' Create the sentence object path: (string) the path to the particular sample ''' if not have_sk_audiolab: raise ValueError( 'scikits.audiolab module is required to read the TIMIT database.' ) path, ext = os.path.splitext(path) t = path.split('/') # extract the attributes self.dialect = t[-3] self.sex = t[-2][0] self.speaker = t[-2][1:5] self.id = t[-1] # Read in the wav file f = Sndfile(path + '.WAV', 'r') self.data = f.read_frames(f.nframes) self.fs = f.samplerate # Read the sentence text f = open(path + '.TXT', 'r') lines = f.readlines() self.text = ' '.join(lines[0].split()[2:]) f.close() # Read the word list self.words = [] self.phonems = [] # open the word file f = open(path + '.WRD', 'r') w_lines = f.readlines() f.close() # get all lines from the phonem file f_ph = open(path + '.PHN', 'r') ph_lines = f_ph.readlines() ph_l_index = 0 f_ph.close() for line in w_lines: t = line.split() # just a sanity check if len(t) == 3: # the word boundary w_bnd = np.array([int(t[0]), int(t[1])]) # recover the phonems making up the word w_ph_list = [] while ph_l_index < len(ph_lines): ph_line = ph_lines[ph_l_index] u = ph_line.split() # phonem boundary ph_bnd = np.array([int(u[0]), int(u[1])]) # Check phonem boundary does not exceeds word boundary if ph_bnd[1] > w_bnd[1]: break # add to sentence object phonems list self.phonems.append({'name': u[2], 'bnd': ph_bnd}) # increase index ph_l_index += 1 # now skip until beginning of word if ph_bnd[0] < w_bnd[0]: continue # add phonem to word if (with adjusted boundaries wrt to start of word) w_ph_list.append({'name': u[2], 'bnd': ph_bnd - w_bnd[0]}) # Finally create word object self.words.append( Word(t[2], w_bnd, self.data, self.fs, phonems=w_ph_list)) # Read the remaining phonem(s) while ph_l_index < len(ph_lines): ph_line = ph_lines[ph_l_index] u = ph_line.split() if len(u) == 3: # phonem boundary ph_bnd = (int(u[0]), int(u[1])) # add to sentence object phonems list self.phonems.append({'name': u[2], 'bnd': ph_bnd}) ph_l_index += 1