D:/Programmation/Cpp/SFML/src/SFML/Audio/SoundFileDefault.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/Audio/SoundFileDefault.hpp>
00029 #include <iostream>
00030 
00031 
00032 namespace sf_private
00033 {
00037 sfSoundFileDefault::sfSoundFileDefault() :
00038 myFile(NULL)
00039 {
00040 
00041 }
00042 
00043 
00047 sfSoundFileDefault::~sfSoundFileDefault()
00048 {
00049     if (myFile)
00050         sf_close(myFile);
00051 }
00052 
00053 
00057 bool sfSoundFileDefault::OpenRead(const std::string& Filename, std::size_t& NbSamples, unsigned int& ChannelsCount, unsigned int& Frequency)
00058 {
00059     // If the file is already opened, first close it
00060     if (myFile)
00061         sf_close(myFile);
00062 
00063     // Open the sound file
00064     SF_INFO FileInfos;
00065     myFile = sf_open(Filename.c_str(), SFM_READ, &FileInfos);
00066     if (!myFile)
00067     {
00068         std::cerr << "Failed to read sound file \"" << Filename << "\"" << std::endl;
00069         return false;
00070     }
00071 
00072     // Set the sound parameters
00073     ChannelsCount = FileInfos.channels;
00074     Frequency     = FileInfos.samplerate;
00075     NbSamples     = static_cast<std::size_t>(FileInfos.frames) * ChannelsCount;
00076 
00077     return true;
00078 }
00079 
00080 
00084 bool sfSoundFileDefault::OpenWrite(const std::string& Filename, unsigned int ChannelsCount, unsigned int Frequency)
00085 {
00086     // If the file is already opened, first close it
00087     if (myFile)
00088         sf_close(myFile);
00089 
00090     // Fill the sound infos with parameters
00091     SF_INFO FileInfos;
00092     FileInfos.channels   = ChannelsCount;
00093     FileInfos.samplerate = Frequency;
00094     FileInfos.format     = SF_FORMAT_PCM_16;
00095 
00096     // Extract the file extension
00097     std::string Ext = "wav";
00098     std::string::size_type Pos = Filename.find_last_of(".");
00099     if (Pos != std::string::npos)
00100         Ext = Filename.substr(Pos + 1);
00101 
00102     // Find the right format according to the file extension
00103     if      (Ext == "wav"  || Ext == "WAV" ) FileInfos.format |= SF_FORMAT_WAV;
00104     else if (Ext == "aif"  || Ext == "AIF" ) FileInfos.format |= SF_FORMAT_AIFF;
00105     else if (Ext == "aiff" || Ext == "AIFF") FileInfos.format |= SF_FORMAT_AIFF;
00106     else if (Ext == "au"   || Ext == "AU"  ) FileInfos.format |= SF_FORMAT_AU;
00107     else if (Ext == "raw"  || Ext == "RAW" ) FileInfos.format |= SF_FORMAT_RAW;
00108     else if (Ext == "paf"  || Ext == "PAF" ) FileInfos.format |= SF_FORMAT_PAF;
00109     else if (Ext == "svx"  || Ext == "SVX" ) FileInfos.format |= SF_FORMAT_SVX;
00110     else if (Ext == "voc"  || Ext == "VOC" ) FileInfos.format |= SF_FORMAT_VOC;
00111     else if (Ext == "sf"   || Ext == "SF"  ) FileInfos.format |= SF_FORMAT_IRCAM;
00112     else if (Ext == "w64"  || Ext == "W64" ) FileInfos.format |= SF_FORMAT_W64;
00113     else if (Ext == "mat4" || Ext == "MAT4") FileInfos.format |= SF_FORMAT_MAT4;
00114     else if (Ext == "mat5" || Ext == "MAT5") FileInfos.format |= SF_FORMAT_MAT5;
00115     else if (Ext == "pvf"  || Ext == "PVF" ) FileInfos.format |= SF_FORMAT_PVF;
00116     else if (Ext == "htk"  || Ext == "HTK" ) FileInfos.format |= SF_FORMAT_HTK;
00117     else if (Ext == "caf"  || Ext == "CAF" ) FileInfos.format |= SF_FORMAT_CAF;
00118     else if (Ext == "nist" || Ext == "NIST") FileInfos.format |= SF_FORMAT_NIST; // SUPPORTED ?
00119     else if (Ext == "sds"  || Ext == "SDS" ) FileInfos.format |= SF_FORMAT_SDS;  // SUPPORTED ?
00120     else if (Ext == "avr"  || Ext == "AVR" ) FileInfos.format |= SF_FORMAT_AVR;  // SUPPORTED ?
00121     else if (Ext == "sd2"  || Ext == "SD2" ) FileInfos.format |= SF_FORMAT_SD2;  // SUPPORTED ?
00122     else if (Ext == "flac" || Ext == "FLAC") FileInfos.format |= SF_FORMAT_FLAC; // SUPPORTED ?
00123     else
00124     {
00125         // Error : unrecognized extension
00126         std::cerr << "Failed to create sound file \"" << Filename << "\" : unknown format" << std::endl;
00127         return false;
00128     }
00129 
00130     // Open the sound file for writing
00131     myFile = sf_open(Filename.c_str(), SFM_WRITE, &FileInfos);
00132     if (!myFile)
00133     {
00134         std::cerr << "Failed to create sound file \"" << Filename << "\"" << std::endl;
00135         return false;
00136     }
00137 
00138     return true;
00139 }
00140 
00141 
00145 std::size_t sfSoundFileDefault::Read(sfInt16* Data, std::size_t NbSamples)
00146 {
00147     if (myFile && Data && NbSamples)
00148         return static_cast<std::size_t>(sf_read_short(myFile, Data, NbSamples));
00149     else
00150         return 0;
00151 }
00152 
00153 
00157 void sfSoundFileDefault::Write(const sfInt16* Data, std::size_t NbSamples)
00158 {
00159     if (myFile && Data && NbSamples)
00160         sf_write_short(myFile, Data, NbSamples);
00161 }
00162 
00163 } // namespace sf_private

Generated for SFML by  doxygen 1.5.2