Back to index

libsfml  1.6+dfsg2
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
sf::priv::SoundFileOgg Class Reference

Specialization of SoundFile that handles ogg-vorbis files (.ogg) (does not support variable bitrate / channels and writing) /////////////////////////////////////////////////////////. More...

#include <SoundFileOgg.hpp>

Inheritance diagram for sf::priv::SoundFileOgg:
Inheritance graph
[legend]
Collaboration diagram for sf::priv::SoundFileOgg:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 SoundFileOgg ()
 Default constructor.
 ~SoundFileOgg ()
 Destructor.
virtual std::size_t Read (Int16 *Data, std::size_t NbSamples)
 /see sf::SoundFile::Read
std::size_t GetSamplesCount () const
 Get the total number of samples in the file.
unsigned int GetChannelsCount () const
 Get the number of channels used by the sound.
unsigned int GetSampleRate () const
 Get the sample rate of the sound.
bool Restart ()
 Restart the sound from the beginning.
virtual void Write (const Int16 *Data, std::size_t NbSamples)
 Write samples to the file.

Static Public Member Functions

static bool IsFileSupported (const std::string &Filename, bool Read)
 Check if a given file is supported by this loader.
static bool IsFileSupported (const char *Data, std::size_t SizeInBytes)
 Check if a given file in memory is supported by this loader.
static SoundFileCreateRead (const std::string &Filename)
 Create a new sound from a file, for reading.
static SoundFileCreateRead (const char *Data, std::size_t SizeInBytes)
 Create a new sound from a file in memory, for reading.
static SoundFileCreateWrite (const std::string &Filename, unsigned int ChannelsCount, unsigned int SampleRate)
 Create a new sound from a file, for writing.

Private Member Functions

virtual bool OpenRead (const std::string &Filename, std::size_t &NbSamples, unsigned int &ChannelsCount, unsigned int &SampleRate)
 /see sf::SoundFile::OpenRead
virtual bool OpenRead (const char *Data, std::size_t SizeInBytes, std::size_t &NbSamples, unsigned int &ChannelsCount, unsigned int &SampleRate)
 /see sf::SoundFile::OpenRead

Private Attributes

stb_vorbismyStream
 Vorbis stream.
unsigned int myChannelsCount
 Number of channels (1 = mono, 2 = stereo)

Detailed Description

Specialization of SoundFile that handles ogg-vorbis files (.ogg) (does not support variable bitrate / channels and writing) /////////////////////////////////////////////////////////.

Definition at line 44 of file SoundFileOgg.hpp.


Constructor & Destructor Documentation

Default constructor.

Default constructor /////////////////////////////////////////////////////////.

/////////////////////////////////////////////////////////

Definition at line 40 of file SoundFileOgg.cpp.

                           :
myStream       (NULL),
myChannelsCount(0)
{

}

Destructor.

Destructor /////////////////////////////////////////////////////////.

/////////////////////////////////////////////////////////

Definition at line 51 of file SoundFileOgg.cpp.

Here is the call graph for this function:


Member Function Documentation

SoundFile * sf::priv::SoundFile::CreateRead ( const std::string &  Filename) [static, inherited]

Create a new sound from a file, for reading.

Create a new sound from a file, for reading /////////////////////////////////////////////////////////.

Parameters:
Filename: Path of sound file
NbSamples: Number of samples in the file
ChannelsCount: Number of channels in the loaded sound
SampleRate: Sample rate of the loaded sound
Returns:
Pointer to the new sound file (NULL if failed)

/////////////////////////////////////////////////////////

Definition at line 41 of file SoundFile.cpp.

{
    // Create the file according to its type
    SoundFile* File = NULL;
    if      (SoundFileOgg::IsFileSupported(Filename, true))     File = new SoundFileOgg;
    else if (SoundFileDefault::IsFileSupported(Filename, true)) File = new SoundFileDefault;

    // Open it for reading
    if (File)
    {
        std::size_t  SamplesCount;
        unsigned int ChannelsCount;
        unsigned int SampleRate;

        if (File->OpenRead(Filename, SamplesCount, ChannelsCount, SampleRate))
        {
            File->myFilename      = Filename;
            File->myData          = NULL;
            File->mySize          = 0;
            File->myNbSamples     = SamplesCount;
            File->myChannelsCount = ChannelsCount;
            File->mySampleRate    = SampleRate;
        }
        else
        {
            delete File;
            File = NULL;
        }
    }

    return File;
}

Here is the call graph for this function:

SoundFile * sf::priv::SoundFile::CreateRead ( const char *  Data,
std::size_t  SizeInBytes 
) [static, inherited]

Create a new sound from a file in memory, for reading.

Create a new sound from a file in memory, for reading /////////////////////////////////////////////////////////.

Parameters:
Data: Pointer to the file data in memory
SizeInBytes: Size of the data to load, in bytes
NbSamples: Number of samples in the file
ChannelsCount: Number of channels in the loaded sound
SampleRate: Sample rate of the loaded sound
Returns:
Pointer to the new sound file (NULL if failed)

/////////////////////////////////////////////////////////

