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/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
00063 if (myFile)
00064 {
00065 ov_clear(&myStream);
00066 fclose(myFile);
00067 }
00068
00069
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
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
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
00092
00093
00094
00095
00096
00097
00098 return true;
00099 }
00100
00101
00105 std::size_t sfSoundFileOgg::Read(sfInt16* Data, std::size_t NbSamples)
00106 {
00107
00108
00109 long TotalSize = static_cast<long>(NbSamples * sizeof(sfInt16));
00110 long TotalRead = 0;
00111 while (TotalRead < TotalSize)
00112 {
00113
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
00121 TotalRead += Read;
00122 }
00123 else if (Read == 0)
00124 {
00125
00126 break;
00127 }
00128 else
00129 {
00130
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 }