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         myFile = NULL;
00053     }
00054 }
00055 
00056 
00060 bool sfSoundFileOgg::OpenRead(const std::string& Filename, std::size_t& NbSamples, unsigned int& ChannelsCount, unsigned int& SampleRate)
00061 {
00062     // Close the file if already opened
00063     if (myFile)
00064     {
00065         ov_clear(&myStream);
00066         myFile = NULL;
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         fclose(myFile);
00083         myFile = NULL;
00084         return false;
00085     }
00086 
00087     // Set the sound parameters
00088     vorbis_info* Infos = ov_info(&myStream, -1);
00089     ChannelsCount = Infos->channels;
00090     SampleRate    = Infos->rate;
00091     NbSamples     = static_cast<std::size_t>(ov_pcm_total(&myStream, -1) * ChannelsCount);
00092 
00093     // Make sure that the ogg stream has a fixed bitrate (VBR is not supported yet...)
00094     /*if ((Infos->bitrate_lower != Infos->bitrate_nominal) || (Infos->bitrate_lower != Infos->bitrate_upper))
00095     {
00096         std::cerr << "Failed to read sound file \"" << Filename << "\" (variable bitrate is not supported)" << std::endl;
00097         return false;
00098     }*/
00099 
00100     return true;
00101 }
00102 
00103 
00107 std::size_t sfSoundFileOgg::Read(sfInt16* Data, std::size_t NbSamples)
00108 {
00109     // ov_read will only read one chunk, even if it is smaller than the requested size,
00110     // so we loop until all bytes have been read
00111     long TotalSize = static_cast<long>(NbSamples * sizeof(sfInt16));
00112     long TotalRead = 0;
00113     while (TotalRead < TotalSize)
00114     {
00115         // Decode sound data
00116         char* Ptr = reinterpret_cast<char*>(Data) + TotalRead;
00117         int   Len = TotalSize - TotalRead;
00118         long Read = ov_read(&myStream, Ptr, Len, 0, 2, 1, NULL);
00119 
00120         if (Read > 0)
00121         {
00122             // Ok, Read bytes have been read
00123             TotalRead += Read;
00124         }
00125         else if (Read == 0)
00126         {
00127             // End of file reached
00128             break;
00129         }
00130         else
00131         {
00132             // Error...
00133             std::cerr << "Failed to read from ogg file" << std::endl;
00134             break;
00135         }
00136     }
00137 
00138     return static_cast<std::size_t>(TotalRead / sizeof(sfInt16));
00139 }
00140 
00141 } // namespace sf_private