Definition at line 78 of file SoundFile.cpp.

{
    // Create the file according to its type
    SoundFile* File = NULL;
    if      (SoundFileOgg::IsFileSupported(Data, SizeInMemory))     File = new SoundFileOgg;
    else if (SoundFileDefault::IsFileSupported(Data, SizeInMemory)) File = new SoundFileDefault;

    // Open it for reading
    if (File)
    {
        std::size_t  SamplesCount;
        unsigned int ChannelsCount;
        unsigned int SampleRate;

        if (File->OpenRead(Data, SizeInMemory, SamplesCount, ChannelsCount, SampleRate))
        {
            File->myFilename      = "";
            File->myData          = Data;
            File->mySize          = SizeInMemory;
            File->myNbSamples     = SamplesCount;
            File->myChannelsCount = ChannelsCount;
            File->mySampleRate    = SampleRate;
        }
        else
        {
            delete File;
            File = NULL;
        }
    }

    return File;
}

Here is the call graph for this function:

SoundFile * sf::priv::SoundFile::CreateWrite ( const std::string &  Filename,
unsigned int  ChannelsCount,
unsigned int  SampleRate 
) [static, inherited]

Create a new sound from a file, for writing.

Create a new sound from a file, for writing /////////////////////////////////////////////////////////.

Parameters:
Filename: Path of sound file
ChannelsCount: Number of channels in the sound
SampleRate: Sample rate of the sound
Returns:
Pointer to the new sound file (NULL if failed)

/////////////////////////////////////////////////////////

Definition at line 115 of file SoundFile.cpp.

{
    // Create the file according to its type
    SoundFile* File = NULL;
    if      (SoundFileOgg::IsFileSupported(Filename, false))     File = new SoundFileOgg;
    else if (SoundFileDefault::IsFileSupported(Filename, false)) File = new SoundFileDefault;

    // Open it for writing
    if (File)
    {
        if (File->OpenWrite(Filename, ChannelsCount, SampleRate))
        {
            File->myFilename      = "";
            File->myData          = NULL;
            File->mySize          = 0;
            File->myNbSamples     = 0;
            File->myChannelsCount = ChannelsCount;
            File->mySampleRate    = SampleRate;
        }
        else
        {
            delete File;
            File = NULL;
        }
    }

    return File;
}

Here is the call graph for this function:

unsigned int sf::priv::SoundFile::GetChannelsCount ( ) const [inherited]

Get the number of channels used by the sound.

Get the number of channels used by the sound /////////////////////////////////////////////////////////.

Returns:
Number of channels (1 = mono, 2 = stereo)

/////////////////////////////////////////////////////////

Definition at line 178 of file SoundFile.cpp.

{
    return myChannelsCount;
}
unsigned int sf::priv::SoundFile::GetSampleRate ( ) const [inherited]

Get the sample rate of the sound.

Get the sample rate of the sound /////////////////////////////////////////////////////////.

Returns:
Sample rate, in samples / sec

/////////////////////////////////////////////////////////

Definition at line 187 of file SoundFile.cpp.

{
    return mySampleRate;
}
std::size_t sf::priv::SoundFile::GetSamplesCount ( ) const [inherited]

Get the total number of samples in the file.

Get the total number of samples in the file /////////////////////////////////////////////////////////.

Returns:
Number of samples

/////////////////////////////////////////////////////////

Definition at line 169 of file SoundFile.cpp.

{
    return myNbSamples;
}
bool sf::priv::SoundFileOgg::IsFileSupported ( const std::string &  Filename,
bool  Read 
) [static]

Check if a given file is supported by this loader.

Check if a given file is supported by this loader /////////////////////////////////////////////////////////.

Parameters:
Filename: Path of the file to check
Read: Is the file opened for reading or writing ?
returnTrue if the loader can handle this file

/////////////////////////////////////////////////////////

Definition at line 61 of file SoundFileOgg.cpp.

