def get_header(self, wav_path=None): if wav_path is None: with wave.open(self.__wav_path, 'rb') as wav_fp: return wav_fp.readframes(nframes=44) else: with wave.open(wav_path, 'rb') as wav_fp: return wav_fp.readframes(nframes=44)
def make_wav(self, wav_path): """ To Do List: make folder along sample rate and channels """ if wav_path.split('.')[-1] != 'pcm' or 'PCM': wav_path = wav_path + self.__pcm_path.split('/')[-1].split( '.')[0] + '.wav' with wave.open(wav_path, 'wb') as wavdata: wavdata.setnchannels(self.__numchannels) wavdata.setframerate(self.__sample_rate) wavdata.setsampwidth(int(self.__bits_per_sample / 8)) wavdata.writeframes(self.__pcmdata) else: with wave.open(wav_path, 'wb') as wavdata: wavdata.setnchannels(self.__numchannels) wavdata.setframerate(self.__sample_rate) wavdata.setsampwidth(int(self.__bits_per_sample / 8)) wavdata.writeframes(self.__pcmdata)
def get_pcm(self, wav_path=None): if wav_path is None: with wave.open(self.__wav_path, 'rb') as wav_fp: # getparams: (nchannels, sampwidth, framerate, nframes, comptype, compname) wav_params = wav_fp.getparams(wav_path) # get nframes nframes = wav_params[3] # update wav data return wav_fp.readframes(nframes=nframes)[44:] else: with wave.open(wav_path, 'rb') as wav_fp: # getparams: (nchannels, sampwidth, framerate, nframes, comptype, compname) wav_params = wav_fp.getparams(wav_path) # get nframes nframes = wav_params[3] # update wav data return wav_fp.readframes(nframes=nframes)[44:]
def set_file(self, filename): self.file_path = self.folder_path + filename if filename.split(".")[-1] == 'pcm': with open(self.file_path, 'rb') as pcm_fp: self.file_data = pcm_fp.read() elif filename.split(".")[-1] == 'wav': with wave.open(self.file_path, 'rb') as wav_fp: self.file_data = wav_fp.readframes(wav_fp.getparams()[3])[44:] elif filename.split(".")[-1] == 'txt': with open(self.file_path, 'rb') as txt_fp: x_num, sample_rate = txt_fp.readline().split() x_num = int(x_num) sample_rate = 1 / float(sample_rate) self.sample_rate = int(sample_rate) self.file_data = np.ndarray((x_num, )) for i in range(x_num): _, amplitude = txt_fp.readline().split() self.file_data[i] = float(amplitude)
def get_wav(self, wav_path): with wave.open(wav_path, 'rb') as wav_fp: # getparams: (nchannels, sampwidth, framerate, nframes, comptype, compname) wav_params = wav_fp.getparams() # update params self.__numchannels = wav_params[0] # nchannels self.__byte_per_sample = wav_params[1] # sampwidth self.__sample_rate = wav_params[2] # framerate self.__nframes = wav_params[3] # nframes # update wav data self.__wav_data = wav_fp.readframes(nframes=self.__nframes) # update wav header # fixed header 44 bytes self.__wav_header = self.__wav_data[0:44] # update pcm data self.__pcm_data = self.__wav_data[44:] return self.__wav_data