我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用wave.Wave_read()。
def _open_wav(wav): '''Internal helper function to open an unknown parameter as a wave file. wav: An open `wave.Wave_read` object, a `bytes` object containing a wave file, or a valid argument to `wave.open`. ''' if isinstance(wav, wave.Wave_read): yield wav return if isinstance(wav, bytes) and wav[:4] == b'RIFF': w = wave.open(BytesIO(wav), 'rb') else: w = wave.open(wav, 'rb') yield w w.close()
def _wave(self): """Return a wave.Wave_read instance from the ``wave`` module.""" try: return wave.open(StringIO(self.contents)) except wave.Error, err: err.message += "\nInvalid wave file: %s" % self err.args = (err.message,) raise
def play(wav, device_id=None): '''Plays a wave file using a playback device. The function will block until playback is complete. wav: An open `wave.Wave_read` object, a `bytes` object containing a wave file, or a valid argument to `wave.open`. device_id: The device to play over. Defaults to the first available. ''' if device_id is None: device_id = get_playback_devices()[0][1] with _open_wav(wav) as w: return _play(device_id, w)
def build_data(wav,begin=None,end=None): wav_in_file = wave.Wave_read(wav) wav_in_num_samples = wav_in_file.getnframes() N = wav_in_file.getnframes() dstr = wav_in_file.readframes(N) data = np.fromstring(dstr, np.int16) if begin is not None and end is not None: return data[begin*16000:end*16000] X = [] l = len(data) for i in range(0, l-100, 160): X.append(data[i:i + 480]) return X
def is_valid_wav(filename): # check the sampling rate and number bits of the WAV try: wav_file = wave.Wave_read(filename) except: return False if wav_file.getframerate() != 16000 or wav_file.getsampwidth() != 2 or wav_file.getnchannels() != 1 \ or wav_file.getcomptype() != 'NONE': return False return True
def __init__(self, audio_reader, little_endian, samples_24_bit_pretending_to_be_32_bit): self.audio_reader = audio_reader # an audio file object (e.g., a `wave.Wave_read` instance) self.little_endian = little_endian # whether the audio data is little-endian (when working with big-endian things, we'll have to convert it to little-endian before we process it) self.samples_24_bit_pretending_to_be_32_bit = samples_24_bit_pretending_to_be_32_bit # this is true if the audio is 24-bit audio, but 24-bit audio isn't supported, so we have to pretend that this is 32-bit audio and convert it on the fly