Ejemplo n.º 1
0
def test_same_data_as_flac_file_using_as_array(pyogg_config):
    import numpy  # type: ignore

    # Load the demonstration file that is exactly 5 seconds long
    filename = str(pyogg_config.rootdir / "examples/left-right-demo-5s.flac")

    # Open the file using FlacFile to read the entire file into memory
    flac_file = pyogg.FlacFile(filename)

    # Open the file (again) using FlacFileStream, which does not read
    # the entire file immediately.
    flac_stream = pyogg.FlacFileStream(filename)

    # Loop through the FlacFileStream until we've read all the data
    buf_all = None
    while True:
        # Read the next part of the stream
        buf = flac_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 == flac_file.as_array())
Ejemplo n.º 2
0
def test_same_data_as_flac_file(pyogg_config) -> None:
    # Load the demonstration file that is exactly 5 seconds long
    filename = str(pyogg_config.rootdir / "examples/left-right-demo-5s.flac")

    # Open the file using FlacFile to read the entire file into memory
    flac_file = pyogg.FlacFile(filename)

    # Open the file (again) using FlacFileStream, which does not read
    # the entire file immediately.
    flac_stream = pyogg.FlacFileStream(filename)

    # Loop through the FlacFileStream until we've read all the data
    buf_all = bytes()
    while True:
        # Read the next part of the stream
        buf = flac_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 == bytes(flac_file.buffer)
Ejemplo n.º 3
0
def test_output_via_wav():
    # Load the demonstration file that is exactly 5 seconds long
    filename = "../examples/left-right-demo-5s.flac"
    flac_file = pyogg.FlacFile(filename)

    import wave
    wave_out = wave.open("test_flac_file__test_output_via_wav.wav", "wb")
    wave_out.setnchannels(flac_file.channels)
    wave_out.setsampwidth(flac_file.bytes_per_sample)
    wave_out.setframerate(flac_file.frequency)
    wave_out.writeframes(flac_file.buffer)
Ejemplo n.º 4
0
def test_as_array():
    # Load the demonstration file that is exactly 5 seconds long
    filename = "../examples/left-right-demo-5s.flac"
    flac_file = pyogg.FlacFile(filename)

    # Test that the loaded file is indeed 5 seconds long (using
    # as_array())
    expected_duration_seconds = 5
    samples_per_second = flac_file.frequency
    expected_duration_samples = (expected_duration_seconds *
                                 samples_per_second)
    duration_samples = flac_file.as_array().shape[0]
    assert duration_samples == expected_duration_samples
Ejemplo n.º 5
0
def test_as_bytes():
    # Load the demonstration file that is exactly 5 seconds long
    filename = "../examples/left-right-demo-5s.flac"
    flac_file = pyogg.FlacFile(filename)

    # Test that the loaded file is indeed 5 seconds long (using
    # the buffer member variable)
    expected_duration_seconds = 5
    samples_per_second = flac_file.frequency
    channels = flac_file.channels
    bytes_per_sample = flac_file.bytes_per_sample
    expected_duration_bytes = (expected_duration_seconds * samples_per_second *
                               bytes_per_sample * channels)
    duration_bytes = len(flac_file.buffer)
    assert duration_bytes == expected_duration_bytes
Ejemplo n.º 6
0
def test_output_via_wav(pyogg_config) -> None:
    # Load the demonstration file that is exactly 5 seconds long
    filename = str(pyogg_config.rootdir / "examples/left-right-demo-5s.flac")
    flac_file = pyogg.FlacFile(filename)

    import wave
    out_filename = str(
        pyogg_config.outdir
        / "test_flac_file__test_output_via_wav.wav"
    )
    wave_out = wave.open(
        out_filename,
        "wb"
    )
    wave_out.setnchannels(flac_file.channels)
    wave_out.setsampwidth(flac_file.bytes_per_sample)
    wave_out.setframerate(flac_file.frequency)
    wave_out.writeframes(flac_file.buffer)
Ejemplo n.º 7
0
def test_error_in_filename() -> None:
    # Load a non-existant file
    filename = "does-not-exist.flac"
    with pytest.raises(pyogg.PyOggError):
        flac_file = pyogg.FlacFile(filename)
#    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.flac"

# Read the file using FlacFile
print("Reading Ogg Flac file...")
flac_file = pyogg.FlacFile(filename)

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

# Get the data as a NumPy array
buf = flac_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)