D:/Programmation/Cpp/SFML/src/SFML/Audio/SoundFileOgg.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/SoundFileOgg.hpp>
00029 #include <iostream>
00030 
00031 
00032 namespace sf_private
00033 {
00037 sfSoundFileOgg::sfSoundFileOgg() :
00038 myFile(NULL)
00039 {
00040 
00041 }
00042 
00043 
00047 sfSoundFileOgg::~sfSoundFileOgg()
00048 {
00049     if (myFile)
00050     {
00051         ov_clear(&myStream);
00052         fclose(myFile);
00053     }
00054 }
00055 
00056 
00060 bool sfSoundFileOgg::OpenRead(const std::string& Filename, std::size_t& NbSamples, unsigned int& ChannelsCount, unsigned int& Frequency)
00061 {
00062     // Close the file if already opened
00063     if (myFile)
00064     {
00065         ov_clear(&myStream);
00066         fclose(myFile);
00067     }
00068 
00069     // Open the file
00070     myFile = fopen(Filename.c_str(), "rb");
00071     if (!myFile)
00072     {
00073         std::cerr << "Failed to read sound file \"" << Filename << "\" (cannot open the file)" << std::endl;
00074         return false;
00075     }
00076 
00077     // Bind the file to the ogg-vorbis stream
00078     int Error = ov_open(myFile, &myStream, NULL, 0);
00079     if (Error < 0)
00080     {
00081         std::cerr << "Failed to read sound file \"" << Filename << "\" (not a valid ogg-vorbis file)" << std::endl;
00082         return false;
00083     }
00084 
00085     // Set the sound parameters
00086     vorbis_info* Infos = ov_info(&myStream, -1);
00087     ChannelsCount = Infos->channels;
00088     Frequency     = Infos->rate;
00089     NbSamples     = static_cast<std::size_t>(ov_pcm_total(&myStream, -1) * ChannelsCount);
00090 
00091     // Make sure that the ogg stream has a fixed bitrate (VBR is not supported yet...)
00092     /*if ((Infos->bitrate_lower != Infos->bitrate_nominal) || (Infos->bitrate_lower != Infos->bitrate_upper))
00093     {
00094         std::cerr << "Failed to read sound file \"" << Filename << "\" (variable bitrate is not supported)" << std::endl;
00095         return false;
00096     }*/
00097 
00098     return true;
00099 }
00100 
00101 
00105 std::size_t sfSoundFileOgg::Read(sfInt16* Data, std::size_t NbSamples)
00106 {
00107     // ov_read will only read one chunk, even if it is smaller than the requested size,
00108     // so we loop until all bytes have been read
00109     long TotalSize = static_cast<long>(NbSamples * sizeof(sfInt16));
00110     long TotalRead = 0;
00111     while (TotalRead < TotalSize)
00112     {
00113         // Decode sound data
00114         char* Ptr = reinterpret_cast<char*>(Data) + TotalRead;
00115         int   Len = TotalSize - TotalRead;
00116         long Read = ov_read(&myStream, Ptr, Len, 0, 2, 1, NULL);
00117 
00118         if (Read > 0)
00119         {
00120             // Ok, Read bytes have been read
00121             TotalRead += Read;
00122         }
00123         else if (Read == 0)
00124         {
00125             // End of file reached
00126             break;
00127         }
00128         else
00129         {
00130             // Error...
00131             std::cerr << "Failed to read from ogg file" << std::endl;
00132             break;
00133         }
00134     }
00135 
00136     return static_cast<std::size_t>(TotalRead / sizeof(sfInt16));
00137 }
00138 
00139 } // namespace sf_private

Generated for SFML by  doxygen 1.5.2