def test_same_data_as_vorbis_file_using_as_array(): import numpy # Load the demonstration file that is exactly 5 seconds long filename = "../examples/left-right-demo-5s.ogg" # Open the file using VorbisFile to read the entire file into memory vorbis_file = pyogg.VorbisFile(filename) # Open the file (again) using VorbisFileStream, which does not read # the entire file immediately. vorbis_stream = pyogg.VorbisFileStream(filename) # Loop through the VorbisFileStream until we've read all the data buf_all = None while True: # Read the next part of the stream buf = vorbis_stream.get_buffer_as_array() # Check if we've reached the end of the stream if buf is None: break # Add the bytes we've read to buf_all. Note that this # technique isn't efficient and shouldn't be used in # production code. if buf_all is None: buf_all = buf else: buf_all = numpy.concatenate((buf_all, buf)) # Check that every byte is identical for both buffers assert numpy.all(buf_all == vorbis_file.as_array())
def test_same_data_as_vorbis_file(): # Load the demonstration file that is exactly 5 seconds long filename = "../examples/left-right-demo-5s.ogg" # Open the file using VorbisFile to read the entire file into memory vorbis_file = pyogg.VorbisFile(filename) # Open the file (again) using VorbisFileStream, which does not read # the entire file immediately. vorbis_stream = pyogg.VorbisFileStream(filename) # Loop through the VorbisFileStream until we've read all the data buf_all = bytes() while True: # Read the next part of the stream buf = vorbis_stream.get_buffer() # Check if we've reached the end of the stream if buf is None: break # Add the bytes we've read to buf_all. Note that this # technique isn't efficient and shouldn't be used in # production code. buf_all += buf assert buf_all == vorbis_file.buffer
def test_total_length(): # Load the demonstration file that is exactly 5 seconds long filename = "../examples/left-right-demo-5s.ogg" # Open the file using VorbisFileStream, which does not read the entire # file immediately. vorbis_stream = pyogg.VorbisFileStream(filename) # Loop through the VorbisFileStream until we've read all the data samples_read = 0 while True: # Read the next part of the stream buf = vorbis_stream.get_buffer_as_array() # Check if we've reached the end of the stream if buf is None: break # Increment the number of samples read samples_read += buf.shape[0] expected_duration_seconds = 5 samples_per_second = vorbis_stream.frequency expected_duration_samples = (expected_duration_seconds * samples_per_second) duration_samples = samples_read assert duration_samples == expected_duration_samples
vorbis_file_stream_filename = "left-right-demo-5s.ogg" # Open the file using VorbisFile, which reads the entire file # immediately and places it into an internal buffer. start_time = time.time() vorbis_file = pyogg.VorbisFile(vorbis_file_filename) end_time = time.time() duration = (end_time - start_time) * 1000 array = vorbis_file.as_array() array_index = 0 print("Read {:d} samples from VorbisFile (in {:.1f} milliseconds).".format( len(array), duration)) # Open the file using VorbisFileStream, which does not read the entire # file immediately. stream = pyogg.VorbisFileStream(vorbis_file_stream_filename) # Loop through the VorbisFileStream until we've read all the data samples_read = 0 identical = True times = [] while True: # Read the next part of the stream start_time = time.time() buf = stream.get_buffer_as_array() end_time = time.time() duration = (end_time - start_time) * 1000 times.append(duration) # Check if we've reached the end of the stream if buf is None:
def test_error_in_filename(): # Load a non-existant file filename = "does-not-exist.ogg" with pytest.raises(pyogg.PyOggError): vorbis_stream = pyogg.VorbisFileStream(filename)