00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024
00026
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
00060 if (myFile)
00061 sf_close(myFile);
00062
00063
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
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
00087 if (myFile)
00088 sf_close(myFile);
00089
00090
00091 SF_INFO FileInfos;
00092 FileInfos.channels = ChannelsCount;
00093 FileInfos.samplerate = Frequency;
00094 FileInfos.format = SF_FORMAT_PCM_16;
00095
00096
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
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;
00119 else if (Ext == "sds" || Ext == "SDS" ) FileInfos.format |= SF_FORMAT_SDS;
00120 else if (Ext == "avr" || Ext == "AVR" ) FileInfos.format |= SF_FORMAT_AVR;
00121 else if (Ext == "sd2" || Ext == "SD2" ) FileInfos.format |= SF_FORMAT_SD2;
00122 else if (Ext == "flac" || Ext == "FLAC") FileInfos.format |= SF_FORMAT_FLAC;
00123 else
00124 {
00125
00126 std::cerr << "Failed to create sound file \"" << Filename << "\" : unknown format" << std::endl;
00127 return false;
00128 }
00129
00130
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 }