def test_bytes_per_sample(pyogg_config: Config):
    # Load the demonstration file that is exactly 5 seconds long
    filename = str(pyogg_config.rootdir / "examples/left-right-demo-5s.ogg")
    vorbis_file_1 = pyogg.VorbisFile(filename, bytes_per_sample=1)
    vorbis_file_2 = pyogg.VorbisFile(filename, bytes_per_sample=2)

    # Test that the buffer lengths differ by a factor of two.
    assert len(vorbis_file_2.buffer) == len(vorbis_file_1.buffer) * 2
Exemple #2
0
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())
Exemple #3
0
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
Exemple #4
0
 def check_music_file(filename):
     try:
         pyogg.VorbisFile(filename)
         return True
     except pyogg.ogg.PyOggError:
         print(
             f'File {filename} doesn\'t exist or incorrect: only OGG '
             f'Vorbis files are supported',
             file=sys.stderr)
         return False
Exemple #5
0
def test_output_via_wav():
    # Load the demonstration file that is exactly 5 seconds long
    filename = "../examples/left-right-demo-5s.ogg"
    vorbis_file = pyogg.VorbisFile(filename)

    import wave
    wave_out = wave.open("test_vorbis_file__test_output_via_wav.wav", "wb")
    wave_out.setnchannels(vorbis_file.channels)
    wave_out.setsampwidth(vorbis_file.bytes_per_sample)
    wave_out.setframerate(vorbis_file.frequency)
    wave_out.writeframes(vorbis_file.buffer)
def test_as_array(pyogg_config: Config):
    # Load the demonstration file that is exactly 5 seconds long
    filename = str(pyogg_config.rootdir / "examples/left-right-demo-5s.ogg")
    vorbis_file = pyogg.VorbisFile(filename)

    # Test that the loaded file is indeed 5 seconds long (using
    # as_array())
    expected_duration_seconds = 5
    samples_per_second = vorbis_file.frequency
    expected_duration_samples = (expected_duration_seconds *
                                 samples_per_second)
    duration_samples = vorbis_file.as_array().shape[0]
    assert duration_samples == expected_duration_samples
def test_output_via_wav_one_byte_per_sample(pyogg_config: Config):
    # Load the demonstration file that is exactly 5 seconds long
    filename = str(pyogg_config.rootdir / "examples/left-right-demo-5s.ogg")
    vorbis_file = pyogg.VorbisFile(filename, bytes_per_sample=1, signed=False)

    import wave
    out_filename = str(
        pyogg_config.outdir /
        "test_vorbis_file__test_output_via_wav_one_byte_per_sample.wav")
    wave_out = wave.open(out_filename, "wb")
    wave_out.setnchannels(vorbis_file.channels)
    wave_out.setsampwidth(vorbis_file.bytes_per_sample)
    wave_out.setframerate(vorbis_file.frequency)
    wave_out.writeframes(vorbis_file.buffer)
def test_as_bytes_one_byte_per_sample(pyogg_config: Config):
    # Load the demonstration file that is exactly 5 seconds long
    filename = str(pyogg_config.rootdir / "examples/left-right-demo-5s.ogg")
    vorbis_file = pyogg.VorbisFile(filename, bytes_per_sample=1)

    # Test that the loaded file is indeed 5 seconds long (using
    # the buffer member variable)
    expected_duration_seconds = 5
    samples_per_second = vorbis_file.frequency
    channels = vorbis_file.channels
    bytes_per_sample = vorbis_file.bytes_per_sample
    expected_duration_bytes = (expected_duration_seconds * samples_per_second *
                               bytes_per_sample * channels)
    duration_bytes = len(vorbis_file.buffer)
    assert duration_bytes == expected_duration_bytes
Exemple #9
0
#    960000
# Shape of numpy array (number of samples per channel, number of channels):
#    (240000, 2)
#
# Playing...
# Finished.

import pyogg
import simpleaudio  # type: ignore

# Specify the filename to read
filename = "left-right-demo-5s.ogg"

# Read the file using VorbisFile
print("Reading Ogg Vorbis file...")
vorbis_file = pyogg.VorbisFile(filename)

# Display summary information about the audio
print("\nRead Ogg Vorbis file")
print("Channels:\n  ", vorbis_file.channels)
print("Frequency (samples per second):\n  ", vorbis_file.frequency)
print("Buffer Length (bytes):\n  ", len(vorbis_file.buffer))

# Get the data as a NumPy array
buf = vorbis_file.as_array()

# The shape of the array can be read as
# "(number of samples per channel, number of channels)".
print(
    "Shape of numpy array (number of samples per channel, " +
    "number of channels):\n  ", buf.shape)
def test_error_in_filename():
    # Load a non-existant file
    filename = "does-not-exist.ogg"
    with pytest.raises(pyogg.PyOggError):
        vorbis_file = pyogg.VorbisFile(filename)