Back to index

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

Specialization of SoundFile that can handle a lot of sound formats (see libsndfile homepage for a complete list) /////////////////////////////////////////////////////////. More...

#include <SoundFileDefault.hpp>

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

List of all members.

Classes

struct  MemoryInfos
 Structure holding data related to memory operations /////////////////////////////////////////////////////////. More...

Public Member Functions

 SoundFileDefault ()
 Default constructor.
 ~SoundFileDefault ()
 Destructor.
virtual std::size_t Read (Int16 *Data, std::size_t NbSamples)
 /see sf::SoundFile::Read
virtual void Write (const Int16 *Data, std::size_t NbSamples)
 /see sf::SoundFile::Write
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.

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
virtual bool OpenWrite (const std::string &Filename, unsigned int ChannelsCount, unsigned int SampleRate)
 /see sf::SoundFile::OpenWrite

Static Private Member Functions

static int GetFormatFromFilename (const std::string &Filename)
 Get the internal format of an audio file according to its filename extension.
static sf_count_t MemoryGetLength (void *UserData)
 Functions for implementing custom read and write to memory files.
static sf_count_t MemoryRead (void *Ptr, sf_count_t Count, void *UserData)
static sf_count_t MemorySeek (sf_count_t Offset, int Whence, void *UserData)
static sf_count_t MemoryTell (void *UserData)
static sf_count_t MemoryWrite (const void *Ptr, sf_count_t Count, void *UserData)

Private Attributes

SNDFILE * myFile
 File descriptor.
MemoryInfos myMemory
 Memory read / write data.

Detailed Description

Specialization of SoundFile that can handle a lot of sound formats (see libsndfile homepage for a complete list) /////////////////////////////////////////////////////////.

Definition at line 43 of file SoundFileDefault.hpp.


Class Documentation

struct sf::priv::SoundFileDefault::MemoryInfos

Structure holding data related to memory operations /////////////////////////////////////////////////////////.

Definition at line 137 of file SoundFileDefault.hpp.

Class Members
const char * DataPtr Pointer to the current read / write position.
const char * DataStart Pointer to the begining of the data.
sf_count_t TotalSize Total size of the data, in bytes.

Constructor & Destructor Documentation

Default constructor.

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

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

Definition at line 40 of file SoundFileDefault.cpp.

                                   :
myFile(NULL)
{

}

Destructor.

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

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

Definition at line 50 of file SoundFileDefault.cpp.

{
    if (myFile)
        sf_close(myFile);
}

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;
}
int sf::priv::SoundFileDefault::GetFormatFromFilename ( const std::string &  Filename) [static, private]

Get the internal format of an audio file according to its filename extension.

Get the internal format of an audio file according to its filename extension /////////////////////////////////////////////////////////.

Parameters:
Filename: Filename to check
Returns:
Internal format matching the filename (-1 if no match)

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

Definition at line 250 of file SoundFileDefault.cpp.

{
    // Extract the extension
    std::string Ext = "wav";
    std::string::size_type Pos = Filename.find_last_of(".");
    if (Pos != std::string::npos)
        Ext = Filename.substr(Pos + 1);

    // Match every supported extension with its format constant
    if (Ext == "wav"  || Ext == "WAV" ) return SF_FORMAT_WAV;
    if (Ext == "aif"  || Ext == "AIF" ) return SF_FORMAT_AIFF;
    if (Ext == "aiff" || Ext == "AIFF") return SF_FORMAT_AIFF;
    if (Ext == "au"   || Ext == "AU"  ) return SF_FORMAT_AU;
    if (Ext == "raw"  || Ext == "RAW" ) return SF_FORMAT_RAW;
    if (Ext == "paf"  || Ext == "PAF" ) return SF_FORMAT_PAF;
    if (Ext == "svx"  || Ext == "SVX" ) return SF_FORMAT_SVX;
    if (Ext == "voc"  || Ext == "VOC" ) return SF_FORMAT_VOC;
    if (Ext == "sf"   || Ext == "SF"  ) return SF_FORMAT_IRCAM;
    if (Ext == "w64"  || Ext == "W64" ) return SF_FORMAT_W64;
    if (Ext == "mat4" || Ext == "MAT4") return SF_FORMAT_MAT4;
    if (Ext == "mat5" || Ext == "MAT5") return SF_FORMAT_MAT5;
    if (Ext == "pvf"  || Ext == "PVF" ) return SF_FORMAT_PVF;
    if (Ext == "htk"  || Ext == "HTK" ) return SF_FORMAT_HTK;
    if (Ext == "caf"  || Ext == "CAF" ) return SF_FORMAT_CAF;
    if (Ext == "nist" || Ext == "NIST") return SF_FORMAT_NIST; // SUPPORTED ?
    if (Ext == "sds"  || Ext == "SDS" ) return SF_FORMAT_SDS;  // SUPPORTED ?
    if (Ext == "avr"  || Ext == "AVR" ) return SF_FORMAT_AVR;  // SUPPORTED ?
    if (Ext == "sd2"  || Ext == "SD2" ) return SF_FORMAT_SD2;  // SUPPORTED ?
    if (Ext == "flac" || Ext == "FLAC") return SF_FORMAT_FLAC; // SUPPORTED ?

    return -1;
}

