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
00033 {
00034 namespace priv
00035 {
00039 SoundFileDefault::SoundFileDefault() :
00040 myFile(NULL)
00041 {
00042 
00043 }
00044 
00045 
00049 SoundFileDefault::~SoundFileDefault()
00050 {
00051     if (myFile)
00052         sf_close(myFile);
00053 }
00054 
00055 
00059 bool SoundFileDefault::OpenRead(const std::string& Filename, std::size_t& NbSamples, unsigned int& ChannelsCount, unsigned int& SampleRate)
00060 {
00061     // If the file is already opened, first close it
00062     if (myFile)
00063         sf_close(myFile);
00064 
00065     // Open the sound file
00066     SF_INFO FileInfos;
00067     myFile = sf_open(Filename.c_str(), SFM_READ, &FileInfos);
00068     if (!myFile)
00069     {
00070         std::cerr << "Failed to read sound file \"" << Filename << "\"" << std::endl;
00071         return false;
00072     }
00073 
00074     // Set the sound parameters
00075     ChannelsCount = FileInfos.channels;
00076     SampleRate    = FileInfos.samplerate;
00077     NbSamples     = static_cast<std::size_t>(FileInfos.frames) * ChannelsCount;
00078 
00079     return true;
00080 }
00081 
00082 
00086 bool SoundFileDefault::OpenWrite(const std::string& Filename, unsigned int ChannelsCount, unsigned int SampleRate)
00087 {
00088     // If the file is already opened, first close it
00089     if (myFile)
00090         sf_close(myFile);
00091 
00092     // Fill the sound infos with parameters
00093     SF_INFO FileInfos;
00094     FileInfos.channels   = ChannelsCount;
00095     FileInfos.samplerate = SampleRate;
00096     FileInfos.format     = SF_FORMAT_PCM_16;
00097 
00098     // Extract the file extension
00099     std::string Ext = "wav";
00100     std::string::size_type Pos = Filename.find_last_of(".");
00101     if (Pos != std::string::npos)
00102         Ext = Filename.substr(Pos + 1);
00103 
00104     // Find the right format according to the file extension
00105     if      (Ext == "wav"  || Ext == "WAV" ) FileInfos.format |= SF_FORMAT_WAV;
00106     else if (Ext == "aif"  || Ext == "AIF" ) FileInfos.format |= SF_FORMAT_AIFF;
00107     else if (Ext == "aiff" || Ext == "AIFF") FileInfos.format |= SF_FORMAT_AIFF;
00108     else if (Ext == "au"   || Ext == "AU"  ) FileInfos.format |= SF_FORMAT_AU;
00109     else if (Ext == "raw"  || Ext == "RAW" ) FileInfos.format |= SF_FORMAT_RAW;
00110     else if (Ext == "paf"  || Ext == "PAF" ) FileInfos.format |= SF_FORMAT_PAF;
00111     else if (Ext == "svx"  || Ext == "SVX" ) FileInfos.format |= SF_FORMAT_SVX;
00112     else if (Ext == "voc"  || Ext == "VOC" ) FileInfos.format |= SF_FORMAT_VOC;
00113     else if (Ext == "sf"   || Ext == "SF"  ) FileInfos.format |= SF_FORMAT_IRCAM;
00114     else if (Ext == "w64"  || Ext == "W64" ) FileInfos.format |= SF_FORMAT_W64;
00115     else if (Ext == "mat4" || Ext == "MAT4") FileInfos.format |= SF_FORMAT_MAT4;
00116     else if (Ext == "mat5" || Ext == "MAT5") FileInfos.format |= SF_FORMAT_MAT5;
00117     else if (Ext == "pvf"  || Ext == "PVF" ) FileInfos.format |= SF_FORMAT_PVF;
00118     else if (Ext == "htk"  || Ext == "HTK" ) FileInfos.format |= SF_FORMAT_HTK;
00119     else if (Ext == "caf"  || Ext == "CAF" ) FileInfos.format |= SF_FORMAT_CAF;
00120     else if (Ext == "nist" || Ext == "NIST") FileInfos.format |= SF_FORMAT_NIST; // SUPPORTED ?
00121     else if (Ext == "sds"  || Ext == "SDS" ) FileInfos.format |= SF_FORMAT_SDS;  // SUPPORTED ?
00122     else if (Ext == "avr"  || Ext == "AVR" ) FileInfos.format |= SF_FORMAT_AVR;  // SUPPORTED ?
00123     else if (Ext == "sd2"  || Ext == "SD2" ) FileInfos.format |= SF_FORMAT_SD2;  // SUPPORTED ?
00124     else if (Ext == "flac" || Ext == "FLAC") FileInfos.format |= SF_FORMAT_FLAC; // SUPPORTED ?
00125     else
00126     {
00127         // Error : unrecognized extension
00128         std::cerr << "Failed to create sound file \"" << Filename << "\" : unknown format" << std::endl;
00129         return false;
00130     }
00131 
00132     // Open the sound file for writing
00133     myFile = sf_open(Filename.c_str(), SFM_WRITE, &FileInfos);
00134     if (!myFile)
00135     {
00136         std::cerr << "Failed to create sound file \"" << Filename << "\"" << std::endl;
00137         return false;
00138     }
00139 
00140     return true;
00141 }
00142 
00143 
00147 std::size_t SoundFileDefault::Read(Int16* Data, std::size_t NbSamples)
00148 {
00149     if (myFile && Data && NbSamples)
00150         return static_cast<std::size_t>(sf_read_short(myFile, Data, NbSamples));
00151     else
00152         return 0;
00153 }
00154 
00155 
00159 void SoundFileDefault::Write(const Int16* Data, std::size_t NbSamples)
00160 {
00161     if (myFile && Data && NbSamples)
00162         sf_write_short(myFile, Data, NbSamples);
00163 }
00164 
00165 } // namespace priv
00166 
00167 } // namespace sf