{
    if (Read)
    {
        // Open the vorbis stream
        stb_vorbis* Stream = stb_vorbis_open_filename(const_cast<char*>(Filename.c_str()), NULL, NULL);

        if (Stream)
        {
            stb_vorbis_close(Stream);
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        // No support for writing ogg files yet...
        return false;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool sf::priv::SoundFileOgg::IsFileSupported ( const char *  Data,
std::size_t  SizeInBytes 
) [static]

Check if a given file in memory is supported by this loader.

Check if a given file in memory is supported by this loader /////////////////////////////////////////////////////////.

Parameters:
Data: Pointer to the file data in memory
SizeInBytes: Size of the data to load, in bytes
returnTrue if the loader can handle this file

/////////////////////////////////////////////////////////

Definition at line 89 of file SoundFileOgg.cpp.

{
    // Open the vorbis stream
    unsigned char* Buffer = reinterpret_cast<unsigned char*>(const_cast<char*>(Data));
    int Length = static_cast<int>(SizeInBytes);
    stb_vorbis* Stream = stb_vorbis_open_memory(Buffer, Length, NULL, NULL);

    if (Stream)
    {
        stb_vorbis_close(Stream);
        return true;
    }
    else
    {
        return false;
    }
}

Here is the call graph for this function:

bool sf::priv::SoundFileOgg::OpenRead ( const std::string &  Filename,
std::size_t &  NbSamples,
unsigned int &  ChannelsCount,
unsigned int &  SampleRate 
) [private, virtual]

/see sf::SoundFile::OpenRead

Open the sound file for reading /////////////////////////////////////////////////////////.

/////////////////////////////////////////////////////////

Reimplemented from sf::priv::SoundFile.

Definition at line 111 of file SoundFileOgg.cpp.

{
    // Close the file if already opened
    if (myStream)
        stb_vorbis_close(myStream);

    // Open the vorbis stream
    myStream = stb_vorbis_open_filename(const_cast<char*>(Filename.c_str()), NULL, NULL);
    if (myStream == NULL)
    {
        std::cerr << "Failed to read sound file \"" << Filename << "\" (cannot open the file)" << std::endl;
        return false;
    }

    // Get the music parameters
    stb_vorbis_info Infos = stb_vorbis_get_info(myStream);
    ChannelsCount = myChannelsCount = Infos.channels;
    SampleRate    = Infos.sample_rate;
    NbSamples     = static_cast<std::size_t>(stb_vorbis_stream_length_in_samples(myStream) * ChannelsCount);

    return true;
}

Here is the call graph for this function:

bool sf::priv::SoundFileOgg::OpenRead ( const char *  Data,
std::size_t  SizeInBytes,
std::size_t &  NbSamples,
unsigned int &  ChannelsCount,
unsigned int &  SampleRate 
) [private, virtual]

/see sf::SoundFile::OpenRead

/see sf::SoundFile::OpenRead /////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////

Reimplemented from sf::priv::SoundFile.

Definition at line 138 of file SoundFileOgg.cpp.

{
    // Close the file if already opened
    if (myStream)
        stb_vorbis_close(myStream);

    // Open the vorbis stream
    unsigned char* Buffer = reinterpret_cast<unsigned char*>(const_cast<char*>(Data));
    int Length = static_cast<int>(SizeInBytes);
    myStream = stb_vorbis_open_memory(Buffer, Length, NULL, NULL);
    if (myStream == NULL)
    {
        std::cerr << "Failed to read sound file from memory (cannot open the file)" << std::endl;
        return false;
    }

    // Get the music parameters
    stb_vorbis_info Infos = stb_vorbis_get_info(myStream);
    ChannelsCount = myChannelsCount = Infos.channels;
    SampleRate    = Infos.sample_rate;
    NbSamples     = static_cast<std::size_t>(stb_vorbis_stream_length_in_samples(myStream) * ChannelsCount);

    return true;
}

Here is the call graph for this function:

std::size_t sf::priv::SoundFileOgg::Read ( Int16 *  Data,
std::size_t  NbSamples 
) [virtual]

/see sf::SoundFile::Read

Read samples from the loaded sound /////////////////////////////////////////////////////////.

/////////////////////////////////////////////////////////

Reimplemented from sf::priv::SoundFile.

Definition at line 167 of file SoundFileOgg.cpp.

{
    if (myStream && Data && NbSamples)
    {
        int Read = stb_vorbis_get_samples_short_interleaved(myStream, myChannelsCount, Data, static_cast<int>(NbSamples));
        return static_cast<std::size_t>(Read * myChannelsCount);
    }
    else
    {
        return 0;
    }
}

Here is the call graph for this function:

bool sf::priv::SoundFile::Restart ( ) [inherited]

Restart the sound from the beginning.

Restart the sound from the beginning /////////////////////////////////////////////////////////.

Returns:
True if restart was successful

/////////////////////////////////////////////////////////

Definition at line 196 of file SoundFile.cpp.

{
    if (myData)
    {
        // Reopen from memory
        return OpenRead(myData, mySize, myNbSamples, myChannelsCount, mySampleRate);
    }
    else if (myFilename != "")
    {
        // Reopen from file
        return OpenRead(myFilename, myNbSamples, myChannelsCount, mySampleRate);
    }
    else
    {
        // Trying to reopen a file opened in write mode... error
        std::cerr << "Warning : trying to restart a sound opened in write mode, which is not allowed" << std::endl;
        return false;
    }
}

Here is the call graph for this function:

void sf::priv::SoundFile::Write ( const Int16 *  Data,
std::size_t  NbSamples 
) [virtual, inherited]

Write samples to the file.

Write samples to the file /////////////////////////////////////////////////////////.

Parameters:
Data: Pointer to the samples array to write
NbSamples: Number of samples to write

/////////////////////////////////////////////////////////

Reimplemented in sf::priv::SoundFileDefault.

Definition at line 264 of file SoundFile.cpp.

{
    std::cerr << "Failed to write to sound file (not supported)" << std::endl;
}

Member Data Documentation

Number of channels (1 = mono, 2 = stereo)

Reimplemented from sf::priv::SoundFile.

Definition at line 106 of file SoundFileOgg.hpp.

Vorbis stream.

Definition at line 105 of file SoundFileOgg.hpp.


The documentation for this class was generated from the following files: