def test_same_data_as_opus_file_using_as_array(): import numpy # type: ignore # Load the demonstration file that is exactly 5 seconds long filename = "../examples/left-right-demo-5s.opus" # Open the file using OpusFile to read the entire file into memory opus_file = pyogg.OpusFile(filename) # Open the file (again) using OpusFileStream, which does not read # the entire file immediately. opus_stream = pyogg.OpusFileStream(filename) # Loop through the OpusFileStream until we've read all the data buf_all = None while True: # Read the next part of the stream buf = opus_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 == opus_file.as_array())
def test_same_data_as_opus_file(): # Load the demonstration file that is exactly 5 seconds long filename = "../examples/left-right-demo-5s.opus" # Open the file using OpusFile to read the entire file into memory opus_file = pyogg.OpusFile(filename) # Open the file (again) using OpusFileStream, which does not read # the entire file immediately. opus_stream = pyogg.OpusFileStream(filename) # Loop through the OpusFileStream until we've read all the data buf_all = bytes() while True: # Read the next part of the stream buf = opus_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 == opus_file.buffer
def test_total_length(): # Load the demonstration file that is exactly 5 seconds long filename = "../examples/left-right-demo-5s.opus" # Open the file using OpusFileStream, which does not read the entire # file immediately. opus_stream = pyogg.OpusFileStream(filename) # Loop through the OpusFileStream until we've read all the data samples_read = 0 while True: # Read the next part of the stream buf = opus_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 = opus_stream.frequency expected_duration_samples = (expected_duration_seconds * samples_per_second) duration_samples = samples_read assert duration_samples == expected_duration_samples
def play_audio(self, filenames): # Assumes filename points to Opus-encoded audio. # TODO: Check that we're not currently playing anything else # TODO: Deal with more than one filename log.info("Playing these filenames: "+str(filenames)) filename = filenames[0] # Open file as stream try: self._stream = pyogg.OpusFileStream(str(filename)) except Exception as e: raise Exception(f"Failed to open OpusFileStream (with filename '{filename}': "+ str(e)) self._stream_buffer = None
# Open the file using OpusFile, which reads the entire file # immediately and places it into an internal buffer. start_time = time.time() opus_file = pyogg.OpusFile(opus_file_filename) end_time = time.time() duration = (end_time-start_time)*1000 array = opus_file.as_array() array_index = 0 print("Read {:d} samples from OpusFile (in {:.1f} milliseconds).".format( len(array), duration )) # Open the file using OpusFileStream, which does not read the entire # file immediately. stream = pyogg.OpusFileStream(opus_file_stream_filename) # Loop through the OpusFileStream 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.opus" with pytest.raises(pyogg.PyOggError): opus_stream = pyogg.OpusFileStream(filename)