Here is the caller graph for this function:

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::SoundFileDefault::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 60 of file SoundFileDefault.cpp.

{
    if (Read)
    {
        // Open the sound file
        SF_INFO FileInfos;
        SNDFILE* File = sf_open(Filename.c_str(), SFM_READ, &FileInfos);

        if (File)
        {
            sf_close(File);
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        // Check the extension
        return GetFormatFromFilename(Filename) != -1;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool sf::priv::SoundFileDefault::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 SoundFileDefault.cpp.

{
    // Define the I/O custom functions for reading from memory
    SF_VIRTUAL_IO VirtualIO;
    VirtualIO.get_filelen = &SoundFileDefault::MemoryGetLength;
    VirtualIO.read        = &SoundFileDefault::MemoryRead;
    VirtualIO.seek        = &SoundFileDefault::MemorySeek;
    VirtualIO.tell        = &SoundFileDefault::MemoryTell;
    VirtualIO.write       = &SoundFileDefault::MemoryWrite;

    // Initialize the memory data
    MemoryInfos Memory;
    Memory.DataStart = Data;
    Memory.DataPtr   = Data;
    Memory.TotalSize = SizeInBytes;

    // Open the sound file
    SF_INFO FileInfos;
    SNDFILE* File = sf_open_virtual(&VirtualIO, SFM_READ, &FileInfos, &Memory);

    if (File)
    {
        sf_close(File);
        return true;
    }
    else
    {
        return false;
    }
}

Here is the call graph for this function:

sf_count_t sf::priv::SoundFileDefault::MemoryGetLength ( void *  UserData) [static, private]

Functions for implementing custom read and write to memory files.

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

Definition at line 288 of file SoundFileDefault.cpp.

{
    MemoryInfos* Memory = static_cast<MemoryInfos*>(UserData);

    return Memory->TotalSize;
}

Here is the caller graph for this function:

sf_count_t sf::priv::SoundFileDefault::MemoryRead ( void *  Ptr,
sf_count_t  Count,
void *  UserData 
) [static, private]

Definition at line 294 of file SoundFileDefault.cpp.

{
    MemoryInfos* Memory = static_cast<MemoryInfos*>(UserData);

    sf_count_t Position = Memory->DataPtr - Memory->DataStart;
    if (Position + Count >= Memory->TotalSize)
        Count = Memory->TotalSize - Position;

    memcpy(Ptr, Memory->DataPtr, static_cast<std::size_t>(Count));

    Memory->DataPtr += Count;

    return Count;
}

Here is the caller graph for this function:

sf_count_t sf::priv::SoundFileDefault::MemorySeek ( sf_count_t  Offset,
int  Whence,
void *  UserData 
) [static, private]

Definition at line 308 of file SoundFileDefault.cpp.

{
    MemoryInfos* Memory = static_cast<MemoryInfos*>(UserData);

    sf_count_t Position = 0;
    switch (Whence)
    {
        case SEEK_SET :
            Position = Offset;
            break;
        case SEEK_CUR :
            Position = Memory->DataPtr - Memory->DataStart + Offset;
            break;
        case SEEK_END :
            Position = Memory->TotalSize - Offset;
            break;
        default :
            Position = 0;
            break;
    }

    if (Position >= Memory->TotalSize)
        Position = Memory->TotalSize - 1;
    else if (Position < 0)
        Position = 0;

    Memory->DataPtr = Memory->DataStart + Position;

    return Position;
}

Here is the caller graph for this function:

sf_count_t sf::priv::SoundFileDefault::MemoryTell ( void *  UserData) [static, private]

Definition at line 338 of file SoundFileDefault.cpp.

{
    MemoryInfos* Memory = static_cast<MemoryInfos*>(UserData);

    return Memory->DataPtr - Memory->DataStart;
}

Here is the caller graph for this function:

sf_count_t sf::priv::SoundFileDefault::MemoryWrite ( const void *  Ptr,
sf_count_t  Count,
void *  UserData 
) [static, private]

Definition at line 344 of file SoundFileDefault.cpp.

{
    return 0;
}

Here is the caller graph for this function:

bool sf::priv::SoundFileDefault::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 124 of file SoundFileDefault.cpp.

{
    // If the file is already opened, first close it
    if (myFile)
        sf_close(myFile);

    // Open the sound file
    SF_INFO FileInfos;
    myFile = sf_open(Filename.c_str(), SFM_READ, &FileInfos);
    if (!myFile)
    {
        std::cerr << "Failed to read sound file \"" << Filename << "\"" << std::endl;
        return false;
    }

    // Set the sound parameters
    ChannelsCount = FileInfos.channels;
    SampleRate    = FileInfos.samplerate;
    NbSamples     = static_cast<std::size_t>(FileInfos.frames) * ChannelsCount;

    return true;
}
bool sf::priv::SoundFileDefault::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 151 of file SoundFileDefault.cpp.

{
    // If the file is already opened, first close it
    if (myFile)
        sf_close(myFile);

    // Define the I/O custom functions for reading from memory
    SF_VIRTUAL_IO VirtualIO;
    VirtualIO.get_filelen = &SoundFileDefault::MemoryGetLength;
    VirtualIO.read        = &SoundFileDefault::MemoryRead;
    VirtualIO.seek        = &SoundFileDefault::MemorySeek;
    VirtualIO.tell        = &SoundFileDefault::MemoryTell;
    VirtualIO.write       = &SoundFileDefault::MemoryWrite;

    // Initialize the memory data
    myMemory.DataStart = Data;
    myMemory.DataPtr   = Data;
    myMemory.TotalSize = SizeInBytes;

    // Open the sound file
    SF_INFO FileInfos;
    myFile = sf_open_virtual(&VirtualIO, SFM_READ, &FileInfos, &myMemory);
    if (!myFile)
    {
        std::cerr << "Failed to read sound file from memory" << std::endl;
        return false;
    }

    // Set the sound parameters
    ChannelsCount = FileInfos.channels;
    SampleRate    = FileInfos.samplerate;
    NbSamples     = static_cast<std::size_t>(FileInfos.frames) * ChannelsCount;

    return true;
}

Here is the call graph for this function:

bool sf::priv::SoundFileDefault::OpenWrite ( const std::string &  Filename,
unsigned int  ChannelsCount,
unsigned int  SampleRate 
) [private, virtual]

/see sf::SoundFile::OpenWrite

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

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

Reimplemented from sf::priv::SoundFile.

Definition at line 191 of file SoundFileDefault.cpp.

{
    // If the file is already opened, first close it
    if (myFile)
        sf_close(myFile);

    // Find the right format according to the file extension
    int Format = GetFormatFromFilename(Filename);
    if (Format == -1)
    {
        // Error : unrecognized extension
        std::cerr << "Failed to create sound file \"" << Filename << "\" : unknown format" << std::endl;
        return false;
    }

    // Fill the sound infos with parameters
    SF_INFO FileInfos;
    FileInfos.channels   = ChannelsCount;
    FileInfos.samplerate = SampleRate;
    FileInfos.format     = Format | SF_FORMAT_PCM_16;

    // Open the sound file for writing
    myFile = sf_open(Filename.c_str(), SFM_WRITE, &FileInfos);
    if (!myFile)
    {
        std::cerr << "Failed to create sound file \"" << Filename << "\"" << std::endl;
        return false;
    }

    return true;
}

Here is the call graph for this function:

std::size_t sf::priv::SoundFileDefault::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 227 of file SoundFileDefault.cpp.

{
    if (myFile && Data && NbSamples)
        return static_cast<std::size_t>(sf_read_short(myFile, Data, NbSamples));
    else
        return 0;
}
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::SoundFileDefault::Write ( const Int16 *  Data,
std::size_t  NbSamples 
) [virtual]

/see sf::SoundFile::Write

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

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

Reimplemented from sf::priv::SoundFile.

Definition at line 239 of file SoundFileDefault.cpp.

{
    if (myFile && Data && NbSamples)
        sf_write_short(myFile, Data, NbSamples);
}

Member Data Documentation

File descriptor.

Definition at line 147 of file SoundFileDefault.hpp.

Memory read / write data.

Definition at line 148 of file SoundFileDefault.hpp.


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