Python wave.Wave_read() Examples

The following are 7 code examples of wave.Wave_read(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module wave , or try the search function .
Example #1
Source File: extract_features.py    From DeepFormants with MIT License 5 votes vote down vote up
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] #numpy 1.11.0
        return data[np.int(begin*16000):np.int(end*16000)] #numpy 1.14.0
    X = []
    l = len(data)
    for i in range(0, l-100, 160):
        X.append(data[i:i + 480])
    return X 
Example #2
Source File: audio.py    From pyaaf2 with MIT License 4 votes vote down vote up
def __init__(self, f):
        self._blockalign = None
        # can't use super in OldStyle 2.7 class
        wave.Wave_read.__init__(self, f) 
Example #3
Source File: utilities.py    From DeepFormants with MIT License 4 votes vote down vote up
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 
Example #4
Source File: streaming.py    From synthesizer with GNU Lesser General Public License v3.0 4 votes vote down vote up
def __init__(self, wav_reader_or_stream: Union[wave.Wave_read, BinaryIO], frames_per_sample: int) -> None:
        if isinstance(wav_reader_or_stream, io.RawIOBase):
            self.source = wave.open(wav_reader_or_stream, "r")   # type: wave.Wave_read
        else:
            assert isinstance(wav_reader_or_stream, wave.Wave_read)
            self.source = wav_reader_or_stream
        self.samplewidth = self.source.getsampwidth()
        self.samplerate = self.source.getframerate()
        self.nchannels = self.source.getnchannels()
        self.frames_per_sample = frames_per_sample
        self.filters = []           # type: List[SampleFilter]
        self.frames_filters = []    # type: List[FramesFilter]
        self.source.readframes(1)  # warm up the stream 
Example #5
Source File: testwavedecoder.py    From wav2vec with Do What The F*ck You Want To Public License 4 votes vote down vote up
def build_mock_wave(nchannels=2, sampwidth=2, framerate=44100, nframes=100,
                    comptype="NONE", compname="not compressed", bytes=b'\x4a'):
        mock_wave_read = MagicMock(spec=wave.Wave_read)
        mock_wave_read.getparams.return_value = (nchannels, sampwidth,
                                                 framerate, nframes, comptype,
                                                 compname)

        # Return some bytes
        def mock_readframes(frames):
            return bytes * sampwidth * frames * nchannels
        mock_wave_read.readframes = MagicMock(side_effect=mock_readframes)
        mock_wave = MagicMock(spec=wave)
        mock_wave.open.return_value = mock_wave_read
        return mock_wave 
Example #6
Source File: test_speech.py    From python-dlpy with Apache License 2.0 4 votes vote down vote up
def test_read_audio_3(self):
        try:
            import wave
        except ImportError:
            unittest.TestCase.skipTest(self, "wave is not found in the libraries.")

        if self.data_dir_local is None:
            unittest.TestCase.skipTest(self, "DLPY_DATA_DIR_LOCAL is not set in the environment variables.")

        wave_reader, wave_params = read_audio(os.path.join(self.data_dir_local, "sample_16bit_16khz.wav"))
        self.assertIsInstance(wave_reader, wave.Wave_read)
        self.assertIsInstance(wave_params, tuple)
        self.assertIsNotNone(wave_reader)
        self.assertIsNotNone(wave_params)
        wave_reader.close() 
Example #7
Source File: pyDubMod.py    From AlexaBot with MIT License 4 votes vote down vote up
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