Back to index

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

SoundFile is the abstract base class for loading and saving different sound file formats /////////////////////////////////////////////////////////. More...

#include <SoundFile.hpp>

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

List of all members.

Public Member Functions

virtual ~SoundFile ()
 Virtual destructor.
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 std::size_t Read (Int16 *Data, std::size_t NbSamples)
 Read samples from the loaded sound.
virtual void Write (const Int16 *Data, std::size_t NbSamples)
 Write samples to the file.

Static Public Member Functions

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.

Protected Member Functions

 SoundFile ()
 Default constructor.

Private Member Functions

virtual bool OpenRead (const std::string &Filename, std::size_t &NbSamples, unsigned int &ChannelsCount, unsigned int &SampleRate)
 Open the sound file for reading.
virtual bool OpenRead (const char *Data, std::size_t SizeInBytes, std::size_t &NbSamples, unsigned int &ChannelsCount, unsigned int &SampleRate)
 Open the sound file in memory for reading.
virtual bool OpenWrite (const std::string &Filename, unsigned int ChannelsCount, unsigned int SampleRate)
 Open the sound file for writing.

Private Attributes

std::size_t myNbSamples
 Total number of samples in the file.
unsigned int myChannelsCount
 Number of channels used by the sound.
unsigned int mySampleRate
 Number of samples per second.
std::string myFilename
 Path of the file (valid if loaded from a file)
const char * myData
 Pointer to the file in memory (valid if loaded from memory)
std::size_t mySize
 Size of the file in memory (valid if loaded from memory)

Detailed Description

SoundFile is the abstract base class for loading and saving different sound file formats /////////////////////////////////////////////////////////.

Definition at line 43 of file SoundFile.hpp.


Constructor & Destructor Documentation

Virtual destructor.

Virtual destructor /////////////////////////////////////////////////////////.

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

Definition at line 160 of file SoundFile.cpp.

{
    // Nothing to do
}

Default constructor.

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

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

Definition at line 148 of file SoundFile.cpp.


Member Function Documentation

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

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]

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]

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

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

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

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::SoundFile::OpenRead ( const std::string &  Filename,
std::size_t &  NbSamples,
unsigned int &  ChannelsCount,
unsigned int &  SampleRate 
) [private, virtual]

Open the sound file for reading.

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

Parameters:
Filename: Path of sound file to load
NbSamples: Number of samples in the file
ChannelsCount: Number of channels in the loaded sound
SampleRate: Sample rate of the loaded sound
Returns:
True if the file was successfully opened

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

Reimplemented in sf::priv::SoundFileDefault, and sf::priv::SoundFileOgg.

Definition at line 220 of file SoundFile.cpp.

{
    std::cerr << "Failed to open sound file \"" << Filename << "\", format is not supported by SFML" << std::endl;

    return false;
}

Here is the caller graph for this function:

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

Open the sound file in memory for reading.

Open the sound 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:
True if the file was successfully opened

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

Reimplemented in sf::priv::SoundFileDefault, and sf::priv::SoundFileOgg.

Definition at line 231 of file SoundFile.cpp.

{
    std::cerr << "Failed to open sound file from memory, format is not supported by SFML" << std::endl;

    return false;
}
bool sf::priv::SoundFile::OpenWrite ( const std::string &  Filename,
unsigned int  ChannelsCount,
unsigned int  SampleRate 
) [private, virtual]

Open the sound file for writing.

Open the sound file for writing /////////////////////////////////////////////////////////.

Parameters:
Filename: Path of sound file to write
ChannelsCount: Number of channels in the sound
SampleRate: Sample rate of the sound
Returns:
True if the file was successfully opened

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

Reimplemented in sf::priv::SoundFileDefault.

Definition at line 242 of file SoundFile.cpp.

{
    std::cerr << "Failed to open sound file \"" << Filename << "\", format is not supported by SFML" << std::endl;

    return false;
}

Here is the caller graph for this function:

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

Read samples from the loaded sound.

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

Parameters:
Data: Pointer to the samples array to fill
NbSamples: Number of samples to read
Returns:
Number of samples read

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

Reimplemented in sf::priv::SoundFileOgg, and sf::priv::SoundFileDefault.

Definition at line 253 of file SoundFile.cpp.

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

    return 0;
}

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]

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

unsigned int sf::priv::SoundFile::myChannelsCount [private]

Number of channels used by the sound.

Reimplemented in sf::priv::SoundFileOgg.

Definition at line 197 of file SoundFile.hpp.

const char* sf::priv::SoundFile::myData [private]

Pointer to the file in memory (valid if loaded from memory)

Definition at line 200 of file SoundFile.hpp.

std::string sf::priv::SoundFile::myFilename [private]

Path of the file (valid if loaded from a file)

Definition at line 199 of file SoundFile.hpp.

std::size_t sf::priv::SoundFile::myNbSamples [private]

Total number of samples in the file.

Definition at line 196 of file SoundFile.hpp.

unsigned int sf::priv::SoundFile::mySampleRate [private]

Number of samples per second.

Definition at line 198 of file SoundFile.hpp.

std::size_t sf::priv::SoundFile::mySize [private]

Size of the file in memory (valid if loaded from memory)

Definition at line 201 of file SoundFile.hpp.


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